Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,44 @@
function fannkuch(integer n)
sequence start = tagset(n),
perm,
perm1 = start,
count = start
integer maxFlipsCount = 0, r = n+1
integer perm0, flipsCount, k, k2, j, j2
while 1 do
while r!=1 do count[r-1] = r r -= 1 end while
if not (perm1[1]=1 or perm1[n]=n) then
perm = perm1
flipsCount = 0
k = perm[1]
while k!=1 do
k2 = floor((k+1)/2)
perm = reverse(perm[1..k]) & perm[k+1..n]
flipsCount += 1
k = perm[1]
end while
if flipsCount>maxFlipsCount then
maxFlipsCount = flipsCount
end if
end if
-- Use incremental change to generate another permutation
while 1 do
if r>n then return maxFlipsCount end if
perm0 = perm1[1]
j2 = 1
while j2<r do
j = j2+1
perm1[j2] = perm1[j]
j2 = j
end while
perm1[r] = perm0
count[r] = count[r]-1
if count[r]>1 then exit else r += 1 end if
end while
end while
end function -- fannkuch
for i=1 to 10 do
? fannkuch(i)
end for

View file

@ -0,0 +1,54 @@
range = (a, b):
i = 0, l = list(b-a+1)
while (a + i <= b):
l (i) = a + i++.
l.
fannkuch = (n):
flips = 0, maxf = 0, k = 0, m = n - 1, r = n
perml = range(0, n), count = list(n), perm = list(n)
loop:
while (r != 1):
count (r-1) = r
r--.
if (perml (0) != 0 and perml (m) != m):
flips = 0, i = 1
while (i < n):
perm (i) = perml (i)
i++.
k = perml (0)
loop:
i = 1, j = k - 1
while (i < j):
t = perm (i), perm (i) = perm (j), perm (j) = t
i++, j--.
flips++
j = perm (k), perm (k) = k, k = j
if (k == 0): break.
.
if (flips > maxf): maxf = flips.
.
loop:
if (r == n):
(n, maxf) say
return (maxf).
i = 0, j = perml (0)
while (i < r):
k = i + 1
perml (i) = perml (k)
i = k.
perml (r) = j
j = count (r) - 1
count (r) = j
if (j > 0): break.
r++
_ n
n = argv(1) number
if (n<1): n=10.
fannkuch(n)

View file

@ -0,0 +1,20 @@
# "while" as defined here is included in recent versions (>1.4) of jq:
def until(cond; next):
def _until:
if cond then . else (next|_until) end;
_until;
# Generate a stream of permutations of [1, ... n].
# This implementation uses arity-0 filters for speed.
def permutations:
# Given a single array, insert generates a stream by inserting (length+1) at different positions
def insert: # state: [m, array]
.[0] as $m | (1+(.[1]|length)) as $n
| .[1]
| if $m >= 0 then (.[0:$m] + [$n] + .[$m:]), ([$m-1, .] | insert) else empty end;
if .==0 then []
elif . == 1 then [1]
else
. as $n | ($n-1) | permutations | [$n-1, .] | insert
end;

View file

@ -0,0 +1,13 @@
# Input: a permutation; output: an integer
def flips:
# state: [i, array]
[0, .]
| until( .[1][0] == 1;
.[1] as $p | $p[0] as $p0
| [.[0] + 1, ($p[:$p0] | reverse) + $p[$p0:] ] )
| .[0];
# input: n, the number of items
def fannkuch:
reduce permutations as $p
(0; [., ($p|flips) ] | max);

View file

@ -0,0 +1 @@
range(1; 11) | [., fannkuch ]

View file

@ -0,0 +1,11 @@
$ jq -n -c -f topswops.jq
[1,0]
[2,1]
[3,2]
[4,4]
[5,7]
[6,10]
[7,16]
[8,22]
[9,30]
[10,38]