Write a program to elasticize strings

33

5

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

Mario Ishac

Posted 2016-06-20T19:45:36.913

Reputation: 491

2

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 – 2016-06-20T19:50:56.373

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 – 2016-06-20T19:52:24.560

I'm assuming that the shortest code wins? ;) – Adnan – 2016-06-20T19:54:58.277

@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 – 2016-06-20T19:56:13.103

And here I was ready to be clever and start making strings more expensive as more of them were allocated... – J... – 2016-06-21T01:15:10.597

2

Related: 1, 2

– Sp3000 – 2016-06-21T03:36:41.000

Need it be a full program, or simply a function? – None – 2016-06-22T22:08:11.400

@Zymus Based on the answers, I would say function. – Mario Ishac – 2016-06-23T02:07:16.543

Answers

35

Jelly, 3 bytes

Code:

ĖP€

Explanation:

Ė     # Enumerate.
 P€   # Product of each.
      # Implicit joining of everything.

Uses the Jelly encoding. Try it online!.

Adnan

Posted 2016-06-20T19:45:36.913

Reputation: 41 965

17Nice abuse of the fact that Python's * does string multiplication. That's not really intended, but it works. – Dennis – 2016-06-20T20:56:00.610

1@Dennis: which *? There's no such thing in the whole answer. – Thomas Weller – 2016-06-22T06:08:52.187

12@Thomas: Jelly is written in Python, and the Jelly P command calculates product behind the scenes using the Python * operator. This post is abusing the leaky abstraction of the underlying code actually being in Python, so doing a P (product) command on a string works as expected. – mellamokb – 2016-06-22T13:57:30.037

16

J, 4 bytes

#~#\

Usage

   f =: #~#\
   f 'Why'
Whhyyy
   f 'SKype'
SKKyyyppppeeeee
   f 'LobbY'
LoobbbbbbbYYYYY
   f 'A and B'
A  aaannnnddddd      BBBBBBB

Explanation

#~#\  Input: s
  #\  Computes the length of each prefix of s
      This forms the range [1, 2, ..., len(s)]
#~    For each value in the range, copy the character at the
      corresponding index that many times
      Return the created string

miles

Posted 2016-06-20T19:45:36.913

Reputation: 15 654

13

Brainfuck, 15 bytes

,[>+[->+<<.>],]

Pretty straightforward implementation, shifting the memory space by 1 for each input char. Requires an interpreter that gives 0 on EOF, and 32-bit/arbitrary precision cells for inputs longer than 255 chars.

Try it online! (Note: TIO uses 8-bit cells)

Sp3000

Posted 2016-06-20T19:45:36.913

Reputation: 58 729

1Also, I think that this doesn't work for strings longer than 255 characters. – Ismael Miguel – 2016-06-22T10:07:07.590

@IsmaelMiguel That would depend on whether the interpreter in question has arbitrary precision integers or not (but indeed, for most implementations, it would cap at 255) – Sp3000 – 2016-06-22T10:13:56.360

The convention is to use 8-bits. Which is 1 character. But some may indeed implement with 32-bit numbers. Since you specify you need that EOF be 0 (which is a compiler/interpreter-specific behaviour), it should be noted that for strings longer than 255 characters, you need a compiler/interpreter with 32-bit cells. I just though that it should be added to the answer, since it is also a compiler/interpreter-specific behaviour. – Ismael Miguel – 2016-06-22T10:23:07.157

1@IsmaelMiguel Sure, noted. – Sp3000 – 2016-06-22T10:35:20.417

8

Java, 158 121 bytes

Saved a whopping 37 bytes thanks to Kevin Cruijssen!

interface a{static void main(String[]A){int b=0,B;for(char c:A[0].toCharArray())for(B=b+++2;--B>0;)System.out.print(c);}}

As a bonus, this program can handle all Unicode characters in the existence, including the control characters located at the very end of Basic Multilingual Plane.

user8397947

Posted 2016-06-20T19:45:36.913

Reputation: 1 242

3Huh, this is very short for a Java code. – Ave – 2016-06-20T23:47:15.327

1You can shorten it by 1 byte by replacing for(int C=c+1;C>0;C--) with for(int C=c+2;--C>0;) – Kevin Cruijssen – 2016-06-21T06:50:59.153

2Or even shorter (121 bytes): interface a{static void main(String[]A){int x=0,i;for(char c:A[0].toCharArray())for(i=x+++2;--i>0;)System.out.print(c);}} – Kevin Cruijssen – 2016-06-21T06:57:35.983

Well, just make it a lambda or a method – Leaky Nun – 2016-06-21T13:22:55.487

2Wow, using an interface for the default public methods. That's smart. – Justin – 2016-06-22T00:45:21.280

Apologies for no clarification, but your answer would still be valid as a function (therefore no interface is needed, as well as the main method). – Mario Ishac – 2016-06-23T20:30:17.920

I know it's been a while, but you can golf it by 1 more byte: interface a{static void main(String[]A){int b=0,B;for(char c:A[0].toCharArray())for(B=b++;B-->=0;)System.out.print(c);}}. (Also, @MarDev said you are now allowed to use functions, so with a Java 8 lambda you can use s->{int b=0,B;for(char c:s)for(B=b++;B-->=0;)System.out.print(c);}, with a char-array as parameter.) – Kevin Cruijssen – 2017-10-26T13:20:27.633

7

Haskell, 29 bytes

concat.zipWith replicate[1..]

Usage example: concat.zipWith replicate[1..] $ "SKype" -> "SKKyyyppppeeeee".

replicate n c makes n copies of c and concat makes a single list out of all the sublists.

nimi

Posted 2016-06-20T19:45:36.913

Reputation: 34 639

id=<< is a nice touch. :) – sudee – 2016-06-20T20:04:46.750

I just wanted to try it, but assigning f = id=<<zipWith replicate[1..] (in a file) did result in an ugly error, can you tell what I'm doing wrong? – flawr – 2016-06-20T20:37:01.493

Shouldn't it be possible to assign this (unnamed, right?) function to a name, such that we can use it as a function? I mean if it is a function, then (id=<<zipWith replicate[1..] ) "SKype" should still work? Otherwise I would consider it as a snippet. The full program you provided does have "SKype" hardcoded. – flawr – 2016-06-20T20:42:55.193

I'd say if you cannot use it like any other function, it is not a function. E.g. :t does not regard id=<<zipWith replicate[1..] as a function (it just throws an error) however (id=<<).zipWith replicate[1..] is considered as a function. I'd say the first one is just a snipped, that just works if you hardcode the input, but the second one that you just postet is a function (and :t agrees), would you agree on that? – flawr – 2016-06-20T20:55:44.567

Ok, great! If you disagree with my "definition", I think we should start a meta post for clearing this up. In the mean time I'm trying to find some other haskellians for their opinion on this, as this is just my view. – flawr – 2016-06-20T21:11:37.097

On the other hand, can :t consider something as anything but a function? I think everything is a function as long as there is no errror, but then again, I'm still a newbie=) – flawr – 2016-06-20T21:22:32.760

@flawr: there are also values (e.g. :t "test") and kinds.

– nimi – 2016-06-20T21:27:43.780

@flawr: I agree, my first version is a snippet. Thanks for bringing it up for discussion. Btw: :t checks the type (function or value) :k checks the kind. – nimi – 2016-06-20T23:44:51.743

I didn't know about kinds! Couldn't you still consider values as constant functions? – flawr – 2016-06-21T09:14:52.370

@flawr: yes, most of the time (especially outside the Haskell terminology) you can see values as constant functions. Back to :t I'd consider it necessary but not sufficient for some Haskell code to pass a :t test to be a function. If :t throws an error it's definitely not a function. Not everything :taccepts is a function even in a broader sense and in particular not in our (PPCG) sense. – nimi – 2016-06-21T17:42:58.933

7

CJam, 9 8 7 bytes

Thanks to jimmy23013 for saving 1 byte.

Sl+eee~

Test it here.

Explanation

Using the LobbY example:

                                      Stack:
S    e# Push space.                   [" "]
l    e# Read input.                   [" " "LobbY"]
+    e# Append.                       [" LobbY"]
ee   e# Enumerate.                    [[[0 ' ] [1 'L] [2 'o] [3 'b] [4 'b] [5 'Y]]]
e~   e# Run-length decode.            ["LoobbbbbbbYYYYY"]

