Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Look-and-say-sequence/00-META.yaml
Normal file
3
Task/Look-and-say-sequence/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Look-and-say_sequence
|
||||
note: Text processing
|
||||
38
Task/Look-and-say-sequence/00-TASK.txt
Normal file
38
Task/Look-and-say-sequence/00-TASK.txt
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
The [[wp:Look and say sequence|Look and say sequence]] is a recursively defined sequence of numbers studied most notably by [[wp:John Horton Conway|John Conway]].
|
||||
|
||||
|
||||
The '''look-and-say sequence''' is also known as the '''Morris Number Sequence''', after cryptographer Robert Morris, and the puzzle ''What is the next number in the sequence 1, 11, 21, 1211, 111221?'' is sometimes referred to as the ''Cuckoo's Egg'', from a description of Morris in Clifford Stoll's book ''The Cuckoo's Egg''.
|
||||
|
||||
|
||||
'''Sequence Definition'''
|
||||
* Take a decimal number
|
||||
* ''Look'' at the number, visually grouping consecutive runs of the same digit.
|
||||
* ''Say'' the number, from left to right, group by group; as how many of that digit there are - followed by the digit grouped.
|
||||
: This becomes the next number of the sequence.
|
||||
|
||||
|
||||
'''An example:'''
|
||||
* Starting with the number 1, you have ''one'' 1 which produces 11
|
||||
* Starting with 11, you have ''two'' 1's. I.E.: 21
|
||||
* Starting with 21, you have ''one'' 2, then ''one'' 1. I.E.: (12)(11) which becomes 1211
|
||||
* Starting with 1211, you have ''one'' 1, ''one'' 2, then ''two'' 1's. I.E.: (11)(12)(21) which becomes 111221
|
||||
|
||||
|
||||
;Task:
|
||||
Write a program to generate successive members of the look-and-say sequence.
|
||||
|
||||
|
||||
;Related tasks:
|
||||
* [[Fours is the number of letters in the ...]]
|
||||
* [[Number names]]
|
||||
* [[Self-describing numbers]]
|
||||
* [[Self-referential sequence]]
|
||||
* [[Spelling of ordinal numbers]]
|
||||
|
||||
|
||||
;See also:
|
||||
* [https://www.youtube.com/watch?v=ea7lJkEhytA Look-and-Say Numbers (feat John Conway)], A Numberphile Video.
|
||||
* This task is related to, and an application of, the [[Run-length encoding]] task.
|
||||
* Sequence [https://oeis.org/A005150 A005150] on The On-Line Encyclopedia of Integer Sequences.
|
||||
<br><br>
|
||||
|
||||
20
Task/Look-and-say-sequence/11l/look-and-say-sequence.11l
Normal file
20
Task/Look-and-say-sequence/11l/look-and-say-sequence.11l
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
F lookandsay(=number)
|
||||
V result = ‘’
|
||||
V repeat = number[0]
|
||||
number = number[1..]‘ ’
|
||||
V times = 1
|
||||
|
||||
L(actual) number
|
||||
I actual != repeat
|
||||
result ‘’= String(times)‘’repeat
|
||||
times = 1
|
||||
repeat = actual
|
||||
E
|
||||
times++
|
||||
R result
|
||||
|
||||
V num = ‘1’
|
||||
|
||||
L 10
|
||||
print(num)
|
||||
num = lookandsay(num)
|
||||
|
|
@ -0,0 +1,83 @@
|
|||
bdos: equ 5 ; CP/M calls
|
||||
puts: equ 9
|
||||
|
||||
nmemb: equ 15 ; Change this to print more or fewer members
|
||||
|
||||
org 100h
|
||||
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; Generate and output members of the sequence
|
||||
mvi b,nmemb ; Counter
|
||||
|
||||
outloop: push b ; Preserve counter across calls
|
||||
|
||||
mvi c,puts ; Output current member
|
||||
lxi d,memb
|
||||
call bdos ; And newline
|
||||
mvi c,puts
|
||||
lxi d,newline
|
||||
call bdos
|
||||
|
||||
lxi h,memb ; Generate next member
|
||||
call looksay
|
||||
|
||||
pop b ; Restore counter
|
||||
dcr b ; Done yet?
|
||||
jnz outloop
|
||||
rst 0
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; Given a $-terminated string under HL, representing
|
||||
;; a member of the look and say sequence, generate
|
||||
;; the next one in place (ish). The memory after the
|
||||
;; string is assumed to be free.
|
||||
looksay: push h ; Save start of string on stack
|
||||
mov d,h ; And in DE
|
||||
mov e,l
|
||||
mvi a,'$' ; Find end of string
|
||||
findend: cmp m
|
||||
inx h
|
||||
jnz findend
|
||||
xchg ; HL=string, DE=destination
|
||||
push d ; Save start of new string on stack
|
||||
|
||||
lookchar: mvi b,0 ; Zero counter
|
||||
lookloop: mov a,m ; Get current character
|
||||
inr b ; Compare next character
|
||||
inx h
|
||||
cmp m ; While it is the same, keep going
|
||||
jz lookloop
|
||||
|
||||
mov c,a ; Keep character
|
||||
mvi a,'0' ; There are B amount of these characters
|
||||
add b
|
||||
stax d ; Store the amount
|
||||
inx d ; And in the next location
|
||||
mov a,c ; Store the character
|
||||
stax d
|
||||
inx d
|
||||
|
||||
mvi a,'$' ; Are we done?
|
||||
cmp m
|
||||
jnz lookchar ; If not, do next character
|
||||
stax d ; If yes, terminate new string
|
||||
|
||||
;; Free up memory by copying the new string to where the old
|
||||
;; string began.
|
||||
pop d ; Start of new string
|
||||
pop h ; Start of old string
|
||||
copyloop: ldax d ; Get char from new string
|
||||
mov m,a ; Store char where old string was
|
||||
cpi '$' ; are we done yet?
|
||||
inx d
|
||||
inx h
|
||||
jnz copyloop
|
||||
ret
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
newline: db 13,10,'$'
|
||||
;; This is where the string will be stored.
|
||||
memb: db '1$' ; First item
|
||||
; Due to how CP/M loads programs, the memory after here
|
||||
; is free until we hit the stack.
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
bits 16
|
||||
cpu 8086
|
||||
puts: equ 9h ; MS/DOS system call to print a string
|
||||
nmemb: equ 15 ; Change this to print more or fewer members
|
||||
section .text
|
||||
org 100h
|
||||
mov cx,nmemb ; CX = how many members to print
|
||||
outloop: mov dx,memb ; Print current member
|
||||
mov ah,puts
|
||||
int 21h
|
||||
mov dx,newline ; Print newline
|
||||
int 21h
|
||||
mov di,memb ; Generate next member
|
||||
call looksay
|
||||
loop outloop ; Decrease CX, and loop until zero.
|
||||
ret ; Go back to DOS.
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;;; Given a look and say string in ES:DI, generate the next
|
||||
;;; one in place. Assumption: DS = ES.
|
||||
looksay: push cx ; Keep the counter register
|
||||
mov si,di ; Store pointer to string begin in SI
|
||||
mov bx,di ; And another in BX
|
||||
mov al,'$' ; Find the end of the string
|
||||
xor cx,cx ; Max. 65535 tries
|
||||
dec cx
|
||||
repne scasb ; The 8086 has dedicated string search
|
||||
mov dx,di ; Store copy of start of new str in DX
|
||||
;;; Process one character
|
||||
.procchar: mov al,'0' ; Set counter to ASCII 0
|
||||
mov ah,[bx] ; Get current character of string
|
||||
cmp ah,'$' ; Done?
|
||||
je .done
|
||||
.samechar: inc bx ; Increment pointer
|
||||
inc al ; Increment counter
|
||||
cmp ah,[bx] ; Still the same character?
|
||||
je .samechar ; If yes, test next character
|
||||
mov [di],ax ; Store counter and character
|
||||
inc di ; Move ahead two characters
|
||||
inc di
|
||||
jmp .procchar ; Do next character
|
||||
;;; Copy new string into old location
|
||||
.done: mov byte [di],'$' ; Terminate the string
|
||||
mov cx,di ; Calculate how many bytes to copy
|
||||
sub cx,dx ; end + 1 - start, so one too few here
|
||||
shr cx,1 ; Divide by 2 = words
|
||||
inc cx ; Compensate for the missing +1
|
||||
mov di,dx ; Pointer to begin of new string
|
||||
xchg si,di ; Set SI = new string and DI = old
|
||||
rep movsw ; Copy 16 bits at a time
|
||||
pop cx ; Restore counter register
|
||||
ret
|
||||
section .data
|
||||
newline: db 13,10,'$' ; Newline to print in between members
|
||||
memb: db '1$' ; This is where the current member is stored
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
OP + = (STRING s)STRING:
|
||||
BEGIN
|
||||
CHAR item = s[LWB s];
|
||||
STRING out;
|
||||
FOR index FROM LWB s + 1 TO UPB s DO
|
||||
IF item /= s [index] THEN
|
||||
out := whole(index - LWB s, 0) + item + (+(s [index:UPB s]));
|
||||
GO TO return out
|
||||
FI
|
||||
OD;
|
||||
out := whole (UPB s, 0) + item;
|
||||
return out: out
|
||||
END # + #;
|
||||
|
||||
OP + = (CHAR s)STRING:
|
||||
+ STRING(s);
|
||||
|
||||
print ((+"1", new line));
|
||||
print ((+(+"1"), new line));
|
||||
print ((+(+(+"1")), new line));
|
||||
print ((+(+(+(+"1"))), new line));
|
||||
print ((+(+(+(+(+"1")))), new line));
|
||||
print ((+(+(+(+(+(+"1"))))), new line));
|
||||
print ((+(+(+(+(+(+(+"1")))))), new line));
|
||||
print ((+(+(+(+(+(+(+(+"1"))))))), new line));
|
||||
print ((+(+(+(+(+(+(+(+(+"1")))))))), new line));
|
||||
print ((+(+(+(+(+(+(+(+(+(+"1"))))))))), new line))
|
||||
39
Task/Look-and-say-sequence/ALGOL-M/look-and-say-sequence.alg
Normal file
39
Task/Look-and-say-sequence/ALGOL-M/look-and-say-sequence.alg
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
begin
|
||||
string(1) function digit(n);
|
||||
integer n;
|
||||
case n of begin
|
||||
digit := "0"; digit := "1"; digit := "2";
|
||||
digit := "3"; digit := "4"; digit := "5";
|
||||
digit := "6"; digit := "7"; digit := "8";
|
||||
digit := "9";
|
||||
end;
|
||||
|
||||
string(1) array cur[1:128];
|
||||
string(1) array next[1:128];
|
||||
integer curlen, i, cnt, j, n;
|
||||
|
||||
cur[1] := "1";
|
||||
curlen := 1;
|
||||
|
||||
for n := 1 step 1 until 15 do begin
|
||||
write("");
|
||||
for i := 1 step 1 until curlen do
|
||||
writeon(cur[i]);
|
||||
|
||||
i := j := 1;
|
||||
while i <= curlen do begin
|
||||
cnt := 1;
|
||||
while cur[i + cnt] = cur[i] do
|
||||
cnt := cnt + 1;
|
||||
next[j] := digit(cnt);
|
||||
next[j + 1] := cur[i];
|
||||
j := j + 2;
|
||||
i := i + cnt;
|
||||
end;
|
||||
|
||||
for i := 1 step 1 until j-1 do
|
||||
cur[i] := next[i];
|
||||
|
||||
curlen := j - 1;
|
||||
end;
|
||||
end
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
⎕IO←0
|
||||
d←{(1↓⍵)-¯1↓⍵}
|
||||
f←{m←(0≠d ⍵),1 ⋄ ,(d ¯1,m/⍳⍴⍵),[.5](m/⍵)}
|
||||
{(f⍣⍵) ,1}¨⍳10
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
R←LNS V;T
|
||||
R←0⍴0 ⍝ initiate empty reply
|
||||
LOOP:T←↑⍴↑(=\V)⊂V←,V ⍝ t is the length of the 1st digit's run
|
||||
R←R,T,↑V ⍝ append t and the 1st digit
|
||||
→(0≠↑⍴V←T↓V)/LOOP ⍝ drop t digits and iterate
|
||||
26
Task/Look-and-say-sequence/AWK/look-and-say-sequence.awk
Normal file
26
Task/Look-and-say-sequence/AWK/look-and-say-sequence.awk
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
function lookandsay(a)
|
||||
{
|
||||
s = ""
|
||||
c = 1
|
||||
p = substr(a, 1, 1)
|
||||
for(i=2; i <= length(a); i++) {
|
||||
if ( p == substr(a, i, 1) ) {
|
||||
c++
|
||||
} else {
|
||||
s = s sprintf("%d%s", c, p)
|
||||
p = substr(a, i, 1)
|
||||
c = 1
|
||||
}
|
||||
}
|
||||
s = s sprintf("%d%s", c, p)
|
||||
return s
|
||||
}
|
||||
|
||||
BEGIN {
|
||||
b = "1"
|
||||
print b
|
||||
for(k=1; k <= 10; k++) {
|
||||
b = lookandsay(b)
|
||||
print b
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
BYTE FUNC GetLength(CHAR ARRAY s BYTE pos)
|
||||
CHAR c
|
||||
BYTE len
|
||||
|
||||
c=s(pos)
|
||||
len=1
|
||||
DO
|
||||
pos==+1
|
||||
IF pos<=s(0) AND s(pos)=c THEN
|
||||
len==+1
|
||||
ELSE
|
||||
EXIT
|
||||
FI
|
||||
OD
|
||||
RETURN (len)
|
||||
|
||||
PROC Append(CHAR ARRAY text,suffix)
|
||||
BYTE POINTER srcPtr,dstPtr
|
||||
BYTE len
|
||||
|
||||
len=suffix(0)
|
||||
IF text(0)+len>255 THEN
|
||||
len=255-text(0)
|
||||
FI
|
||||
IF len THEN
|
||||
srcPtr=suffix+1
|
||||
dstPtr=text+text(0)+1
|
||||
MoveBlock(dstPtr,srcPtr,len)
|
||||
text(0)==+suffix(0)
|
||||
FI
|
||||
RETURN
|
||||
|
||||
PROC LookAndSay(CHAR ARRAY in,out)
|
||||
BYTE pos,len
|
||||
CHAR ARRAY tmp(5)
|
||||
|
||||
pos=1 len=0 out(0)=0
|
||||
WHILE pos<=in(0)
|
||||
DO
|
||||
len=GetLength(in,pos)
|
||||
StrB(len,tmp)
|
||||
Append(out,tmp)
|
||||
out(0)==+1
|
||||
out(out(0))=in(pos)
|
||||
pos==+len
|
||||
OD
|
||||
RETURN
|
||||
|
||||
PROC Main()
|
||||
CHAR ARRAY s1(256),s2(256)
|
||||
BYTE i
|
||||
|
||||
SCopy(s1,"1")
|
||||
PrintE(s1)
|
||||
FOR i=1 TO 11
|
||||
DO
|
||||
IF (i&1)=0 THEN
|
||||
LookAndSay(s2,s1)
|
||||
PrintE(s1)
|
||||
ELSE
|
||||
LookAndSay(s1,s2)
|
||||
PrintE(s2)
|
||||
FI
|
||||
OD
|
||||
RETURN
|
||||
13
Task/Look-and-say-sequence/Ada/look-and-say-sequence-1.ada
Normal file
13
Task/Look-and-say-sequence/Ada/look-and-say-sequence-1.ada
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
with Ada.Text_IO, Ada.Strings.Fixed;
|
||||
use Ada.Text_IO, Ada.Strings, Ada.Strings.Fixed;
|
||||
|
||||
function "+" (S : String) return String is
|
||||
Item : constant Character := S (S'First);
|
||||
begin
|
||||
for Index in S'First + 1..S'Last loop
|
||||
if Item /= S (Index) then
|
||||
return Trim (Integer'Image (Index - S'First), Both) & Item & (+(S (Index..S'Last)));
|
||||
end if;
|
||||
end loop;
|
||||
return Trim (Integer'Image (S'Length), Both) & Item;
|
||||
end "+";
|
||||
10
Task/Look-and-say-sequence/Ada/look-and-say-sequence-2.ada
Normal file
10
Task/Look-and-say-sequence/Ada/look-and-say-sequence-2.ada
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
Put_Line (+"1");
|
||||
Put_Line (+(+"1"));
|
||||
Put_Line (+(+(+"1")));
|
||||
Put_Line (+(+(+(+"1"))));
|
||||
Put_Line (+(+(+(+(+"1")))));
|
||||
Put_Line (+(+(+(+(+(+"1"))))));
|
||||
Put_Line (+(+(+(+(+(+(+"1")))))));
|
||||
Put_Line (+(+(+(+(+(+(+(+"1"))))))));
|
||||
Put_Line (+(+(+(+(+(+(+(+(+"1")))))))));
|
||||
Put_Line (+(+(+(+(+(+(+(+(+(+"1"))))))))));
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
on lookAndSay(startNumber, howMany)
|
||||
if (howMany < 1) then return {}
|
||||
|
||||
-- The numbers are handled as lists of digit-value integers for efficiency and output as a list of strings.
|
||||
script o
|
||||
property previousNumber : {}
|
||||
property newNumber : {}
|
||||
property output : {}
|
||||
end script
|
||||
|
||||
-- "Digitise" the start number.
|
||||
repeat
|
||||
set beginning of o's newNumber to startNumber mod 10 as integer
|
||||
set startNumber to startNumber div 10
|
||||
if (startNumber is 0) then exit repeat
|
||||
end repeat
|
||||
-- Add it to the output as text and successively derive the remaining numbers.
|
||||
set astid to AppleScript's text item delimiters
|
||||
set AppleScript's text item delimiters to ""
|
||||
set end of o's output to o's newNumber as text
|
||||
repeat (howMany - 1) times
|
||||
set o's previousNumber to o's newNumber
|
||||
set o's newNumber to {}
|
||||
set i to 1
|
||||
set previousLength to (o's previousNumber's length)
|
||||
set currentDigit to beginning of o's previousNumber
|
||||
repeat with j from 2 to previousLength
|
||||
set thisDigit to item j of o's previousNumber
|
||||
if (thisDigit is not currentDigit) then
|
||||
set end of o's newNumber to j - i
|
||||
set end of o's newNumber to currentDigit
|
||||
set i to j
|
||||
set currentDigit to thisDigit
|
||||
end if
|
||||
end repeat
|
||||
set end of o's newNumber to previousLength - i + 1
|
||||
set end of o's newNumber to currentDigit
|
||||
|
||||
set end of o's output to o's newNumber as text
|
||||
end repeat
|
||||
set AppleScript's text item delimiters to astid
|
||||
|
||||
return o's output
|
||||
end lookAndSay
|
||||
|
||||
-- Test code:
|
||||
return lookAndSay(1, 10)
|
||||
|
|
@ -0,0 +1 @@
|
|||
{"1", "11", "21", "1211", "111221", "312211", "13112221", "1113213211", "31131211131221", "13211311123113112211"}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
lookAndSay: function [n][
|
||||
if n=0 -> return "1"
|
||||
previous: lookAndSay n-1
|
||||
|
||||
result: new ""
|
||||
currentCounter: 0
|
||||
currentCh: first previous
|
||||
loop previous 'ch [
|
||||
if? currentCh <> ch [
|
||||
if not? zero? currentCounter ->
|
||||
'result ++ (to :string currentCounter) ++ currentCh
|
||||
currentCounter: 1
|
||||
currentCh: ch
|
||||
]
|
||||
else ->
|
||||
currentCounter: currentCounter + 1
|
||||
]
|
||||
'result ++ (to :string currentCounter) ++ currentCh
|
||||
return result
|
||||
]
|
||||
|
||||
loop 0..10 'x [
|
||||
print [x "->" lookAndSay x]
|
||||
]
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
AutoExecute:
|
||||
Gui, -MinimizeBox
|
||||
Gui, Add, Edit, w500 r20 vInput, 1
|
||||
Gui, Add, Button, x155 w100 Default, &Calculate
|
||||
Gui, Add, Button, xp+110 yp wp, E&xit
|
||||
Gui, Show,, Look-and-Say sequence
|
||||
Return
|
||||
|
||||
|
||||
ButtonCalculate:
|
||||
Gui, Submit, NoHide
|
||||
GuiControl,, Input, % LookAndSay(Input)
|
||||
Return
|
||||
|
||||
|
||||
GuiClose:
|
||||
ButtonExit:
|
||||
ExitApp
|
||||
Return
|
||||
|
||||
|
||||
;---------------------------------------------------------------------------
|
||||
LookAndSay(Input) {
|
||||
;---------------------------------------------------------------------------
|
||||
; credit for this function goes to AutoHotkey forum member Laslo
|
||||
; http://www.autohotkey.com/forum/topic44657-161.html
|
||||
;-----------------------------------------------------------------------
|
||||
Loop, Parse, Input ; look at every digit
|
||||
If (A_LoopField = d) ; I've got another one! (of the same value)
|
||||
c += 1 ; Let's count them ...
|
||||
Else { ; No, this one is different!
|
||||
r .= c d ; remember what we've got so far
|
||||
c := 1 ; It is the first one in a row
|
||||
d := A_LoopField ; Which one is it?
|
||||
}
|
||||
Return, r c d
|
||||
}
|
||||
12
Task/Look-and-say-sequence/BASIC/look-and-say-sequence.basic
Normal file
12
Task/Look-and-say-sequence/BASIC/look-and-say-sequence.basic
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
10 DEFINT A-Z: I$="1"
|
||||
20 FOR Z=1 TO 15
|
||||
30 PRINT I$
|
||||
40 O$=""
|
||||
50 FOR I=1 TO LEN(I$)
|
||||
60 C=1
|
||||
70 IF MID$(I$,I,1)=MID$(I$,I+C,1) THEN C=C+1: GOTO 70
|
||||
80 O$=O$+CHR$(C+48)+MID$(I$,I,1)
|
||||
90 I=I+C-1
|
||||
100 NEXT I
|
||||
110 I$=O$
|
||||
120 NEXT Z
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
# look and say
|
||||
|
||||
dim a$(2)
|
||||
|
||||
i = 0 # input string index
|
||||
|
||||
a$[i] = "1"
|
||||
|
||||
print a$[i]
|
||||
|
||||
for n=1 to 10
|
||||
j = 1 - i # output string index
|
||||
a$[j] = ""
|
||||
k = 1
|
||||
while (k <= length(a$[i]))
|
||||
k0 = k + 1
|
||||
while ((k0 <= length(a$[i])) and (mid(a$[i], k, 1) = mid(a$[i], k0, 1)))
|
||||
k0 = k0 + 1
|
||||
end while
|
||||
a$[j] += string(k0 - k) + mid(a$[i], k, 1)
|
||||
k = k0
|
||||
end while
|
||||
i = j
|
||||
print a$[j]
|
||||
next n
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
number$ = "1"
|
||||
FOR i% = 1 TO 10
|
||||
number$ = FNlooksay(number$)
|
||||
PRINT number$
|
||||
NEXT
|
||||
END
|
||||
|
||||
DEF FNlooksay(n$)
|
||||
LOCAL i%, j%, c$, o$
|
||||
i% = 1
|
||||
REPEAT
|
||||
c$ = MID$(n$,i%,1)
|
||||
j% = i% + 1
|
||||
WHILE MID$(n$,j%,1) = c$
|
||||
j% += 1
|
||||
ENDWHILE
|
||||
o$ += STR$(j%-i%) + c$
|
||||
i% = j%
|
||||
UNTIL i% > LEN(n$)
|
||||
= o$
|
||||
51
Task/Look-and-say-sequence/BCPL/look-and-say-sequence.bcpl
Normal file
51
Task/Look-and-say-sequence/BCPL/look-and-say-sequence.bcpl
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
get "libhdr"
|
||||
|
||||
manifest $(
|
||||
amount = 15
|
||||
bufsize = 128
|
||||
$)
|
||||
|
||||
let move(dest,src) be
|
||||
$( until !src = 0 do
|
||||
$( !dest := !src
|
||||
dest := dest + 1
|
||||
src := src + 1
|
||||
$)
|
||||
!dest := 0
|
||||
$)
|
||||
|
||||
let count(v) = valof
|
||||
$( let i=1
|
||||
while v!i = !v do i := i + 1
|
||||
resultis i
|
||||
$)
|
||||
|
||||
let looksay(in,out) be
|
||||
$( until !in = 0 do
|
||||
$( let n = count(in)
|
||||
out!0 := n
|
||||
out!1 := !in
|
||||
out := out + 2
|
||||
in := in + n
|
||||
$)
|
||||
!out := 0
|
||||
$)
|
||||
|
||||
let show(v) be
|
||||
$( until !v = 0 do
|
||||
$( writen(!v)
|
||||
v := v + 1
|
||||
$)
|
||||
wrch('*N')
|
||||
$)
|
||||
|
||||
let start() be
|
||||
$( let buf1 = vec bufsize and buf2 = vec bufsize
|
||||
buf1!0 := 1
|
||||
buf1!1 := 0
|
||||
for n = 1 to amount do
|
||||
$( show(buf1)
|
||||
looksay(buf1,buf2)
|
||||
move(buf1,buf2)
|
||||
$)
|
||||
$)
|
||||
3
Task/Look-and-say-sequence/BQN/look-and-say-sequence.bqn
Normal file
3
Task/Look-and-say-sequence/BQN/look-and-say-sequence.bqn
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
LookSay ← ∾´((⊑∾˜ ≠+'0'˙)¨1↓((+`»≠⊢)⊸⊔))
|
||||
|
||||
>((⌈´≠¨)↑¨⊢) LookSay⍟(↕15)"1"
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
( 1:?number
|
||||
& 0:?lines
|
||||
& whl
|
||||
' ( 1+!lines:~>10:?lines
|
||||
& :?say { This will accumulate all that has to be said after one iteration. }
|
||||
& 0:?begin
|
||||
& ( @( !number { Pattern matching. The '@' indicates we're looking in a string rather than a tree structure. }
|
||||
: ?
|
||||
( [!begin
|
||||
%@?digit
|
||||
?
|
||||
[?end
|
||||
( (|(%@:~!digit) ?) { The %@ guarantees we're testing one character - not less (%) and not more (@). The ? takes the rest. }
|
||||
& !say !end+-1*!begin !digit:?say
|
||||
& !end:?begin { When backtracking, 'begin' advances to the begin of the next sequence, or to the end of the string. }
|
||||
)
|
||||
& ~ { fail! This forces backtracking. Backtracking stops when all begin positions have been tried. }
|
||||
)
|
||||
)
|
||||
| out$(str$!say:?number) { After backtracking, output string and set number to string for next iteration. }
|
||||
)
|
||||
)
|
||||
);
|
||||
30
Task/Look-and-say-sequence/C++/look-and-say-sequence.cpp
Normal file
30
Task/Look-and-say-sequence/C++/look-and-say-sequence.cpp
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
std::string lookandsay(const std::string& s)
|
||||
{
|
||||
std::ostringstream r;
|
||||
|
||||
for (std::size_t i = 0; i != s.length();) {
|
||||
auto new_i = s.find_first_not_of(s[i], i + 1);
|
||||
|
||||
if (new_i == std::string::npos)
|
||||
new_i = s.length();
|
||||
|
||||
r << new_i - i << s[i];
|
||||
i = new_i;
|
||||
}
|
||||
return r.str();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string laf = "1";
|
||||
|
||||
std::cout << laf << '\n';
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
laf = lookandsay(laf);
|
||||
std::cout << laf << '\n';
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
using System;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
|
||||
class Program
|
||||
{
|
||||
static string lookandsay(string number)
|
||||
{
|
||||
StringBuilder result = new StringBuilder();
|
||||
|
||||
char repeat = number[0];
|
||||
number = number.Substring(1, number.Length-1)+" ";
|
||||
int times = 1;
|
||||
|
||||
foreach (char actual in number)
|
||||
{
|
||||
if (actual != repeat)
|
||||
{
|
||||
result.Append(Convert.ToString(times)+repeat);
|
||||
times = 1;
|
||||
repeat = actual;
|
||||
}
|
||||
else
|
||||
{
|
||||
times += 1;
|
||||
}
|
||||
}
|
||||
return result.ToString();
|
||||
}
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
string num = "1";
|
||||
|
||||
foreach (int i in Enumerable.Range(1, 10)) {
|
||||
Console.WriteLine(num);
|
||||
num = lookandsay(num);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace RosettaCode_Cs_LookAndSay
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static int Main(string[] args)
|
||||
{
|
||||
Array.Resize<string>(ref args, 2);
|
||||
string ls = args[0] ?? "1";
|
||||
int n;
|
||||
if (!int.TryParse(args[1], out n)) n = 10;
|
||||
do {
|
||||
Console.WriteLine(ls);
|
||||
if (--n <= 0) break;
|
||||
ls = say(look(ls));
|
||||
} while(true);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static string[] look(string input)
|
||||
{
|
||||
int i = -1;
|
||||
return Array.FindAll(Regex.Split(input, @"((\d)\2*)"),
|
||||
delegate(string p) { ++i; i %= 3; return i == 1; }
|
||||
);
|
||||
}
|
||||
|
||||
public static string say(string[] groups)
|
||||
{
|
||||
return string.Concat(
|
||||
Array.ConvertAll<string, string>(groups,
|
||||
delegate(string p) { return string.Concat(p.Length, p[0]); }
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
22
Task/Look-and-say-sequence/C/look-and-say-sequence.c
Normal file
22
Task/Look-and-say-sequence/C/look-and-say-sequence.c
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
char *a = malloc(2), *b = 0, *x, c;
|
||||
int cnt, len = 1;
|
||||
|
||||
for (sprintf(a, "1"); (b = realloc(b, len * 2 + 1)); a = b, b = x) {
|
||||
puts(x = a);
|
||||
for (len = 0, cnt = 1; (c = *a); ) {
|
||||
if (c == *++a)
|
||||
cnt++;
|
||||
else if (c) {
|
||||
len += sprintf(b + len, "%d%c", cnt, c);
|
||||
cnt = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
34
Task/Look-and-say-sequence/CLU/look-and-say-sequence.clu
Normal file
34
Task/Look-and-say-sequence/CLU/look-and-say-sequence.clu
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
look_and_say = proc (s: string) returns (string)
|
||||
out: array[char] := array[char]$[]
|
||||
count: int := 0
|
||||
last: char := '\000'
|
||||
|
||||
for c: char in string$chars(s) do
|
||||
if c ~= last then
|
||||
if count ~= 0 then
|
||||
array[char]$addh(out, char$i2c(count + 48))
|
||||
array[char]$addh(out, last)
|
||||
end
|
||||
last := c
|
||||
count := 1
|
||||
else
|
||||
count := count + 1
|
||||
end
|
||||
end
|
||||
|
||||
array[char]$addh(out, char$i2c(count + 48))
|
||||
array[char]$addh(out, last)
|
||||
return (string$ac2s(out))
|
||||
end look_and_say
|
||||
|
||||
start_up = proc ()
|
||||
lines = 15
|
||||
|
||||
po: stream := stream$primary_output()
|
||||
cur: string := "1"
|
||||
|
||||
for i: int in int$from_to(1, lines) do
|
||||
stream$putl(po, cur)
|
||||
cur := look_and_say(cur)
|
||||
end
|
||||
end start_up
|
||||
43
Task/Look-and-say-sequence/COBOL/look-and-say-sequence.cobol
Normal file
43
Task/Look-and-say-sequence/COBOL/look-and-say-sequence.cobol
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. LOOK-AND-SAY-SEQ.
|
||||
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 SEQUENCES.
|
||||
02 CUR-SEQ PIC X(80) VALUE "1".
|
||||
02 CUR-CHARS REDEFINES CUR-SEQ
|
||||
PIC X OCCURS 80 TIMES INDEXED BY CI.
|
||||
02 CUR-LENGTH PIC 99 COMP VALUE 1.
|
||||
02 NEXT-SEQ PIC X(80).
|
||||
02 NEXT-CHARS REDEFINES NEXT-SEQ
|
||||
PIC X OCCURS 80 TIMES INDEXED BY NI.
|
||||
01 ALG-STATE.
|
||||
02 STEP-AMOUNT PIC 99 VALUE 14.
|
||||
02 ITEM-COUNT PIC 9.
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
LOOK-AND-SAY.
|
||||
DISPLAY CUR-SEQ.
|
||||
SET CI TO 1.
|
||||
SET NI TO 1.
|
||||
MAKE-NEXT-ENTRY.
|
||||
MOVE 0 TO ITEM-COUNT.
|
||||
IF CI IS GREATER THAN CUR-LENGTH GO TO STEP-DONE.
|
||||
TALLY-ITEM.
|
||||
ADD 1 TO ITEM-COUNT.
|
||||
SET CI UP BY 1.
|
||||
IF CI IS NOT GREATER THAN CUR-LENGTH
|
||||
AND CUR-CHARS(CI) IS EQUAL TO CUR-CHARS(CI - 1)
|
||||
GO TO TALLY-ITEM.
|
||||
INSERT-ENTRY.
|
||||
MOVE ITEM-COUNT TO NEXT-CHARS(NI).
|
||||
MOVE CUR-CHARS(CI - 1) TO NEXT-CHARS(NI + 1).
|
||||
SET NI UP BY 2.
|
||||
GO TO MAKE-NEXT-ENTRY.
|
||||
STEP-DONE.
|
||||
MOVE NEXT-SEQ TO CUR-SEQ.
|
||||
SET NI DOWN BY 1.
|
||||
SET CUR-LENGTH TO NI.
|
||||
SUBTRACT 1 FROM STEP-AMOUNT.
|
||||
IF STEP-AMOUNT IS NOT EQUAL TO ZERO GO TO LOOK-AND-SAY.
|
||||
STOP RUN.
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
shared void run() {
|
||||
|
||||
function lookAndSay(Integer|String input) {
|
||||
|
||||
variable value digits = if (is Integer input) then input.string else input;
|
||||
value builder = StringBuilder();
|
||||
|
||||
while (exists currentChar = digits.first) {
|
||||
if (exists index = digits.firstIndexWhere((char) => char != currentChar)) {
|
||||
digits = digits[index...];
|
||||
builder.append("``index````currentChar``");
|
||||
}
|
||||
else {
|
||||
builder.append("``digits.size````currentChar``");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return builder.string;
|
||||
}
|
||||
|
||||
variable String|Integer result = 1;
|
||||
print(result);
|
||||
for (i in 1..14) {
|
||||
result = lookAndSay(result);
|
||||
print(result);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
(defn digits-seq
|
||||
"Returns a seq of the digits of a number (L->R)."
|
||||
[n]
|
||||
(loop [digits (), number n]
|
||||
(if (zero? number) (seq digits)
|
||||
(recur (cons (mod number 10) digits)
|
||||
(quot number 10)))))
|
||||
|
||||
(defn join-digits
|
||||
"Converts a digits-seq back in to a number."
|
||||
[ds]
|
||||
(reduce (fn [n d] (+ (* 10 n) d)) ds))
|
||||
|
||||
(defn look-and-say [n]
|
||||
(->> n digits-seq (partition-by identity)
|
||||
(mapcat (juxt count first)) join-digits))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
user> (take 8 (iterate look-and-say 1))
|
||||
(1 11 21 1211 111221 312211 13112221 1113213211)
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
(defn look-and-say
|
||||
[n]
|
||||
(->> (re-seq #"(.)\1*" n)
|
||||
(mapcat (comp (juxt count first) first))
|
||||
(apply str)))
|
||||
|
||||
(take 12 (iterate look-and-say "1"))
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
("1"
|
||||
"11"
|
||||
"21"
|
||||
"1211"
|
||||
"111221"
|
||||
"312211"
|
||||
"13112221"
|
||||
"1113213211"
|
||||
"31131211131221"
|
||||
"13211311123113112211"
|
||||
"11131221133112132113212221"
|
||||
"3113112221232112111312211312113211")
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
(defun compress (array &key (test 'eql) &aux (l (length array)))
|
||||
"Compresses array by returning a list of conses each of whose car is
|
||||
a number of occurrences and whose cdr is the element occurring. For
|
||||
instance, (compress \"abb\") produces ((1 . #\a) (2 . #\b))."
|
||||
(if (zerop l) nil
|
||||
(do* ((i 1 (1+ i))
|
||||
(segments (acons 1 (aref array 0) '())))
|
||||
((eql i l) (nreverse segments))
|
||||
(if (funcall test (aref array i) (cdar segments))
|
||||
(incf (caar segments))
|
||||
(setf segments (acons 1 (aref array i) segments))))))
|
||||
|
||||
(defun next-look-and-say (number)
|
||||
(reduce #'(lambda (n pair)
|
||||
(+ (* 100 n)
|
||||
(* 10 (car pair))
|
||||
(parse-integer (string (cdr pair)))))
|
||||
(compress (princ-to-string number))
|
||||
:initial-value 0))
|
||||
|
|
@ -0,0 +1 @@
|
|||
(next-look-and-say 9887776666) ;=> 19283746
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
(defun look-and-say (s)
|
||||
(let ((out (list (char s 0) 0)))
|
||||
(loop for x across s do
|
||||
(if (char= x (first out))
|
||||
(incf (second out))
|
||||
(setf out (list* x 1 out))))
|
||||
(format nil "~{~a~^~}" (nreverse out))))
|
||||
|
||||
(loop for s = "1" then (look-and-say s)
|
||||
repeat 10
|
||||
do (write-line s))
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
include "cowgol.coh";
|
||||
include "strings.coh";
|
||||
|
||||
# Given a string with a member of the look-and-say sequence,
|
||||
# generate the next member of the sequence.
|
||||
sub LookSay(cur: [uint8], next: [uint8]) is
|
||||
while [cur] != 0 loop
|
||||
var count: uint8 := 1;
|
||||
var curch: uint8 := [cur];
|
||||
|
||||
# count how many of this character we have
|
||||
loop
|
||||
cur := @next cur;
|
||||
if [cur] != curch then break; end if;
|
||||
count := count + 1;
|
||||
end loop;
|
||||
|
||||
# add it and its count to the next sequence
|
||||
[next] := '0' + count;
|
||||
next := @next next;
|
||||
[next] := curch;
|
||||
next := @next next;
|
||||
end loop;
|
||||
[next] := 0;
|
||||
end sub;
|
||||
|
||||
# amount of members to print
|
||||
# (don't forget to enlarge the buffers if you make this bigger)
|
||||
var members: uint8 := 15;
|
||||
|
||||
# define two buffers
|
||||
var curbuf: uint8[255];
|
||||
var nextbuf: uint8[255];
|
||||
|
||||
# start with "1"
|
||||
CopyString("1", &curbuf as [uint8]);
|
||||
|
||||
# generate and print successive members
|
||||
while members > 0 loop
|
||||
print(&curbuf as [uint8]);
|
||||
print_nl();
|
||||
LookSay(&curbuf as [uint8], &nextbuf as [uint8]);
|
||||
CopyString(&nextbuf as [uint8], &curbuf as [uint8]);
|
||||
members := members - 1;
|
||||
end loop;
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
class String
|
||||
def lookandsay
|
||||
gsub(/(.)\1*/){ |s| s.size.to_s + s[0] }
|
||||
end
|
||||
end
|
||||
|
||||
ss = '1'
|
||||
12.times { puts ss; ss = ss.to_s.lookandsay }
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
def lookandsay(str)
|
||||
str.gsub(/(.)\1*/) { |s| s.size.to_s + $1 }
|
||||
end
|
||||
|
||||
num = "1"
|
||||
12.times { puts num; num = lookandsay(num) }
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
def lookandsay(str)
|
||||
str.chars.chunks(&.itself).map{ |(c, x)| x.size.to_s + c }.join
|
||||
end
|
||||
|
||||
num = "1"
|
||||
12.times { puts num; num = lookandsay(num) }
|
||||
7
Task/Look-and-say-sequence/D/look-and-say-sequence-1.d
Normal file
7
Task/Look-and-say-sequence/D/look-and-say-sequence-1.d
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import std.stdio, std.algorithm, std.range;
|
||||
|
||||
enum say = (in string s) pure => s.group.map!q{ text(a[1],a[0]) }.join;
|
||||
|
||||
void main() {
|
||||
"1".recurrence!((t, n) => t[n - 1].say).take(8).writeln;
|
||||
}
|
||||
17
Task/Look-and-say-sequence/D/look-and-say-sequence-2.d
Normal file
17
Task/Look-and-say-sequence/D/look-and-say-sequence-2.d
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import std.stdio, std.conv, std.array;
|
||||
|
||||
pure string lookAndSay(string s){
|
||||
auto result = appender!string;
|
||||
auto i=0, j=i+1;
|
||||
while(i<s.length){
|
||||
while(j<s.length && s[i]==s[j]) j++;
|
||||
result.put( text(j-i) ~ s[i] );
|
||||
i = j++;
|
||||
}
|
||||
return result.data;
|
||||
}
|
||||
void main(){
|
||||
auto s="1";
|
||||
for(auto i=0; i<10; i++)
|
||||
(s = s.lookAndSay).writeln;
|
||||
}
|
||||
94
Task/Look-and-say-sequence/D/look-and-say-sequence-3.d
Normal file
94
Task/Look-and-say-sequence/D/look-and-say-sequence-3.d
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
import core.stdc.stdio, std.math, std.conv, std.algorithm, std.array;
|
||||
|
||||
void showLookAndSay(bool showArrays)(in uint n) nothrow {
|
||||
if (n == 0) // No sequences to generate and show.
|
||||
return;
|
||||
|
||||
enum Digit : char { nil = '\0', one = '1', two = '2', thr = '3' }
|
||||
|
||||
// Allocate an approximate upper bound size for the array.
|
||||
static Digit* allocBuffer(in uint m) nothrow {
|
||||
immutable len = cast(size_t)(100 + 1.05 *
|
||||
exp(0.269 * m + 0.2686)) + 1;
|
||||
auto a = len.uninitializedArray!(Digit[]);
|
||||
printf("Allocated %d bytes.\n", a.length * Digit.sizeof);
|
||||
return a.ptr;
|
||||
}
|
||||
|
||||
// Can't be expressed in the D type system:
|
||||
// a1 and a2 are immutable pointers to mutable data.
|
||||
auto a1 = allocBuffer(n % 2 ? n : n - 1);
|
||||
auto a2 = allocBuffer(n % 2 ? n - 1 : n);
|
||||
printf("\n");
|
||||
|
||||
a1[0] = Digit.one;
|
||||
size_t len1 = 1;
|
||||
a1[len1] = Digit.nil;
|
||||
|
||||
foreach (immutable i; 0 .. n - 1) {
|
||||
static if (showArrays)
|
||||
printf("%2u: %s\n", i + 1, a1);
|
||||
else
|
||||
printf("%2u: n. digits: %u\n", i + 1, len1);
|
||||
auto p1 = a1,
|
||||
p2 = a2;
|
||||
|
||||
S0: final switch (*p1++) with (Digit) { // Initial state.
|
||||
case nil: goto END;
|
||||
case one: goto S1;
|
||||
case two: goto S2;
|
||||
case thr: goto S3;
|
||||
}
|
||||
S1: final switch (*p1++) with (Digit) {
|
||||
case nil: *p2++ = one; *p2++ = one; goto END;
|
||||
case one: goto S11;
|
||||
case two: *p2++ = one; *p2++ = one; goto S2;
|
||||
case thr: *p2++ = one; *p2++ = one; goto S3;
|
||||
}
|
||||
S2: final switch (*p1++) with (Digit) {
|
||||
case nil: *p2++ = one; *p2++ = two; goto END;
|
||||
case one: *p2++ = one; *p2++ = two; goto S1;
|
||||
case two: goto S22;
|
||||
case thr: *p2++ = one; *p2++ = two; goto S3;
|
||||
}
|
||||
S3: final switch (*p1++) with (Digit) {
|
||||
case nil: *p2++ = one; *p2++ = thr; goto END;
|
||||
case one: *p2++ = one; *p2++ = thr; goto S1;
|
||||
case two: *p2++ = one; *p2++ = thr; goto S2;
|
||||
case thr: goto S33;
|
||||
}
|
||||
S11: final switch (*p1++) with (Digit) {
|
||||
case nil: *p2++ = two; *p2++ = one; goto END;
|
||||
case one: *p2++ = thr; *p2++ = one; goto S0;
|
||||
case two: *p2++ = two; *p2++ = one; goto S2;
|
||||
case thr: *p2++ = two; *p2++ = one; goto S3;
|
||||
}
|
||||
S22: final switch (*p1++) with (Digit) {
|
||||
case nil: *p2++ = two; *p2++ = two; goto END;
|
||||
case one: *p2++ = two; *p2++ = two; goto S1;
|
||||
case two: *p2++ = thr; *p2++ = two; goto S0;
|
||||
case thr: *p2++ = two; *p2++ = two; goto S3;
|
||||
}
|
||||
S33: final switch (*p1++) with (Digit) {
|
||||
case nil: *p2++ = two; *p2++ = thr; goto END;
|
||||
case one: *p2++ = two; *p2++ = thr; goto S1;
|
||||
case two: *p2++ = two; *p2++ = thr; goto S2;
|
||||
case thr: *p2++ = thr; *p2++ = thr; goto S0;
|
||||
}
|
||||
END:
|
||||
immutable len2 = p2 - a2;
|
||||
a2[len2] = Digit.nil;
|
||||
a1.swap(a2);
|
||||
len1 = len2;
|
||||
}
|
||||
|
||||
static if (showArrays)
|
||||
printf("%2u: %s\n", n, a1);
|
||||
else
|
||||
printf("%2u: n. digits: %u\n", n, len1);
|
||||
}
|
||||
|
||||
void main(in string[] args) {
|
||||
immutable n = (args.length == 2) ? args[1].to!uint : 10;
|
||||
n.showLookAndSay!true;
|
||||
}
|
||||
1
Task/Look-and-say-sequence/D/look-and-say-sequence-4.d
Normal file
1
Task/Look-and-say-sequence/D/look-and-say-sequence-4.d
Normal file
|
|
@ -0,0 +1 @@
|
|||
70.showLookAndSay!false;
|
||||
19
Task/Look-and-say-sequence/D/look-and-say-sequence-5.d
Normal file
19
Task/Look-and-say-sequence/D/look-and-say-sequence-5.d
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
void main(in string[] args) {
|
||||
import std.stdio, std.conv, std.algorithm, std.array, std.string;
|
||||
|
||||
immutable n = (args.length == 2) ? args[1].to!uint : 10;
|
||||
if (n == 0)
|
||||
return;
|
||||
|
||||
auto seq = ['1'];
|
||||
writefln("%2d: n. digits: %d", 1, seq.length);
|
||||
foreach (immutable i; 2 .. n + 1) {
|
||||
Appender!(typeof(seq)) result;
|
||||
foreach (const digit, const count; seq.representation.group) {
|
||||
result ~= "123"[count - 1];
|
||||
result ~= digit;
|
||||
}
|
||||
seq = result.data;
|
||||
writefln("%2d: n. digits: %d", i, seq.length);
|
||||
}
|
||||
}
|
||||
137
Task/Look-and-say-sequence/D/look-and-say-sequence-6.d
Normal file
137
Task/Look-and-say-sequence/D/look-and-say-sequence-6.d
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
import core.stdc.stdio, std.conv;
|
||||
|
||||
// On Windows this uses the printf from the Microsoft C runtime,
|
||||
// that doesn't handle real type and some of the C99 format
|
||||
// specifiers, but it's faster for bulk printing.
|
||||
version (LDC) version (Windows)
|
||||
extern(C) nothrow @nogc int printf(const char*, ...);
|
||||
|
||||
// http://www.njohnston.ca/2010/10/a-derivation-of-conways-degree-71-look-and-say-polynomial/
|
||||
struct Sequence {
|
||||
string seq;
|
||||
uint[6] next;
|
||||
}
|
||||
|
||||
immutable Sequence[93] sequences = [
|
||||
{"", []},
|
||||
{"1112", [63]},
|
||||
{"1112133", [64, 62]},
|
||||
{"111213322112", [65]},
|
||||
{"111213322113", [66]},
|
||||
{"1113", [68]},
|
||||
{"11131", [69]},
|
||||
{"111311222112", [84, 55]},
|
||||
{"111312", [70]},
|
||||
{"11131221", [71]},
|
||||
{"1113122112", [76]},
|
||||
{"1113122113", [77]},
|
||||
{"11131221131112", [82]},
|
||||
{"111312211312", [78]},
|
||||
{"11131221131211", [79]},
|
||||
{"111312211312113211", [80]},
|
||||
{"111312211312113221133211322112211213322112", [81, 29, 91]},
|
||||
{"111312211312113221133211322112211213322113", [81, 29, 90]},
|
||||
{"11131221131211322113322112", [81, 30]},
|
||||
{"11131221133112", [75, 29, 92]},
|
||||
{"1113122113322113111221131221", [75, 32]},
|
||||
{"11131221222112", [72]},
|
||||
{"111312212221121123222112", [73]},
|
||||
{"111312212221121123222113", [74]},
|
||||
{"11132", [83]},
|
||||
{"1113222", [86]},
|
||||
{"1113222112", [87]},
|
||||
{"1113222113", [88]},
|
||||
{"11133112", [89, 92]},
|
||||
{"12", [1]},
|
||||
{"123222112", [3]},
|
||||
{"123222113", [4]},
|
||||
{"12322211331222113112211", [2, 61, 29, 85]},
|
||||
{"13", [5]},
|
||||
{"131112", [28]},
|
||||
{"13112221133211322112211213322112", [24, 33, 61, 29, 91]},
|
||||
{"13112221133211322112211213322113", [24, 33, 61, 29, 90]},
|
||||
{"13122112", [7]},
|
||||
{"132", [8]},
|
||||
{"13211", [9]},
|
||||
{"132112", [10]},
|
||||
{"1321122112", [21]},
|
||||
{"132112211213322112", [22]},
|
||||
{"132112211213322113", [23]},
|
||||
{"132113", [11]},
|
||||
{"1321131112", [19]},
|
||||
{"13211312", [12]},
|
||||
{"1321132", [13]},
|
||||
{"13211321", [14]},
|
||||
{"132113212221", [15]},
|
||||
{"13211321222113222112", [18]},
|
||||
{"1321132122211322212221121123222112", [16]},
|
||||
{"1321132122211322212221121123222113", [17]},
|
||||
{"13211322211312113211", [20]},
|
||||
{"1321133112", [6, 61, 29, 92]},
|
||||
{"1322112", [26]},
|
||||
{"1322113", [27]},
|
||||
{"13221133112", [25, 29, 92]},
|
||||
{"1322113312211", [25, 29, 67]},
|
||||
{"132211331222113112211", [25, 29, 85]},
|
||||
{"13221133122211332", [25, 29, 68, 61, 29, 89]},
|
||||
{"22", [61]},
|
||||
{"3", [33]},
|
||||
{"3112", [40]},
|
||||
{"3112112", [41]},
|
||||
{"31121123222112", [42]},
|
||||
{"31121123222113", [43]},
|
||||
{"3112221", [38, 39]},
|
||||
{"3113", [44]},
|
||||
{"311311", [48]},
|
||||
{"31131112", [54]},
|
||||
{"3113112211", [49]},
|
||||
{"3113112211322112", [50]},
|
||||
{"3113112211322112211213322112", [51]},
|
||||
{"3113112211322112211213322113", [52]},
|
||||
{"311311222", [47, 38]},
|
||||
{"311311222112", [47, 55]},
|
||||
{"311311222113", [47, 56]},
|
||||
{"3113112221131112", [47, 57]},
|
||||
{"311311222113111221", [47, 58]},
|
||||
{"311311222113111221131221", [47, 59]},
|
||||
{"31131122211311122113222", [47, 60]},
|
||||
{"3113112221133112", [47, 33, 61, 29, 92]},
|
||||
{"311312", [45]},
|
||||
{"31132", [46]},
|
||||
{"311322113212221", [53]},
|
||||
{"311332", [38, 29, 89]},
|
||||
{"3113322112", [38, 30]},
|
||||
{"3113322113", [38, 31]},
|
||||
{"312", [34]},
|
||||
{"312211322212221121123222113", [36]},
|
||||
{"312211322212221121123222112", [35]},
|
||||
{"32112", [37]}
|
||||
];
|
||||
|
||||
void evolve(in uint seq, in uint n) nothrow @nogc {
|
||||
if (n <= 0) {
|
||||
printf(sequences[seq].seq.ptr);
|
||||
} else {
|
||||
foreach (immutable next; sequences[seq].next) {
|
||||
if (next == 0)
|
||||
break;
|
||||
evolve(next, n - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void main(in string[] args) {
|
||||
immutable uint n = (args.length != 2) ? 10 : args[1].to!uint;
|
||||
|
||||
immutable base = 8;
|
||||
immutable string[base] results = ["", "1", "11", "21", "1211",
|
||||
"111221", "312211", "13112221"];
|
||||
if (n < base) {
|
||||
printf("%s\n", results[n].ptr);
|
||||
return;
|
||||
}
|
||||
|
||||
evolve(24, n - base);
|
||||
evolve(39, n - base);
|
||||
'\n'.putchar;
|
||||
}
|
||||
32
Task/Look-and-say-sequence/Draco/look-and-say-sequence.draco
Normal file
32
Task/Look-and-say-sequence/Draco/look-and-say-sequence.draco
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
\util.g
|
||||
|
||||
proc nonrec look_and_say(*char inp, outp) void:
|
||||
char cur;
|
||||
byte count;
|
||||
channel output text outch;
|
||||
open(outch, outp);
|
||||
while cur := inp*; cur ~= '\e' do
|
||||
count := 1;
|
||||
while
|
||||
inp := inp + 1;
|
||||
inp* = cur
|
||||
do
|
||||
count := count + 1
|
||||
od;
|
||||
write(outch; count, cur)
|
||||
od;
|
||||
close(outch)
|
||||
corp
|
||||
|
||||
proc nonrec main() void:
|
||||
[256] char buf1, buf2;
|
||||
byte i;
|
||||
byte LINES = 14;
|
||||
|
||||
CharsCopy(&buf1[0], "1");
|
||||
for i from 1 upto LINES do
|
||||
writeln(&buf1[0]);
|
||||
look_and_say(&buf1[0], &buf2[0]);
|
||||
CharsCopy(&buf1[0], &buf2[0])
|
||||
od
|
||||
corp
|
||||
26
Task/Look-and-say-sequence/E/look-and-say-sequence.e
Normal file
26
Task/Look-and-say-sequence/E/look-and-say-sequence.e
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
def lookAndSayNext(number :int) {
|
||||
var seen := null
|
||||
var count := 0
|
||||
var result := ""
|
||||
def put() {
|
||||
if (seen != null) {
|
||||
result += count.toString(10) + E.toString(seen)
|
||||
}
|
||||
}
|
||||
for ch in number.toString(10) {
|
||||
if (ch != seen) {
|
||||
put()
|
||||
seen := ch
|
||||
count := 0
|
||||
}
|
||||
count += 1
|
||||
}
|
||||
put()
|
||||
return __makeInt(result, 10)
|
||||
}
|
||||
|
||||
var number := 1
|
||||
for _ in 1..20 {
|
||||
println(number)
|
||||
number := lookAndSayNext(number)
|
||||
}
|
||||
24
Task/Look-and-say-sequence/ERRE/look-and-say-sequence.erre
Normal file
24
Task/Look-and-say-sequence/ERRE/look-and-say-sequence.erre
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
PROGRAM LOOK
|
||||
|
||||
PROCEDURE LOOK_AND_SAY(N$->N$)
|
||||
LOCAL I%,J%,C$,O$
|
||||
I%=1
|
||||
REPEAT
|
||||
C$=MID$(N$,I%,1)
|
||||
J%=I%+1
|
||||
WHILE MID$(N$,J%,1)=C$ DO
|
||||
J%+=1
|
||||
END WHILE
|
||||
O$+=MID$(STR$(J%-I%),2)+C$
|
||||
I%=J%
|
||||
UNTIL I%>LEN(N$)
|
||||
N$=O$
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
NUMBER$="1"
|
||||
FOR I%=1 TO 10 DO
|
||||
LOOK_AND_SAY(NUMBER$->NUMBER$)
|
||||
PRINT(NUMBER$)
|
||||
END FOR
|
||||
END PROGRAM
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
(lib 'math) ;; for (number->list) = explode function
|
||||
(lib 'list) ;; (group)
|
||||
|
||||
(define (next L)
|
||||
(for/fold (acc null) ((g (group L)))
|
||||
(append acc (list (length g) (first g)))))
|
||||
|
||||
|
||||
(define (task n starter)
|
||||
(for/fold (L (number->list starter)) ((i n))
|
||||
(writeln (list->string L))
|
||||
(next L)))
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
(task 10 1)
|
||||
1
|
||||
11
|
||||
21
|
||||
1211
|
||||
111221
|
||||
312211
|
||||
13112221
|
||||
1113213211
|
||||
31131211131221
|
||||
13211311123113112211
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
defmodule LookAndSay do
|
||||
def next(n) do
|
||||
Enum.chunk_by(to_char_list(n), &(&1))
|
||||
|> Enum.map(fn cl=[h|_] -> Enum.concat(to_char_list(length cl), [h]) end)
|
||||
|> Enum.concat
|
||||
|> List.to_integer
|
||||
end
|
||||
|
||||
def sequence_from(n) do
|
||||
Stream.iterate n, &(next/1)
|
||||
end
|
||||
|
||||
def main([start_str|_]) do
|
||||
{start_val,_} = Integer.parse(start_str)
|
||||
IO.inspect sequence_from(start_val) |> Enum.take 9
|
||||
end
|
||||
|
||||
def main([]) do
|
||||
main(["1"])
|
||||
end
|
||||
end
|
||||
|
||||
LookAndSay.main(System.argv)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
defmodule RC do
|
||||
def look_and_say(n) do
|
||||
Regex.replace(~r/(.)\1*/, to_string(n), fn x,y -> [to_string(String.length(x)),y] end)
|
||||
|> String.to_integer
|
||||
end
|
||||
end
|
||||
|
||||
IO.inspect Enum.reduce(1..9, [1], fn _,acc -> [RC.look_and_say(hd(acc)) | acc] end) |> Enum.reverse
|
||||
18
Task/Look-and-say-sequence/Erlang/look-and-say-sequence.erl
Normal file
18
Task/Look-and-say-sequence/Erlang/look-and-say-sequence.erl
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
-module(str).
|
||||
-export([look_and_say/1, look_and_say/2]).
|
||||
|
||||
%% converts a single number
|
||||
look_and_say([H|T]) -> lists:reverse(look_and_say(T,H,1,"")).
|
||||
|
||||
%% converts and accumulates as a loop
|
||||
look_and_say(_, 0) -> [];
|
||||
look_and_say(Start, Times) when Times > 0 ->
|
||||
[Start | look_and_say(look_and_say(Start), Times-1)].
|
||||
|
||||
%% does the actual conversion for a number
|
||||
look_and_say([], Current, N, Acc) ->
|
||||
[Current, $0+N | Acc];
|
||||
look_and_say([H|T], H, N, Acc) ->
|
||||
look_and_say(T, H, N+1, Acc);
|
||||
look_and_say([H|T], Current, N, Acc) ->
|
||||
look_and_say(T, H, 1, [Current, $0+N | Acc]).
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
let rec brk p lst =
|
||||
match lst with
|
||||
| [] -> (lst, lst)
|
||||
| x::xs ->
|
||||
if p x
|
||||
then ([], lst)
|
||||
else
|
||||
let (ys, zs) = brk p xs
|
||||
(x::ys, zs)
|
||||
|
||||
let span p lst = brk (not << p) lst
|
||||
|
||||
let rec groupBy eq lst =
|
||||
match lst with
|
||||
| [] -> []
|
||||
| x::xs ->
|
||||
let (ys,zs) = span (eq x) xs
|
||||
(x::ys)::groupBy eq zs
|
||||
|
||||
let group lst : list<list<'a>> when 'a : equality = groupBy (=) lst
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
let lookAndSay =
|
||||
let describe (xs: char list) =
|
||||
List.append (List.ofSeq <| (List.length xs).ToString()) [List.head xs]
|
||||
let next xs = List.collect describe (group xs)
|
||||
let toStr xs = String (Array.ofList xs)
|
||||
Seq.map toStr <| Seq.unfold (fun xs -> Some (xs, next xs)) ['1']
|
||||
|
||||
let getNthLookAndSay n = Seq.nth n lookAndSay
|
||||
|
||||
Seq.take 10 lookAndSay
|
||||
33
Task/Look-and-say-sequence/FOCAL/look-and-say-sequence.focal
Normal file
33
Task/Look-and-say-sequence/FOCAL/look-and-say-sequence.focal
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
01.10 A "HOW MANY",M
|
||||
01.20 S B(0)=1;S B(1)=0
|
||||
01.30 F Z=1,M;D 4;D 2
|
||||
01.40 Q
|
||||
|
||||
02.10 S X=0;S Y=0
|
||||
02.15 I (B(X)),2.5
|
||||
02.17 S CN=0
|
||||
02.20 S CN=CN+1
|
||||
02.25 S X=X+1
|
||||
02.30 I (FABS(B(X)-B(X-1))),2.2
|
||||
02.35 S C(Y)=CN;S C(Y+1)=B(X-1)
|
||||
02.40 S Y=Y+2
|
||||
02.45 G 2.15
|
||||
02.50 S C(Y)=0
|
||||
02.55 F X=0,Y;S B(X)=C(X)
|
||||
|
||||
03.10 I (A-9)3.2;T "9";R
|
||||
03.20 I (A-8)3.3;T "8";R
|
||||
03.30 I (A-7)3.4;T "7";R
|
||||
03.40 I (A-6)3.5;T "6";R
|
||||
03.50 I (A-5)3.6;T "5";R
|
||||
03.60 I (A-4)3.7;T "4";R
|
||||
03.70 I (A-3)3.8;T "3";R
|
||||
03.80 I (A-2)3.9;T "2";R
|
||||
03.90 T "1"
|
||||
|
||||
04.10 S X=0
|
||||
04.20 S A=B(X)
|
||||
04.30 I (-A)4.4;T !;R
|
||||
04.40 D 3
|
||||
04.50 S X=X+1
|
||||
04.60 G 4.2
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
: (look-and-say) ( str -- )
|
||||
unclip-slice swap [ 1 ] 2dip [
|
||||
2dup = [ drop [ 1 + ] dip ] [
|
||||
[ [ number>string % ] dip , 1 ] dip
|
||||
] if
|
||||
] each [ number>string % ] [ , ] bi* ;
|
||||
|
||||
: look-and-say ( str -- str' ) [ (look-and-say) ] "" make ;
|
||||
|
||||
"1" 10 [ dup print look-and-say ] times print
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
(fn look-and-say [t]
|
||||
(let [lst t
|
||||
ret []]
|
||||
(while (> (length lst) 0)
|
||||
(var (num cnt) (values (table.remove lst 1) 1))
|
||||
(while (= num (. lst 1))
|
||||
(set cnt (+ cnt 1))
|
||||
(when (> (length lst) 0)
|
||||
(set num (table.remove lst 1))))
|
||||
(tset ret (+ (length ret) 1) cnt)
|
||||
(tset ret (+ (length ret) 1) num))
|
||||
ret))
|
||||
|
||||
(var lst [1])
|
||||
(for [i 1 10]
|
||||
(print (table.concat lst))
|
||||
(set lst (look-and-say lst)))
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
(fn look-and-say [s]
|
||||
(var ret [])
|
||||
(var (num cnt) (values (s:sub 1 1) 1))
|
||||
(for [i 2 (length s)]
|
||||
(var cur-num (s:sub i i))
|
||||
(if (= num cur-num)
|
||||
(set cnt (+ cnt 1))
|
||||
(do
|
||||
(table.insert ret (.. cnt num))
|
||||
(set cnt 1)
|
||||
(set num cur-num))))
|
||||
(table.insert ret (.. cnt num))
|
||||
(table.concat ret))
|
||||
|
||||
(var str "1")
|
||||
(for [i 1 10]
|
||||
(print str)
|
||||
(set str (look-and-say str)))
|
||||
28
Task/Look-and-say-sequence/Forth/look-and-say-sequence.fth
Normal file
28
Task/Look-and-say-sequence/Forth/look-and-say-sequence.fth
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
create buf1 256 allot
|
||||
create buf2 256 allot
|
||||
buf1 value src
|
||||
buf2 value dest
|
||||
|
||||
s" 1" src place
|
||||
|
||||
: append-run ( digit run -- )
|
||||
dest count +
|
||||
tuck c! 1+ c!
|
||||
dest c@ 2 + dest c! ;
|
||||
|
||||
: next-look-and-say
|
||||
0 dest c!
|
||||
src 1+ c@ [char] 0 ( digit run )
|
||||
src count bounds do
|
||||
over i c@ =
|
||||
if 1+
|
||||
else append-run i c@ [char] 1
|
||||
then
|
||||
loop
|
||||
append-run
|
||||
src dest to src to dest ;
|
||||
|
||||
: look-and-say ( n -- )
|
||||
0 do next-look-and-say cr src count type loop ;
|
||||
|
||||
10 look-and-say
|
||||
31
Task/Look-and-say-sequence/Fortran/look-and-say-sequence-1.f
Normal file
31
Task/Look-and-say-sequence/Fortran/look-and-say-sequence-1.f
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
module LookAndSay
|
||||
implicit none
|
||||
|
||||
contains
|
||||
|
||||
subroutine look_and_say(in, out)
|
||||
character(len=*), intent(in) :: in
|
||||
character(len=*), intent(out) :: out
|
||||
|
||||
integer :: i, c
|
||||
character(len=1) :: x
|
||||
character(len=2) :: d
|
||||
|
||||
out = ""
|
||||
c = 1
|
||||
x = in(1:1)
|
||||
do i = 2, len(trim(in))
|
||||
if ( x == in(i:i) ) then
|
||||
c = c + 1
|
||||
else
|
||||
write(d, "(I2)") c
|
||||
out = trim(out) // trim(adjustl(d)) // trim(x)
|
||||
c = 1
|
||||
x = in(i:i)
|
||||
end if
|
||||
end do
|
||||
write(d, "(I2)") c
|
||||
out = trim(out) // trim(adjustl(d)) // trim(x)
|
||||
end subroutine look_and_say
|
||||
|
||||
end module LookAndSay
|
||||
17
Task/Look-and-say-sequence/Fortran/look-and-say-sequence-2.f
Normal file
17
Task/Look-and-say-sequence/Fortran/look-and-say-sequence-2.f
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
program LookAndSayTest
|
||||
use LookAndSay
|
||||
implicit none
|
||||
|
||||
integer :: i
|
||||
character(len=200) :: t, r
|
||||
t = "1"
|
||||
print *,trim(t)
|
||||
call look_and_say(t, r)
|
||||
print *, trim(r)
|
||||
do i = 1, 10
|
||||
call look_and_say(r, t)
|
||||
r = t
|
||||
print *, trim(r)
|
||||
end do
|
||||
|
||||
end program LookAndSayTest
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
Dim As Integer n, j, k, k0, r
|
||||
Dim As String X(2)
|
||||
Dim As Integer i = 0 ' índice de cadena de entrada
|
||||
X(0) = "1"
|
||||
|
||||
Input "Indica cuantas repeticiones: ", r
|
||||
Print Chr(10) & "Secuencia:"
|
||||
|
||||
Print X(i)
|
||||
For n = 1 To r-1
|
||||
j = 1 - i ' índice de cadena de salida
|
||||
X(j) = ""
|
||||
k = 1
|
||||
While k <= Len(X(i))
|
||||
k0 = k + 1
|
||||
While ((k0 <= Len(X(i))) And (Mid(X(i), k, 1) = Mid(X(i), k0, 1)))
|
||||
k0 += 1
|
||||
Wend
|
||||
X(j) += Str(k0 - k) + Mid(X(i), k, 1)
|
||||
k = k0
|
||||
Wend
|
||||
i = j
|
||||
Print X(j)
|
||||
Next n
|
||||
End
|
||||
24
Task/Look-and-say-sequence/Frink/look-and-say-sequence.frink
Normal file
24
Task/Look-and-say-sequence/Frink/look-and-say-sequence.frink
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
LookAndSay = "12211123"
|
||||
println["Starting Value: " + LookAndSay]
|
||||
|
||||
LASStr = { |LaS|
|
||||
length[LaS@0] + length[LaS@1] + LaS@0
|
||||
/*
|
||||
The returned results from the Regex are divided between a distinct matching
|
||||
character and any following identical characters. For example, a string
|
||||
of 2222 would be returned from this function as [2,222].
|
||||
The function adds the length of both elements (1 and 3 in the example
|
||||
above) and returns that value with the matching character.
|
||||
i.e. Length of 1st element = 1, Length of 2nd element = 3, value of 1st element = 2
|
||||
1 + 3 = 4 & value 2. Returned result is "42" i.e. "Four 2s."
|
||||
*/
|
||||
}
|
||||
|
||||
// Calculate the next 10 Look and Say Sequence Values
|
||||
|
||||
for i = 1 to 10
|
||||
{
|
||||
LookAndSayReg = LookAndSay =~ %r/(\d)(\1{0,})/g
|
||||
LookAndSay = join["",mapList[LASStr,LookAndSayReg]]
|
||||
println["$i - $LookAndSay"]
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
include "NSLog.incl"
|
||||
|
||||
local fn LookAndSay( testWord as CFStringRef ) as CFStringRef
|
||||
NSUInteger i, length, times
|
||||
CFMutableStringRef result = fn MutableStringWithCapacity(0)
|
||||
|
||||
unichar repeat = fn StringCharacterAtIndex( testWord, 0 )
|
||||
times = 1
|
||||
testWord = fn StringWithFormat( @"%@ ", fn StringSubstringFromIndex( testWord, 1 ) )
|
||||
length = len(testWord)
|
||||
|
||||
for i = 0 to length - 1
|
||||
unichar actual = fn StringCharacterAtIndex( testWord, i )
|
||||
if ( actual != repeat )
|
||||
MutableStringAppendFormat( result, @"%d%c", times, repeat )
|
||||
times = 1
|
||||
repeat = actual
|
||||
else
|
||||
times++
|
||||
end if
|
||||
next
|
||||
end fn = fn StringWithString( result )
|
||||
|
||||
void local fn DoIt
|
||||
NSUInteger i
|
||||
CFStringRef numStr = @"1"
|
||||
|
||||
for i = 1 to i <= 15
|
||||
NSLog( @"%@", numStr )
|
||||
numStr = fn LookAndSay( numStr )
|
||||
next
|
||||
end fn
|
||||
|
||||
fn DoIt
|
||||
|
||||
HandleEvents
|
||||
37
Task/Look-and-say-sequence/GAP/look-and-say-sequence.gap
Normal file
37
Task/Look-and-say-sequence/GAP/look-and-say-sequence.gap
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
LookAndSay := function(s)
|
||||
local c, r, cur, ncur, v;
|
||||
v := "123";
|
||||
r := "";
|
||||
cur := 0;
|
||||
ncur := 0;
|
||||
for c in s do
|
||||
if c = cur then
|
||||
ncur := ncur + 1;
|
||||
else
|
||||
if ncur > 0 then
|
||||
Add(r, v[ncur]);
|
||||
Add(r, cur);
|
||||
fi;
|
||||
cur := c;
|
||||
ncur := 1;
|
||||
fi;
|
||||
od;
|
||||
Add(r, v[ncur]);
|
||||
Add(r, cur);
|
||||
return r;
|
||||
end;
|
||||
|
||||
LookAndSay("1"); # "11"
|
||||
LookAndSay(last); # "21"
|
||||
LookAndSay(last); # "1211"
|
||||
LookAndSay(last); # "111221"
|
||||
LookAndSay(last); # "312211"
|
||||
LookAndSay(last); # "13112221"
|
||||
LookAndSay(last); # "1113213211"
|
||||
LookAndSay(last); # "31131211131221"
|
||||
LookAndSay(last); # "13211311123113112211"
|
||||
LookAndSay(last); # "11131221133112132113212221"
|
||||
LookAndSay(last); # "3113112221232112111312211312113211"
|
||||
LookAndSay(last); # "1321132132111213122112311311222113111221131221"
|
||||
LookAndSay(last); # "11131221131211131231121113112221121321132132211331222113112211"
|
||||
LookAndSay(last); # "311311222113111231131112132112311321322112111312211312111322212311322113212221"
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
Public Sub Main()
|
||||
Dim i, j, cnt As Integer
|
||||
Dim txt$, curr$, result$ As String
|
||||
|
||||
txt$ = "1211"
|
||||
i = 1
|
||||
|
||||
Print "Sequence: " & txt$ & " = ";
|
||||
Repeat
|
||||
j = 1
|
||||
result$ = ""
|
||||
Repeat
|
||||
curr$ = Mid(txt$, j, 1)
|
||||
cnt = 0
|
||||
Repeat
|
||||
Inc cnt
|
||||
Inc j
|
||||
Until Mid(txt$, j, 1) <> curr$
|
||||
result$ &= Str(cnt) & curr$
|
||||
Until j > Len(txt$)
|
||||
Print result$
|
||||
txt$ = result$
|
||||
Dec i
|
||||
Until i <= 0
|
||||
End
|
||||
31
Task/Look-and-say-sequence/Go/look-and-say-sequence.go
Normal file
31
Task/Look-and-say-sequence/Go/look-and-say-sequence.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func lss(s string) (r string) {
|
||||
c := s[0]
|
||||
nc := 1
|
||||
for i := 1; i < len(s); i++ {
|
||||
d := s[i]
|
||||
if d == c {
|
||||
nc++
|
||||
continue
|
||||
}
|
||||
r += strconv.Itoa(nc) + string(c)
|
||||
c = d
|
||||
nc = 1
|
||||
}
|
||||
return r + strconv.Itoa(nc) + string(c)
|
||||
}
|
||||
|
||||
func main() {
|
||||
s := "1"
|
||||
fmt.Println(s)
|
||||
for i := 0; i < 8; i++ {
|
||||
s = lss(s)
|
||||
fmt.Println(s)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
def lookAndSay(sequence) {
|
||||
def encoded = new StringBuilder()
|
||||
(sequence.toString() =~ /(([0-9])\2*)/).each { matcher ->
|
||||
encoded.append(matcher[1].size()).append(matcher[2])
|
||||
}
|
||||
encoded.toString()
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
def sequence = "1"
|
||||
(1..12).each {
|
||||
println sequence
|
||||
sequence = lookAndSay(sequence)
|
||||
}
|
||||
25
Task/Look-and-say-sequence/Haskell/look-and-say-sequence.hs
Normal file
25
Task/Look-and-say-sequence/Haskell/look-and-say-sequence.hs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import Control.Monad (liftM2)
|
||||
import Data.List (group)
|
||||
|
||||
-- this function is composed out of many functions; data flows from the bottom up
|
||||
lookAndSay :: Integer -> Integer
|
||||
lookAndSay = read -- convert digits to integer
|
||||
. concatMap -- concatenate for each run,
|
||||
(liftM2 (++) (show . length) -- the length of it
|
||||
(take 1)) -- and an example member
|
||||
. group -- collect runs of the same digit
|
||||
. show -- convert integer to digits
|
||||
|
||||
-- less comments
|
||||
lookAndSay2 :: Integer -> Integer
|
||||
lookAndSay2 = read . concatMap (liftM2 (++) (show . length)
|
||||
(take 1))
|
||||
. group . show
|
||||
|
||||
|
||||
-- same thing with more variable names
|
||||
lookAndSay3 :: Integer -> Integer
|
||||
lookAndSay3 n = read (concatMap describe (group (show n)))
|
||||
where describe run = show (length run) ++ take 1 run
|
||||
|
||||
main = mapM_ print (iterate lookAndSay 1) -- display sequence until interrupted
|
||||
39
Task/Look-and-say-sequence/Haxe/look-and-say-sequence.haxe
Normal file
39
Task/Look-and-say-sequence/Haxe/look-and-say-sequence.haxe
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
using Std;
|
||||
|
||||
class Main
|
||||
{
|
||||
|
||||
static function main()
|
||||
{
|
||||
var test = "1";
|
||||
for (i in 0...11) {
|
||||
Sys.println(test);
|
||||
test = lookAndSay(test);
|
||||
}
|
||||
}
|
||||
|
||||
static function lookAndSay(s:String)
|
||||
{
|
||||
if (s == null || s == "") return "";
|
||||
|
||||
var results = "";
|
||||
var repeat = s.charAt(0);
|
||||
var amount = 1;
|
||||
for (i in 1...s.length)
|
||||
{
|
||||
var actual = s.charAt(i);
|
||||
if (actual != repeat)
|
||||
{
|
||||
results += amount.string();
|
||||
results += repeat;
|
||||
repeat = actual;
|
||||
amount = 0;
|
||||
}
|
||||
amount++;
|
||||
}
|
||||
results += amount.string();
|
||||
results += repeat;
|
||||
|
||||
return results;
|
||||
}
|
||||
}
|
||||
14
Task/Look-and-say-sequence/Icon/look-and-say-sequence.icon
Normal file
14
Task/Look-and-say-sequence/Icon/look-and-say-sequence.icon
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
procedure main()
|
||||
every 1 to 10 do
|
||||
write(n := nextlooknsayseq(\n | 1))
|
||||
end
|
||||
|
||||
procedure nextlooknsayseq(n) #: return next element in look and say sequence
|
||||
n2 := ""
|
||||
n ? until pos(0) do {
|
||||
i := tab(any(&digits)) | fail # or fail if not digits
|
||||
move(-1)
|
||||
n2 ||:= *tab(many(i)) || i # accumulate count+digit
|
||||
}
|
||||
return n2
|
||||
end
|
||||
1
Task/Look-and-say-sequence/J/look-and-say-sequence-1.j
Normal file
1
Task/Look-and-say-sequence/J/look-and-say-sequence-1.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
las=: ,@((# , {.);.1~ 1 , 2 ~:/\ ])&.(10x&#.inv)@]^:(1+i.@[)
|
||||
2
Task/Look-and-say-sequence/J/look-and-say-sequence-2.j
Normal file
2
Task/Look-and-say-sequence/J/look-and-say-sequence-2.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
10 las 1
|
||||
1 11 21 1211 111221 312211 13112221 1113213211 31131211131221 13211311123113112211 11131221133112132113212221
|
||||
18
Task/Look-and-say-sequence/Java/look-and-say-sequence-1.java
Normal file
18
Task/Look-and-say-sequence/Java/look-and-say-sequence-1.java
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
public static String lookandsay(String number){
|
||||
StringBuilder result= new StringBuilder();
|
||||
|
||||
char repeat= number.charAt(0);
|
||||
number= number.substring(1) + " ";
|
||||
int times= 1;
|
||||
|
||||
for(char actual: number.toCharArray()){
|
||||
if(actual != repeat){
|
||||
result.append(times + "" + repeat);
|
||||
times= 1;
|
||||
repeat= actual;
|
||||
}else{
|
||||
times+= 1;
|
||||
}
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
public static void main(String[] args){
|
||||
String num = "1";
|
||||
|
||||
for (int i=1;i<=10;i++) {
|
||||
System.out.println(num);
|
||||
num = lookandsay(num);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
function lookandsay(str) {
|
||||
return str.replace(/(.)\1*/g, function(seq, p1){return seq.length.toString() + p1})
|
||||
}
|
||||
|
||||
var num = "1";
|
||||
for (var i = 10; i > 0; i--) {
|
||||
alert(num);
|
||||
num = lookandsay(num);
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
function lookAndSay( s="" ){
|
||||
var tokens=[]
|
||||
var i=0, j=1
|
||||
while( i<s.length ) {
|
||||
while( j<s.length && s[j]===s[i] ) j++
|
||||
tokens.push( `${j-i}${s[i]}` )
|
||||
i=j++
|
||||
}
|
||||
return tokens.join("")
|
||||
}
|
||||
var phrase="1"
|
||||
for(var n=0; n<10; n++ )
|
||||
console.log( phrase = lookAndSay( phrase ) )
|
||||
15
Task/Look-and-say-sequence/Jq/look-and-say-sequence.jq
Normal file
15
Task/Look-and-say-sequence/Jq/look-and-say-sequence.jq
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
def look_and_say:
|
||||
def head(c; n): if .[n:n+1] == c then head(c; n+1) else n end;
|
||||
tostring
|
||||
| if length == 0 then ""
|
||||
else head(.[0:1]; 1) as $len
|
||||
| .[0:$len] as $head
|
||||
| ($len | tostring) + $head[0:1] + (.[$len:] | look_and_say)
|
||||
end ;
|
||||
|
||||
# look and say n times
|
||||
def look_and_say(n):
|
||||
if n == 0 then empty
|
||||
else look_and_say as $lns
|
||||
| $lns, ($lns|look_and_say(n-1))
|
||||
end ;
|
||||
25
Task/Look-and-say-sequence/Julia/look-and-say-sequence.julia
Normal file
25
Task/Look-and-say-sequence/Julia/look-and-say-sequence.julia
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
function lookandsay(s::String)
|
||||
rst = IOBuffer()
|
||||
c = 1
|
||||
for i in 1:length(s)
|
||||
if i != length(s) && s[i] == s[i+1]
|
||||
c += 1
|
||||
else
|
||||
print(rst, c, s[i])
|
||||
c = 1
|
||||
end
|
||||
end
|
||||
String(take!(rst))
|
||||
end
|
||||
|
||||
|
||||
function lookandsayseq(n::Integer)
|
||||
rst = Vector{String}(undef, n)
|
||||
rst[1] = "1"
|
||||
for i in 2:n
|
||||
rst[i] = lookandsay(rst[i-1])
|
||||
end
|
||||
rst
|
||||
end
|
||||
|
||||
println(lookandsayseq(10))
|
||||
3
Task/Look-and-say-sequence/K/look-and-say-sequence.k
Normal file
3
Task/Look-and-say-sequence/K/look-and-say-sequence.k
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
las: {x{0$,//$(#:'n),'*:'n:(&1,~=':x)_ x:0$'$x}\1}
|
||||
las 8
|
||||
1 11 21 1211 111221 312211 13112221 1113213211 31131211131221
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
// version 1.0.6
|
||||
|
||||
fun lookAndSay(s: String): String {
|
||||
val sb = StringBuilder()
|
||||
var digit = s[0]
|
||||
var count = 1
|
||||
for (i in 1 until s.length) {
|
||||
if (s[i] == digit)
|
||||
count++
|
||||
else {
|
||||
sb.append("$count$digit")
|
||||
digit = s[i]
|
||||
count = 1
|
||||
}
|
||||
}
|
||||
return sb.append("$count$digit").toString()
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
var las = "1"
|
||||
for (i in 1..15) {
|
||||
println(las)
|
||||
las = lookAndSay(las)
|
||||
}
|
||||
}
|
||||
28
Task/Look-and-say-sequence/Lasso/look-and-say-sequence.lasso
Normal file
28
Task/Look-and-say-sequence/Lasso/look-and-say-sequence.lasso
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
define rle(str::string)::string => {
|
||||
local(orig = #str->values->asCopy,newi=array, newc=array, compiled=string)
|
||||
while(#orig->size) => {
|
||||
if(not #newi->size) => {
|
||||
#newi->insert(1)
|
||||
#newc->insert(#orig->first)
|
||||
#orig->remove(1)
|
||||
else
|
||||
if(#orig->first == #newc->last) => {
|
||||
#newi->get(#newi->size) += 1
|
||||
else
|
||||
#newi->insert(1)
|
||||
#newc->insert(#orig->first)
|
||||
}
|
||||
#orig->remove(1)
|
||||
}
|
||||
}
|
||||
loop(#newi->size) => {
|
||||
#compiled->append(#newi->get(loop_count)+#newc->get(loop_count))
|
||||
}
|
||||
return #compiled
|
||||
}
|
||||
define las(n::integer,run::integer) => {
|
||||
local(str = #n->asString)
|
||||
loop(#run) => { #str = rle(#str) }
|
||||
return #str
|
||||
}
|
||||
loop(15) => {^ las(1,loop_count) + '\r' ^}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
function lookAndSay S
|
||||
put 0 into C
|
||||
put char 1 of S into lastChar
|
||||
repeat with i = 2 to length(S)
|
||||
add 1 to C
|
||||
if char i of S is lastChar then next repeat
|
||||
put C & lastChar after R
|
||||
put 0 into C
|
||||
put char i of S into lastChar
|
||||
end repeat
|
||||
return R & C + 1 & lastChar
|
||||
end lookAndSay
|
||||
|
||||
on demoLookAndSay
|
||||
put 1 into x
|
||||
repeat 10
|
||||
put x & cr after message
|
||||
put lookAndSay(x) into x
|
||||
end repeat
|
||||
put x after message
|
||||
end demoLookAndSay
|
||||
11
Task/Look-and-say-sequence/Logo/look-and-say-sequence.logo
Normal file
11
Task/Look-and-say-sequence/Logo/look-and-say-sequence.logo
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
to look.and.say.loop :in :run :c :out
|
||||
if empty? :in [output (word :out :run :c)]
|
||||
if equal? first :in :c [output look.and.say.loop bf :in :run+1 :c :out]
|
||||
output look.and.say.loop bf :in 1 first :in (word :out :run :c)
|
||||
end
|
||||
to look.and.say :in
|
||||
if empty? :in [output :in]
|
||||
output look.and.say.loop bf :in 1 first :in "||
|
||||
end
|
||||
|
||||
show cascade 10 [print ? look.and.say ?] 1
|
||||
19
Task/Look-and-say-sequence/Lua/look-and-say-sequence-1.lua
Normal file
19
Task/Look-and-say-sequence/Lua/look-and-say-sequence-1.lua
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
--returns an iterator over the first n copies of the look-and-say sequence
|
||||
function lookandsayseq(n)
|
||||
local t = {1}
|
||||
return function()
|
||||
local ret = {}
|
||||
for i, v in ipairs(t) do
|
||||
if t[i-1] and v == t[i-1] then
|
||||
ret[#ret - 1] = ret[#ret - 1] + 1
|
||||
else
|
||||
ret[#ret + 1] = 1
|
||||
ret[#ret + 1] = v
|
||||
end
|
||||
end
|
||||
t = ret
|
||||
n = n - 1
|
||||
if n > 0 then return table.concat(ret) end
|
||||
end
|
||||
end
|
||||
for i in lookandsayseq(10) do print(i) end
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
require "lpeg"
|
||||
local P, C, Cf, Cc = lpeg.P, lpeg.C, lpeg.Cf, lpeg.Cc
|
||||
lookandsay = Cf(Cc"" * C(P"1"^1 + P"2"^1 + P"3"^1)^1, function (a, b) return a .. #b .. string.sub(b,1,1) end)
|
||||
t = "1"
|
||||
for i = 1, 10 do
|
||||
print(t)
|
||||
t = lookandsay:match(t)
|
||||
end
|
||||
19
Task/Look-and-say-sequence/Lua/look-and-say-sequence-3.lua
Normal file
19
Task/Look-and-say-sequence/Lua/look-and-say-sequence-3.lua
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
function lookandsay(t)
|
||||
return t:gsub("(1*)(2*)(3*)", function (...)
|
||||
local ret = {}
|
||||
for i = 1, select("#", ...) do
|
||||
local v = select(i, ...)
|
||||
if #v > 0 then
|
||||
ret[#ret + 1] = #v
|
||||
ret[#ret + 1] = v:sub(1,1)
|
||||
end
|
||||
end
|
||||
return table.concat(ret)
|
||||
end)
|
||||
end
|
||||
|
||||
local t = "1"
|
||||
for i = 1, 10 do
|
||||
print(t)
|
||||
t = lookandsay(t)
|
||||
end
|
||||
13
Task/Look-and-say-sequence/Lua/look-and-say-sequence-4.lua
Normal file
13
Task/Look-and-say-sequence/Lua/look-and-say-sequence-4.lua
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
function lookandsay2(t)
|
||||
return t:gsub("(1*)(2*)(3*)", function (x, y, z)
|
||||
return (x == "" and x or (#x .. x:sub(1, 1))) ..
|
||||
(y == "" and y or (#y .. y:sub(1, 1))) ..
|
||||
(z == "" and z or (#z .. z:sub(1, 1)))
|
||||
end)
|
||||
end
|
||||
|
||||
local t = "1"
|
||||
for i = 1, 10 do
|
||||
print(t)
|
||||
t = lookandsay2(t)
|
||||
end
|
||||
16
Task/Look-and-say-sequence/M4/look-and-say-sequence.m4
Normal file
16
Task/Look-and-say-sequence/M4/look-and-say-sequence.m4
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
divert(-1)
|
||||
define(`for',
|
||||
`ifelse($#,0,``$0'',
|
||||
`ifelse(eval($2<=$3),1,
|
||||
`pushdef(`$1',$2)$4`'popdef(`$1')$0(`$1',incr($2),$3,`$4')')')')
|
||||
|
||||
define(`las',
|
||||
`patsubst(`$1',`\(\(.\)\2*\)',`len(\1)`'\2')')
|
||||
|
||||
|
||||
define(`v',1)
|
||||
divert
|
||||
for(`x',1,10,
|
||||
`v
|
||||
define(`v',las(v))')dnl
|
||||
v
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
.TITLE LOKSAY
|
||||
.MCALL .TTYOUT,.EXIT
|
||||
LOKSAY::MOV #START,R0
|
||||
MOV #BUFR1,R1
|
||||
JSR PC,COPY
|
||||
MOV #^D14,R5
|
||||
$1: MOV #BUFR1,R1
|
||||
JSR PC,PRINT
|
||||
MOV #NEWLIN,R1
|
||||
JSR PC,PRINT
|
||||
JSR PC,STEP
|
||||
SOB R5,$1
|
||||
.EXIT
|
||||
STEP: MOV #BUFR1,R0
|
||||
MOV #BUFR2,R1
|
||||
BR 2$
|
||||
1$: INC R3
|
||||
CMPB (R0)+,R4
|
||||
BEQ 1$
|
||||
ADD #60,R3
|
||||
MOVB R3,(R1)+
|
||||
MOVB R4,(R1)+
|
||||
DEC R0
|
||||
2$: CLR R3
|
||||
MOVB (R0)+,R4
|
||||
BNE 1$
|
||||
MOV #BUFR2,R0
|
||||
MOV #BUFR1,R1
|
||||
COPY: MOVB (R0)+,(R1)+
|
||||
BNE COPY
|
||||
RTS PC
|
||||
PRINT: MOVB (R1)+,R0
|
||||
.TTYOUT
|
||||
BNE PRINT
|
||||
RTS PC
|
||||
NEWLIN: .BYTE 15,12,0,0
|
||||
START: .ASCIZ /1/
|
||||
BUFR1: .BLKB 400
|
||||
BUFR2: .BLKB 400
|
||||
.END LOKSAY
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
fn lookAndSay num =
|
||||
(
|
||||
local result = ""
|
||||
num += " "
|
||||
local current = num[1]
|
||||
local numReps = 1
|
||||
|
||||
for digit in 2 to num.count do
|
||||
(
|
||||
if num[digit] != current then
|
||||
(
|
||||
result += (numReps as string) + current
|
||||
numReps = 1
|
||||
current = num[digit]
|
||||
)
|
||||
else
|
||||
(
|
||||
numReps += 1
|
||||
)
|
||||
)
|
||||
result
|
||||
)
|
||||
|
||||
local num = "1"
|
||||
|
||||
for i in 1 to 10 do
|
||||
(
|
||||
print num
|
||||
num = lookAndSay num
|
||||
)
|
||||
27
Task/Look-and-say-sequence/Maple/look-and-say-sequence.maple
Normal file
27
Task/Look-and-say-sequence/Maple/look-and-say-sequence.maple
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
generate_seq := proc(s)
|
||||
local times, output, i;
|
||||
times := 1;
|
||||
output := "";
|
||||
for i from 2 to StringTools:-Length(s) do
|
||||
if (s[i] <> s[i-1]) then
|
||||
output := cat(output, times, s[i-1]);
|
||||
times := 1; # re-assign
|
||||
else
|
||||
times ++;
|
||||
end if;
|
||||
end do;
|
||||
cat(output, times, s[i - 1]);
|
||||
end proc:
|
||||
|
||||
Look_and_Say :=proc(n)
|
||||
local value, i;
|
||||
value := "1";
|
||||
print(value);
|
||||
for i from 2 to n do
|
||||
value := generate_seq(value);
|
||||
print(value);
|
||||
end do;
|
||||
end proc:
|
||||
|
||||
#Test:
|
||||
Look_and_Say(10);
|
||||
|
|
@ -0,0 +1 @@
|
|||
LookAndSay[n_Integer?Positive]:= Reverse @@@ Tally /@ Split @ IntegerDigits @ n // Flatten // FromDigits
|
||||
|
|
@ -0,0 +1 @@
|
|||
NestList[LookAndSay, 1, 12] // Column
|
||||
|
|
@ -0,0 +1 @@
|
|||
NestList[LookAndSay, 7, 12] // Column
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
collect(a) := block(
|
||||
[n: length(a), b: [ ], x: a[1], m: 1],
|
||||
for i from 2 thru n do
|
||||
(if a[i] = x then m: m + 1 else (b: endcons([x, m], b), x: a[i], m: 1)),
|
||||
b: endcons([x, m], b)
|
||||
)$
|
||||
|
||||
look_and_say(s) := apply(sconcat, map(lambda([p], sconcat(string(p[2]), p[1])), collect(charlist(s))))$
|
||||
|
||||
block([s: "1"], for i from 1 thru 10 do (disp(s), s: look_and_say(s)));
|
||||
/* "1"
|
||||
"11"
|
||||
"21"
|
||||
"1211"
|
||||
"111221"
|
||||
"312211"
|
||||
"13112221"
|
||||
"1113213211"
|
||||
"31131211131221"
|
||||
"13211311123113112211" */
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue