54
\$\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\$
0

42 Answers 42

1
2
1
\$\begingroup\$

Python, 66 62 bytes

Try it here

Golfed

def s(w,o=[],n=""):
 for x in w:o+=x;n+=x*o.count(x)
 return n

Ungolfed

def stretch(word):
    occurrences = []
    newWord = ""
    for w in word:
        occurrences.append(w)
        newWord += w * occurrences.count(w)
    return newWord
\$\endgroup\$
1
\$\begingroup\$

Vyxal sr, 4 bytes

¦¨£↔

Try it Online!

\$\endgroup\$
1
\$\begingroup\$

Knight, 77 bytes

;=x=yP;Wx;E++"=x"A Ax" 0"=xSxF1""Wy;E++++"O+*A Ay=x"=aA Ay"+1x"a"'\'"=ySyF1""

Try it online!

Ungolfed:

; = x (= y PROMPT)
; WHILE x
    ; EVAL (+ (+ "=x" (ASCII (ASCII x))) " 0")
    : = x (SUBSTITUTE x 0 1 "")
: WHILE y
    ; EVAL (+ (+ (+ (+ "O+*A Ay=x" (= a (ASCII (ASCII y)))) "+1x") a) "'\'")
    : = y (SUBSTITUTE y 0 1 "")
\$\endgroup\$
1
\$\begingroup\$

BQN, 7 6 bytes

⊢/˜1+⊒

Anonymous tacit function. Try it at BQN online!

Explanation

⊢/˜1+⊒
     ⊒   For each character, how many times has it appeared previously?
   1+    Add 1 to each
 /˜      Replicate that many times the elements of
⊢        The original input
\$\endgroup\$
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\$

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\$
1
  • \$\begingroup\$ This isn't quite valid; you aren't supposed to double each time the letter reappears, but just increase the number of repetitions by 1. So aaa should become aaaaaa ([a] [aa] [aaa]), whereas yours outputs aaaaaaa ([a] [aa] [aaaa]). \$\endgroup\$
    – hyper-neutrino
    Commented Nov 20, 2021 at 21:20
0
\$\begingroup\$

brev, 46 bytes

(with(ct*'a 0)(as-list flatten(over(it x x))))

If output like ("m" "i" "s" "ss" "ii" "sss" "ssss" "iii" "p" "pp" "iiii") were legal you could cut out the as list flatten for seventeen bytes.

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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