Martin Ender

Posted 2016-06-20T19:45:36.913

Reputation: 184 808

7

Perl, 16 bytes

s/./$&x$+[0]/ge

+1 byte for the -p flag.

s/./        /    find every character
             g   globally
              e  and replace with the eval'd result of
    $&           the matched string
      x          repeated
       $+[0]     by the index of the character after the match

Doorknob

Posted 2016-06-20T19:45:36.913

Reputation: 68 138

6

Python, 39 bytes

f=lambda s:s and f(s[:-1])+s[-1]*len(s)

Test it on Ideone.

Dennis

Posted 2016-06-20T19:45:36.913

Reputation: 196 637

5

Javascript ES6, 39 bytes

x=>x.replace(/./g,(y,i)=>y+y.repeat(i))

Same length, but more fun:

x=>x.replace(i=/./g,y=>y.repeat(i=-~i))

Snippet demo:

f= x=>x.replace(/./g,(y,i)=>y+y.repeat(i))
run.onclick=_=>output.textContent=f(input.value)
<input id="input" value="SKype">
<button id="run">Go</button>
<pre id="output"></pre>

nderscore

Posted 2016-06-20T19:45:36.913

Reputation: 4 912

Small error, the program does not support spaces, which is required as a submission (check the OP). – Mario Ishac – 2016-06-20T21:59:08.733

@MarDev I changed the snippet to use <pre> instead of <div>, that should help. – Neil – 2016-06-20T22:55:00.490

1@Neil Ah, so the result was correctly computed, but the output was rendered incorrectly by the HTML. Forgot that <div> does that. – Mario Ishac – 2016-06-21T00:43:52.187

..."and *output* the result" – spender – 2016-06-23T01:30:06.547

1@spender returning is a valid form of output for functions – cat – 2016-06-23T15:15:41.180

4

APL (8)

{⍵/⍨⍳⍴⍵}

I.e.:

      {⍵/⍨⍳⍴⍵} ¨  'Why' 'SKype' 'LobbY'
┌──────┬───────────────┬───────────────┐
│Whhyyy│SKKyyyppppeeeee│LoobbbbbbbYYYYY│
└──────┴───────────────┴───────────────┘

Explanation:

  • ⍴⍵: length of given vector
  • : numbers 1..N
  • ⍵/⍨: replicate each element in N times.

marinus

Posted 2016-06-20T19:45:36.913

Reputation: 30 224

4

MATLAB, 45 bytes

g=@(m)sort(m(m>0));@(s)s(g(hankel(1:nnz(s))))

Explanation: The key is hankel, which produces a Hankel matrix of a given vector. From this matrix, we can extract a vector of indices, which defines which character of the string is at which position in the output. E.g. hankel(1:4) produces following matrix:

 1  2  3  4
 2  3  4  0
 3  4  0  0
 4  0  0  0

From this matrix we can extrac the vector 1,2,2,3,3,3,4,4,4,4,4. This vector allows us to output the first character of the string once, the second one twice e.t.c.

flawr

Posted 2016-06-20T19:45:36.913

Reputation: 40 560

4

APL (dzaima/APL), 6 5 bytes

⍳∘≢⌿⊢

Try it online!

⍳∘≢ enumeration of the argument... (indices of its length)
replicates the elements of...
the unmodified argument

Adám

Posted 2016-06-20T19:45:36.913

Reputation: 37 779

link to interpreter? – cat – 2016-06-23T15:14:56.443

@cat See edit (in header). – Adám – 2016-06-23T15:31:41.967

@cat What was your edit? – Adám – 2016-06-23T15:48:19.880

Identical to yours down to the character, because I googled it myself and my edit took 10 minutes to submit – cat – 2016-06-23T16:31:47.710

Also, in which codepage is this 6 bytes? – cat – 2016-06-23T18:12:05.230

Wow, that's twice on the same answer I idly answered my own question. – cat – 2016-06-23T18:13:19.527

Wait, each of the lines in the explanation is a separate UCS-2 character? That's interesting. – cat – 2016-06-23T18:14:06.453

3

R, 83 50 bytes

-23 Thanks to Giuseppe, though he used essentially an entire new method altogether

function(s)intToUtf8(rep(utf8ToInt(s),1:nchar(s)))

My original post:

function(s){r="";for(i in 1:nchar(s))r=paste0(r,strrep(el(strsplit(s,""))[i],i));r}

Try it online!

I feel like there's definitely a better way to do this, but with my new knowledge of a few functions in R, this is my approach.

Sumner18

Posted 2016-06-20T19:45:36.913

Reputation: 1 334

1

Not a golfing tip, but your code link output was messed up. Here

– Robert S. – 2018-08-31T19:28:12.857

Ah, I see. I'm new to TIO, so I didn't quite understand the header/footer portions. Thank You! – Sumner18 – 2018-08-31T19:37:11.957

Using scan saves 1 byte! – Robert S. – 2018-08-31T20:21:13.160

1

Very nice! However, using rep and the argument collapse="" to paste is shorter, and utf8ToInt is shorter still! TIO

– Giuseppe – 2018-08-31T20:36:56.030

3

PowerShell v2+, 36 bytes

-join([char[]]$args[0]|%{"$_"*++$i})

Takes input $args[0], explicitly casts it as a char array, sends that into a loop |%{...}. Each iteration we take the current letter/character "$_" and use the * overloaded operator to concatenate the string pre-incremented $i times. The result of each loop iteration is encapsulated in parens to form an array and then -joined together to form a string. That string is left on the pipeline and output is implicit.

Examples

PS C:\Tools\Scripts\golfing> .\elasticize-a-word.ps1 Why
Whhyyy

PS C:\Tools\Scripts\golfing> .\elasticize-a-word.ps1 SKype
SKKyyyppppeeeee

PS C:\Tools\Scripts\golfing> .\elasticize-a-word.ps1 LobbY
LoobbbbbbbYYYYY

PS C:\Tools\Scripts\golfing> .\elasticize-a-word.ps1 'a b'
a  bbb

AdmBorkBork

Posted 2016-06-20T19:45:36.913

Reputation: 41 581

3

Javascript ES6, 42 41 bytes

s=>[,...s].map((e,i)=>e.repeat(i)).join``

Example runs:

f=s=>[,...s].map((e,i)=>e.repeat(i)).join``

f("Why")   => "Whhyyy"
f("SKype") => "SKKyyyppppeeeee"
f("LobbY") => "LoobbbbbbbYYYYY"

Dendrobium

Posted 2016-06-20T19:45:36.913

Reputation: 2 412

Same length: s=>[...s].reduce((a,b,i)=>a+b.repeat(i+1)) – Bassdrop Cumberwubwubwub – 2016-06-21T12:27:08.897

2-1 byte: s=>[,...s].map((e,i)=>e.repeat(i)).join\`` – nderscore – 2016-06-21T16:14:52.693

@nderscore Aha, thats clever, thanks! – Dendrobium – 2016-06-21T16:28:56.517

3

Brachylog, 13 bytes

:ImC,0:Ie,Cw\

This prints the result to STDOUT.

Explanation

This is a good example of exploiting backtracking to loop.

:ImC            C is the Ith character of the Input
    ,
     0:Ie       Unify an implicit variable with an integer between 0 and I
         ,
          Cw    Write C to STDOUT
            \   False, trigger backtracking. It will go back to 0:Ie and unify the implicit
                variable with another integer, until all integers were used. After that, it
                will backtrack to :ImC and unify I and C with the next character.

Fatalize

Posted 2016-06-20T19:45:36.913

Reputation: 32 976

3

MATLAB, 23 bytes

@(x)repelem(x,1:nnz(x))

Creates an anonymous function ans that can be called using ans('stringtoelacticize')

Suever

Posted 2016-06-20T19:45:36.913

Reputation: 10 257

What version are you using? Cannot find repelem in my (relatively old) version =( – flawr – 2016-06-21T09:12:17.577

1

@flawr repelem was introduced in R2015a

– Luis Mendo – 2016-06-21T14:05:11.697

3

K/Kona, 14 bytes

{,/(1+!#x)#'x}

Usage:

k){,/(1+!#x)#'x}"A and B"
"A  aaannnnddddd      BBBBBBB"

Simon Major

Posted 2016-06-20T19:45:36.913

Reputation: 401

3

Perl 6,  22 20  19 bytes

{S:g/(.)/{$0 x$/.to}/}
{S:g[(.)]=$0 x$/.to}
{[~] .comb Zx 1..*}

Explanation:

{          # implicit parameter $_
  [~]      # string concatenate the following list
    .comb  # the NFG characters from $_
    Z[x]   # zip combined using the string repetition operator
    1 .. * # 1 to infinity
}

Brad Gilbert b2gills

Posted 2016-06-20T19:45:36.913

Reputation: 12 713

3

VBA, 75 bytes

Function e(s):For a=1 To Len(s):e=e &String(a,Mid(s,a,1)):Next:End Function

Call as e.g. a user function in a spreadsheet.

=e(A1)

┌─────────┬───────────────┐
│   SKype │SKKyyyppppeeeee│
└─────────┴───────────────┘

It truncates if you feed it its own output a few times :-).

Joffan

Posted 2016-06-20T19:45:36.913

Reputation: 832

2Welcome to the site! =) – James – 2016-06-21T07:28:38.253

3

Retina, 22 bytes

Byte count assumes ISO 8859-1 encoding.

.
$&$.`$*·
+`(.)·
$1$1

Try it online!

Basically, we insert the right amount of · as placeholders between the characters (since these extended ASCII characters can't appear in the input), then fill them up with the adjacent character in the second stage.

Martin Ender

Posted 2016-06-20T19:45:36.913

Reputation: 184 808

3

PHP, 68 bytes

<?php foreach(str_split($argv[1])as$i=>$a)echo str_repeat($a,$i+1);

Simon

Posted 2016-06-20T19:45:36.913

Reputation: 141

Hi, and welcome to PPCG! Nice first post! – Rɪᴋᴇʀ – 2016-06-21T14:41:58.677

You can get it down to 47 bytes: for(;$a=$argv[1][$i++];)echo str_repeat($a,$i);. – insertusernamehere – 2016-06-21T19:59:30.573

2

Pyth - 5 bytes

1 byte saved thanks to @FryAmTheEggman.

s*VSl

Test Suite.

Maltysen

Posted 2016-06-20T19:45:36.913

Reputation: 25 023

@FryAmTheEggman ah, nice one. – Maltysen – 2016-06-20T20:05:16.450

2

Actually, 7 bytes

' +ñ♂πΣ

Try it online!

Explanation:

' +ñ♂πΣ
' +      prepend a space
   ñ     enumerate ("abc" -> [[0, 'a'], [1, 'b'], [2, 'c']])
    ♂π   map: for each character, repeat it n times
      Σ  concatenate

Mego

Posted 2016-06-20T19:45:36.913

Reputation: 32 998

2

Python 3, 48 47 bytes

Thanks to mego for saving a byte with the -~i trick.

lambda s:''.join(c*-~i for i,c in enumerate(s))

This is mostly self-explanatory. One thing for those not versed in Python: The * operator is overloaded to act like Perl's x operator, repeating its string argument the number of times specified by its numeric argument. E.g. 'foo' * 3 == 'foofoofoo'

jqblz

Posted 2016-06-20T19:45:36.913

Reputation: 2 062

c*-~i is shorter than c*(i+1). – Mego – 2016-06-20T20:07:52.450

2

C#, 81 Bytes

void f(string s){for(int i=0;i<s.Length;i++)Console.Write(new String(s[i],i+1));}

ScifiDeath

Posted 2016-06-20T19:45:36.913

Reputation: 151

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++)); – Abbath – 2016-06-20T23:09:30.620

but if its a foreach we don't have the i variable so you'd need to declare it. – ScifiDeath – 2016-06-21T09:43:13.923

It seems you're missing a using System or a System. in front of the Console. – Martin Ender – 2016-06-21T11:36:53.947

@ScifiDeath That's true - but the end result is still one byte shorter. Sorry for omitting it and causing confusion int i=1; – Abbath – 2016-06-21T13:56:42.737

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. – linac – 2016-06-22T15:20:13.673

2

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

Suever

Posted 2016-06-20T19:45:36.913

Reputation: 10 257

2

Python, 40 bytes

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

xnor

Posted 2016-06-20T19:45:36.913

Reputation: 115 687

2

Julia, 34 bytes

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

Try it online!

Dennis

Posted 2016-06-20T19:45:36.913

Reputation: 196 637

Your solution was good. But I managed to beat it. – Glen O – 2016-06-21T09:24:19.527

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. – Dennis – 2016-06-21T15:00:37.533

2

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

t-clausen.dk

Posted 2016-06-20T19:45:36.913

Reputation: 2 874

2

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"

Glen O

Posted 2016-06-20T19:45:36.913

Reputation: 2 548

2

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"]).

LLlAMnYP

Posted 2016-06-20T19:45:36.913

Reputation: 361

2

LINQPad w/ C#, 82 bytes

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

Single output operation.

Søren D. Ptæus

Posted 2016-06-20T19:45:36.913

Reputation: 141

@MartinEnder using System; & using System.Linq; are not necessary in LINQPad. – Søren D. Ptæus – 2016-06-21T12:05:00.517

Oh, I wasn't aware of that. – Martin Ender – 2016-06-21T12:19:35.420

2

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]);
        }
    }
}

