# Write a program to elasticize strings

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 :)

• 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. Jun 20, 2016 at 19:50
• 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. Jun 20, 2016 at 19:52
• I'm assuming that the shortest code wins? ;) Jun 20, 2016 at 19:54
• @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. Jun 20, 2016 at 19:56
• Related: 1, 2 Jun 21, 2016 at 3:36

# 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


## Python, 40 bytes

f=lambda s,i=1:s and s[0]*i+f(s[1:],i+1)


# Julia, 34 bytes

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


Try it online!

• Your solution was good. But I managed to beat it. Jun 21, 2016 at 9:24
• 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. Jun 21, 2016 at 15:00 # 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 ## 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"]). # LINQPad w/ C#, 82 bytes void m(string s){Console.Write(s.SelectMany((x,i)=>new string(x,++i)).ToArray());}  Single output operation. • @MartinEnder using System; & using System.Linq; are not necessary in LINQPad. Jun 21, 2016 at 12:05 • Oh, I wasn't aware of that. Jun 21, 2016 at 12:19 # 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
.!02o:$;!-1<|  Try the animated version Basically if the counter is 0 skip to the second :, otherwise the first :. Properly adjust the counters. ## 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 • This one looks like it has a question :D Jun 20, 2016 at 20:10 # 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 # Ruby, 29 bytes Try it online ->s{i=0;s.gsub(/./){$&*i+=1}}


## Pyke, 4 bytes

Foh*


Try it here!

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


# 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);


# 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

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

# Zsh, 49 bytes

for i ({1..$#1})c=${1[i]} s+=${(pl:i::$c:)}
<<<$s  Try it online! # brev, 42 bytes (as-list flatten(over(make-list(+ i 1)x)))  # Burlesque, 10 bytes {+..*}wi\[  Try it online! { +. # Increment .* # Repeat }wi # Apply with 0-index \[ # Concatenate  # C# (Visual C# Interactive Compiler), 53 bytes a=>string.Concat(a.Select((c,i)=>new@String(c,i+1)));  Try it online! ## 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(
gv(undefined),
i,
tc(
cut,
gv(input),
i - 1,
1
)
)"
)$ # 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... # Pip, 14 bytes Fi,#aOa@iXi+1x  Try It Online! # 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"


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()

• 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). Jun 21, 2016 at 10:58
• @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;"? Jun 21, 2016 at 11:28
• @MartinEnder same for this example: codegolf.stackexchange.com/a/83400/13116 Jun 21, 2016 at 11:29
• 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. Jun 21, 2016 at 11:36
• 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.
– cat
Jun 23, 2016 at 14:07