Skip to content

Instantly share code, notes, and snippets.

@norohind
Created November 16, 2022 12:48
Show Gist options
  • Save norohind/9ef9c09ebfecc2c9dd57bd44cd585691 to your computer and use it in GitHub Desktop.
Save norohind/9ef9c09ebfecc2c9dd57bd44cd585691 to your computer and use it in GitHub Desktop.
from typing import Iterable
import numbers
def finite_difference(data: Iterable[numbers.Number]) -> list[Iterable[numbers.Number]]:
"""A function to calculate finite difference of given sequence of numbers"""
to_iterate = [data]
while len(to_iterate[-1]) != 1:
to_iterate.append([])
for i in range(len(to_iterate[-2]) - 1):
to_iterate[-1].append(to_iterate[-2][i + 1] - to_iterate[-2][i])
return to_iterate
if __name__ == '__main__':
example_data = (1.9, 9.3, 2.2, 2.1)
res = finite_difference(example_data)
print(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment