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
(nth_term: float | str, power: float | str)
| 13 | |
| 14 | |
| 15 | def p_series(nth_term: float | str, power: float | str) -> list[str]: |
| 16 | """ |
| 17 | Pure Python implementation of P-Series algorithm |
| 18 | :return: The P-Series starting from 1 to last (nth) term |
| 19 | Examples: |
| 20 | >>> p_series(5, 2) |
| 21 | ['1', '1 / 4', '1 / 9', '1 / 16', '1 / 25'] |
| 22 | >>> p_series(-5, 2) |
| 23 | [] |
| 24 | >>> p_series(5, -2) |
| 25 | ['1', '1 / 0.25', '1 / 0.1111111111111111', '1 / 0.0625', '1 / 0.04'] |
| 26 | >>> p_series("", 1000) |
| 27 | [''] |
| 28 | >>> p_series(0, 0) |
| 29 | [] |
| 30 | >>> p_series(1, 1) |
| 31 | ['1'] |
| 32 | """ |
| 33 | if nth_term == "": |
| 34 | return [""] |
| 35 | nth_term = int(nth_term) |
| 36 | power = int(power) |
| 37 | series: list[str] = [] |
| 38 | for temp in range(int(nth_term)): |
| 39 | series.append(f"1 / {pow(temp + 1, int(power))}" if series else "1") |
| 40 | return series |
| 41 | |
| 42 | |
| 43 | if __name__ == "__main__": |