Skip to content

Instantly share code, notes, and snippets.

@ChenCodes
Created June 1, 2017 21:21
Show Gist options
  • Save ChenCodes/0ac6ea42e3a0fc73f9543b326708b11f to your computer and use it in GitHub Desktop.
Save ChenCodes/0ac6ea42e3a0fc73f9543b326708b11f to your computer and use it in GitHub Desktop.
/*Algorithm spec:
Given a long list of numbers:
let numbers = 1...1000000
Write a function that transforms the list of numbers to strings with the following mapping and put it into a variable called results.
1->A, 2->B, 3->C ... 10->A0, 11->AA, 12->AB etc.
With the results from the last part, write a function that adds the following elements.
addedByThree = results[3] + results[33] + results[333] + ...
*/
func mapper<C: Collection>(list: C) -> [String] {
var results: [String] = []
for number in list {
var mapped_element = ""
let array = (String(describing: number)).characters.flatMap{Int(String($0))}
for elem in array {
switch elem {
case 1:
mapped_element += "A"
case 2:
mapped_element += "B"
case 3:
mapped_element += "C"
case 4:
mapped_element += "D"
case 5:
mapped_element += "E"
case 6:
mapped_element += "F"
case 7:
mapped_element += "G"
case 8:
mapped_element += "H"
case 9:
mapped_element += "I"
case 0:
mapped_element += "0"
default:
break
}
}
results += [mapped_element]
}
return results
}
func addedByThree(numbers: [String]) -> String {
var threeElements: [String] = []
var numberOfDigits = 1
var currentString = ""
while numberOfDigits < String(describing: numbers.count).characters.count {
currentString += "3"
threeElements += [currentString]
numberOfDigits += 1
}
var finalString = ""
for elem in threeElements {
finalString += numbers[Int(elem)!]
}
return finalString
}
//print(addedByThree(numbers: mapper(list: 1...100)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment