Die Sequenz "Schauen und sagen" oder "Sagen, was Sie sehen" besteht aus einer Reihe von Zahlen, von denen jede die letzte beschreibt.
1
11 (one one)
21 (two ones)
1211 (one two, one one)
111221 (one one, one two, two ones)
312211 (three ones, two twos, one one)
und weiter und weiter ... https://oeis.org/A005150
Wie auch immer, dies ist eine normale Code-Golf-Herausforderung (Mindestanzahl an Bytes), um ein Programm zu erstellen, das aus zwei Argumenten besteht, einer Anfangszahl und der Anzahl der Iterationen. Wenn Sie beispielsweise "1" und "2" einstecken, ist das Ergebnis "21". Wenn Sie "2" und "4" einstecken, lautet das Ergebnis "132112". Habe Spaß!
Antworten:
Pyth,
108 Bytes-2 Bytes von @FryAmTheEggman
Erläuterung:
Probieren Sie es hier aus .
quelle
ussrG8Qz
CJam, 8 bytes
Input format is the initial number first, iterations second, separated by some whitespace.
Test it here.
Explanation
The array is also flattened before being printed so the result is just the required number.
quelle
JavaScript, 57 bytes
Recursion works well for this problem. The first parameter is the initial number as a string, and the second is the number of iterations.
quelle
b=>F=a=>b--?F(a.replace(/(.)\1*/g,c=>c.length+c[0])):a
Found that while golfing my answer before I realized it was pretty much identical to yours ;)MATL, 9 bytes
Inputs are: number of iterations, initial number.
Try it online!
quelle
R, 87 bytes
Ungolfed & explained
quelle
Perl 6, 63 bytes
This is as short as I could get it for now, there might be some tricky flags that could reduce it, I'm not sure
quelle
Ruby, 63 bytes
A full program, since the question seems to ask for that. Takes input as command line arguments.
No,
gsub!
can't be used, since the strings in$*
are frozen :/quelle
-p
flag to save bytes? If you use it,gsub
operates on a line of STDIN as if it were$_.gsub!
. Then the command line argument is the iterations, son,=$*
, and the other input is read from STDIN.Retina,
464527 bytesMartin did lots to help golf this.
Try it online
Takes input in the format:
<start>
is the initial number.<count>
is in unary, all underscores, and is how many iterations are performed.Single iteration,
2016 bytes:quelle
Haskell, 62 bytes
Try it online!
quelle
JavaScript ES6, 71 bytes
Takes input as a string and a number.
quelle
('1',2)
gives me12
, when it should be21
. Your length should come before the character in the replace.Perl 5, 50 bytes
The arguments are in reverse order (number of iterations then seed). Example:
quelle
$_
instead ofsay
, I suppose, but I haven't tested it. The current solution is a program.05AB1E, 9 bytes (Non-competing)
Corrected due to Emigna's comments, see below/edits.
Try it online!
quelle
F
at the beginning and take the arguments asiterations,initialNo
Dgs
withgy
.y
do in that context?R,
6157 bytes-4 thanks to @JayCe, just when I was sure it couldn't be done any simpler!
Try it online!
quelle
t(sapply(z,c))
call is clever.Mathematica,
8173 bytesquelle
Jelly, 6 bytes (non-competing)
Try it online!
quelle
Stax, 10 bytes
Run and debug online!
Spent too many bytes on proper IO format ...
Explanation
Uses the unpacked version to explain.
The essential part is
D|R{rm:f
(8 bytes).If the first input can be taken as an array of digits, the whole program can be written in 9 bytes: Run and debug online!
quelle
Python 3, 138 bytes
I used a recursive approach.
The function accepts two ints,
a
andb
as described.I'm amazed at how terse the entries here are! Maybe someone will come along with a better Python method too.
quelle
Perl, 38 + 2 bytes
Requires the
-p
flag:Input is a multi line string:
If all the steps are required as well then we can change it to the following, which is 44 + 2 bytes:
quelle
Pylons, 11
How it works:
quelle
SmileBASIC,
10098 bytesPrints out all the steps.
T/T
is there to end the program when T is 0.quelle
APL (Dyalog Classic), 22 bytes
Try it online!
quelle
Retina, 27 bytes
Try it online!
quelle
K (ngn/k), 30 bytes
Try it online!
quelle
Python 3.6,
1009893 bytesTry it online!
Note this creates a lambda that takes a string and an integer, and returns a string. Example:
f('1', 5) == '312211'
Finds all repeated characters (
((.)\2*)
regex), makes a f-string out of their length and the character itself (r'{len("\1")}\2'
), then evaluates it. Uses recursion on the counter (n and ...f(s,n-1)... or s
) to avoid having to define a proper function and a loop.quelle