Skip to content

Instantly share code, notes, and snippets.

@ytyubox
Created November 1, 2021 00:26
Show Gist options
  • Save ytyubox/e10e400970e1b73a96222c01405494f9 to your computer and use it in GitHub Desktop.
Save ytyubox/e10e400970e1b73a96222c01405494f9 to your computer and use it in GitHub Desktop.
final class FibTests: XCTestCase {
class DPO1 {
func fib(_ n: Int) -> Int {
var dp = [0,1]
if n < 2 {return dp[n]}
for i in 2...n {
dp[i&1] = dp[0] + dp[1]
}
return dp[n&1]
}
}
func test() throws {
let fibs = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377 ,610]
for i in fibs.indices {
XCTAssertEqual(DPO1().fib(i), fibs[i])
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment