mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
ae0fc85401
* Fix ruff errors Renamed neural_network/input_data.py to neural_network/input_data.py_tf because it should be left out of the directory for the following reasons: 1. Its sole purpose is to be used by neural_network/gan.py_tf, which is itself left out of the directory because of issues with TensorFlow. 2. It was taken directly from TensorFlow's codebase and is actually already deprecated. If/when neural_network/gan.py_tf is eventually re-added back to the directory, its implementation should be changed to not use neural_network/input_data.py anyway. * updating DIRECTORY.md * Change input_data.py_tf file extension Change input_data.py_tf file extension because algorithms-keeper bot is being picky about it --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
53 lines
1.4 KiB
Python
53 lines
1.4 KiB
Python
"""
|
|
This is a pure Python implementation of the P-Series algorithm
|
|
https://en.wikipedia.org/wiki/Harmonic_series_(mathematics)#P-series
|
|
For doctests run following command:
|
|
python -m doctest -v p_series.py
|
|
or
|
|
python3 -m doctest -v p_series.py
|
|
For manual testing run:
|
|
python3 p_series.py
|
|
"""
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
def p_series(nth_term: float | str, power: float | str) -> list[str]:
|
|
"""
|
|
Pure Python implementation of P-Series algorithm
|
|
:return: The P-Series starting from 1 to last (nth) term
|
|
Examples:
|
|
>>> p_series(5, 2)
|
|
['1', '1 / 4', '1 / 9', '1 / 16', '1 / 25']
|
|
>>> p_series(-5, 2)
|
|
[]
|
|
>>> p_series(5, -2)
|
|
['1', '1 / 0.25', '1 / 0.1111111111111111', '1 / 0.0625', '1 / 0.04']
|
|
>>> p_series("", 1000)
|
|
['']
|
|
>>> p_series(0, 0)
|
|
[]
|
|
>>> p_series(1, 1)
|
|
['1']
|
|
"""
|
|
if nth_term == "":
|
|
return [""]
|
|
nth_term = int(nth_term)
|
|
power = int(power)
|
|
series: list[str] = []
|
|
for temp in range(int(nth_term)):
|
|
series.append(f"1 / {pow(temp + 1, int(power))}" if series else "1")
|
|
return series
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|
|
|
|
nth_term = int(input("Enter the last number (nth term) of the P-Series"))
|
|
power = int(input("Enter the power for P-Series"))
|
|
print("Formula of P-Series => 1+1/2^p+1/3^p ..... 1/n^p")
|
|
print(p_series(nth_term, power))
|