# Create a stem-and-leaf plot

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

• Sandbox Commented Jun 23, 2017 at 13:34
• It need to be sorted and/or preserve the input order?
– Rod
Commented Jun 23, 2017 at 13:35
• @Rod by definition, it sorts the input by tens, then by ones. Input order doesn't matter. Commented Jun 23, 2017 at 13:37
• The output format MUST be like that? Is my answer valid?
– Rod
Commented Jun 23, 2017 at 14:04
• @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 Commented Jun 23, 2017 at 16:56

# Jelly, 17 bytes

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


Try it online!

# Python 2, 7875 79 bytes

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


Try it online!

## 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! • (?<=(\b.+ ).)¶\1 saves you two bytes. – Neil Commented Jun 23, 2017 at 16:11 • 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/… – Leo Commented Jun 23, 2017 at 16:15 • @Leo Thanks, I'm going with a slight variant that doesn't end up with a leading linefeed. Commented Jun 23, 2017 at 17:28 # 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  • I think just stem would be sufficient, as it takes an array as input. Commented Jun 29, 2018 at 16:43 • 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. – ngm Commented Jun 29, 2018 at 16:47 • Like this Commented Jun 29, 2018 at 17:06 • Agree with @Giuseppe, the answer should be just stem :) Commented Jul 18, 2018 at 23:32 ## 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   # Python 2, 14614013312412011810910790868491828170 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]}  • you can replace the try/except with bitwise validation – Rod Commented Jun 23, 2017 at 13:52 • r[i/10]=r.get(i/10,'')+i%10 for 82 bytes – ovs Commented Jun 23, 2017 at 17:09 • I think dicts are sorted by default? just not in the original order Commented Jun 24, 2017 at 22:36 # Mathematica, 103 bytes Code taken from @user202729's deleted answer Grid[Table[{Keys[#][[i]],""<>ToString/@#[[i]]},{i,Length@#}]]&@(GroupBy[Sort@#,⌊#/10⌋&]~Mod~10&@#)&  ## ><>, 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.

# 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