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:
Christian Clauss
2021-09-07 13:37:03 +02:00
committed by GitHub
parent 5d5831bdd0
commit cecf43d648
142 changed files with 523 additions and 530 deletions

View File

@ -3,10 +3,10 @@ Python program for Bitonic Sort.
Note that this program works only when size of input is a power of 2.
"""
from typing import List
from __future__ import annotations
def comp_and_swap(array: List[int], index1: int, index2: int, direction: int) -> None:
def comp_and_swap(array: list[int], index1: int, index2: int, direction: int) -> None:
"""Compare the value at given index1 and index2 of the array and swap them as per
the given direction.
@ -37,7 +37,7 @@ def comp_and_swap(array: List[int], index1: int, index2: int, direction: int) ->
array[index1], array[index2] = array[index2], array[index1]
def bitonic_merge(array: List[int], low: int, length: int, direction: int) -> None:
def bitonic_merge(array: list[int], low: int, length: int, direction: int) -> None:
"""
It recursively sorts a bitonic sequence in ascending order, if direction = 1, and in
descending if direction = 0.
@ -61,7 +61,7 @@ def bitonic_merge(array: List[int], low: int, length: int, direction: int) -> No
bitonic_merge(array, low + middle, middle, direction)
def bitonic_sort(array: List[int], low: int, length: int, direction: int) -> None:
def bitonic_sort(array: list[int], low: int, length: int, direction: int) -> None:
"""
This function first produces a bitonic sequence by recursively sorting its two
halves in opposite sorting orders, and then calls bitonic_merge to make them in the

View File

@ -27,7 +27,7 @@ If k = O(n), time complexity is O(n)
Source: https://en.wikipedia.org/wiki/Bucket_sort
"""
from typing import List
from __future__ import annotations
def bucket_sort(my_list: list) -> list:
@ -52,7 +52,7 @@ def bucket_sort(my_list: list) -> list:
return []
min_value, max_value = min(my_list), max(my_list)
bucket_count = int(max_value - min_value) + 1
buckets: List[list] = [[] for _ in range(bucket_count)]
buckets: list[list] = [[] for _ in range(bucket_count)]
for i in range(len(my_list)):
buckets[(int(my_list[i] - min_value) // bucket_count)].append(my_list[i])

View File

@ -4,10 +4,10 @@ It used the binary representation of the integers to sort
them.
https://en.wikipedia.org/wiki/Radix_sort
"""
from typing import List
from __future__ import annotations
def msd_radix_sort(list_of_ints: List[int]) -> List[int]:
def msd_radix_sort(list_of_ints: list[int]) -> list[int]:
"""
Implementation of the MSD radix sort algorithm. Only works
with positive integers
@ -36,7 +36,7 @@ def msd_radix_sort(list_of_ints: List[int]) -> List[int]:
return _msd_radix_sort(list_of_ints, most_bits)
def _msd_radix_sort(list_of_ints: List[int], bit_position: int) -> List[int]:
def _msd_radix_sort(list_of_ints: list[int], bit_position: int) -> list[int]:
"""
Sort the given list based on the bit at bit_position. Numbers with a
0 at that position will be at the start of the list, numbers with a
@ -74,7 +74,7 @@ def _msd_radix_sort(list_of_ints: List[int], bit_position: int) -> List[int]:
return res
def msd_radix_sort_inplace(list_of_ints: List[int]):
def msd_radix_sort_inplace(list_of_ints: list[int]):
"""
Inplace implementation of the MSD radix sort algorithm.
Sorts based on the binary representation of the integers.
@ -109,7 +109,7 @@ def msd_radix_sort_inplace(list_of_ints: List[int]):
def _msd_radix_sort_inplace(
list_of_ints: List[int], bit_position: int, begin_index: int, end_index: int
list_of_ints: list[int], bit_position: int, begin_index: int, end_index: int
):
"""
Sort the given list based on the bit at bit_position. Numbers with a

View File

@ -1,7 +1,8 @@
from __future__ import annotations
from bisect import bisect_left
from functools import total_ordering
from heapq import merge
from typing import List
"""
A pure Python implementation of the patience sort algorithm
@ -44,7 +45,7 @@ def patience_sort(collection: list) -> list:
>>> patience_sort([-3, -17, -48])
[-48, -17, -3]
"""
stacks: List[Stack] = []
stacks: list[Stack] = []
# sort into stacks
for element in collection:
new_stacks = Stack([element])
@ -55,7 +56,7 @@ def patience_sort(collection: list) -> list:
stacks.append(new_stacks)
# use a heap-based merge to merge stack efficiently
collection[:] = merge(*[reversed(stack) for stack in stacks])
collection[:] = merge(*(reversed(stack) for stack in stacks))
return collection

View File

@ -9,10 +9,10 @@
For manual testing run:
python pigeon_sort.py
"""
from typing import List
from __future__ import annotations
def pigeon_sort(array: List[int]) -> List[int]:
def pigeon_sort(array: list[int]) -> list[int]:
"""
Implementation of pigeon hole sort algorithm
:param array: Collection of comparable items

View File

@ -7,7 +7,7 @@ python3 -m doctest -v quick_sort.py
For manual testing run:
python3 quick_sort.py
"""
from typing import List
from __future__ import annotations
def quick_sort(collection: list) -> list:
@ -27,8 +27,8 @@ def quick_sort(collection: list) -> list:
if len(collection) < 2:
return collection
pivot = collection.pop() # Use the last element as the first pivot
greater: List[int] = [] # All elements greater than pivot
lesser: List[int] = [] # All elements less than or equal to pivot
greater: list[int] = [] # All elements greater than pivot
lesser: list[int] = [] # All elements less than or equal to pivot
for element in collection:
(greater if element > pivot else lesser).append(element)
return quick_sort(lesser) + [pivot] + quick_sort(greater)

View File

@ -9,10 +9,8 @@ python radix_sort.py
"""
from __future__ import annotations
from typing import List
def radix_sort(list_of_ints: List[int]) -> List[int]:
def radix_sort(list_of_ints: list[int]) -> list[int]:
"""
Examples:
>>> radix_sort([0, 5, 3, 2, 2])
@ -30,7 +28,7 @@ def radix_sort(list_of_ints: List[int]) -> List[int]:
max_digit = max(list_of_ints)
while placement <= max_digit:
# declare and initialize empty buckets
buckets: List[list] = [list() for _ in range(RADIX)]
buckets: list[list] = [list() for _ in range(RADIX)]
# split list_of_ints between the buckets
for i in list_of_ints:
tmp = int((i / placement) % RADIX)

View File

@ -1,11 +1,8 @@
"""
A recursive implementation of the insertion sort algorithm
"""
from __future__ import annotations
from typing import List
def rec_insertion_sort(collection: list, n: int):
"""
@ -72,6 +69,6 @@ def insert_next(collection: list, index: int):
if __name__ == "__main__":
numbers = input("Enter integers separated by spaces: ")
number_list: List[int] = [int(num) for num in numbers.split()]
number_list: list[int] = [int(num) for num in numbers.split()]
rec_insertion_sort(number_list, len(number_list))
print(number_list)

View File

@ -8,13 +8,10 @@ in their paper Pessimal Algorithms and Simplexity Analysis
Source: https://en.wikipedia.org/wiki/Slowsort
"""
from typing import Optional
from __future__ import annotations
def slowsort(
sequence: list, start: Optional[int] = None, end: Optional[int] = None
) -> None:
def slowsort(sequence: list, start: int | None = None, end: int | None = None) -> None:
"""
Sorts sequence[start..end] (both inclusive) in-place.
start defaults to 0 if not given.