19
\$\begingroup\$

The challenge is relatively simple, but will hopefully lead to some wonderfully creative answers. The task is to take a string as an input (via command line, function parameter etc.) and output/return a stringsploded version of it.

A stringsploded version of a string is its first letter followed by its first 2 letters, followed by its first 3 letters,... until you finally output the complete word.

You are guaranteed that the input will consist of printable ASCII characters and that the input will all be on one line (no new line characters).

Here are examples to clarify what stringsplosions are:

Input                 Output
==================================================================
"Code"       ---->   "CCoCodCode"
"name"       ---->   "nnanamname"
"harambe"    ---->   "hhaharharaharamharambharambe"
"sp aces?"   ---->   "sspsp sp asp acsp acesp acessp aces?"
"1234"       ---->   "1121231234"
"ss"         ---->   "sss"

This is code golf so shortest code wins + up votes for interesting uses of your respective language.

Good luck!

\$\endgroup\$
16
  • 8
    \$\begingroup\$ String challenges should usually specify the valid input characters, because it's not clear whether the input may contain a) non-letters, b) unprintable/control characters, c) null bytes, d) extended ASCII characters, e) Unicode characters beyond code point 256, (... f) combining marks, g) surrogates ...), all of which can arbitrarily complicate the challenge in many languages. And since your test cases only cover letters one might assume that that's your intended input range. \$\endgroup\$ Commented Sep 7, 2016 at 14:13
  • 2
    \$\begingroup\$ You should add a test case where the first two characters are the same. \$\endgroup\$
    – mbomb007
    Commented Sep 7, 2016 at 14:26
  • 3
    \$\begingroup\$ As someone with a stutter, I'm OOfOffOffeOffenOffendOffendeOffended. \$\endgroup\$
    – Shaun Wild
    Commented Sep 7, 2016 at 15:40
  • 2
    \$\begingroup\$ Alternate title: SStStrStriStrinStringStringsStringspStringsplStringsploStringsplodStringsplode \$\endgroup\$
    – DJMcMayhem
    Commented Sep 7, 2016 at 17:38
  • 5
    \$\begingroup\$ Also related, near duplicate. \$\endgroup\$ Commented Sep 7, 2016 at 22:43

43 Answers 43

1
2
1
\$\begingroup\$

GNU sed, 30 bytes

:;G;h;s/.\n.*//;/./t;g;s/\n//g

Run:

echo -n "name" | sed -f stringsplode.sed
\$\endgroup\$
1
\$\begingroup\$

Perl 6,  23  16 bytes

{S:g/.?/{$/.prematch}/}  # <-- the Perl 5 entry as a Perl 6 lambda
{[R~] m:ex/^.*/}
{[~] m:ex/^.*?/}

Explanation:

# bare block lambda with implicit parameter 「$_」
{
  # list reduce using string concatenation operator
  [~]

  # the input implicitly matched against
  m

  # in all possible ways
  :exhaustive

  /
    ^   # must match from the beginning
    .   # any character
    *?  # any number of times non-greedily
  /
}

The m:ex/^.*?/ part produces a list of Match values (「」,「C」,「Co」,「Cod」,「Code」).

The other option m:ex/^.*/ produces the Match values in exactly the opposite order, so they have to be combined in reverse using the R meta operator.

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

Pyke, 5 bytes

FKoh<

Try it here!

      - o = 0
FK    - for i in input:
  oh  -   1+o++
    < -  input[:^]
      - "".join(^)
\$\endgroup\$
3
  • \$\begingroup\$ What does K do? \$\endgroup\$
    – Cyoce
    Commented Sep 7, 2016 at 23:24
  • \$\begingroup\$ The F automatically pushes each character to the stack, the K removes it \$\endgroup\$
    – Blue
    Commented Sep 7, 2016 at 23:25
  • \$\begingroup\$ then I suggest you put FK on the same line in the explanation \$\endgroup\$
    – Cyoce
    Commented Sep 7, 2016 at 23:26
1
\$\begingroup\$

Lua, 58

u=''for s in io.read():gmatch('.')do u=u..s io.write(u)end

Saved 5 Bytes thanks to manatwork

I love using lua's gmatch and gsub for golfing.

Simply takes io.read(), gmatch's it for any single character, iterating through the entire string. it assigns the variable st, which slowly builds the entire string as lua iterates, and at every iteration, writes the contents of st.

\$\endgroup\$
1
  • \$\begingroup\$ Just some general golfing things: you can choose shorter variable name and there is an unnecessary space in front of end. \$\endgroup\$
    – manatwork
    Commented Sep 8, 2016 at 6:25
0
\$\begingroup\$

Go, 90 bytes

package main;import."fmt";func main(){s:="";Scan(&s);for i:=0;i<=len(s);i++{Print(s[:i])}}
\$\endgroup\$
0
\$\begingroup\$

BASH (grep + sed + tr) 36

grep -o .|sed ':;p;N;$!b'|tr -d '\n'
\$\endgroup\$
1
  • \$\begingroup\$ '\n'\\n. \$\endgroup\$
    – manatwork
    Commented Sep 8, 2016 at 6:14
0
\$\begingroup\$

PHP , 52 Bytes

for(;$i<strlen($t=$argv[1]);)echo substr($t,0,++$i);
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Seems you forgot to clean up after previous attempt: variable $l is not used anymore. \$\endgroup\$
    – manatwork
    Commented Sep 7, 2016 at 16:37
0
\$\begingroup\$

MATL, 8 bytes

"GX@:)v!

This uses a different approach from @Suever's answer.

Try it online!

Explanation

        % Implicit input: string
"       % For each. This repeats as many times as chars in the input string
  G     %   Push input again
  X@    %   Push iteration index, k (varies from 1 to input length)
  :)    %   Get k first chars from input
  v!    %   Concatenate all stack contents horizontally
        % Implicit end
        % Implicit display
\$\endgroup\$
0
\$\begingroup\$

Japt, 5 bytes

Uå+ q

Test it online!

How it works

       // Implicit: U = input string
Uå+    // Cumulative-reduce the input by addition.
       // This returns an array of all prefixes.
    q  // Join with the empty string.
       // Implicit: output last expression
\$\endgroup\$
0
\$\begingroup\$

Java 7, 74 bytes

String h(String a){int l=a.length();return l>1?h(a.substring(0,l-1))+a:a;}

or

String j(String a){return a.isEmpty()?a:j(a.substring(0,a.length()-1))+a;}

Recursive version of Geobits answer.

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

Haskell (lambdabot), 10 bytes

join.inits  

Sample:

(join.inits) "Code"
"CCoCodCode"
\$\endgroup\$
1
  • \$\begingroup\$ This doesn't work in standard Haskell, because you need imports for both join and inits which are added to the byte count. If you have an interpreter/compiler that imports them by default, please specify it as part of the language name. Thanks! \$\endgroup\$
    – nimi
    Commented Sep 7, 2016 at 22:32
0
\$\begingroup\$

Racket 81 bytes

(λ(s)(apply string-append(for/list((i(+ 1(string-length s))))(substring s 0 i))))

More readable form:

(define(f s)
  (apply
   string-append
   (for/list
       ((i(+ 1(string-length s))))
     (substring s 0 i))))

Testing:

(f "Code")
"CCoCodCode"
\$\endgroup\$
0
\$\begingroup\$

PHP, 39 37 35 bytes

while($c=$argv[1][$i++])echo$s.=$c;

works for all examples. String should not countain the 0 character.

PHP, 50 40 38 bytes

while(''<$c=$argv[1][$i++])echo$s.=$c;
\$\endgroup\$
1
2

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