From 9efb4eac2ef1fe27090d916c0c461238bbd22dbb Mon Sep 17 00:00:00 2001 From: John Doe Date: Thu, 3 Mar 2016 12:21:57 +0000 Subject: [PATCH] initial solution --- longest-run-linux/Package.swift | 0 longest-run-linux/Source/main.swift | 49 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 longest-run-linux/Package.swift create mode 100644 longest-run-linux/Source/main.swift diff --git a/longest-run-linux/Package.swift b/longest-run-linux/Package.swift new file mode 100644 index 0000000..e69de29 diff --git a/longest-run-linux/Source/main.swift b/longest-run-linux/Source/main.swift new file mode 100644 index 0000000..3059625 --- /dev/null +++ b/longest-run-linux/Source/main.swift @@ -0,0 +1,49 @@ +/* +The challenge here is to implement the longestRun function so that the +longest run of the same continuous character will be selected from a String. +This excercise lends itself to TDD and we have provided a few basic tests below. +*/ + +// import Cocoa + +func longestRun(whole: String) -> String { + var currentMaxLen = 0 + var currentMaxChar:Character = " " + var currentLen = 0 + var currentChar:Character = " " + for char in whole.characters { + if char != currentChar { + currentChar = char + currentLen = 1 + } + else { + currentLen++ + } + + if currentLen > currentMaxLen { + currentMaxLen = currentLen + currentMaxChar = currentChar + } + + } + + var subStr = "" + for _ in 1...currentMaxLen { + subStr.append(currentMaxChar) + } + + return subStr +} + +let result = longestRun("zzzz") + +// identity test +var zzzz = "zzzz" +assert(zzzz == longestRun(zzzz), "\(zzzz) should be the longest run of chars") + +print("hello linux \(result)") + +// book +// var book = "book" +// assert("oo" == longestRun(book), "longest run in \(book) is 'oo'") +