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,11 @@
shared test void searchAListTask() {
value haystack = [
"Zig", "Zag", "Wally", "Ronald", "Bush",
"Krusty", "Charlie", "Bush", "Bozo"];
assert(exists firstIdx = haystack.firstOccurrence("Bush"));
assert(exists lastIdx = haystack.lastOccurrence("Bush"));
assertEquals(firstIdx, 4);
assertEquals(lastIdx, 7);
}

View file

@ -0,0 +1,9 @@
local(haystack) = array('Zig', 'Zag', 'Wally', 'Ronald', 'Bush', 'Krusty', 'Charlie', 'Bush', 'Bozo')
#haystack->findindex('Bush')->first // 5
#haystack->findindex('Bush')->last // 8
protect => {^
handle_error => {^ error_msg ^}
fail_if(not #haystack->findindex('Washington')->first,'Washington is not in haystack.')
^}

View file

@ -0,0 +1,11 @@
haystack = ["apples", "oranges", "bananas", "oranges"]
needle = "oranges"
pos = haystack.getPos(needle)
if pos then
put "needle found at index "&pos
else
put "needle not found in haystack"
end if
-- "needle found at index 2"

View file

@ -0,0 +1,8 @@
let haystack = ["Zig","Zag","Wally","Ronald","Bush","Krusty","Charlie","Bush","Bozo"]
for needle in ["Bush", "Washington"]:
let f = haystack.find(needle)
if f >= 0:
echo f, " ", needle
else:
raise newException(ValueError, needle & " not in haystack")

View file

@ -0,0 +1,8 @@
: needleIndex(needle, haystack)
haystack indexOf(needle) dup ifNull: [ drop ExRuntime throw("Not found", needle) ] ;
[ "Zig", "Zag", "Wally", "Ronald", "Bush", "Krusty", "Charlie", "Bush", "Boz" ] const: Haystack
needleIndex("Bush", Haystack) println
Haystack lastIndexOf("Bush") println
needleIndex("Washington", Haystack) println

View file

@ -0,0 +1,7 @@
constant s = {"Zig", "Zag", "Wally", "Ronald", "Bush", "Krusty", "Charlie", "Bush", "Boz", "Zag"}
integer r = find("Zag",s) ?r -- 2 (first)
r = find("Zag",s,r+1) ?r -- 10 (next)
r = find("Zag",s,r+1) ?r -- 0 (no more)
r = rfind("Zag",s) ?r -- 10 (last)
r = find("Zog",s) ?r -- 0 (none)

View file

@ -0,0 +1,18 @@
haystack = ["alpha","bravo","charlie","delta","echo","foxtrot","golf",
"hotel","india","juliet","kilo","lima","mike","needle",
"november","oscar","papa","quebec","romeo","sierra","tango",
"needle","uniform","victor","whisky","x-ray","yankee","zulu"]
needle = "needle"
maxindex = len(haystack)
for index = 1 to maxindex
if needle = haystack[index] exit ok
next
if index <= maxindex
see "first found at index " + index + nl ok
for last = maxindex to 0 step -1
if needle = haystack[last] exit ok
next
if !=index see " last found at index " + last + nl
else see "not found" + nl ok

View file

@ -0,0 +1,10 @@
var haystack = %w(Zig Zag Wally Ronald Bush Krusty Charlie Bush Bozo);
%w(Bush Washington).each { |needle|
var i = haystack.first_index{|item| item == needle};
if (i >= 0) {
say "#{i} #{needle}";
} else {
die "#{needle} is not in haystack";
}
}

View file

@ -0,0 +1,2 @@
var haystack = %w(Zig Zag Wally Ronald Bush Krusty Charlie Bush Bozo);
say haystack.last_index{|item| item == "Bush"};

View file

@ -0,0 +1,8 @@
let haystack = ["Zig","Zag","Wally","Ronald","Bush","Krusty","Charlie","Bush","Bozo"]
for needle in ["Washington","Bush"] {
if let index = haystack.indexOf(needle) {
print("\(index) \(needle)")
} else {
print("\(needle) is not in haystack")
}
}

View file

@ -0,0 +1,8 @@
let haystack = ["Zig","Zag","Wally","Ronald","Bush","Krusty","Charlie","Bush","Bozo"]
for needle in ["Washington","Bush"] {
if let index = find(haystack, needle) {
println("\(index) \(needle)")
} else {
println("\(needle) is not in haystack")
}
}

View file

@ -0,0 +1,20 @@
// the second part can be done several ways, but extending any Array of Comparable objects is the most generic approach
extension Array where Element : Comparable {
func lastIndexMatching(needle:Element) -> Int? {
for i in stride(from: count-1, through: 0, by: -1) {
if self[i] == needle {
return i
}
}
return nil
}
}
for needle in ["Washington","Bush"] {
if let index = haystack.lastIndexMatching(needle) {
print("\(index) \(needle)")
} else {
print("\(needle) is not in haystack")
}
}

View file

@ -0,0 +1,6 @@
def (pos x (seq | (head ... tail)) n)
default n :to 0
if seq
if (head = x)
n
(pos x tail n+1)

View file

@ -0,0 +1,15 @@
["a","b","c"] | index("b")
# => 1
["a","b","c","b"] | index("b")
# => 1
["a","b","c","b"]
| index("x") as $ix
| if $ix then $ix else error("element not found") end
# => jq: error: element not found
# Extra task - the last element of an array can be retrieved
# using -1 as an index:
["a","b","c","b","d"] | indices("b")[-1]
# => 3