Buen verbo allí, en el título.
Escriba un programa que, dada una cadena de entrada, "elastice" esta cadena y genere el resultado. La elastización de una cadena se realiza de la siguiente manera:
El primer personaje se muestra una vez. El segundo personaje se muestra dos veces. El tercer personaje se muestra tres veces, y así sucesivamente.
Como puede ver, la cantidad de duplicaciones de un determinado carácter está relacionada con el índice del personaje en oposición a sus ocurrencias anteriores en la cadena.
Puede esperar recibir solo caracteres ASCII imprimibles. Basado en el siguiente enlace , estos caracteres tienen valores decimales 32-126.
Ejemplos:
Why: Whhyyy
SKype: SKKyyyppppeeeee
LobbY: LoobbbbbbbYYYYY
(Observe cómo hay 7 b's ya que la primera b se muestra 3 veces y la segunda b se muestra 4 veces, haciendo un total de 7 b's).
A and B: A aaannnnddddd BBBBBBB
Los bytes más cortos ganan :)
Respuestas:
Jalea , 3 bytes
Código:
Explanation:
Uses the Jelly encoding. Try it online!.
fuente
*
does string multiplication. That's not really intended, but it works.*
? There's no such thing in the whole answer.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 aP
(product) command on a string works as expected.J, 4 bytes
Usage
Explanation
fuente
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)
fuente
Java,
158121 bytesSaved a whopping 37 bytes thanks to Kevin Cruijssen!
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.
fuente
for(int C=c+1;C>0;C--)
withfor(int C=c+2;--C>0;)
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);}}
interface
for the defaultpublic
methods. That's smart.Perl, 16 bytes
+1 byte for the
-p
flag.fuente
Haskell, 29 bytes
Usage example:
concat.zipWith replicate[1..] $ "SKype"
->"SKKyyyppppeeeee"
.replicate n c
makes n copies of c andconcat
makes a single list out of all the sublists.fuente
id=<<
is a nice touch. :)f = id=<<zipWith replicate[1..]
(in a file) did result in an ugly error, can you tell what I'm doing wrong?(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.:t
does not regardid=<<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?CJam,
987 bytesThanks to jimmy23013 for saving 1 byte.
Test it here.
Explanation
Using the
LobbY
example:fuente
Python, 39 bytes
Test it on Ideone.
fuente
Javascript ES6, 39 bytes
Same length, but more fun:
Snippet demo:
fuente
<pre>
instead of<div>
, that should help.APL (8)
I.e.:
Explanation:
⍴⍵
: length of given vector⍳
: numbers 1..N⍵/⍨
: replicate each element in⍵
N times.fuente
MATLAB, 45 bytes
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: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.fuente
NARS2000, 6 chars = 12 bytes
⍳∘⍴
enumeration of the argument... (indices of its length)/⊙
replicates the elements of...⊢
the unmodified argumentfuente
PowerShell v2+, 36 bytes
Takes input
$args[0]
, explicitly casts it as achar
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-join
ed together to form a string. That string is left on the pipeline and output is implicit.Examples
fuente
Brachylog, 13 bytes
This prints the result to
STDOUT
.Explanation
This is a good example of exploiting backtracking to loop.
fuente
MATLAB, 23 bytes
Creates an anonymous function
ans
that can be called usingans('stringtoelacticize')
fuente
repelem
in my (relatively old) version =(repelem
was introduced in R2015aK/Kona, 14 bytes
Usage:
fuente
Perl 6,
22 2019 bytesExplanation:
fuente
VBA, 75 bytes
Call as e.g. a user function in a spreadsheet.
=e(A1)
It truncates if you feed it its own output a few times :-).
fuente
=)
PHP, 68 bytes
fuente
for(;$a=$argv[1][$i++];)echo str_repeat($a,$i);
.Javascript ES6,
4241 bytesExample runs:
fuente
s=>[...s].reduce((a,b,i)=>a+b.repeat(i+1))
s=>[,...s].map((e,i)=>e.repeat(i)).join``
Retina, 22 bytes
Byte count assumes ISO 8859-1 encoding.
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.fuente
R,
8350 bytes-23 Thanks to Giuseppe, though he used essentially an entire new method altogether
My original post:
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.
fuente
scan
saves 1 byte!rep
and the argumentcollapse=""
topaste
is shorter, andutf8ToInt
is shorter still! TIOActually, 7 bytes
Try it online!
Explanation:
fuente
Pyth - 5 bytes
1 byte saved thanks to @FryAmTheEggman.
Test Suite.
fuente
Python 3,
4847 bytesThanks to mego for saving a byte with the
-~i
trick.This is mostly self-explanatory. One thing for those not versed in Python: The
*
operator is overloaded to act like Perl'sx
operator, repeating its string argument the number of times specified by its numeric argument. E.g.'foo' * 3 == 'foofoofoo'
fuente
c*-~i
is shorter thanc*(i+1)
.C#, 81 Bytes
fuente
foreach(var a in s)Console.Write(new C(a,1*i++));
using System
or aSystem.
in front of theConsole
.int i=1;
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.MATL, 5 bytes
Try it Online
Explanation
fuente
Python, 40 bytes
fuente
Julia, 34 bytes
Try it online!
fuente
c%n="$c"^n;~s=join([s[r=1:end]...].%r)
, but that's actually longer.split
was the missing piece of the puzzle.TSQL, 97 bytes
Golfed:
Ungolfed:
Try it online
fuente