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,16 @@
Lbl BSEARCH
0→L
r₃-1→H
While L≤H
(L+H)/2→M
If {L+M}>r₂
M-1→H
ElseIf {L+M}<r₂
M+1→L
Else
M
Return
End
End
-1
Return

View file

@ -0,0 +1,13 @@
fun main(as: [n]int, value: int): int =
let low = 0
let high = n-1
loop ((low,high)) = while low <= high do
-- invariants: value > as[i] for all i < low
-- value < as[i] for all i > high
let mid = (low+high) / 2
in if as[mid] > value
then (low, mid - 1)
else if as[mid] < value
then (mid + 1, high)
else (mid, mid-1) -- Force termination.
in low

View file

@ -0,0 +1,4 @@
import algorithm
let s = @[2,3,4,5,6,7,8,9,10,12,14,16,18,20,22,25,27,30]
echo binarySearch(s, 10)

View file

@ -0,0 +1,7 @@
proc binarySearch[T](a: openArray[T], key: T): int =
var b = len(a)
while result < b:
var mid = (result + b) div 2
if a[mid] < key: result = mid + 1
else: b = mid
if result >= len(a) or a[result] != key: result = -1

View file

@ -0,0 +1,16 @@
function binary_search(sequence s, object val, integer low, integer high)
integer mid, cmp
if high < low then
return 0 -- not found
else
mid = floor( (low + high) / 2 )
cmp = compare(s[mid], val)
if cmp > 0 then
return binary_search(s, val, low, mid-1)
elsif cmp < 0 then
return binary_search(s, val, mid+1, high)
else
return mid
end if
end if
end function

View file

@ -0,0 +1,17 @@
function binary_search(sequence s, object val)
integer low, high, mid, cmp
low = 1
high = length(s)
while low <= high do
mid = floor( (low + high) / 2 )
cmp = compare(s[mid], val)
if cmp > 0 then
high = mid - 1
elsif cmp < 0 then
low = mid + 1
else
return mid
end if
end while
return 0 -- not found
end function

View file

@ -0,0 +1,20 @@
decimals(0)
array = [7, 14, 21, 28, 35, 42, 49, 56, 63, 70]
find= 42
index = where(array, find, 0, len(array))
if index >= 0
see "the value " + find+ " was found at index " + index
else
see "the value " + find + " was not found"
ok
func where a, s, b, t
h = 2
while h<(t-b) h *= 2 end
h /= 2
while h != 0
if (b+h)<=t if s>=a[b+h] b += h ok ok
h /= 2
end
if s=a[b] return b-1 else return -1 ok

View file

@ -0,0 +1,11 @@
binarySearch(A(1), value(0), low(0), high(0)) :=
let
mid := low + (high - low) / 2;
in
-1 when high < low //Not Found
else
binarySearch(A, value, low, mid - 1) when A[mid] > value
else
binarySearch(A, value, mid + 1, high) when A[mid] < value
else
mid;

View file

@ -0,0 +1,14 @@
func binary_search(a, i) {
var l = 0;
var h = a.end;
while (l <= h) {
var mid = (h+l / 2 -> int);
a[mid] > i && (h = mid-1; next);
a[mid] < i && (l = mid+1; next);
return mid;
}
return -1;
}

View file

@ -0,0 +1,13 @@
func binary_search(arr, value, low=0, high=arr.end) {
high < low && return -1;
var middle = (high+low / 2 -> int);
if (value < arr[middle]) {
return binary_search(arr, value, low, middle-1);
}
elsif (value > arr[middle]) {
return binary_search(arr, value, middle+1, high);
}
return middle;
}

View file

@ -0,0 +1 @@
say binary_search([34, 42, 55, 778], 55); #=> 2

View file

@ -0,0 +1,10 @@
func binarySearch<T: Comparable>(xs: [T], x: T) -> Int? {
var recurse: ((Int, Int) -> Int?)!
recurse = {(low, high) in switch (low + high) / 2 {
case _ where high < low: return nil
case let mid where xs[mid] > x: return recurse(low, mid - 1)
case let mid where xs[mid] < x: return recurse(mid + 1, high)
case let mid: return mid
}}
return recurse(0, xs.count - 1)
}

View file

@ -0,0 +1,11 @@
func binarySearch<T: Comparable>(xs: [T], x: T) -> Int? {
var (low, high) = (0, xs.count - 1)
while low <= high {
switch (low + high) / 2 {
case let mid where xs[mid] > x: high = mid - 1
case let mid where xs[mid] < x: low = mid + 1
case let mid: return mid
}
}
return nil
}

View file

@ -0,0 +1,15 @@
func testBinarySearch(n: Int) {
let odds = Array(stride(from: 1, through: n, by: 2))
let result = flatMap(0...n) {binarySearch(odds, $0)}
assert(result == Array(0..<odds.count))
println("\(odds) are odd natural numbers")
for it in result {
println("\(it) is ordinal of \(odds[it])")
}
}
testBinarySearch(12)
func flatMap<T, U>(source: [T], transform: (T) -> U?) -> [U] {
return source.reduce([]) {(var xs, x) in if let x = transform(x) {xs.append(x)}; return xs}
}

View file

@ -0,0 +1,24 @@
; Recursive
@var rec &[a v l h] [
@if < h l @return null
@var m @/ +h l 2
@? {
> `m a v @!rec[a v l -m 1]
< `m a v @!rec[a v +1 m h]
m
}
]
; Iterative
@var itr &[a v] [
@vars{l 0 h #-a}
@while <= l h [
@var m @/ +l h 2
@iff {
> `m a v :h -m 1
< `m a v :l +m 1
@return m
}
]
null
]

View file

@ -0,0 +1,11 @@
def binarySearch(value):
# To avoid copying the array, simply pass in the current low and high offsets
def binarySearch(low; high):
if (high < low) then (-1 - low)
else ( (low + high) / 2 | floor) as $mid
| if (.[$mid] > value) then binarySearch(low; $mid-1)
elif (.[$mid] < value) then binarySearch($mid+1; high)
else $mid
end
end;
binarySearch(0; length-1);

View file

@ -0,0 +1 @@
[-1,-1.1,1,1,null,[null]] | binarySearch(1)