33
\$\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\$
  • 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\$ – FryAmTheEggman Jun 20 '16 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\$ – Mar Dev Jun 20 '16 at 19:52
  • \$\begingroup\$ I'm assuming that the shortest code wins? ;) \$\endgroup\$ – Adnan Jun 20 '16 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\$ – Mar Dev Jun 20 '16 at 19:56
  • 2
    \$\begingroup\$ Related: 1, 2 \$\endgroup\$ – Sp3000 Jun 21 '16 at 3:36

48 Answers 48

2
\$\begingroup\$

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"

\$\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\$
  • \$\begingroup\$ @MartinEnder using System; & using System.Linq; are not necessary in LINQPad. \$\endgroup\$ – Søren D. Ptæus Jun 21 '16 at 12:05
  • \$\begingroup\$ Oh, I wasn't aware of that. \$\endgroup\$ – Martin Ender Jun 21 '16 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\$
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
    \$\begingroup\$ This one looks like it has a question :D \$\endgroup\$ – dorukayhan Jun 20 '16 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\$

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\$
  • \$\begingroup\$ You can do i;main(j){... and save a bye \$\endgroup\$ – cat Jun 23 '16 at 23:06
  • \$\begingroup\$ @cat Good idea; thanks! \$\endgroup\$ – homersimpson Jun 29 '16 at 22:26
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\$

Ruby, 29 bytes

Try it online

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

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

\$\endgroup\$
  • \$\begingroup\$ 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). \$\endgroup\$ – Martin Ender Jun 21 '16 at 10:58
  • \$\begingroup\$ @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;"? \$\endgroup\$ – series0ne Jun 21 '16 at 11:28
  • \$\begingroup\$ @MartinEnder same for this example: codegolf.stackexchange.com/a/83400/13116 \$\endgroup\$ – series0ne Jun 21 '16 at 11:29
  • \$\begingroup\$ 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. \$\endgroup\$ – Martin Ender Jun 21 '16 at 11:36
  • \$\begingroup\$ 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. \$\endgroup\$ – cat Jun 23 '16 at 14:07
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\$

Jelly, 2 bytes (non-competing)

xJ

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Why non-competing? it's completely okay. \$\endgroup\$ – lol Mar 17 '17 at 8:02
  • \$\begingroup\$ @MatthewRoh J did not exist back then. \$\endgroup\$ – Erik the Outgolfer Mar 17 '17 at 12:28
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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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