"""This is a pure Python implementation of Dynamic Programming solution to the fibonaccisequence problem."""class Fibonacci:def __init__(self) -> None:self.sequence = [0, 1]def get(self, index: int) -> list:"""Get the Fibonacci number of `index`. If the number does not exist,calculate all missing numbers leading up to the number of `index`.>>> Fibonacci().get(10)[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]>>> Fibonacci().get(5)[0, 1, 1, 2, 3]"""difference = index - (len(self.sequence) - 2)if difference >= 1:for _ in range(difference):self.sequence.append(self.sequence[-1] + self.sequence[-2])return self.sequence[:index]def main():print("Fibonacci Series Using Dynamic Programming\n","Enter the index of the Fibonacci number you want to calculate ","in the prompt below. (To exit enter exit or Ctrl-C)\n",sep="",)fibonacci = Fibonacci()while True:prompt: str = input(">> ")if prompt in {"exit", "quit"}:breaktry:index: int = int(prompt)except ValueError:print("Enter a number or 'exit'")continueprint(fibonacci.get(index))if __name__ == "__main__":main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。