Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wvdk/9e0ad11f2b4399cd6f4aa2faa0a7c078 to your computer and use it in GitHub Desktop.
Save wvdk/9e0ad11f2b4399cd6f4aa2faa0a7c078 to your computer and use it in GitHub Desktop.
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