33
Given an input sentence consisting of one or more words [a-z]+
and zero or more spaces , output an ASCII-art histogram (bar graph) of the letter distribution of the input sentence.
The histogram must be laid out horizontally, i.e. with the letter key along the bottom in alphabetical order from left to right, with a Y-axis labeled 1-
and every 5 units. The Y-axis must be the smallest multiple of five that is at least as tall as the tallest bar, and must be right-aligned. The X-axis is labeled with the input letters, with no gaps between. For example, input a bb dd
should have label abd
and not ab d
, skipping the c
. The bars themselves can be made of any consistent ASCII character -- I'll be using X
here in my examples.
test example
5-
X
X X
1-XXXXXXXX
aelmpstx
Since there are three e
, two t
, and one of almsx
.
More examples:
the quick brown fox jumped over the lazy dogs
5-
X X
X X
XX X X X XX
1-XXXXXXXXXXXXXXXXXXXXXXXXXX
abcdefghijklmnopqrstuvwxyz
now is the time for all good men to come to the aid of their country
10-
X
X
X X
X X X
5- X X X
X X X X
X XX XXXX X
XXXXX XXXXXXX X
1-XXXXXXXXXXXXXXXXXX
acdefghilmnorstuwy
a bb ccc dddddddddddd
15-
X
X
10- X
X
X
X
X
5- X
X
XX
XXX
1-XXXX
abcd
a bb ccccc
5- X
X
X
XX
1-XXX
abc
I/O and Rules
- Input can be taken in any reasonable format and by any convenient method. This also means you can take input in all-uppercase, if that makes more sense for your code.
- Leading/trailing newlines or other whitespace are optional, provided that the characters line up appropriately.
- Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
- Output can be to the console, returned as a list of strings, returned as a single string, etc.
- Standard loopholes are forbidden.
- This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
3I think this would be a bar graph rather than a histogram, as it's categorical rather than numeric data, but I'm mostly being pedantic. – Giuseppe – 2018-04-11T19:03:04.647
is the input guaranteed to be non-empty? – dzaima – 2018-04-11T19:03:11.613
@dzaima Yes, the input is guaranteed non-empty. There will be at least one word. – AdmBorkBork – 2018-04-11T19:04:29.240
To confirm, we can't go up to, say, 15 lines in the first example and include y axis markers for 10 and 15, right? – dylnan – 2018-04-11T19:24:42.390
@dylnan Right. The Y-axis should only be large enough to contain the data. – AdmBorkBork – 2018-04-11T19:26:25.883
@dylnan No, not necessarily. The input could consist of only one word without any spaces. – AdmBorkBork – 2018-04-11T19:34:18.777
2
Just being a pendant, but this isn't a histogram, it's a bar chart. Still a nice challenge though!
– caird coinheringaahing – 2018-04-11T19:36:57.587"Leading/trailing newlines or other whitespace are optional, provided that the characters line up appropriately." -- so columns of whitespace are acceptable? – Jonathan Allan – 2018-04-11T19:42:40.743
@JonathanAllan If you're asking if you could do something like
abc e
for the columns, no that's not okay. If you have a whole leading column of whitespace (e.g., a leading space on every line), that's fine. – AdmBorkBork – 2018-04-11T19:48:08.453I was asking for
a d e h
style rather thana de h
– Jonathan Allan – 2018-04-11T19:50:12.263@JonathanAllan The letters must be adjacent. I'll make that explicitly clear. – AdmBorkBork – 2018-04-11T19:51:20.497
Maybe add test cases that test extension of the Y-axis at the right points, i.e. with 10 and with 11? – Adám – 2018-04-11T19:51:58.780
Can the y-axis labels be left-aligned? – Magic Octopus Urn – 2018-04-11T19:53:12.760
@MagicOctopusUrn Nope, right-aligned. That's just the rules. ;-) – AdmBorkBork – 2018-04-11T19:56:00.210
Does the X axis need to be sorted? – Erik the Outgolfer – 2018-04-11T19:57:39.757
4A Tuftian approach would be to make the bars out of the characters represented and not have a separate label row. – dmckee --- ex-moderator kitten – 2018-04-11T21:43:17.443
@dmckee For some cases that could be golfier too! :P – Erik the Outgolfer – 2018-04-11T21:56:35.993
Related, but only in the output style. – FryAmTheEggman – 2018-04-12T04:27:50.173
2The histogram character has to be consistent, but across cases or within each case? – Adám – 2018-04-12T07:38:05.770
@Adám It needs only be consistent within the same case. If it's different on subsequent runs, or different inputs, that's fine. I just don't want a mishmash of characters so you can't understand the graph. – AdmBorkBork – 2018-04-12T12:29:53.073
a quick brown fox jumpest over a lazy dog
has fewer extra letters. – mbomb007 – 2018-04-12T14:57:13.637