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
11
Task/Search-a-list/Ceylon/search-a-list.ceylon
Normal file
11
Task/Search-a-list/Ceylon/search-a-list.ceylon
Normal 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);
|
||||
}
|
||||
9
Task/Search-a-list/Lasso/search-a-list.lasso
Normal file
9
Task/Search-a-list/Lasso/search-a-list.lasso
Normal 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.')
|
||||
^}
|
||||
11
Task/Search-a-list/Lingo/search-a-list.lingo
Normal file
11
Task/Search-a-list/Lingo/search-a-list.lingo
Normal 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"
|
||||
8
Task/Search-a-list/Nim/search-a-list.nim
Normal file
8
Task/Search-a-list/Nim/search-a-list.nim
Normal 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")
|
||||
8
Task/Search-a-list/Oforth/search-a-list.oforth
Normal file
8
Task/Search-a-list/Oforth/search-a-list.oforth
Normal 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
|
||||
7
Task/Search-a-list/Phix/search-a-list.phix
Normal file
7
Task/Search-a-list/Phix/search-a-list.phix
Normal 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)
|
||||
18
Task/Search-a-list/Ring/search-a-list.ring
Normal file
18
Task/Search-a-list/Ring/search-a-list.ring
Normal 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
|
||||
10
Task/Search-a-list/Sidef/search-a-list-1.sidef
Normal file
10
Task/Search-a-list/Sidef/search-a-list-1.sidef
Normal 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";
|
||||
}
|
||||
}
|
||||
2
Task/Search-a-list/Sidef/search-a-list-2.sidef
Normal file
2
Task/Search-a-list/Sidef/search-a-list-2.sidef
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
var haystack = %w(Zig Zag Wally Ronald Bush Krusty Charlie Bush Bozo);
|
||||
say haystack.last_index{|item| item == "Bush"};
|
||||
8
Task/Search-a-list/Swift/search-a-list-1.swift
Normal file
8
Task/Search-a-list/Swift/search-a-list-1.swift
Normal 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")
|
||||
}
|
||||
}
|
||||
8
Task/Search-a-list/Swift/search-a-list-2.swift
Normal file
8
Task/Search-a-list/Swift/search-a-list-2.swift
Normal 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")
|
||||
}
|
||||
}
|
||||
20
Task/Search-a-list/Swift/search-a-list-3.swift
Normal file
20
Task/Search-a-list/Swift/search-a-list-3.swift
Normal 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")
|
||||
}
|
||||
}
|
||||
6
Task/Search-a-list/Wart/search-a-list.wart
Normal file
6
Task/Search-a-list/Wart/search-a-list.wart
Normal 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)
|
||||
15
Task/Search-a-list/jq/search-a-list.jq
Normal file
15
Task/Search-a-list/jq/search-a-list.jq
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue