Skip to content

Commit 663f902

Browse files
author
billbarbour
committed
linter cleanup
1 parent 4813ee1 commit 663f902

File tree

2 files changed

+48
-26
lines changed

2 files changed

+48
-26
lines changed

Rabin-Karp/Rabin-Karp.playground/Contents.swift

+28-17
Original file line numberDiff line numberDiff line change
@@ -16,70 +16,81 @@ func ** (radix: Double, power: Int) -> Double {
1616
}
1717

1818
extension Character {
19-
var asInt:Int {
19+
var asInt: Int {
2020
let s = String(self).unicodeScalars
2121
return Int(s[s.startIndex].value)
2222
}
2323
}
2424

2525
// Find first position of pattern in the text using Rabin Karp algorithm
26-
public func search(text: String , pattern: String) -> Int {
26+
public func search(text: String, pattern: String) -> Int {
2727
// convert to array of ints
2828
let patternArray = pattern.characters.flatMap { $0.asInt }
2929
let textArray = text.characters.flatMap { $0.asInt }
30-
30+
3131
if textArray.count < patternArray.count {
3232
return -1
3333
}
34-
34+
3535
let patternHash = hash(array: patternArray)
3636
var endIdx = patternArray.count - 1
3737
let firstChars = Array(textArray[0...endIdx])
3838
let firstHash = hash(array: firstChars)
39-
40-
if (patternHash == firstHash) {
39+
40+
if patternHash == firstHash {
4141
// Verify this was not a hash collison
4242
if firstChars == patternArray {
4343
return 0
4444
}
4545
}
46-
46+
4747
var prevHash = firstHash
4848
// Now slide the window across the text to be searched
4949
for idx in 1...(textArray.count - patternArray.count) {
5050
endIdx = idx + (patternArray.count - 1)
5151
let window = Array(textArray[idx...endIdx])
52-
let windowHash = nextHash(prevHash: prevHash, dropped: textArray[idx - 1], added: textArray[endIdx], patternSize: patternArray.count - 1)
53-
52+
let windowHash = nextHash(
53+
prevHash: prevHash,
54+
dropped: textArray[idx - 1],
55+
added: textArray[endIdx],
56+
patternSize: patternArray.count - 1
57+
)
58+
5459
if windowHash == patternHash {
5560
if patternArray == window {
5661
return idx
5762
}
5863
}
59-
64+
6065
prevHash = windowHash
6166
}
62-
67+
6368
return -1
6469
}
6570

6671
public func hash(array: Array<Int>) -> Double {
67-
var total : Double = 0
72+
var total: Double = 0
6873
var exponent = array.count - 1
6974
for i in array {
7075
total += Double(i) * (Double(Constants.hashMultiplier) ** exponent)
7176
exponent -= 1
7277
}
73-
78+
7479
return Double(total)
7580
}
7681

7782
public func nextHash(prevHash: Double, dropped: Int, added: Int, patternSize: Int) -> Double {
78-
let oldHash = prevHash - (Double(dropped) * (Double(Constants.hashMultiplier) ** patternSize))
83+
let oldHash = prevHash - (Double(dropped) *
84+
(Double(Constants.hashMultiplier) ** patternSize))
7985
return Double(Constants.hashMultiplier) * oldHash + Double(added)
8086
}
8187

8288
// TESTS
83-
assert(search(text:"The big dog jumped over the fox", pattern:"ump") == 13, "Invalid index returned")
84-
assert(search(text:"The big dog jumped over the fox", pattern:"missed") == Int(-1), "Invalid index returned")
85-
assert(search(text:"The big dog jumped over the fox", pattern:"T") == 0, "Invalid index returned")
89+
assert(search(text:"The big dog jumped over the fox",
90+
pattern:"ump") == 13, "Invalid index returned")
91+
92+
assert(search(text:"The big dog jumped over the fox",
93+
pattern:"missed") == -1, "Invalid index returned")
94+
95+
assert(search(text:"The big dog jumped over the fox",
96+
pattern:"T") == 0, "Invalid index returned")

Rabin-Karp/rabin-karp.swift

+20-9
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ func ** (radix: Double, power: Int) -> Double {
3434
}
3535

3636
extension Character {
37-
var asInt:Int {
37+
var asInt: Int {
3838
let s = String(self).unicodeScalars
3939
return Int(s[s.startIndex].value)
4040
}
4141
}
4242

4343
// Find first position of pattern in the text using Rabin Karp algorithm
44-
public func search(text: String , pattern: String) -> Int {
44+
public func search(text: String, pattern: String) -> Int {
4545
// convert to array of ints
4646
let patternArray = pattern.characters.flatMap { $0.asInt }
4747
let textArray = text.characters.flatMap { $0.asInt }
@@ -55,7 +55,7 @@ public func search(text: String , pattern: String) -> Int {
5555
let firstChars = Array(textArray[0...endIdx])
5656
let firstHash = hash(array: firstChars)
5757

58-
if (patternHash == firstHash) {
58+
if patternHash == firstHash {
5959
// Verify this was not a hash collison
6060
if firstChars == patternArray {
6161
return 0
@@ -67,7 +67,12 @@ public func search(text: String , pattern: String) -> Int {
6767
for idx in 1...(textArray.count - patternArray.count) {
6868
endIdx = idx + (patternArray.count - 1)
6969
let window = Array(textArray[idx...endIdx])
70-
let windowHash = nextHash(prevHash: prevHash, dropped: textArray[idx - 1], added: textArray[endIdx], patternSize: patternArray.count - 1)
70+
let windowHash = nextHash(
71+
prevHash: prevHash,
72+
dropped: textArray[idx - 1],
73+
added: textArray[endIdx],
74+
patternSize: patternArray.count - 1
75+
)
7176

7277
if windowHash == patternHash {
7378
if patternArray == window {
@@ -82,7 +87,7 @@ public func search(text: String , pattern: String) -> Int {
8287
}
8388

8489
public func hash(array: Array<Int>) -> Double {
85-
var total : Double = 0
90+
var total: Double = 0
8691
var exponent = array.count - 1
8792
for i in array {
8893
total += Double(i) * (Double(Constants.hashMultiplier) ** exponent)
@@ -93,11 +98,17 @@ public func hash(array: Array<Int>) -> Double {
9398
}
9499

95100
public func nextHash(prevHash: Double, dropped: Int, added: Int, patternSize: Int) -> Double {
96-
let oldHash = prevHash - (Double(dropped) * (Double(Constants.hashMultiplier) ** patternSize))
101+
let oldHash = prevHash - (Double(dropped) *
102+
(Double(Constants.hashMultiplier) ** patternSize))
97103
return Double(Constants.hashMultiplier) * oldHash + Double(added)
98104
}
99105

100106
// TESTS
101-
assert(search(text:"The big dog jumped over the fox", pattern:"ump") == 13, "Invalid index returned")
102-
assert(search(text:"The big dog jumped over the fox", pattern:"missed") == -1, "Invalid index returned")
103-
assert(search(text:"The big dog jumped over the fox", pattern:"T") == 0, "Invalid index returned")
107+
assert(search(text:"The big dog jumped over the fox",
108+
pattern:"ump") == 13, "Invalid index returned")
109+
110+
assert(search(text:"The big dog jumped over the fox",
111+
pattern:"missed") == -1, "Invalid index returned")
112+
113+
assert(search(text:"The big dog jumped over the fox",
114+
pattern:"T") == 0, "Invalid index returned")

0 commit comments

Comments
 (0)