37
\$\begingroup\$

Nice verb there, in the title.

Write a program that given an input string, will "elasticize" this string and output the result. Elasticizing a string is done as follows:

The first character is shown once. The second character is shown twice. The third character is shown thrice, and so on.

As you can see, the amount of duplications of a certain character is related to the character's index as opposed to its previous occurrences in the string.

You can expect to receive only printable ASCII characters. Based off the following link, these characters have decimal values 32-126.

Examples:

Why: Whhyyy

SKype: SKKyyyppppeeeee

LobbY: LoobbbbbbbYYYYY (Note how there are 7 b's since the first b is shown 3 times and the second b is shown 4 times, making a total of 7 b's).

A and B: A aaannnnddddd BBBBBBB

Shortest bytes wins :)

\$\endgroup\$
8
  • 2
    \$\begingroup\$ That seems to disagree with "no support for whitespace is needed, other than the space character". Should the output be the same as the input then? (Two one letter words?) Also note we have a nice place called the Sandbox where you can put challenges for people to give you feedback before posting them. \$\endgroup\$ Jun 20, 2016 at 19:50
  • \$\begingroup\$ FryAmTheEggman your assumption is valid. @TimmyD I realize where I was unclear, you may end up with strings separated my multiple spaces, as seen in the example FryAmTheEggman posted. \$\endgroup\$ Jun 20, 2016 at 19:52
  • \$\begingroup\$ I'm assuming that the shortest code wins? ;) \$\endgroup\$
    – Adnan
    Jun 20, 2016 at 19:54
  • \$\begingroup\$ @Adnan Yep, though I'm not sure if I should mark the answer with the shorted program as accepted, as certain languages are made for golfing purposes unlike others. \$\endgroup\$ Jun 20, 2016 at 19:56
  • 2
    \$\begingroup\$ Related: 1, 2 \$\endgroup\$
    – Sp3000
    Jun 21, 2016 at 3:36

64 Answers 64

2
\$\begingroup\$

MATL, 5 bytes

tn:Y"

Try it Online

Explanation

    % Implictly grab input as a string
tn  % Duplicate and compute the length (N)
:   % Create an array from [1...N]
Y"  % Perform run-length decoding to elacticize the string
    % Implicitly display the result
\$\endgroup\$
2
\$\begingroup\$

Python, 40 bytes

f=lambda s,i=1:s and s[0]*i+f(s[1:],i+1)
\$\endgroup\$
0
2
\$\begingroup\$

Julia, 34 bytes

!s=s>""?!s[1:(e=end)-1]*s[e:e]^e:s

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Your solution was good. But I managed to beat it. \$\endgroup\$
    – Glen O
    Jun 21, 2016 at 9:24
  • \$\begingroup\$ I saw. I had c%n="$c"^n;~s=join([s[r=1:end]...].%r), but that's actually longer. split was the missing piece of the puzzle. \$\endgroup\$
    – Dennis
    Jun 21, 2016 at 15:00
2
\$\begingroup\$

TSQL, 97 bytes

Golfed:

DECLARE @x varchar(max)='Lobby'
DECLARE @ int=LEN(@x)WHILE @>0SELECT
@x=STUFF(@x,@,1,REPLICATE(SUBSTRING(@x,@,1),@)),@-=1PRINT @x

Ungolfed:

DECLARE @x varchar(max)='Lobby'

DECLARE @ int=LEN(@x)
WHILE @>0
  SELECT 
    @x=STUFF(@x,@,1,REPLICATE(SUBSTRING(@x,@,1),@)),
    @-=1

PRINT @x

Try it online

\$\endgroup\$
2
\$\begingroup\$

Mathematica, 46 bytes

""<>Table@@@(#^Range@Length@#&@Characters[#])&

Unnamed function. Takes the characters of the input string and raises them to the power of their position in the string (i.e. "abc" becomes {"a"^1, "b"^2, "c"^3}). Yes, MMA don't give a shit 'bout types :-)

The FullForm of the above list elements is Power["a", 1], etc.
Table@@@ acts on the list, replacing the head of each element (in this case Power) with Table.
Results in {Table["a"], Table["b", 2], Table["c", 3]} (because "a"^1 -> "a").
This evaluates to {"a", {"b", "b"}, {"c", "c", "c"}} Finally the infix concatenation operator <> concatenates this with the empty string.

I exploited the fact that Power is Listable, i.e. it automatically threads over corresponding elements of lists, but not orderless (because "a" + 1 would evaluate to Plus[1, "a"]).

\$\endgroup\$
2
\$\begingroup\$

LINQPad w/ C#, 82 bytes

void m(string s){Console.Write(s.SelectMany((x,i)=>new string(x,++i)).ToArray());}

Single output operation.

\$\endgroup\$
2
  • \$\begingroup\$ @MartinEnder using System; & using System.Linq; are not necessary in LINQPad. \$\endgroup\$ Jun 21, 2016 at 12:05
  • \$\begingroup\$ Oh, I wasn't aware of that. \$\endgroup\$ Jun 21, 2016 at 12:19
2
\$\begingroup\$

C, 84 bytes

main(c,v,i,j)char**v;{for(i=0;i<strlen(v[1]);++i)for(j=0;j<=i;++j)putchar(v[1][i]);}

This should compile on gcc with no flags. Input is taken through the first command-line argument. E.g.

$ ./elasticize Why
Whhyyy
$ ./elasticize SKype
SKKyyyppppeeeee
$ ./elasticize LobbY
LoobbbbbbbYYYYY
$ ./elasticize A and B
A  aaannnnddddd      BBBBBBB

Ungolfed:

int main(int argc, char** argv) {
    int i, j;
    for(i = 0; i < strlen(argv[1]); ++i) {
        for(j = 0; j <= i; ++j) {
            putchar(v[1][i]);
        }
    }
}
\$\endgroup\$
2
\$\begingroup\$

Clojure, 86 bytes

(fn[s](apply str(mapcat #(repeat(+(nth % 1)1)(nth % 0))(map list s(range(count s))))))

Well, nearly beat C. Create a list of pairs (symbol, its position), then repeat each symbol given number of times and flatten the result list and concatenate the list into one string.

See it here: https://ideone.com/uQNar2

\$\endgroup\$
2
\$\begingroup\$

Jelly, 2 bytes

xJ

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Why non-competing? it's completely okay. \$\endgroup\$ Mar 17, 2017 at 8:02
  • \$\begingroup\$ @MatthewRoh J did not exist back then. \$\endgroup\$ Mar 17, 2017 at 12:28
2
\$\begingroup\$

Dart, 47 bytes

f(s,[c=0])=>s.split("").map((i)=>i*++c).join();

Try it online!

\$\endgroup\$
2
+100
\$\begingroup\$

Knight, 33 32 bytes

;=i!=sP;W<iLsO+*Gs iT=i+1i"\"O""

Try it online!

expanded:

; = i ! = s PROMPT
; WHILE < i LENGTH s
    : OUTPUT
        +
            * (GET s i TRUE) (= i + 1 i)
        "\" # so no trailing newline
: OUTPUT "" # now output the trailing newline

-1 did =i! instead of ;=iF

\$\endgroup\$
5
  • \$\begingroup\$ You forgot to put =i+1i in your expanded version. \$\endgroup\$
    – Aiden Chow
    Aug 21 at 4:35
  • \$\begingroup\$ oh, right, thanks \$\endgroup\$
    – Sampersand
    Aug 21 at 4:38
  • \$\begingroup\$ Yo that ! thing is actually super smart. \$\endgroup\$
    – Aiden Chow
    Aug 21 at 4:38
  • \$\begingroup\$ Heh, thanks. :-) \$\endgroup\$
    – Sampersand
    Aug 21 at 4:39
  • \$\begingroup\$ Why do you have to output a trailing newline? No one said you did. \$\endgroup\$
    – Steffan
    Aug 21 at 18:27
2
\$\begingroup\$

C (clang), 56 bytes

i;j;main(c){while(read(0,&c,1))for(j=++i;j--;)write(1);}

Try it online!

How it works

i;                  // declares a zero initialized int
while(read(0,&c,1)) // while we read one byte from 0 (stdin) into c 
for(j=++i;...       // increment i and assign its new value to j
...j--;);           // repeat that number of times
write(1)            // write to stdout abusing the stack we prepared for read(0,&c,1)

Caveat

This may not work with other compilers because of the stack manipulation, certainly not gcc.

C (clang), 51 bytes

i,j;f(*s){for(i=0;*s;s++)for(j=++i;j--;)printf(s);}

This function takes a wide-character string as input and works in the same way as the full program.

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ This is pretty interesting, but it's 54 bytes to declare a function and write to stdout: Try it online! \$\endgroup\$
    – Steffan
    Aug 21 at 20:23
  • 1
    \$\begingroup\$ you'd need to reset i=0 if you want the ability to call the function more than once \$\endgroup\$
    – c--
    Aug 21 at 20:29
  • \$\begingroup\$ 53 bytes \$\endgroup\$
    – jdt
    Aug 22 at 18:47
2
\$\begingroup\$

Japt -m, 3 bytes

+pV

Try it here

\$\endgroup\$
2
\$\begingroup\$

><> (Fish), 33 bytes

1:i:0(?^$:?v~~1+00.
.!02o:$;!-1<|

Try the animated version

enter image description here

Basically if the counter is 0 skip to the second :, otherwise the first :. Properly adjust the counters.

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

05AB1E, 6 bytes

vN>Fy?

Explained

v       # for each char in string
 N>F    # index+1 number of times do
    y?  # print current char

Try it online

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This one looks like it has a question :D \$\endgroup\$
    – dorukayhan
    Jun 20, 2016 at 20:10
1
\$\begingroup\$

Retina, 62 bytes

It wasn't as easy or short as I thought it'd be. Note that the code contains no spaces. They are all tabs (which are rendered incorrectly here), and the last line is blank.

.*
$0¶ ¶
{+`^(.)(.*)¶    (.*¶.*)
$1$2    ¶$3$1
(   +)¶
¶   $1
}`^.

    |¶

Try it online

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

Ruby, 29 bytes

Try it online

->s{i=0;s.gsub(/./){$&*i+=1}}
\$\endgroup\$
1
\$\begingroup\$

Pyke, 4 bytes

Foh*

Try it here!

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

Python 2.7 - 47 Bytes

''.join([s[i-1]*i for i in range(1, len(s)+1)])

where 's' is the given string Output:

welcome: weelllccccooooommmmmmeeeeeee
00004:   000000000044444
Why:     Whhyyy
SKype:   SKKyyyppppeeeee
A and B: A  aaannnnddddd      BBBBBBB
\$\endgroup\$
1
\$\begingroup\$

Oracle SQL 11.2, 125 bytes

SELECT LISTAGG(SUBSTR(RPAD(' ',LEVEL+1,SUBSTR(:1,LEVEL,1)),2))WITHIN GROUP(ORDER BY 1)FROM DUAL CONNECT BY LEVEL<=LENGTH(:1);
\$\endgroup\$
1
\$\begingroup\$

C, 77 bytes

Not much room for golfing here. If only there were a string repeat operator.

i;main(j){char s[999];gets(s);for(;s[i];i++)for(j=0;j<=i;j++)putchar(s[i]);}

Try it online! http://ideone.com/UliJfD

\$\endgroup\$
2
  • \$\begingroup\$ You can do i;main(j){... and save a bye \$\endgroup\$
    – cat
    Jun 23, 2016 at 23:06
  • \$\begingroup\$ @cat Good idea; thanks! \$\endgroup\$ Jun 29, 2016 at 22:26
1
\$\begingroup\$

Zsh, 49 bytes

for i ({1..$#1})c=${1[i]} s+=${(pl:i::$c:)}
<<<$s

Try it online!

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

brev, 42 bytes

(as-list flatten(over(make-list(+ i 1)x)))
\$\endgroup\$
1
\$\begingroup\$

Burlesque, 10 bytes

{+..*}wi\[

Try it online!

{
 +. # Increment
 .* # Repeat
}wi # Apply with 0-index
\[  # Concatenate
\$\endgroup\$
1
\$\begingroup\$

C# (Visual C# Interactive Compiler), 53 bytes

a=>string.Concat(a.Select((c,i)=>new@String(c,i+1)));

Try it online!

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

Kustom, 67 bytes

This makes use of the fact that an undefined global variable works as empty string. The extra byte is for the name of the defined global variable.

$fl(1,tc(len,gv(n)),"i+1","tc(lpad,gv(x),i,tc(cut,gv(n),i-1,1))")$

Sort of ungolfed:

$fl(
  1,
  tc(len, gv(input)),
  "i + 1",
  "tc(
    lpad,
    gv(undefined),
    i,
    tc(
      cut,
      gv(input),
      i - 1,
      1
    )
  )"
)$
\$\endgroup\$
1
\$\begingroup\$

Prolog (SWI), 69 bytes

S*O:-append(H,[T],S),maplist([_,Y]>>(Y=T),S,R),H*Y,append(Y,R,O);O=S.

Try it online!

I feel like this should be shorter...

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

Pip, 14 bytes

Fi,#aOa@iXi+1x

Try It Online!

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

PHP, 76 bytes

I'm sure this can be improved...

<?php for($i=0;$i<strlen($argv[1]);$i++)echo str_repeat($argv[1]{$i},$i+1);

Run from command line:

$ php [file] "Testing"
\$\endgroup\$
0
\$\begingroup\$

C#, 80 Bytes

x.Select((c,i)=>new{c=c,i=i+1}).ToList().ForEach(o=>Write(new string(o.c,o.i)));

Where x is a string.

required: using static System.Console;

If Microsoft had implemented ForEach on IEnumerable<T>, (which is incredibly easy to do), this would be shorter by 9 bytes by removing the .ToList()

\$\endgroup\$
5
  • \$\begingroup\$ Welcome to PPCG! A couple of things: by default all submissions have to be full programs or functions (which may be unnamed), but not just snippets that expect the input to be stored in a variable. Also, usings should be counted in the score (so it's actually shorter to do System.Console.Write in your case). \$\endgroup\$ Jun 21, 2016 at 10:58
  • \$\begingroup\$ @MartinEnder Similar examples appear on this page (codegolf.stackexchange.com/a/83474/13116) which have not been scrutinized by the same rules; is not "string s" a method scoped variable? and does not "Console.Write" rely on "using System;"? \$\endgroup\$ Jun 21, 2016 at 11:28
  • \$\begingroup\$ @MartinEnder same for this example: codegolf.stackexchange.com/a/83400/13116 \$\endgroup\$ Jun 21, 2016 at 11:29
  • \$\begingroup\$ You're right that those are missing the System. (or a using), but they are complete functions, and not just snippets. As for why I haven't left a comment on those yet: because I haven't seen them. \$\endgroup\$ Jun 21, 2016 at 11:36
  • \$\begingroup\$ This is neither a program nor a function and is thus invalid. You are aware of C#'s concise lambda construction syntax, so you could use it to make this answer valid. \$\endgroup\$
    – cat
    Jun 23, 2016 at 14:07

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.