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,26 @@
import algorithm
proc pancakeSort[T](list: var openarray[T]) =
var length = list.len
if length < 2: return
var moves = 0
for i in countdown(length, 2):
var maxNumPos = 0
for a in 0 .. <i:
if list[a] > list[maxNumPos]:
maxNumPos = a
if maxNumPos == i - 1: continue
if maxNumPos > 0:
inc moves
reverse(list, 0, maxNumPos)
inc moves
reverse(list, 0, i - 1)
var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782]
pancakeSort a
echo a

View file

@ -0,0 +1,29 @@
function flip(sequence s, integer n)
for i=1 to floor(n/2) do
{s[i],s[n-i+1]} = {s[n-i+1],s[i]}
end for
return s
end function
function pancake_sort(sequence s)
integer m
for i=length(s) to 2 by -1 do
m = 1
for j=2 to i do
if s[j]>s[m] then
m = j
end if
end for
if m<i then
if m>1 then
s = flip(s,m)
end if
s = flip(s,i)
end if
end for
return s
end function
constant s = shuffle(tagset(10))
? s
? pancake_sort(s)

View file

@ -0,0 +1,29 @@
pancakeList = [6, 7, 8, 9, 2, 5, 3, 4, 1]
flag = 0
see "Before :" + nl
for n = 1 to len(pancakeList)
see pancakeList[n] + " "
next
see nl
pancakeSort(pancakeList)
see "After :" + nl
for n = 1 to len(pancakeList)
see pancakeList[n] + " "
next
see nl
func pancakeSort A
n = len(A)
while flag = 0
flag = 1
for i = 1 to n-1
if A[i] < A[i+1]
temp = A[i]
A[i] = A[i+1]
A [i+1] = temp
flag = 0 ok
next
end
return A

View file

@ -0,0 +1,14 @@
func pancake(a) {
for idx in ^(a.end) {
var min = idx
for i in (idx+1 .. a.end) { min = i if (a[min] > a[i]) }
next if (a[min] == a[idx])
a[min..a.end] = [a[min..a.end]].reverse...
a[idx..a.end] = [a[idx..a.end]].reverse...
}
return a
}
var arr = 10.of{ 100.irand }
say "Before: #{arr}"
say "After: #{pancake(arr)}"

View file

@ -0,0 +1,66 @@
import Foundation
struct PancakeSort {
var arr:[Int]
mutating func flip(n:Int) {
for i in 0 ..< (n + 1) / 2 {
swap(&arr[n - i], &arr[i])
}
println("flip(0.. \(n)): \(arr)")
}
func minmax(n:Int) -> [Int] {
var xm = arr[0]
var xM = arr[0]
var posm = 0
var posM = 0
for i in 1..<n {
if (arr[i] < xm) {
xm = arr[i]
posm = i
} else if (arr[i] > xM) {
xM = arr[i]
posM = i
}
}
return [posm, posM]
}
mutating func sort(var n:Int, var dir:Int) {
if n == 0 {
return
}
let mM = minmax(n)
let bestXPos = mM[dir]
let altXPos = mM[1 - dir]
var flipped = false
if bestXPos == n - 1 {
n--
} else if bestXPos == 0 {
flip(n - 1)
n--
} else if altXPos == n - 1 {
dir = 1 - dir
n--
flipped = true
} else {
flip(bestXPos)
}
sort(n, dir: dir)
if flipped {
flip(n)
}
}
}
let arr = [2, 3, 6, 1, 4, 5, 10, 8, 7, 9]
var a = PancakeSort(arr: arr)
a.sort(arr.count, dir: 1)
println(a.arr)

View file

@ -0,0 +1,22 @@
def pancakeSort:
def flip(i):
. as $in | ($in[0:i+1]|reverse) + $in[i+1:] ;
# If input is [] then return null
def index_of_max:
. as $in
| reduce range(1; length) as $i
# state: [ix, max]
( [ 0, $in[0] ];
if $in[$i] > .[1] then [ $i, $in[$i] ] else . end )
| .[0] ;
reduce range(0; length) as $iup
(.;
(length - $iup - 1) as $i
| (.[0:$i+1] | index_of_max) as $max
# flip about $max and then about $i unless $i == $max
| if ($i == $max) then .
else flip($max) | flip($i)
end ) ;

View file

@ -0,0 +1,2 @@
[range(0;2), null, 1.0, 0.5, [1], [2], {"b":1}, {"a":2}, range(2;4)]
| pancakeSort

View file

@ -0,0 +1,2 @@
$ jq -M -c -n -f pancake_sort.jq
[null,0,0.5,1,1,2,3,[1],[2],{"a":2},{"b":1}]