40
\$\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

67 Answers 67

2
\$\begingroup\$

C#, 81 Bytes

void f(string s){for(int i=0;i<s.Length;i++)Console.Write(new String(s[i],i+1));}
\$\endgroup\$
5
  • \$\begingroup\$ you can save 1 byte by changing to a foreach loop, e.g. foreach(var a in s)Console.Write(new C(a,1*i++)); \$\endgroup\$
    – Abbath
    Jun 20, 2016 at 23:09
  • \$\begingroup\$ but if its a foreach we don't have the i variable so you'd need to declare it. \$\endgroup\$
    – ScifiDeath
    Jun 21, 2016 at 9:43
  • \$\begingroup\$ It seems you're missing a using System or a System. in front of the Console. \$\endgroup\$ Jun 21, 2016 at 11:36
  • \$\begingroup\$ @ScifiDeath That's true - but the end result is still one byte shorter. Sorry for omitting it and causing confusion int i=1; \$\endgroup\$
    – Abbath
    Jun 21, 2016 at 13:56
  • \$\begingroup\$ Also one byte shorter using Linq: void f(string s){s.Select((c,i)=>{Console.Write(new string(c,i+1));return c;});}. The need for a (unused) return value is ugly though. Edit: just found similar snippets in other answers further back. \$\endgroup\$
    – linac
    Jun 22, 2016 at 15:20
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\$
3
  • \$\begingroup\$ You forgot to put =i+1i in your expanded version. \$\endgroup\$
    – Aiden Chow
    Aug 21, 2022 at 4:35
  • \$\begingroup\$ Yo that ! thing is actually super smart. \$\endgroup\$
    – Aiden Chow
    Aug 21, 2022 at 4:38
  • \$\begingroup\$ Why do you have to output a trailing newline? No one said you did. \$\endgroup\$
    – naffetS
    Aug 21, 2022 at 18:27
2
\$\begingroup\$

Japt -m, 3 bytes

+pV

Try it here

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

Zsh, 38 bytes

for i (${(s::)@});repeat ++n printf $i

Try it online!

-8 bytes thanks to roblogic

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

C (clang), 54 bytes

i;c;main(j){for(;j=j-1?:read(0,&c,1)*++i;)putchar(c);}

Try it online!

How it works

i;            // declares a zero initialized int
main(j){      // j := argc (should be 1, program called with no arguments)
 for(;j=j-1?: // if (j > 1) j = j - 1, otherwise...
 read(0,&c,1) // try to read 1 byte, return number of bytes read (0 on EOF or 1)
 *++i;)       // increment i, j = read? * i (stopping on EOF)
  putchar(c); // write to stdout
}

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\$
    – naffetS
    Aug 21, 2022 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, 2022 at 20:29
  • \$\begingroup\$ 53 bytes \$\endgroup\$
    – jdt
    Aug 22, 2022 at 18:47
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\$
    – SE is dead
    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\$

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\$
1
\$\begingroup\$

Arturo, 44 bytes

$[s][i:0join map split s'c[i:i+1repeat c i]]

Try it

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

Thunno J, \$9\log_{256}(96)\approx\$ 7.41 bytes

DLR1+ZZ.P

Attempt This Online!

Explanation

DLR1+ZZ.P  # Implicit input
D          # Duplicate
 LR        # Push range(0, len(input))
   1+      # Add one to each
     ZZ    # Zip with the input
       .P  # Product of each pair
           # Implicit output, joined by J flag 
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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