mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
[mypy] Fix type annotations for maths directory (#5782)
* [mypy] Fix annotations in `maths/series/p_series.py` * Update p_series.py * Update p_series.py * Remove from excluded in mypy.ini * Type annotation for series * Annotate maths/proth_number.py (properly) * Remove from excluded in mypy.ini * Annotate average_mode.py * Update average_mode.py * Update average_mode.py * Update average_mode.py * Update average_mode.py * Remove from excluded in mypy.ini * Fix annotations in gamma_recursive.py * Remove from excluded in mypy.ini * Annotations for geometric_series.py * Update geometric_series.py * Update mypy.ini * Update average_mode.py * Update average_mode.py * Update average_mode.py * Update mypy.ini * Update mypy.ini * Update mypy.ini * Update average_mode.py * Update proth_number.py * Update average_mode.py * Update gamma_recursive.py * Update proth_number.py * Update mypy.ini * Update geometric_series.py * Update average_mode.py * Update proth_number.py * Update geometric_series.py * Update geometric_series.py * Update geometric_series.py * Update p_series.py * Update geometric_series.py * Update p_series.py * Update p_series.py * Update geometric_series.py * Update p_series.py * Update p_series.py * Remove data_structures/stacks/next_greater_element.py| Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
db5aa1d188
commit
a98465230f
|
@ -1,34 +1,29 @@
|
|||
def mode(input_list: list) -> list: # Defining function "mode."
|
||||
from typing import Any
|
||||
|
||||
|
||||
def mode(input_list: list) -> list[Any]:
|
||||
"""This function returns the mode(Mode as in the measures of
|
||||
central tendency) of the input data.
|
||||
|
||||
The input list may contain any Datastructure or any Datatype.
|
||||
|
||||
>>> input_list = [2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2]
|
||||
>>> mode(input_list)
|
||||
>>> mode([2, 3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 2, 2, 2])
|
||||
[2]
|
||||
>>> input_list = [3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 2, 2, 2]
|
||||
>>> mode(input_list)
|
||||
>>> mode([3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 2, 2, 2])
|
||||
[2]
|
||||
>>> input_list = [3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 4, 2, 2, 4, 2]
|
||||
>>> mode(input_list)
|
||||
>>> mode([3, 4, 5, 3, 4, 2, 5, 2, 2, 4, 4, 4, 2, 2, 4, 2])
|
||||
[2, 4]
|
||||
>>> input_list = ["x", "y", "y", "z"]
|
||||
>>> mode(input_list)
|
||||
>>> mode(["x", "y", "y", "z"])
|
||||
['y']
|
||||
>>> input_list = ["x", "x" , "y", "y", "z"]
|
||||
>>> mode(input_list)
|
||||
>>> mode(["x", "x" , "y", "y", "z"])
|
||||
['x', 'y']
|
||||
"""
|
||||
result = list() # Empty list to store the counts of elements in input_list
|
||||
for x in input_list:
|
||||
result.append(input_list.count(x))
|
||||
if not result:
|
||||
if not input_list:
|
||||
return []
|
||||
y = max(result) # Gets the maximum value in the result list.
|
||||
result = [input_list.count(value) for value in input_list]
|
||||
y = max(result) # Gets the maximum count in the input list.
|
||||
# Gets values of modes
|
||||
result = {input_list[i] for i, value in enumerate(result) if value == y}
|
||||
return sorted(result)
|
||||
return sorted({input_list[i] for i, value in enumerate(result) if value == y})
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
Gamma function is a very useful tool in math and physics.
|
||||
It helps calculating complex integral in a convenient way.
|
||||
for more info: https://en.wikipedia.org/wiki/Gamma_function
|
||||
|
||||
Python's Standard Library math.gamma() function overflows around gamma(171.624).
|
||||
"""
|
||||
from math import pi, sqrt
|
||||
|
@ -71,7 +70,7 @@ if __name__ == "__main__":
|
|||
from doctest import testmod
|
||||
|
||||
testmod()
|
||||
num = 1
|
||||
num = 1.0
|
||||
while num:
|
||||
num = float(input("Gamma of: "))
|
||||
print(f"gamma({num}) = {gamma(num)}")
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
"""
|
||||
Calculate the nth Proth number
|
||||
|
||||
Source:
|
||||
https://handwiki.org/wiki/Proth_number
|
||||
"""
|
||||
|
@ -12,22 +11,17 @@ def proth(number: int) -> int:
|
|||
"""
|
||||
:param number: nth number to calculate in the sequence
|
||||
:return: the nth number in Proth number
|
||||
|
||||
Note: indexing starts at 1 i.e. proth(1) gives the first Proth number of 3
|
||||
|
||||
>>> proth(6)
|
||||
25
|
||||
|
||||
>>> proth(0)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: Input value of [number=0] must be > 0
|
||||
|
||||
>>> proth(-1)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: Input value of [number=-1] must be > 0
|
||||
|
||||
>>> proth(6.0)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
|
@ -44,14 +38,12 @@ def proth(number: int) -> int:
|
|||
elif number == 2:
|
||||
return 5
|
||||
else:
|
||||
block_index = number // 3
|
||||
"""
|
||||
+1 for binary starting at 0 i.e. 2^0, 2^1, etc.
|
||||
+1 to start the sequence at the 3rd Proth number
|
||||
Hence, we have a +2 in the below statement
|
||||
"""
|
||||
block_index = math.log(block_index, 2) + 2
|
||||
block_index = int(block_index)
|
||||
block_index = int(math.log(number // 3, 2)) + 2
|
||||
|
||||
proth_list = [3, 5]
|
||||
proth_index = 2
|
||||
|
@ -66,6 +58,10 @@ def proth(number: int) -> int:
|
|||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
||||
|
||||
for number in range(11):
|
||||
value = 0
|
||||
try:
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
"""
|
||||
This is a pure Python implementation of the Geometric Series algorithm
|
||||
https://en.wikipedia.org/wiki/Geometric_series
|
||||
|
||||
Run the doctests with the following command:
|
||||
python3 -m doctest -v geometric_series.py
|
||||
or
|
||||
|
@ -11,8 +10,17 @@ python3 geometric_series.py
|
|||
"""
|
||||
|
||||
|
||||
def geometric_series(nth_term: int, start_term_a: int, common_ratio_r: int) -> list:
|
||||
"""Pure Python implementation of Geometric Series algorithm
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def geometric_series(
|
||||
nth_term: float | int,
|
||||
start_term_a: float | int,
|
||||
common_ratio_r: float | int,
|
||||
) -> list[float | int]:
|
||||
"""
|
||||
Pure Python implementation of Geometric Series algorithm
|
||||
|
||||
:param nth_term: The last term (nth term of Geometric Series)
|
||||
:param start_term_a : The first term of Geometric Series
|
||||
:param common_ratio_r : The common ratio between all the terms
|
||||
|
@ -20,15 +28,15 @@ def geometric_series(nth_term: int, start_term_a: int, common_ratio_r: int) -> l
|
|||
ration with first term with increase in power till last term (nth term)
|
||||
Examples:
|
||||
>>> geometric_series(4, 2, 2)
|
||||
[2, '4.0', '8.0', '16.0']
|
||||
[2, 4.0, 8.0, 16.0]
|
||||
>>> geometric_series(4.0, 2.0, 2.0)
|
||||
[2.0, '4.0', '8.0', '16.0']
|
||||
[2.0, 4.0, 8.0, 16.0]
|
||||
>>> geometric_series(4.1, 2.1, 2.1)
|
||||
[2.1, '4.41', '9.261000000000001', '19.448100000000004']
|
||||
[2.1, 4.41, 9.261000000000001, 19.448100000000004]
|
||||
>>> geometric_series(4, 2, -2)
|
||||
[2, '-4.0', '8.0', '-16.0']
|
||||
[2, -4.0, 8.0, -16.0]
|
||||
>>> geometric_series(4, -2, 2)
|
||||
[-2, '-4.0', '-8.0', '-16.0']
|
||||
[-2, -4.0, -8.0, -16.0]
|
||||
>>> geometric_series(-4, 2, 2)
|
||||
[]
|
||||
>>> geometric_series(0, 100, 500)
|
||||
|
@ -38,9 +46,9 @@ def geometric_series(nth_term: int, start_term_a: int, common_ratio_r: int) -> l
|
|||
>>> geometric_series(0, 0, 0)
|
||||
[]
|
||||
"""
|
||||
if "" in (nth_term, start_term_a, common_ratio_r):
|
||||
return ""
|
||||
series = []
|
||||
if not all((nth_term, start_term_a, common_ratio_r)):
|
||||
return []
|
||||
series: list[float | int] = []
|
||||
power = 1
|
||||
multiple = common_ratio_r
|
||||
for _ in range(int(nth_term)):
|
||||
|
@ -48,16 +56,20 @@ def geometric_series(nth_term: int, start_term_a: int, common_ratio_r: int) -> l
|
|||
series.append(start_term_a)
|
||||
else:
|
||||
power += 1
|
||||
series.append(str(float(start_term_a) * float(multiple)))
|
||||
series.append(float(start_term_a * multiple))
|
||||
multiple = pow(float(common_ratio_r), power)
|
||||
return series
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
nth_term = input("Enter the last number (n term) of the Geometric Series")
|
||||
start_term_a = input("Enter the starting term (a) of the Geometric Series")
|
||||
common_ratio_r = input(
|
||||
"Enter the common ratio between two terms (r) of the Geometric Series"
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
||||
|
||||
nth_term = float(input("Enter the last number (n term) of the Geometric Series"))
|
||||
start_term_a = float(input("Enter the starting term (a) of the Geometric Series"))
|
||||
common_ratio_r = float(
|
||||
input("Enter the common ratio between two terms (r) of the Geometric Series")
|
||||
)
|
||||
print("Formula of Geometric Series => a + ar + ar^2 ... +ar^n")
|
||||
print(geometric_series(nth_term, start_term_a, common_ratio_r))
|
||||
|
|
|
@ -1,48 +1,52 @@
|
|||
"""
|
||||
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
|
||||
"""
|
||||
|
||||
|
||||
def p_series(nth_term: int, power: int) -> list:
|
||||
"""Pure Python implementation of P-Series algorithm
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def p_series(nth_term: int | float | str, power: int | 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']
|
||||
['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']
|
||||
['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]
|
||||
['1']
|
||||
"""
|
||||
if nth_term == "":
|
||||
return nth_term
|
||||
return [""]
|
||||
nth_term = int(nth_term)
|
||||
power = int(power)
|
||||
series = []
|
||||
series: list[str] = []
|
||||
for temp in range(int(nth_term)):
|
||||
series.append(f"1/{pow(temp + 1, int(power))}" if series else 1)
|
||||
series.append(f"1 / {pow(temp + 1, int(power))}" if series else "1")
|
||||
return series
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
nth_term = input("Enter the last number (nth term) of the P-Series")
|
||||
power = input("Enter the power for P-Series")
|
||||
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))
|
||||
|
|
3
mypy.ini
3
mypy.ini
|
@ -2,5 +2,4 @@
|
|||
ignore_missing_imports = True
|
||||
install_types = True
|
||||
non_interactive = True
|
||||
exclude = (data_structures/stacks/next_greater_element.py|graphs/boruvka.py|graphs/breadth_first_search.py|graphs/breadth_first_search_2.py|graphs/check_cycle.py|graphs/finding_bridges.py|graphs/greedy_min_vertex_cover.py|graphs/random_graph_generator.py|maths/average_mode.py|maths/gamma_recursive.py|maths/proth_number.py|maths/series/geometric_series.py|maths/series/p_series.py|matrix_operation.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py)
|
||||
|
||||
exclude = (graphs/boruvka.py|graphs/breadth_first_search.py|graphs/breadth_first_search_2.py|graphs/check_cycle.py|graphs/finding_bridges.py|graphs/greedy_min_vertex_cover.py|graphs/random_graph_generator.py|matrix_operation.py|other/least_recently_used.py|other/lfu_cache.py|other/lru_cache.py|searches/simulated_annealing.py|searches/ternary_search.py)
|
||||
|
|
Loading…
Reference in New Issue
Block a user