From 21d66ece80f4899f14503606e1f2d7aeb7bc0eb6 Mon Sep 17 00:00:00 2001 From: daniel david kovacs Date: Thu, 3 Mar 2016 12:53:31 +0100 Subject: [PATCH 1/3] solution ? --- longest-run.playground/Contents.swift | 29 ++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/longest-run.playground/Contents.swift b/longest-run.playground/Contents.swift index 7a5f9f5..78b395a 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 sorted = whole.characters.sort() + + var currentRunCharacter = sorted.first + var currentRun = 0 + var longestRunCharacter = currentRunCharacter + var longestRun = 0 + + if currentRunCharacter == nil { + return "" + } + + sorted.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,6 @@ 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" +assert("oo" == longestRun(book), "longest run in \(book) is 'oo'") From 19cb1854032f47ad93f1cdd287c45811b3142f60 Mon Sep 17 00:00:00 2001 From: daniel david kovacs Date: Thu, 3 Mar 2016 15:21:05 +0100 Subject: [PATCH 2/3] fix --- longest-run.playground/Contents.swift | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/longest-run.playground/Contents.swift b/longest-run.playground/Contents.swift index 78b395a..c8c0e4e 100644 --- a/longest-run.playground/Contents.swift +++ b/longest-run.playground/Contents.swift @@ -7,9 +7,9 @@ This excercise lends itself to TDD and we have provided a few basic tests below. import Cocoa func longestRun(whole: String) -> String { - let sorted = whole.characters.sort() + let characters = whole.characters - var currentRunCharacter = sorted.first + var currentRunCharacter = characters.first var currentRun = 0 var longestRunCharacter = currentRunCharacter var longestRun = 0 @@ -18,7 +18,7 @@ func longestRun(whole: String) -> String { return "" } - sorted.forEach({(let char: Character) -> () in + characters.forEach({(let char: Character) -> () in if char == currentRunCharacter! { currentRun += 1 } else { @@ -38,6 +38,6 @@ 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'") From 0cf1c2458ea160dfb28f5df2af39ecf7adfcc9e5 Mon Sep 17 00:00:00 2001 From: daniel david kovacs Date: Thu, 17 Mar 2016 07:51:20 +0100 Subject: [PATCH 3/3] wip --- longest-run.playground/Contents.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/longest-run.playground/Contents.swift b/longest-run.playground/Contents.swift index c8c0e4e..2c7e280 100644 --- a/longest-run.playground/Contents.swift +++ b/longest-run.playground/Contents.swift @@ -40,4 +40,3 @@ assert(zzzz == longestRun(zzzz), "\(zzzz) should be the longest run of chars") // book var book = "book🐰🐰🐰🐰aaa" assert("🐰🐰🐰🐰" == longestRun(book), "longest run in \(book) is 'oo'") -