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 – 8 years ago
is the input guaranteed to be non-empty? – dzaima – 8 years ago
@dzaima Yes, the input is guaranteed non-empty. There will be at least one word. – AdmBorkBork – 8 years ago
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 – 8 years ago
@dylnan Right. The Y-axis should only be large enough to contain the data. – AdmBorkBork – 8 years ago
@dylnan No, not necessarily. The input could consist of only one word without any spaces. – AdmBorkBork – 8 years ago
2
Just being a pendant, but this isn't a histogram, it's a bar chart. Still a nice challenge though!
– caird coinheringaahing – 8 years ago"Leading/trailing newlines or other whitespace are optional, provided that the characters line up appropriately." -- so columns of whitespace are acceptable? – Jonathan Allan – 8 years ago
@JonathanAllan If you're asking if you could do something like
abc efor 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 – 8 years agoI was asking for
a d e hstyle rather thana de h– Jonathan Allan – 8 years ago@JonathanAllan The letters must be adjacent. I'll make that explicitly clear. – AdmBorkBork – 8 years ago
Maybe add test cases that test extension of the Y-axis at the right points, i.e. with 10 and with 11? – Adám – 8 years ago
Can the y-axis labels be left-aligned? – Magic Octopus Urn – 8 years ago
@MagicOctopusUrn Nope, right-aligned. That's just the rules. ;-) – AdmBorkBork – 8 years ago
Does the X axis need to be sorted? – Erik the Outgolfer – 8 years ago
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 – 8 years ago
@dmckee For some cases that could be golfier too! :P – Erik the Outgolfer – 8 years ago
Related, but only in the output style. – FryAmTheEggman – 8 years ago
2The histogram character has to be consistent, but across cases or within each case? – Adám – 8 years ago
@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 – 8 years ago
a quick brown fox jumpest over a lazy doghas fewer extra letters. – mbomb007 – 8 years ago