Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial solution #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added longest-run-linux/Package.swift
Empty file.
49 changes: 49 additions & 0 deletions longest-run-linux/Source/main.swift
Original file line number Diff line number Diff line change
@@ -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'")