Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

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")
}
}