Last active
June 18, 2017 22:32
-
-
Save ProbablyRusty/360109ee054edac1971b6e9bcf275a83 to your computer and use it in GitHub Desktop.
Swift Concatenated Integers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In reference to: | |
// https://www.reddit.com/r/dailyprogrammer/comments/69y21t/20170508_challenge_314_easy_concatenated_integers/ | |
let input = """ | |
79 82 34 83 69 | |
420 34 19 71 341 | |
17 32 91 7 46 | |
""" | |
func pad (_ strToPad: String, maxLength: Int) -> String { | |
var str = strToPad | |
while str.count < maxLength { | |
str = str + String(str.characters.first!) | |
} | |
return str | |
} | |
func arrayToInt (_ arr: [String]) -> Int? { | |
var outString = "" | |
for i in arr { | |
outString += i | |
} | |
return Int(outString) | |
} | |
var lines = input.split(separator: "\n") | |
for line in lines { | |
let lineArray = line.split(separator: " ") | |
let lineArrayAsStrings = lineArray.map { String($0) } | |
var maxLength = 0 | |
for i in lineArrayAsStrings { | |
if i.count > maxLength { maxLength = i.count } | |
} | |
let max = lineArrayAsStrings.sorted() { pad($1, maxLength: maxLength) < pad($0, maxLength: maxLength) } | |
let min = lineArrayAsStrings.sorted() { pad($1, maxLength: maxLength) > pad($0, maxLength: maxLength) } | |
print("\(arrayToInt(min)!) \(arrayToInt(max)!)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment