14
\$\begingroup\$

Related: Validate a stem-and-leaf plot

Input

A non-empty list of positive integers. If needed, they can be taken as strings. You cannot assume it is sorted.

Output

A stem-and-leaf plot of the numbers. In a this stem-and-leaf plot, numbers are ordered into stems by tens, then all numbers that fit into that stem have their ones value placed into the stem, and then all are sorted. In this challenge, newlines separate the stems, and spaces separate the stems from the leaves.

You may either include or exclude all empty stems that are between non-empty stems.

Test Cases

(lists can be taken in your language's list default, I used JSON for the below)

Including empty stems:

[1, 2, 3, 3, 3, 3, 3, 10, 15, 15, 18, 1, 100]

0 11233333
1 0558
2
3
4
5
6
7
8
9
10 0

[55, 59, 49, 43, 58, 59, 54, 44, 49, 51, 44, 40, 50, 59, 59, 59]

4 034499
5 0145899999

[10000, 10100]

1000 0
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010 0

Excluding empty stems:

[1, 2, 3, 3, 3, 3, 3, 10, 15, 15, 18, 1, 100]

0 11233333
1 0558
10 0

[55, 59, 49, 43, 58, 59, 54, 44, 49, 51, 44, 40, 50, 59, 59, 59]

4 034499
5 0145899999

[10000, 10100]

1000 0
1010 0
\$\endgroup\$
8
  • \$\begingroup\$ Sandbox \$\endgroup\$
    – Stephen
    Jun 23, 2017 at 13:34
  • \$\begingroup\$ It need to be sorted and/or preserve the input order? \$\endgroup\$
    – Rod
    Jun 23, 2017 at 13:35
  • \$\begingroup\$ @Rod by definition, it sorts the input by tens, then by ones. Input order doesn't matter. \$\endgroup\$
    – Stephen
    Jun 23, 2017 at 13:37
  • 2
    \$\begingroup\$ The output format MUST be like that? Is my answer valid? \$\endgroup\$
    – Rod
    Jun 23, 2017 at 14:04
  • 1
    \$\begingroup\$ @totallyhuman tuples are OK, but yes, the leaves need to be sorted, that's the whole point of the plot, to visualize patterns and distributions \$\endgroup\$
    – Stephen
    Jun 23, 2017 at 16:56

9 Answers 9

3
\$\begingroup\$

Jelly, 17 bytes

Ṣµ:©⁵Ġṁ@%⁵®Q¤żK€Y

Try it online!

\$\endgroup\$
0
3
\$\begingroup\$

Python 2, 78 75 79 bytes

s=-1
for i in sorted(input()):
 if i/10^s:s=i/10;print'\n%d-'%s,
 print`i`[-1],

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Retina, 38 30 bytes

Thanks to Neil for saving 2 bytes, and to Leo for saving another 6.

Byte count assumes ISO 8859-1 encoding.

O#`
.\b
 $&
\B 
0 
D$`¶?.+ 
$*

Input is a linefeed-separated list of integers. Output omits empty prefixes.

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ (?<=(\b.+ ).)¶\1 saves you two bytes. \$\endgroup\$
    – Neil
    Jun 23, 2017 at 16:11
  • \$\begingroup\$ Bytes can be saved by using a deduplication stage instead of a replacement one as the last stage (you must then deal with the first line though) tio.run/##K0otycxL/… \$\endgroup\$
    – Leo
    Jun 23, 2017 at 16:15
  • \$\begingroup\$ @Leo Thanks, I'm going with a slight variant that doesn't end up with a leading linefeed. \$\endgroup\$ Jun 23, 2017 at 17:28
2
\$\begingroup\$

R, 12 bytes

stem(scan())

Try it online!

Explanation:

s               # imports RAND's "Million Random Digits"
  e  )          # cooks a pound of spaghetti and places it on the stack
 t              # transposes the output 42 times
       can      # goes for a pee
   m(           # grows moustache, turns head to side and frowns
      s   (     # implicitly ignores all criticism
           )    # makes a stemplot of the input
\$\endgroup\$
4
  • \$\begingroup\$ I think just stem would be sufficient, as it takes an array as input. \$\endgroup\$
    – Giuseppe
    Jun 29, 2018 at 16:43
  • \$\begingroup\$ That was the only way I could get an example to actually work on TIO. And I guess I'm used to the "program or function" style of answering, and am unsure about other formats. \$\endgroup\$
    – ngm
    Jun 29, 2018 at 16:47
  • 1
    \$\begingroup\$ Like this \$\endgroup\$
    – Giuseppe
    Jun 29, 2018 at 17:06
  • \$\begingroup\$ Agree with @Giuseppe, the answer should be just stem :) \$\endgroup\$
    – JayCe
    Jul 18, 2018 at 23:32
2
\$\begingroup\$

JavaScript (ES6), 89 bytes

a=>a.sort((a,b)=>a-b).map(e=>r[d=e/10|0]=(r[d]||d+` `)+e%10,r=[])&&r.filter(e=>e).join`
`
\$\endgroup\$
2
\$\begingroup\$

Python 2, 146 140 133 124 120 118 109 107 90 86 84 91 82 81 70 63 bytes

-6 bytes thanks to Rod. -9 bytes thanks to ovs.

lambda l:{i/10:[j%10for j in sorted(l)if j/10==i/10]for i in l}

Try it online!

Okay, something is slightly wonky. As all Python programmers should know, dicts are unordered, meaning the original order of the key-value pairs is not preserved. However, in my current code, I do not sort the resulting dict at all. Yet, I have tested multiple times, checking for equality and order every single time, and the dict always comes out right. If anybody either disproves that it always comes out right or knows why this works, I'd love to know.

Input as a python list and output as a dict. Example:

Input:

[1, 2, 3, 3, 3, 3, 3, 10, 15, 15, 18, 1, 100]

Output:

{0: [1, 1, 2, 3, 3, 3, 3, 3], 1: [0, 5, 5, 8], 10: [0]}
\$\endgroup\$
3
1
\$\begingroup\$

Mathematica, 103 bytes

Code taken from @user202729's deleted answer

Grid[Table[{Keys[#][[i]],""<>ToString/@#[[i]]},{i,Length@#}]]&@(GroupBy[Sort@#,⌊#/10⌋&]~Mod~10&@#)&
\$\endgroup\$
1
\$\begingroup\$

><>, 84 bytes

1&0\n~a7+3.
 :}<$?)@:$@:v!?
r~&^?-l&:+1&/&:,a-%a::
&=?v~&1+:&ao>n" "o:?!;::a%:@-a,&:

Try it online, or at the fish playground!

Assumes the input numbers are already on the stack.

Explanation: First, we sort the stack using a bubble sort, with this bit of code:

1&0\
 :}<$?)@:$@:v!?
   ^?-l&:+1&/

Next, we compute the integer-quotient of the first thing in the stack by 10 using ::a%-a,, put that in the register, and go through the stack printing the last digits of the numbers until their first digits aren't the same as the register, then incrementing the register and continuing. When we reach the end of the list, marked with a 0, we stop.

\$\endgroup\$
1
\$\begingroup\$

PostgreSQL, 53 bytes

SELECT n/10,json_agg(n%10ORDER BY n)FROM t GROUP BY 1

The list of integers must reside in an integer column n of an existing table t. The result is a two-column table: each row consists of a "stem" column and a "leaves" column. The "leaves" column is in JSON array format. (As noted in the comments, it is not necessary to adhere exactly to the format shown under "Test Cases".)

Though the order of stems is not guaranteed (to save 10 bytes, ORDER BY 1 is omitted from the end of the query), in my testing, the stems did seem to end up in the correct order.

View result on SQL Fiddle

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.