Skip to content

Instantly share code, notes, and snippets.

@bhakes
Created November 14, 2018 16:41
Show Gist options
  • Save bhakes/1d38d2049036e4d584af9f3a0e55417a to your computer and use it in GitHub Desktop.
Save bhakes/1d38d2049036e4d584af9f3a0e55417a to your computer and use it in GitHub Desktop.
// assumption:
// - I will only get passed ints
// - I can get passed both negative and positive ints
//
//
import Foundation
// main funct
func expandedNumber(_ number: Int) -> [Int]{
// instantiate vars
var divResult: Int = number
var remainder: Int
var digitCount: Int = 0
var componentArray: [Int] = []
// catch input of zero here and return
if (divResult == 0){
return [0]
}
// while the result of integer division is greater than absolute value of 1
// add new components to the array
while (divResult >= 1 || divResult <= -1){
// get the coefficient of the digit
remainder = divResult % 10
// insert coeffient + digit specific power term into from of the component array
componentArray.insert(Int(Double(remainder) * pow(10.0, Double(digitCount))), at: 0)
// prepare to handle the next coefficient and power term
divResult = divResult / 10
digitCount += 1
}
return componentArray
}
// test cases
var testCases = [43443,341,562,1,2,0001,-00001,0,102,32,23424, -1, -2, -51, -5051,-99990]
// run func on test cases
for test in testCases {
print(expandedNumber(test))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment