diff --git a/longest-run.playground/Contents.swift b/longest-run.playground/Contents.swift index 7a5f9f5..2c7e280 100644 --- a/longest-run.playground/Contents.swift +++ b/longest-run.playground/Contents.swift @@ -7,7 +7,30 @@ This excercise lends itself to TDD and we have provided a few basic tests below. import Cocoa func longestRun(whole: String) -> String { - return whole + let characters = whole.characters + + var currentRunCharacter = characters.first + var currentRun = 0 + var longestRunCharacter = currentRunCharacter + var longestRun = 0 + + if currentRunCharacter == nil { + return "" + } + + characters.forEach({(let char: Character) -> () in + if char == currentRunCharacter! { + currentRun += 1 + } else { + currentRunCharacter = char + currentRun = 1 + } + if longestRun < currentRun { + longestRun = currentRun + longestRunCharacter = currentRunCharacter + } + }) + return String([Character](count: longestRun, repeatedValue: longestRunCharacter!)) } // identity test @@ -15,6 +38,5 @@ var zzzz = "zzzz" assert(zzzz == longestRun(zzzz), "\(zzzz) should be the longest run of chars") // book -// var book = "book" -// assert("oo" == longestRun(book), "longest run in \(book) is 'oo'") - +var book = "book🐰🐰🐰🐰aaa" +assert("🐰🐰🐰🐰" == longestRun(book), "longest run in \(book) is 'oo'")