Pancake numbers: Difference between revisions

→‎{{header|REXX}}: Show examples as required for this task
m (Added language identifier.)
(→‎{{header|REXX}}: Show examples as required for this task)
Line 1,162:
 
=={{header|REXX}}==
{{incomplete|REXX|Show examples requiring p(1..9) flips}}
{{trans|Go}}
{{trans|Phix}}
Line 1,205 ⟶ 1,204:
19 21
20 23
</pre>
Show examples as required for this task
<syntaxhighlight lang="rexx">
/* REXX Driver for pancake.test */
do n=2 To 10
Call pancake n
End
/**********************************************************************
* REXX pancake.rex
* The task is to determine p(n) for n = 1 to 9,
* and for each show an example requiring p(n) flips.
* inspired by java and output like Phix
* 20230531 Walter Pachl
**********************************************************************/
Call time 'R'
parse arg n -- Number of pancakes
init=left('123456789abc',n) -- ordered pancakes
Call o 'heureka' n
q.=0 -- implements the queue
qp=1
ex=0
call qadd init
stackFlips.=-1 -- initialize map
stackFlips.init=0 -- stackFlips.v: number of flips
-- from init to v
cnt.=0
cnt.1=1
max=0
Do while qp<=q.0 -- as long we can flip
s=qget() -- get head of queue
flips=stackFlips.s+1 -- flips for the successors
cnt.flips=cnt.flips+1 -- count them
If flips>max Then ex=0 -- a new maximum
max=max(max,flips)
Do i=2 To n -- process n-1 successors
t=flip(s,i) -- one of them
If stackFlips.t=-1 Then Do -- not set so far
stackFlips.t=flips -- flips from init to t
Call qadd t -- append it to the queue
If ex<3 Then Do -- show the forst 3 examples
call o flips t
If ex>=0 Then Do -- record the data to be shown
example='' -- as an example (see o2)
Do While t<>''
Parse Var t c +1 t
Select
When c='a' Then c=10
When c='b' Then c=11
When c='c' Then c=12
Otherwise Nop
End
example=example||c||','
End
exf=flips
example=strip(example,'T',',')
End
ex=ex+1
End
End
End
End
Call o 'max cnt.max:' max cnt.max
te=time('E') -- elaüsed time
te=strip(format(te,8,1))
Call o te 'seconds'
Call o ''
Call o2 'p('n') = 'exf', example: {'example'} (of' cnt.max', 'te's)'
Exit
 
flip: Procedure
Parse Arg s,k -- cf. flipStack in java
Return substr(s,k,1)reverse(left(s,k-1))||substr(s,k+1)
 
qadd: -- add an element to the queue
Parse Arg e
z=q.0+1
q.z=e
q.0=z
Return
 
qget: -- get top element from the queue
e=q.qp -- and remove it from the queue
qp=qp+1
Return e
 
o: -- investigation and debug output
Say arg(1)
Return lineout('heureka.txt',arg(1))
 
o2: -- result to be shown in rosettacode
Say arg(1)
Call lineout 'heureka.out',arg(1)
Call lineout 'heureka.out'
Return</syntaxhighlight>
{{out|output|text=&nbsp; when using pancake_test.rex:}}
<pre>p(2) = 1, example: {2,1} (of 1, 0.0s)
p(3) = 3, example: {1,3,2} (of 1, 0.0s)
p(4) = 4, example: {3,1,4,2} (of 3, 0.0s)
p(5) = 5, example: {1,3,5,2,4} (of 20, 0.0s)
p(6) = 7, example: {4,6,2,5,1,3} (of 2, 0.0s)
p(7) = 8, example: {5,7,3,1,6,2,4} (of 35, 0.1s)
p(8) = 9, example: {6,8,4,2,3,1,7,5} (of 455, 1.1s)
p(9) = 10, example: {8,5,7,9,1,3,2,4,6} (of 5804, 11.6s)
p(10) = 11, example: {5,1,3,2,4,6,10,8,9,7} (of 73232, 238.5s)
</pre>
 
2,295

edits