Tell me more ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Given a string, mark which parts in the string repeat in box brackets, and write how many times it does so before the box.

Example:st st st Hello ThereThere
st(with a space after) repeats 3 times, There repeats 2 times, and l repeats 2 times in Hello, so the output will be:
3[st ]He2[l]o 2[There]
There is no space between [st ] and Hello because the space in the string is being taken up by the repeating part. [st ], 3 times with a Hello after it would be st st st Hello, which is what the string has.

Other examples:
st Hello st st Bye -> st He2[l]o 2[st ]Bye
Bookkeeper -> B2[o]2[k]2[e]per
/\/\/\\\\\\\\/\/\/ -> 3[/\]6[\]3[\/]

Shortestest code wins.

EDIT: To clarify the choice of selections, choose the largest possible repeating section, and try and leave as little non-repeating characters as possible. Do not nest boxed-in sections.

share|improve this question
2  
This is ambiguous. For example, ababababcabcabc -> 4[ab]2[cab]c? 4[ab]c2[abc]? 3[ab]3[abc]? What bout abcdefdefghiabcdefdefghi -> 2[abcdefdefghi]? abc2[def]ghiabc2[def]ghi? 2[abc2[def]ghi]? – Mechanical snail Jul 26 '12 at 6:06
1  
this is not possible ;) just the typical first answer I get whenever I ask a developer something. – BinaryNights Jul 26 '12 at 6:41
3  
Shouldn't the output to your first example be "3[st ]He2[l]o 2[There]"? (Same for the second.) – Howard Jul 26 '12 at 8:20
1  
Why gets “st Hello st st Bye” transformed into “st Hello 2[st ]Bye” and not into “st Hello2[ st] Bye”? So the repeating substring why not starts with the first repeated character, the space before “st”? – manatwork Jul 26 '12 at 8:54
2  
The specification is still ambiguous because it lists multiple goals without clearly stating their relative priority, and without indicating what level of naïvety is acceptable. I don't see the point in constructing complicated test cases to elucidate the points: either the question needs to be largely rewritten to give precise acceptance criteria or it needs to be amended to indicate that wide latitude will be given. – Peter Taylor Jul 28 '12 at 21:33
show 6 more comments

4 Answers

up vote 4 down vote accepted

PHP 75 bytes

<?=preg_filter('/(.+)\1+/e','substr_count("\0","\1")."[\1]"',fgets(STDIN));

Sample input/output:

$ echo st Hello st st Bye | php string-loops.php
st He2[l]o2[ st] Bye

$ echo Bookkeeper | php string-loops.php
B2[o]2[k]2[e]per

$ echo /\/\/\\\\\\\\/\/\/ | php string-loops.php
3[/\]2[\\\]3[\/]

Perl 46 bytes

Same code in perl:

#!perl -p
s!(.+)\1+!@{[$&=~/\Q$1/g]}."[$1]"!eg
share|improve this answer
+1 for perl -p – xiaomao Aug 22 '12 at 0:51

scala, 102 chars

print("(?!\\s)(.+)\\1+".r.replaceAllIn(readLine,m=>m.matched.size/m.group(1).size+"["+m.group(1)+"]"))

Output

st st st Hello ThereThere ->    3[st ]He2[l]o 2[There]
st Hello st st Bye        ->    st He2[l]o 2[st ]Bye
Bookkeeper                ->    B2[o]2[k]2[e]per
/\/\/\\\\\\\\/\/\/        ->    3[/\]2[\\\]3[\/]
share|improve this answer
+1, nice use of Regex.Match – schnaader Jul 26 '12 at 19:22

C, 224 205 203 199 195 193 chars

The string is given as program parameter. As usual, use double quotes if your string contains spaces, and for empty strings - calling the program without any parameter crashes, but calling with "" works.

Output for test cases is:

st st st Hello ThereThere => 3[st ]He2[l]o 2[There]
Bookkeeper                => B2[o]2[k]2[e]per
/\/\/\\\\\\\\/\/\/        => 3[/\]2[\\\]3[\/]

which looks correct to me regarding "choose the largest possible repeating section".

Code:

l,r,f,i;char*c;main(a,b)char**b;{l=strlen(c=b[1]);for(;putchar(*c);c++)for(a=l;--a;f=0)for(r=a;!f;r+=a){for(i=0;i<a;)f|=r>l||c[i]-c[r+i++];if(f*r>a)c[a]=0,printf("\b%i[%s]",r/a,c),c+=r-1,a=1;}}
share|improve this answer

Javascript

f(s){return s.indexOf("loops")>=0}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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