-4
\$\begingroup\$

This question already has an answer here:

Your task is to take an input, for example:

a,b,4,5

And produce an output, which per this example is aaaabbbbb

a,b,4,5 means: the lowercase character "a" is repeated 4 times, and is then followed by the lowercase character "b" repeated 5 times, making the total number of characters 4+5=9

Another example: the input a,B,5,4, should produce the output aaaaaBBBB, which is: the lowercase letter "a" is repeated 5 times, followed by the uppercase letter "B" repeated 4 times.

Some more examples:

Input --> Output

a,b,4,6         --> aaaabbbbbb
a,b,c,1,2,3     --> abbccc
a,e,d,f,g,1,2,3,4,5 --> aeedddffffggggg
A,b,4,5         --> AAAAbbbbb
A,b,C,1,2,3     --> AbbCCC
A,B,c,D,1,2,3,4 --> ABBcccDDDD

Note how the output must have only the required letters, nothing else, except trailing newlines.

The format of the input should be exactly as you see in the above examples, so your code does not have to support any additional formats/styles of the input.

The output must be displayed in a format exactly as the examples show, this means no commas, spaces, etc, separating the letters. Trailing newlines are okay, if you cannot avoid them.

Standard loopholes are disallowed, and the output is case sensitive.

This is , so the shortest code in bytes wins!

\$\endgroup\$

marked as duplicate by xnor code-golf Aug 20 '16 at 15:34

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    \$\begingroup\$ will the numbers always be increasing? \$\endgroup\$ – Maltysen Aug 20 '16 at 1:08
  • 2
    \$\begingroup\$ This seems quite similar to Run Length Decoding. \$\endgroup\$ – Dennis Aug 20 '16 at 1:13
  • 5
    \$\begingroup\$ @ColdGolf Stricter input/output is a pretty trivial modification to a challenge. \$\endgroup\$ – Suever Aug 20 '16 at 1:22
  • 1
    \$\begingroup\$ Will the numbers always be one digit, as in your examples? \$\endgroup\$ – Jordan Aug 20 '16 at 2:43
  • 1
    \$\begingroup\$ @ColdGolf can we accept input as a list or list of letter-number pairs? I ask because the language I'm thinking of doesn't do well with string parsing, and it's lists look funny when typed out.... \$\endgroup\$ – Socratic Phoenix Aug 20 '16 at 3:54
3
\$\begingroup\$

05AB1E, 11 bytes

',¡2äøvy`×?

Explanation

',¡             # turn input into a list split at commas
   2ä           # divide that list in 2
       ø        # zip, turning the list into pairs of [letter, number]
        v       # for each pair
         y`×?   # print letter number of times

Try it online

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

Pyth - 14 bytes

Ac2cz\,s*VGsMH

Try it online here.

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

APL, 26 bytes

{(∊(//)C⎕VFI⍵)/⍵~⎕D,C←','}

Test:

          {(∊(//)C⎕VFI⍵)/⍵~⎕D,C←','}'A,b,C,1,2,3'
AbbCCC

APL has a built-in for this, so all the code is really doing is parsing the string:

      1 2 3/'AbC'
AbbCCC

Explanation:

  • ⍵~⎕D,C←',': remove all digits and commas from the input, leaving only the letters. (Since it only has to deal with single letters, this works.)
  • ∊(//)C⎕VFI⍵: using the comma as a field separator, get all valid numbers in the input.
  • /: replicate each letter by the required amount
\$\endgroup\$
  • 1
    \$\begingroup\$ Same basic idea: {(⍎⍵∩c)/⍵~c←⎕D,','} \$\endgroup\$ – Adám Aug 28 '16 at 19:31
2
\$\begingroup\$

Python 2 - 88 bytes

X=map(str,input().split(','))
k=len(X)/2
print''.join(X[i]*int(X[i+k])for i in range(k))

Edited to remove whitespace

\$\endgroup\$
  • \$\begingroup\$ Quoted input is acceptable, so you can do input() instead of raw_input(). Additionally, you can drop the whitespace between print and '', and between ) and for. Be sure to take a look at codegolf.stackexchange.com/q/54/45941 for more golfing suggestions! I've also changed the header to Python 2, because we typically only use "Python" for code that can run under either 2 or 3. Last but certainly not least, welcome to PPCG! \$\endgroup\$ – Mego Aug 20 '16 at 3:01
  • \$\begingroup\$ idk if it'll be shorter, but chopping in half can also be done through zip(*[iter(foo)]*2) \$\endgroup\$ – Maltysen Aug 20 '16 at 3:42
1
\$\begingroup\$

Python 2, 66 bytes

i=input();l=len(i)/2
print''.join(s*y for s,y in zip(i[:l],i[l:]))

Uses every Python golfer's favourite function, zip(). Takes input with letters in quotes e.g.:

'a','b',4,6

Ideone it!

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

PHP, 84 bytes

$b=split(',',$argv[1]);while($i<$n=count($b)/2)echo str_repeat($b[+$i],$b[$n+$i++]);

Takes the input string as the first argument as in php script.php a,B,c,3,2,6.

\$\endgroup\$

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