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

1 2
3
1
\$\begingroup\$

Brachylog v2, 6 bytes

i₁j₎w⊥

Try it online!

Half-port of Fatalize's v1 answer.

i₁        Choose a pair [element, 1-index] from the input.
  j₎      Replicate the element by its index.
    w     Print the result with no trailing newline.
     ⊥    Backtrack.
\$\endgroup\$
1
\$\begingroup\$

Thunno 2 J, 3 bytes

dż×

Attempt This Online!

Explanation

dż×  # Implicit input           ->  "Why"
d    # Split into characters    ->  ["W","h","y"]
 ż   # Length range             ->  ["W","h","y"]  [1,2,3]
  ×  # Multiply together        ->  ["W","hh","yyy"]
     # Implicit output, joined  ->  "Whhyyy"
\$\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\$

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
\$\endgroup\$
0
\$\begingroup\$

Javascript, 58 bytes

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

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

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
        }
    }
}
\$\endgroup\$
0
\$\begingroup\$

Jelly, 2 bytes

xJ

Try it online!

\$\endgroup\$
1 2
3

Your Answer

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

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