Skip to content

Instantly share code, notes, and snippets.

@mredig
Created May 1, 2019 15:40
Show Gist options
  • Save mredig/854855e06cef34c232d81847455aeca4 to your computer and use it in GitHub Desktop.
Save mredig/854855e06cef34c232d81847455aeca4 to your computer and use it in GitHub Desktop.
swift string contains without contains
import Cocoa
extension String {
func anotherContains(_ string: String) -> Bool {
let lcSelf = self.lowercased()
let lc = string.lowercased()
return lcSelf.range(of: lc) != nil
}
func anotherAnotherContains(_ string: String) -> Bool {
let lcSelf = self.lowercased()
let lc = string.lowercased()
return lcSelf.components(separatedBy: lc).count > 1
}
func anotherAnotherAnotherContains(_ string: String) -> Bool {
let lcSelf = self.lowercased()
let lc = string.lowercased()
lcSelf.replacingOccurrences(of: lc, with: "")
return lcSelf.replacingOccurrences(of: lc, with: "").count != self.count
}
func anotherAnotherAnotherAnotherContains(_ string: String) -> Bool {
let lcSelf = Array(self.lowercased().utf8)
let lc = Array(string.lowercased().utf8)
outer: for (index, value) in lcSelf.enumerated() {
if lc.count > lcSelf.count - index {
break
}
if value == lc.first {
for (lcIndex, lcValue) in lc.enumerated() {
let testIndex = index + lcIndex
if lcSelf[testIndex] != lcValue {
continue outer
}
}
return true
}
}
return false
}
}
print("Where is WaLdO".anotherContains("WALDO"))
print("Where is waldo".anotherContains("waldo"))
print("Where is WaLdfO".anotherContains("WALDO"))
print("Where is WaLdfO".anotherContains(""))
print("Where is WaLdO".anotherAnotherContains("WALDO"))
print("Where is waldo".anotherAnotherContains("waldo"))
print("Where is WaLdfO".anotherAnotherContains("WALDO"))
print("Where is WaLdfO".anotherAnotherContains(""))
print("Where is WaLdO".anotherAnotherAnotherContains("WALDO"))
print("Where is waldo".anotherAnotherAnotherContains("waldo"))
print("Where is WaLdfO".anotherAnotherAnotherContains("WALDO"))
print("Where is WaLdfO".anotherAnotherAnotherContains(""))
print("Where is WaLdO".anotherAnotherAnotherAnotherContains("WALDO"))
print("Where is waldo".anotherAnotherAnotherAnotherContains("waldo"))
print("Where is WaLdfO".anotherAnotherAnotherAnotherContains("WALDO"))
print("Where is WaLdfO".anotherAnotherAnotherAnotherContains(""))
// every sequence has been tested to return true, true, false, false
@jkaunert
Copy link

jkaunert commented May 1, 2019

That's many functions. Moving forward, I would love to have you include your thoughts about each implementation- pros/cons/usefullness. With these early code challenges, I am trying to reinforce how valuable documentation is in solving a problem. I am noticing a lot of students going straight to stack overflow, not realizing that every answer is not always correct... But also, trying to have them be challenging enough for more advanced students, like you, to start to see the nuance. In this example, if you dug deep enough, you may have learned, among other things, about ObjC class bridging into Swift, and that Swift's range() is a struct, whereas NSString's range(of:) is a class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment