Created
May 7, 2017 16:57
-
-
Save wvdk/9e0ad11f2b4399cd6f4aa2faa0a7c078 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
func progression(from: Double, to: Double, numberOfItems: Int) -> [Double] { | |
let delta = (to - from) / Double(numberOfItems) | |
var results: [Double] = [] | |
for _ in 0..<numberOfItems { | |
let lastItem = results.last ?? from | |
let numberToAppend = lastItem + delta | |
results.append(numberToAppend) | |
} | |
return results | |
} | |
progression(from: 0.0, to: 1.0, numberOfItems: 10) // Desired output: [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0] | |
progression(from: 0.0, to: 100.0, numberOfItems: 100) | |
progression(from: 429.0, to: 3232.0, numberOfItems: 53) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Moved to https://github.com/wvdk/A-Swift-Vocabulary/blob/master/progression(from:to:numberOfItems:).swift