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
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")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue