8
0
Recently there was a question on Stack Overflow where the OP was trying to write a function to find the word in a string that has the most repeated letters. It is of course not hard to write one up in seconds, and I wrote one in Javascript as short as I can for fun. But I'm not an expert in code golfing, so I wonder how much shorter can this simple program be!
Challenge
Write a program or function that takes in a string of words and return or print the word with the most repeating single letters.
Rules:
Choose the word with the greatest number of repeating single letters (see examples below)
If no word has repeating letters, return -1.
If two words have the same maximum number of repeating letters, choose the one closer to the start of the string.
The shortest submission in bytes wins.
Input
Take as input a string consisting of one or more space-delimited words. Input can be from STDIN (or closest alternative), command line parameters, or function arguments.
Output
Print the output to STDOUT to return it.
Examples
Consider the string aaabbb cccc
. This contains two words: aaabbb
and cccc
. The word aaabbb
has 3 a
's and 3 b
's, and cccc
has 4 c
's. So the maximum number of repeated letters in aaabbb
is 3 and the maximum in cccc
is 4. We want to choose the word with the maximum number of repeated single letters, so the output for aaabbb cccc
should be cccc
.
Other test cases:
Today, is the greatest day ever! --> greatest
This is a great day --> -1
aaabbb cccc --> cccc
What if more than one words have the same # of repeated letters? Do we print any or all? – Maltysen – 2015-07-20T06:54:20.710
@Maltysen Look at the first example - see
ever
– isaacg – 2015-07-20T06:57:29.937@isaacg but greatest has two repeating, t and e. – Maltysen – 2015-07-20T06:58:47.470
2I'm not sure what is meant by the number of repeated letters. I assume
aabb
has 2 repeated letters. Wouldaaaabb
be considered to have 4 repeated letters (2nd, 3rd, 4tha
, 2ndb
) or 2 repeated letters (a
andb
). – feersum – 2015-07-20T06:59:02.643@feersum
aaaabb
would count as 4. Pick the maximum of the numbers of repetitions in each word. – Derek 朕會功夫 – 2015-07-20T07:02:38.940@Maltysen Only print one of them if there are more than one word with the same number of repeated letters. – Derek 朕會功夫 – 2015-07-20T07:05:53.743
@Derek朕會功夫 To clarify, by
repetition
do you simply mean a letter that has appeared previously in the word? – isaacg – 2015-07-20T07:33:05.610@isaacg I mean, for example
aaaabb
would be chosen overaabbb
since the former has 4 repetitions of "a" which is more than 3 repetitions of "b" in the latter. – Derek 朕會功夫 – 2015-07-20T07:35:54.817So, which would win,
aaabbb
orcccc
- do the 2nd and 3rd a's and 2nd and 3rd b's win, or does the 4 times repeatedc
win? – isaacg – 2015-07-20T07:44:12.447@isaacg
cccc
wins, since althoughaaabbb
has 3 a's and 3 b's,cccc
has 4 c's which is greater than both 3's. – Derek 朕會功夫 – 2015-07-20T07:45:47.653@Derek朕會功夫 I'm still not totally sure on the rules for greatest repeats. Could you please write out the conditions explicitly? – xnor – 2015-07-20T10:03:37.940
The way I understand it based on the examples and comments, I think it would be clearer to say "most repeated letter" instead of "most repeated letters" in the question, because only a single letter counts. Or maybe "the word that contains the highest frequency of a single letter". – Reto Koradi – 2015-07-20T14:46:36.607
Will the words contain any non-alphabetical characters (other than ending punctuation)? – kirbyfan64sos – 2015-07-20T17:15:14.233
@xnor I added some clarifications in the question. Sorry for the confusion! – Derek 朕會功夫 – 2015-07-20T19:44:19.460
@kirbyfan64sos You can assume punctuation marks will not repeat in the same word, ie "a,,,b" will not happen. – Derek 朕會功夫 – 2015-07-20T19:46:50.273
I made edits to the wording and formatting of the challenge. If my edits do not match your original intent, please roll back. Note that it was unclear what should happen in the case of multiple output candidates, so I added the specification from the original SO question. – Alex A. – 2015-07-21T17:49:03.893
1Note that the original question is from Coderbyte. I looked on their website for copyright information (since this is a reproduction of Letter Count I) but I couldn't find anything. – Alex A. – 2015-07-21T17:54:30.570