Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
16
Task/Binary-search/Axe/binary-search.axe
Normal file
16
Task/Binary-search/Axe/binary-search.axe
Normal 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
|
||||
13
Task/Binary-search/Futhark/binary-search.futhark
Normal file
13
Task/Binary-search/Futhark/binary-search.futhark
Normal 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
|
||||
4
Task/Binary-search/Nim/binary-search-1.nim
Normal file
4
Task/Binary-search/Nim/binary-search-1.nim
Normal 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)
|
||||
7
Task/Binary-search/Nim/binary-search-2.nim
Normal file
7
Task/Binary-search/Nim/binary-search-2.nim
Normal 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
|
||||
16
Task/Binary-search/Phix/binary-search-1.phix
Normal file
16
Task/Binary-search/Phix/binary-search-1.phix
Normal 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
|
||||
17
Task/Binary-search/Phix/binary-search-2.phix
Normal file
17
Task/Binary-search/Phix/binary-search-2.phix
Normal 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
|
||||
20
Task/Binary-search/Ring/binary-search.ring
Normal file
20
Task/Binary-search/Ring/binary-search.ring
Normal 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
|
||||
11
Task/Binary-search/SequenceL/binary-search.sequencel
Normal file
11
Task/Binary-search/SequenceL/binary-search.sequencel
Normal 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;
|
||||
14
Task/Binary-search/Sidef/binary-search-1.sidef
Normal file
14
Task/Binary-search/Sidef/binary-search-1.sidef
Normal 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;
|
||||
}
|
||||
13
Task/Binary-search/Sidef/binary-search-2.sidef
Normal file
13
Task/Binary-search/Sidef/binary-search-2.sidef
Normal 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;
|
||||
}
|
||||
1
Task/Binary-search/Sidef/binary-search-3.sidef
Normal file
1
Task/Binary-search/Sidef/binary-search-3.sidef
Normal file
|
|
@ -0,0 +1 @@
|
|||
say binary_search([34, 42, 55, 778], 55); #=> 2
|
||||
10
Task/Binary-search/Swift/binary-search-1.swift
Normal file
10
Task/Binary-search/Swift/binary-search-1.swift
Normal 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)
|
||||
}
|
||||
11
Task/Binary-search/Swift/binary-search-2.swift
Normal file
11
Task/Binary-search/Swift/binary-search-2.swift
Normal 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
|
||||
}
|
||||
15
Task/Binary-search/Swift/binary-search-3.swift
Normal file
15
Task/Binary-search/Swift/binary-search-3.swift
Normal 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}
|
||||
}
|
||||
24
Task/Binary-search/Wortel/binary-search.wortel
Normal file
24
Task/Binary-search/Wortel/binary-search.wortel
Normal 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
|
||||
]
|
||||
11
Task/Binary-search/jq/binary-search-1.jq
Normal file
11
Task/Binary-search/jq/binary-search-1.jq
Normal 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);
|
||||
1
Task/Binary-search/jq/binary-search-2.jq
Normal file
1
Task/Binary-search/jq/binary-search-2.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
[-1,-1.1,1,1,null,[null]] | binarySearch(1)
|
||||
Loading…
Add table
Add a link
Reference in a new issue