Sagen Sie, was Sie sehen

30

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ß!

HeyLlama
quelle
2
Können wir eine Ziffernliste erhalten / zurücksenden?
LegionMammal978
5
Ich würde die älteren Fragen bei Bedarf als Dupes schließen. Dies hat nicht die Einschränkungen.
Lirtosiast
4
Ich sehe das nicht als Duplikat. Sowohl die vorherigen Look-and-Say-Herausforderungen waren sehr restriktiv (eine ohne Zahlen im Quellcode, die andere ohne benannte Variablen, benannte Funktionen oder benannte Argumente). Sehr wenige Sprachen werden Antworten auf die früheren Herausforderungen ermöglichen, die auch hier wettbewerbsfähig sind.
Trichoplax
3
Dürfen wir als Nummernliste ausgeben?
Lirtosiast

Antworten:

9

Pyth, 10 8 Bytes

-2 Bytes von @FryAmTheEggman

ussrG8Qz

Erläuterung:

            Implicit: z=first line as string, Q=second line
u         the result of reducing lambda G:
  s s rG8   flattened run-length-encoded G
  Q       Q times
  z     starting with z

Probieren Sie es hier aus .

Lirtosiast
quelle
But at least I don't output brackets and commas; only spaces between the numbers :-P
Luis Mendo
2
In Soviet Russia, ussrG8Qz
mbomb007
8

CJam, 8 bytes

q~{se`}*

Input format is the initial number first, iterations second, separated by some whitespace.

Test it here.

Explanation

q~   e# Read and evaluate input, dumping both numbers on the stack.
{    e# Run this block once for each iteration...
  s  e#   Convert to string... in the first iteration this just stringifies the input
     e#   number again. In subsequent iterations it flattens and then stringifies the
     e#   array we get from the run-length encoding.
  e` e#   Run-length encode.
}*

The array is also flattened before being printed so the result is just the required number.

Martin Ender
quelle
6

JavaScript, 57 bytes

F=(a,b)=>b?F(a.replace(/(.)\1*/g,c=>c.length+c[0]),b-1):a

Recursion works well for this problem. The first parameter is the initial number as a string, and the second is the number of iterations.

Mwr247
quelle
You can save three bytes with a weird recursive curry: 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 ;)
ETHproductions
4

MATL, 9 bytes

:"Y'wvX:!

Inputs are: number of iterations, initial number.

Try it online!

:      % implicit input: number of iterations. Create vector with that size
"      % for loop
  Y'   %   RLE. Pushes two arrays: elements and numbers of repetitions.
       %   First time implicitly asks for input: initial number
  w    %   swap
  v    %   concatenate vertically
  X:   %   linearize to column array
  !    %   transpose to row array
       % implicitly end loop
       % implicitly display
Luis Mendo
quelle
If you can output as an array then Pyth has 8.
lirtosiast
@ThomasKwa Good point. I assumed it was possible
Luis Mendo
4

R, 87 bytes

function(a,n){for(i in 1:n){r=rle(el(strsplit(a,"")));a=paste0(r$l,r$v,collapse="")};a}

Ungolfed & explained

f=function(a,n){
    for(i in 1:n){                      # For 1...n
        r=rle(el(strsplit(a,"")))       # Run length encoding
        a=paste0(r$l,r$v,collapse="")   # concatenate length vector and values vector and collapse
    };
    a                                   # print final result
}
Billywob
quelle
3

Perl 6, 63 bytes

say (@*ARGS[0],*.trans(/(.)$0*/=>{$/.chars~$0})…*)[@*ARGS[1]]

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

Hotkeys
quelle
3

Ruby, 63 bytes

A full program, since the question seems to ask for that. Takes input as command line arguments.

i,n=$*
n.to_i.times{i=i.gsub(/(.)\1*/){"#{$&.size}#$1"}}
puts i

No, gsub! can't be used, since the strings in $* are frozen :/

daniero
quelle
Could you perhaps use the -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, so n,=$*, and the other input is read from STDIN.
Value Ink
3

Retina, 46 45 27 bytes

Martin did lots to help golf this.

+`(\d)(\1?)*(?=.*_)_?
$#2$1

Try it online

Takes input in the format:

<start><count>

<start> is the initial number.

<count> is in unary, all underscores, and is how many iterations are performed.

Single iteration, 20 16 bytes:

(\d)(\1?)*
$#2$1
mbomb007
quelle
2

JavaScript ES6, 71 bytes

(m,n)=>[...Array(n)].map(_=>m=m.replace(/(.)\1*/g,x=>x.length+x[0]))&&m

Takes input as a string and a number.

ETHproductions
quelle
('1',2) gives me 12, when it should be 21. Your length should come before the character in the replace.
Mwr247
@Mwr247 Whoops, sorry.
ETHproductions
2

Perl 5, 50 bytes

$_=pop;for$i(1..pop){s/(.)\1*/length($&).$1/ge}say

The arguments are in reverse order (number of iterations then seed). Example:

> perl -E'$_=pop;for$i(1..pop){s/(.)\1*/length($&).$1/ge}say' 4 2
132112
> perl -E'$_=pop;for$i(1..pop){s/(.)\1*/length($&).$1/ge}say' 0 2
2
> perl -E'$_=pop;for$i(1..pop){s/(.)\1*/length($&).$1/ge}say' 2 0
1110
> perl -E'$_=pop;for$i(1..pop){s/(.)\1*/length($&).$1/ge}say' 1 10
1110
> perl -E'$_=pop;for$i(1..pop){s/(.)\1*/length($&).$1/ge}say' 11 1
3113112221232112111312211312113211
msh210
quelle
As a subroutine, I can shave a byte by ending it with $_ instead of say, I suppose, but I haven't tested it. The current solution is a program.
msh210
2

05AB1E, 9 bytes (Non-competing)

Corrected due to Emigna's comments, see below/edits.

F.¡vygyÙJ

Try it online!

Magic Octopus Urn
quelle
1
I think you missed the part about taking 2 arguments (initial number and number of iterations). Luckily you can just add F at the beginning and take the arguments as iterations,initialNo
Emigna
1
And the byte lost by that could be regained by replacing Dgs with gy.
Emigna
@Emigna what's y do in that context?
Magic Octopus Urn
1
Same as the first y, push the current value in the loop. So instead of duplicating y and swapping it to the top, you just push it again when you need it.
Emigna
@Emigna it should seem I still have a lot to learn haha.
Magic Octopus Urn
2

R, 61 57 bytes

-4 thanks to @JayCe, just when I was sure it couldn't be done any simpler!

f=function(a,n)`if`(n,f(t(sapply(rle(c(a)),c)),n-1),c(a))

Try it online!

J.Doe
quelle
1
Slightly golfed: TIO
JayCe
That t(sapply(z,c)) call is clever.
J.Doe
1

Mathematica, 81 73 bytes

FromDigits@Nest[Flatten[(Tally/@Split@#)~Reverse~3]&,IntegerDigits@#,#2]&
A Simmons
quelle
Prepend your code with four spaces to get it show up as code :)
Ogaday
1

Jelly, 6 bytes (non-competing)

ŒrUFµ¡

Try it online!

           Implicit input: first argument.
     µ¡    Do this to it <second argument> times:
Œr            Run-length encode into [value, times] pairs
  U           Flip them
   F          Flatten list
Lynn
quelle
1

Stax, 10 bytes

Çα▲ì4↔┌j█♀

Run and debug online!

Spent too many bytes on proper IO format ...

Explanation

Uses the unpacked version to explain.

DE|R{rm:f$e
D              Do `2nd parameter` times
 E             Convert number to digits
                   Starting from the `1st parmeter`
  |R           Convert to [element, count] pairs for each run
    {rm        Revert each pair
       :f      Flatten the array
         $     Convert array to string of digits
          e    Convert string of digits to integer

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!

Weijun Zhou
quelle
0

Python 3, 138 bytes

I used a recursive approach.

def g(a,b):
 if b<1:return a
 else:
  c,n=1,'';f,*a=str(a)+'_'
  for i in a:
   if i==f:c+=1
   else:n+=str(c)+f;f,c=i,1
  return g(n,b-1)

The function accepts two ints, a and b as described.

I'm amazed at how terse the entries here are! Maybe someone will come along with a better Python method too.

Ogaday
quelle
0

Perl, 38 + 2 bytes

for$i(1..<>){s/(.)\1*/(length$&).$1/ge}

Requires the -p flag:

$ perl -pe'for$i(1..<>){s/(.)\1*/(length$&).$1/ge}' <<< $'1\n5'
312211

Input is a multi line string:

input number
numbers of iterations

If all the steps are required as well then we can change it to the following, which is 44 + 2 bytes:

$ perl -nE'for$i(1..<>){s/(.)\1*/(length$&).$1/ge,print}' <<< $'1\n5'
11
21
1211
111221
312211
andlrc
quelle
0

Pylons, 11

i:At,{n,A}j

How it works:

i      # Get input from command line.
:A     # Initialize A
  t    # Set A to the top of the stack.
,      # Pop the top of the stack.
{      # Start a for loop.
 n     # Run length encode the stack.
  ,    # Seperate command and iteration
   A   # Repeat A times.
    }  # End for loop.
j      # Join the stack with '' and print it and then exit. 
Morgan Thrapp
quelle
0

SmileBASIC, 100 98 bytes

DEF S N,T?N
WHILE""<N
C=C+1C$=SHIFT(N)IF C$!=(N+@L)[0]THEN O$=O$+STR$(C)+C$C=0
WEND
S O$,T-T/T
END

Prints out all the steps. T/T is there to end the program when T is 0.

12Me21
quelle
0

Retina, 27 bytes

~(`^
K`
$
+`(.)\1$*¶$$.&$$1

Try it online!

TwiNight
quelle
0

Python 3.6, 100 98 93 bytes

import re
f=lambda s,n:n and eval("f'"+re.sub(r'((.)\2*)',r'{len("\1")}\2',f(s,n-1))+"'")or s

Try 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.

BoppreH
quelle