jqblz

Posted 2016-06-20T19:45:36.913

Reputation: 2 062

2

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

cliffroot

Posted 2016-06-20T19:45:36.913

Reputation: 1 080

1

05AB1E, 2 bytes

ƶJ

Try it online or verify all test cases.

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)

Kevin Cruijssen

Posted 2016-06-20T19:45:36.913

Reputation: 67 575

1

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

Emigna

Posted 2016-06-20T19:45:36.913

Reputation: 50 798

1This one looks like it has a question :D – user8397947 – 2016-06-20T20:10:32.673

1

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

mbomb007

Posted 2016-06-20T19:45:36.913

Reputation: 21 944

1

Pyke, 4 bytes

Foh*

Try it here!

Blue

Posted 2016-06-20T19:45:36.913

Reputation: 26 661

1

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

Swadhikar C

Posted 2016-06-20T19:45:36.913

Reputation: 141

1

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

Jeto

Posted 2016-06-20T19:45:36.913

Reputation: 1 601

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

homersimpson

Posted 2016-06-20T19:45:36.913

Reputation: 281

You can do i;main(j){... and save a bye – cat – 2016-06-23T23:06:12.457

@cat Good idea; thanks! – homersimpson – 2016-06-29T22:26:53.120

0

Jelly, 2 bytes (non-competing)

xJ

Try it online!

Erik the Outgolfer

Posted 2016-06-20T19:45:36.913

Reputation: 38 134

Why non-competing? it's completely okay. – Matthew Roh – 2017-03-17T08:02:35.940

@MatthewRoh J did not exist back then. – Erik the Outgolfer – 2017-03-17T12:28:19.450

0

Javascript, 58 bytes

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

Kushal Bhabra

Posted 2016-06-20T19:45:36.913

Reputation: 99

0

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.

Ungolfed with comments:

enum b {;
    public static void main(String[]a) {
        int i=0, j=0;                               // Init counters
        for (; i<a[0].length(); i++) {              // For each characters in string
            for (; j < i + 1; j++)                  // Loop from 0 to i
                System.out.print(a[0].charAt(i));   // Print character at i
            j=0;                                    // Reset j
        }
    }
}

cookie

Posted 2016-06-20T19:45:36.913

Reputation: 271

0

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"

InaScuffle

Posted 2016-06-20T19:45:36.913

Reputation: 31

0

Ruby, 29 bytes

Try it online

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

Value Ink

Posted 2016-06-20T19:45:36.913

Reputation: 10 608

0

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

Matthew Layton

Posted 2016-06-20T19:45:36.913

Reputation: 129

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 – 2016-06-21T10:58:15.770

@MartinEnder Similar examples appear on this page (http://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 – 2016-06-21T11:28:37.077

@MartinEnder same for this example: http://codegolf.stackexchange.com/a/83400/13116

– Matthew Layton – 2016-06-21T11:29:40.723

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 – 2016-06-21T11:36:35.883

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 – 2016-06-23T14:07:20.940

0

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

m654

Posted 2016-06-20T19:45:36.913

Reputation: 765