50
\$\begingroup\$

The input is a word of lowercase letters not separated by whitespace. A newline at the end is optional.

The same word must be output in a modified version: For each character, double it the second time it appears in the original word, triple it the third time etc.

Example input:

bonobo

Example output:

bonoobbooo

Standard I/O rules apply. The shortest code in bytes wins.

Tests provided by @Neil :

tutu -> tuttuu
queue -> queuuee
bookkeeper -> boookkkeeepeeer
repetitive -> repeetittiiveee
uncopyrightables -> uncopyrightables
abracadabra -> abraacaaadaaaabbrraaaaa
mississippi -> misssiisssssssiiipppiiii
\$\endgroup\$

38 Answers 38

0
\$\begingroup\$

Ruby, 50 48 bytes

Anonymous function.

->s{i=0
s.chars.map{|c|c*s[0,i+=1].count(c)}*''}
\$\endgroup\$
0
\$\begingroup\$

Go, 142 bytes

No awards, but a neat exercise.

import(."fmt"
."strings")
func f(s string){m:=map[rune]int{}
for _,c:=range s{if _,ok:=m[c];!ok{m[c]=1}
Print(Repeat(string(c),m[c]))
m[c]++}}

You can save 1 byte by changing for _,c to for i,c and changing string(c) to s[i:i+1], but then it works only with ASCII.

\$\endgroup\$
0
\$\begingroup\$

Factor, 120 bytes

[ dup dup length iota [ head* ] with map reverse zip [ first2 over [ = ] curry count swap <array> >string ] map concat ]

Ungolfed, explained:

! "bonobo" -> { "b" "bo" "bon" "bono" "bonob" "bonobo" }
: ascend-string ( string -- string ascending-seq )
    dup dup length iota [ head* ] with map reverse ;

: stretch-word ( string -- stretched )
    ascend-string zip        ! zip string with ascending version of itself
    [
        first2 over          ! get each string index and its ascended version 
        [ = ] curry count    ! count how many times this letter has appeared
        swap <array> >string ! >string
    ] map concat ;           ! "" join

Translation of: @KennyLau's Pyth answer.

\$\endgroup\$
0
\$\begingroup\$

Ruby, 35 bytes

->s{s.gsub(/./){$&*-~$`.count($&)}}

It's an anonymous function using regex and some magical variables. Usage:

->s{s.gsub(/./){$&*-~$`.count($&)}}["bonobo"]
=> "bonoobbooo" 
\$\endgroup\$
0
\$\begingroup\$

Swift, 134 bytes

var c = NSCountedSet(),n = ""
i.characters.forEach({var s = String($0);c.addObject(s);for _ in 0..<c.countForObject(s){n.append($0)}})

Assumes Foundation has been implicitly imported. i is the input of type String, n is the output of type String.

I'm falling back on Foundation classes here as NSCountedSet gives the the character count lookup behavior we want. The problem with mixing Swift and Objective-C types is that Swift structs do not conform to NSObjectProtocol. We see this issue when trying to store a Swift Character (String.CharacterView) in an NSCountedSet, which is expecting objects of type AnyObject (or NSObject). I lose around 17 bytes having to create a String from each Character in the input string.

I also lose some bytes having to create a for loop to append the new characters based on their count in the set. I can't use forEach here since the return type of countForObject is an Int, not a collection type. There is room for improvement here.

Swift String types are immutable, so I can't explicitly mutate the input. There may be room for improvement by using an NSMutableString, but the characters lost by explicitly declaring the input of that type and calling the appendString method may add more characters to the program.

\$\endgroup\$
0
\$\begingroup\$

Java 10, 220 94 bytes

s->{var m=new int[123];for(var c:s.toCharArray())for(int i=++m[c];i-->0;System.out.print(c));}

Try it online.

Explanation:

s->{                   // Method with String parameter and no return-type
  var m=new int[123];  //  Integer-array containing 123 zeros
  for(var c:s.toCharArray())
                       //  Loop over the characters of the input-String
    for(int i=++m[c];  //   Increase the count of the current character in the array first,
                       //   and set `i` to this count
        i-->0;         //   Inner loop that many times
      System.out.print(c));}
                       //    And print that many times the current character `c`
\$\endgroup\$
0
\$\begingroup\$

PHP, 46 bytes

for(;--$X||($C=$argn[$K++])&&$X=++$$C;)echo$C;

Run as pipe with -nR or try it online.

These 42 bytes print the same, but the loop is infinite (which results in a timeout error):

for(;--$X||$X=++${$C=$argn[$K++]};)echo$C;

printf solution, 47 bytes:

for(;$C=$argn[$K++];)printf("%'$C".++$$C.s,"");

Side note: PHP used to yield notices for undefined constants and cast them to string for decades. PHP 7.2 yields warnings with a hint that, in future versions, they will throw fatal errors.

End of an era. :-/ I wonder if they will do the same to undefined variables.

\$\endgroup\$
0
\$\begingroup\$

Kotlin, 60 bytes

{s:String->s.fold(""){a,c->a+"$c".repeat(a.count{it==c}+1)}}

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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