# 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. – FryAmTheEggman Jun 20 '16 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. – Mario Ishac Jun 20 '16 at 19:52
• I'm assuming that the shortest code wins? ;) – Adnan Jun 20 '16 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. – Mario Ishac Jun 20 '16 at 19:56
• Related: 1, 2 – Sp3000 Jun 21 '16 at 3:36

# Julia, 32 bytes

!s=join(split(s[k=1:end],"").^k)


Unlike Dennis's solution, this is not recursive. split with argument "" separates the string into an array of strings of length 1. The [k=1:end] is a trick to create a range from 1 to the number of characters in the string, and this range is used to concatenate n copies of the n-th character. join then recombines the array of strings into a single string, in order.

Usage example: !"SKype"

## 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. – Søren D. Ptæus Jun 21 '16 at 12:05
• Oh, I wasn't aware of that. – Martin Ender Jun 21 '16 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$ ./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]);
}
}
}


# 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

## 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 – user8397947 Jun 20 '16 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

## 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 '16 at 23:06
• @cat Good idea; thanks! – homersimpson Jun 29 '16 at 22:26

# 05AB1E, 2 bytes

ƶJ


Explanation:

ƶ   # Multiply each character in the (implicit) input-string by its 1-based index
J  # Join the list of substrings together to a single string
# (after which the result is output implicitly)


# 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"


# Ruby, 29 bytes

Try it online

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


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). – Martin Ender Jun 21 '16 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;"? – Matthew Layton Jun 21 '16 at 11:28
• @MartinEnder same for this example: codegolf.stackexchange.com/a/83400/13116 – Matthew Layton Jun 21 '16 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. – Martin Ender Jun 21 '16 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 '16 at 14:07

## Silicon(non-competing), 6 bytes

(This language is exactly the same age as this challenge, but I added some useful commands right after I saw it, so non-competing I think.)

biSÚÿj


Silicon uses CP037, which is a 255-bit codepage.

Explanation:

b        Push a space
i        Input
S        Split
Ú        Enumerate
ÿ        Replicate each list item n times
j        Join


# Jelly, 2 bytes (non-competing)

xJ


Try it online!

• Why non-competing? it's completely okay. – Matthew Roh Mar 17 '17 at 8:02
• @MatthewRoh J did not exist back then. – Erik the Outgolfer Mar 17 '17 at 12:28

# Javascript, 58 bytes

e=a=>(Array.from(a).map((e,i)=>(e).repeat(i+1))).join("");

# Java, 136 bytes

enum b{;public static void main(String[]a){int i=0,j=0;for(;i<a[0].length();i++){for(;j<i+1;j++)System.out.print(a[0].charAt(i));j=0;}}}


Takes input as program arguments.

enum b {;