mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-06 10:31:29 +08:00
Pyupgrade to Python 3.9 (#4718)
* Pyupgrade to Python 3.9 * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@ -1,14 +1,15 @@
|
||||
"""
|
||||
Approximates the area under the curve using the trapezoidal rule
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Callable, Union
|
||||
from typing import Callable
|
||||
|
||||
|
||||
def trapezoidal_area(
|
||||
fnc: Callable[[Union[int, float]], Union[int, float]],
|
||||
x_start: Union[int, float],
|
||||
x_end: Union[int, float],
|
||||
fnc: Callable[[int | float], int | float],
|
||||
x_start: int | float,
|
||||
x_end: int | float,
|
||||
steps: int = 100,
|
||||
) -> float:
|
||||
"""
|
||||
|
@ -1,7 +1,7 @@
|
||||
from typing import List
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def mean(nums: List) -> float:
|
||||
def mean(nums: list) -> float:
|
||||
"""
|
||||
Find mean of a list of numbers.
|
||||
Wiki: https://en.wikipedia.org/wiki/Mean
|
||||
|
@ -1,7 +1,7 @@
|
||||
from typing import Union
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def median(nums: list) -> Union[int, float]:
|
||||
def median(nums: list) -> int | float:
|
||||
"""
|
||||
Find median of a list of numbers.
|
||||
Wiki: https://en.wikipedia.org/wiki/Median
|
||||
|
@ -68,7 +68,7 @@ def calculate_prob(text: str) -> None:
|
||||
my_fir_sum += prob * math.log2(prob) # entropy formula.
|
||||
|
||||
# print entropy
|
||||
print("{:.1f}".format(round(-1 * my_fir_sum)))
|
||||
print(f"{round(-1 * my_fir_sum):.1f}")
|
||||
|
||||
# two len string
|
||||
all_sum = sum(two_char_strings.values())
|
||||
@ -83,10 +83,10 @@ def calculate_prob(text: str) -> None:
|
||||
my_sec_sum += prob * math.log2(prob)
|
||||
|
||||
# print second entropy
|
||||
print("{:.1f}".format(round(-1 * my_sec_sum)))
|
||||
print(f"{round(-1 * my_sec_sum):.1f}")
|
||||
|
||||
# print the difference between them
|
||||
print("{:.1f}".format(round((-1 * my_sec_sum) - (-1 * my_fir_sum))))
|
||||
print(f"{round((-1 * my_sec_sum) - (-1 * my_fir_sum)):.1f}")
|
||||
|
||||
|
||||
def analyze_text(text: str) -> tuple[dict, dict]:
|
||||
|
@ -1,3 +1,5 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Iterable, Union
|
||||
|
||||
import numpy as np
|
||||
|
@ -12,12 +12,12 @@ https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
|
||||
# @Email: silentcat@protonmail.com
|
||||
# @Last modified by: pikulet
|
||||
# @Last modified time: 2020-10-02
|
||||
from __future__ import annotations
|
||||
|
||||
import sys
|
||||
from typing import Tuple
|
||||
|
||||
|
||||
def extended_euclidean_algorithm(a: int, b: int) -> Tuple[int, int]:
|
||||
def extended_euclidean_algorithm(a: int, b: int) -> tuple[int, int]:
|
||||
"""
|
||||
Extended Euclidean Algorithm.
|
||||
|
||||
|
@ -37,7 +37,7 @@ def exactPrimeFactorCount(n):
|
||||
if __name__ == "__main__":
|
||||
n = 51242183
|
||||
print(f"The number of distinct prime factors is/are {exactPrimeFactorCount(n)}")
|
||||
print("The value of log(log(n)) is {:.4f}".format(math.log(math.log(n))))
|
||||
print(f"The value of log(log(n)) is {math.log(math.log(n)):.4f}")
|
||||
|
||||
"""
|
||||
The number of distinct prime factors is/are 3
|
||||
|
@ -1,11 +1,13 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from typing import Callable, Union
|
||||
from typing import Callable
|
||||
|
||||
|
||||
def line_length(
|
||||
fnc: Callable[[Union[int, float]], Union[int, float]],
|
||||
x_start: Union[int, float],
|
||||
x_end: Union[int, float],
|
||||
fnc: Callable[[int | float], int | float],
|
||||
x_start: int | float,
|
||||
x_end: int | float,
|
||||
steps: int = 100,
|
||||
) -> float:
|
||||
|
||||
|
@ -6,10 +6,10 @@ Instead of using a nested for loop, in a Brute force approach we will use a tech
|
||||
called 'Window sliding technique' where the nested loops can be converted to a single
|
||||
loop to reduce time complexity.
|
||||
"""
|
||||
from typing import List
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def max_sum_in_array(array: List[int], k: int) -> int:
|
||||
def max_sum_in_array(array: list[int], k: int) -> int:
|
||||
"""
|
||||
Returns the maximum sum of k consecutive elements
|
||||
>>> arr = [1, 4, 2, 10, 2, 3, 1, 0, 20]
|
||||
|
@ -1,7 +1,7 @@
|
||||
from typing import List
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def median_of_two_arrays(nums1: List[float], nums2: List[float]) -> float:
|
||||
def median_of_two_arrays(nums1: list[float], nums2: list[float]) -> float:
|
||||
"""
|
||||
>>> median_of_two_arrays([1, 2], [3])
|
||||
2
|
||||
|
@ -1,14 +1,15 @@
|
||||
"""
|
||||
Approximates the area under the curve using the trapezoidal rule
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Callable, Union
|
||||
from typing import Callable
|
||||
|
||||
|
||||
def trapezoidal_area(
|
||||
fnc: Callable[[Union[int, float]], Union[int, float]],
|
||||
x_start: Union[int, float],
|
||||
x_end: Union[int, float],
|
||||
fnc: Callable[[int | float], int | float],
|
||||
x_start: int | float,
|
||||
x_end: int | float,
|
||||
steps: int = 100,
|
||||
) -> float:
|
||||
|
||||
|
@ -10,13 +10,12 @@ Reference: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
|
||||
doctest provider: Bruno Simas Hadlich (https://github.com/brunohadlich)
|
||||
Also thanks to Dmitry (https://github.com/LizardWizzard) for finding the problem
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from typing import List
|
||||
|
||||
|
||||
def prime_sieve(num: int) -> List[int]:
|
||||
def prime_sieve(num: int) -> list[int]:
|
||||
"""
|
||||
Returns a list with all prime numbers up to n.
|
||||
|
||||
|
@ -3,11 +3,12 @@ Find Volumes of Various Shapes.
|
||||
|
||||
Wikipedia reference: https://en.wikipedia.org/wiki/Volume
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from math import pi, pow
|
||||
from typing import Union
|
||||
|
||||
|
||||
def vol_cube(side_length: Union[int, float]) -> float:
|
||||
def vol_cube(side_length: int | float) -> float:
|
||||
"""
|
||||
Calculate the Volume of a Cube.
|
||||
|
||||
|
Reference in New Issue
Block a user