49
\$\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\$
  • 22
    \$\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 '18 at 22:44
  • 4
    \$\begingroup\$ Can we take in input as a list of words and output as such? \$\endgroup\$ – Quintec Sep 18 '18 at 23:53
  • 4
    \$\begingroup\$ What length words do you need to handle? \$\endgroup\$ – MickyT Sep 19 '18 at 1:14
  • 5
    \$\begingroup\$ Is it OK for words to be separated by a newline in the output(instead of a space)? \$\endgroup\$ – JayCe Sep 19 '18 at 1:37
  • 10
    \$\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 '18 at 8:36

65 Answers 65

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\$
  • 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 '18 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 '18 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 '18 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\$
  • 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 '18 at 6:19
  • \$\begingroup\$ Hello, @JoKing, thanks. I updated the answer. Does this follow the rules now? \$\endgroup\$ – Shikkou Sep 27 '18 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 '18 at 12:34
1
\$\begingroup\$

C#, 193 bytes

using System;class a{static void Main(string[]b){foreach(var c in b)d(c, 0);}static void d(string c,int e){if(e==c.Length-1){Console.Write(" ");return;}Console.Write(c.Substring(e++));d(c, e);}}

Try it Online!

Since .NET automatically splits the arguments at spaces, it's possible to loop over them without splitting them manually.
Other than that, this uses recursion to subtract one character at a time using the overload of string.Substring that takes only 1 argument which is the index to start at.

\$\endgroup\$
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\$
  • \$\begingroup\$ @Keeta, Thank you for your Update. \$\endgroup\$ – Syed Hamza Hassan Oct 5 '18 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 '18 at 10:25
  • \$\begingroup\$ Updated my code. \$\endgroup\$ – Syed Hamza Hassan Oct 19 '18 at 10:29
  • \$\begingroup\$ Err, the program should take the word as input, not jard code "hello world" \$\endgroup\$ – Jo King Oct 19 '18 at 14:04
  • \$\begingroup\$ Updated my code. \$\endgroup\$ – Syed Hamza Hassan Oct 20 '18 at 5:22

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.