Created
November 16, 2022 12:48
-
-
Save norohind/9ef9c09ebfecc2c9dd57bd44cd585691 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
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