53
\$\begingroup\$

Make a program that takes the word you input, and adds that word to the back of itself minus its first letter, then repeats until all letters are gone. For example, cat would become catatt, and hello would become helloellolloloo.

Input
Any of the 26 letters of the English alphabet. There may be multiple words separated by spaces, and the change should be applied to every word.

Output
The word(s) inputted, with each word put after itself with its first letter missing, and then with its second letter missing, and so on until there are no more letters to add.

More examples:

ill eel outputs illlll eelell

laser bat outputs laserasersererr batatt

darth vader outputs dartharthrththh vaderaderdererr

This is code golf, so the shortest code wins.

Clarification:
You can treat the input or output as a list. You can separate words using newline instead of space. You can add a trailing space to the input.

\$\endgroup\$
17
  • 25
    \$\begingroup\$ honestly, the multiple words thing is kinda annoying. All it does is require a split, apply the function on each word, and then join again. It's also quite debilitating for lots of esolangs which have to check for a space manually \$\endgroup\$
    – Jo King
    Sep 18, 2018 at 22:44
  • 6
    \$\begingroup\$ Can we take in input as a list of words and output as such? \$\endgroup\$
    – Quintec
    Sep 18, 2018 at 23:53
  • 5
    \$\begingroup\$ What length words do you need to handle? \$\endgroup\$
    – MickyT
    Sep 19, 2018 at 1:14
  • 6
    \$\begingroup\$ Is it OK for words to be separated by a newline in the output(instead of a space)? \$\endgroup\$
    – JayCe
    Sep 19, 2018 at 1:37
  • 11
    \$\begingroup\$ 1. Please update the spec with the new allowances (array I/O, trailing space, etc.) 2. Please inform the existing solutions in case any can save bytes by taking advantage of them. \$\endgroup\$
    – Shaggy
    Sep 19, 2018 at 8:36

75 Answers 75

1 2
3
1
\$\begingroup\$

Kotlin, 48 bytes

Lambda takes a List<String> as input and returns a List<String> as output, which is allowed. The extra code in the footer is just so the input can be tested easily.

{it.map{it.indices.fold(""){a,v->a+it.drop(v)}}}

Try it online!

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

GolfScript, 23 bytes

" "/{{""=!}{(;}/}/]" "*

Try it online!

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

Julia 0.7, 75 65 bytes

p(a)=for i in a for j=1:endof(i) print(i[j:end])end;print(" ")end

Try it online!

First codegolf. Yay v0.7 becase it it just deprecates the endof method and 1.0 replaces it with lastindex.

Update: As mentioned in the comments I am not sure if an array of words as input is permitted. So I included the bytes to take a string as input

Update2: Apparently an array of strings is ok so here we go down to 65 bytes. Thx to @JonathanFrech for removing some more whitespace

\$\endgroup\$
6
  • \$\begingroup\$ I do not think taking a string array is valid. Please reflect splitting in your byte count. \$\endgroup\$ Sep 19, 2018 at 18:53
  • \$\begingroup\$ @JonathanFrech Yeah I'm not sure too. I just added the 7 more bytes to take a String as the argument. \$\endgroup\$
    – Huanzo
    Sep 19, 2018 at 20:12
  • \$\begingroup\$ You can remove some whitespace. \$\endgroup\$ Sep 19, 2018 at 20:16
  • \$\begingroup\$ @JonathanFrech oh thanks didn't know that you don't need the white spaces after). In the comments of the golf somebody asked if a list of words would be alright. And the author permitted that. One could argue that an array of words could count as a list. So my first answer would be alright. \$\endgroup\$
    – Huanzo
    Sep 19, 2018 at 20:26
  • \$\begingroup\$ @johnathan Actually the OP has commented that taking/returning a list of words is valid \$\endgroup\$
    – Jo King
    Sep 19, 2018 at 21:12
1
\$\begingroup\$

Multi User Forth(121 bytes), Protomuck variant.

" " explode_array { swap foreach nip { swap  begin dup 1 strcut nip dup not until }cat repeat }list " " array_join .tell

I guess if you want to require that I use entirely standard stuff, then .tell it becomes me @ swap notify which brings it up to 132 bytes. If you want to go the other direction, then I guess that swap could become .s and nip .n, dup .d etc, would turn it into

" " explode_array { .s foreach .n { .s  begin .d 1 strcut .n .d not until }cat repeat }list " " array_join .tell

Which has 113 bytes.

Luckily we can continue further, begin turns into .b, until .u and explode_array .e, if we use our thinking caps we can turn foreach into .f by defining that into foreach nip (who needs the index of the array anyway?) and we end at 94 bytes.

" " .e { .s .f { .s  begin .d 1 strcut .n .d not until }cat repeat }list " " array_join .tell

But, this is getting silly, and probably only hurting any chance of this being seen as a meaningful first post on codegolf.

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

C (clang), -DT=t=strtok -DZ=" ") 178 113 106 105 95 91 bytes

i;f(*a){char*t;for(T(a,Z;t;printf(Z,T(0,Z)for(i=0;t[i];)printf(t+i++);}

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ This will potentially fail on input longer than \$99\$ bytes. This is not a platform limitation, but your choice. I do not know if this is a valid answer. \$\endgroup\$ Sep 24, 2018 at 17:59
  • \$\begingroup\$ @JonathanFrech but a dynamically growing array to store input would be much longer code. The only other solution would be passing the input as an argument to the function. \$\endgroup\$
    – user36046
    Sep 24, 2018 at 18:04
  • \$\begingroup\$ Cool that that is also shorter; as I said -- I do not know if the other is invalid; only that it seemed a bit unelegant. \$\endgroup\$ Sep 24, 2018 at 19:03
  • \$\begingroup\$ Suggest T(i=0,Z)for(; instead of T(0,Z)for(i=0; \$\endgroup\$
    – ceilingcat
    Sep 25, 2018 at 4:13
1
\$\begingroup\$

Elixir, 122 bytes

a = ["Hello","World"]
f = fn n,f -> if(n != "") do "#{n}#{f.(String.slice(n, 1..-1), f)}" end end
Enum.map(a, &(f.(&1,f)))

# OUTPUT
["Helloellolloloo", "Worldorldrldldd"]

Try it online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Hi, welcome to PPCG! The input doesn't have to be in the actual code, so you can declare the list in the input section, and reference it by a single letter variable. \$\endgroup\$
    – Shikkou
    Sep 25, 2018 at 7:27
  • 1
    \$\begingroup\$ @AlexGrigore Actually, taking input as a predefined variable is not allowed. That would turn the submission into a snippet. Submissions should either be a full program or a function. \$\endgroup\$
    – Jo King
    Sep 26, 2018 at 0:12
  • 1
    \$\begingroup\$ The input shouldn't be hard-coded, but passed to the program/function. You can see what IO methods are allowed here. \$\endgroup\$
    – Jo King
    Sep 26, 2018 at 5:57
1
\$\begingroup\$

Scala, 81 bytes

def x(l:String):String=if(l=="")" "else l+x(l.tail)
println(args.map(x).mkString)

Try it online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Taking input as a predefined variable is not allowed. That would turn the submission into a snippet. Submissions should either be a full program or a function. \$\endgroup\$
    – Jo King
    Sep 26, 2018 at 6:19
  • \$\begingroup\$ Hello, @JoKing, thanks. I updated the answer. Does this follow the rules now? \$\endgroup\$
    – Shikkou
    Sep 27, 2018 at 10:22
  • \$\begingroup\$ If you're submitting ot as a full program, I'm afraid you're going to have to include the boilerplate as well \$\endgroup\$
    – Jo King
    Sep 27, 2018 at 12:34
1
\$\begingroup\$

sed, 42 37+2 bytes

s:\b:_:g
:A
s:_(.(\w*_))|__:\1\2:g
tA

Try it online!

Repeatedly match parts of words between underscores:

_hello_ _world_
hello_ello_ world_orld_
helloello_llo_ worldorld_rld_
helloellollo_lo_ worldorldrld_ld_
helloellollolo_o_ worldorldrldld_d_
helloellolloloo__ worldorldrldldd__
helloellolloloo worldorldrldldd
\$\endgroup\$
1
\$\begingroup\$

Kotlin Android, 204 bytes

fun f(s:String){ val a=s.split(" ".toRegex()).dropLastWhile{it.isEmpty()}.toTypedArray();for(i in a.indices){var t=0;while(t<a[i].length){for(j in t until a[i].length){print(a[i][j]+"")};t++};print(" ")}

}

Try online

This Program is written in Kotlin Android.

\$\endgroup\$
5
  • \$\begingroup\$ @Keeta, Thank you for your Update. \$\endgroup\$ Oct 5, 2018 at 7:39
  • \$\begingroup\$ Input should not be taken as a predefined variable otherwise this becomes a snippet. Submissions should be a full program or a function equivilant. \$\endgroup\$
    – Jo King
    Oct 19, 2018 at 10:25
  • \$\begingroup\$ Updated my code. \$\endgroup\$ Oct 19, 2018 at 10:29
  • \$\begingroup\$ Err, the program should take the word as input, not jard code "hello world" \$\endgroup\$
    – Jo King
    Oct 19, 2018 at 14:04
  • \$\begingroup\$ Updated my code. \$\endgroup\$ Oct 20, 2018 at 5:22
1
\$\begingroup\$

Javascript(ES6), 34 bytes

s=>s.map(k=x=>x?x+k(x.slice(1)):x)

Try it online!

Takes input as array of words, returns array of words.

Aaah, the power of recursion...

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

JavaScript (V8), 46 42 bytes

-4 thanks to Redwolf

w=>w.split` `.map(f=a=>a&&a+f(a.slice(1)))

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ -2: d=w=>w.split` `.map(f=a=>a&&a+f(a.slice(1))) (and then you don't need to do the d= according to site rules, so another -2) \$\endgroup\$ Dec 16, 2021 at 16:30
1
\$\begingroup\$

BQN, 4 bytes

∾∘↓¨

Anonymous tacit function that takes and returns a list of strings. Run it online!

Explanation

∾∘↓¨
   ¨  For each element of the list:
  ↓     Get a list of its suffixes
 ∘      and
∾       join them into one string
\$\endgroup\$
1
\$\begingroup\$

Thunno 2 , 3 bytes

€ṣṃ

Attempt This Online!

Input as a list of words. Output as a single string.

Explanation

€ṣṃ  # Implicit input
€ṣ   # Suffixes of each string in the list
  ṃ  # Reverse each inner list
     # Implicit output, joined
\$\endgroup\$
0
\$\begingroup\$

Factor + grouping.extras, 46 bytes

[ " "split [ tail-clump concat ] map " "join ]

Try it online!

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

sed, 52 bytes

s/[a-z]+/@&/g
:l
s/@([a-z]([a-z]+))/\1@\2/
tl
s/@//g

Try it online!

\$\endgroup\$
1 2
3

Your Answer

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

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