Add doctests to radix_sort() (#2148)

* Add doctests to radix_sort()

* fixup! Format Python code with psf/black push

* Update radix_sort.py

* updating DIRECTORY.md

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
Christian Clauss 2020-06-23 15:37:24 +02:00 committed by GitHub
parent f1ce2d6e80
commit 5b6ebf8f12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 25 deletions

View File

@ -16,6 +16,7 @@
* [All Subsequences](https://github.com/TheAlgorithms/Python/blob/master/backtracking/all_subsequences.py) * [All Subsequences](https://github.com/TheAlgorithms/Python/blob/master/backtracking/all_subsequences.py)
* [Coloring](https://github.com/TheAlgorithms/Python/blob/master/backtracking/coloring.py) * [Coloring](https://github.com/TheAlgorithms/Python/blob/master/backtracking/coloring.py)
* [Hamiltonian Cycle](https://github.com/TheAlgorithms/Python/blob/master/backtracking/hamiltonian_cycle.py) * [Hamiltonian Cycle](https://github.com/TheAlgorithms/Python/blob/master/backtracking/hamiltonian_cycle.py)
* [Knight Tour](https://github.com/TheAlgorithms/Python/blob/master/backtracking/knight_tour.py)
* [Minimax](https://github.com/TheAlgorithms/Python/blob/master/backtracking/minimax.py) * [Minimax](https://github.com/TheAlgorithms/Python/blob/master/backtracking/minimax.py)
* [N Queens](https://github.com/TheAlgorithms/Python/blob/master/backtracking/n_queens.py) * [N Queens](https://github.com/TheAlgorithms/Python/blob/master/backtracking/n_queens.py)
* [Rat In Maze](https://github.com/TheAlgorithms/Python/blob/master/backtracking/rat_in_maze.py) * [Rat In Maze](https://github.com/TheAlgorithms/Python/blob/master/backtracking/rat_in_maze.py)
@ -71,6 +72,8 @@
## Compression ## Compression
* [Burrows Wheeler](https://github.com/TheAlgorithms/Python/blob/master/compression/burrows_wheeler.py) * [Burrows Wheeler](https://github.com/TheAlgorithms/Python/blob/master/compression/burrows_wheeler.py)
* [Huffman](https://github.com/TheAlgorithms/Python/blob/master/compression/huffman.py) * [Huffman](https://github.com/TheAlgorithms/Python/blob/master/compression/huffman.py)
* [Lempel Ziv](https://github.com/TheAlgorithms/Python/blob/master/compression/lempel_ziv.py)
* [Lempel Ziv Decompress](https://github.com/TheAlgorithms/Python/blob/master/compression/lempel_ziv_decompress.py)
* [Peak Signal To Noise Ratio](https://github.com/TheAlgorithms/Python/blob/master/compression/peak_signal_to_noise_ratio.py) * [Peak Signal To Noise Ratio](https://github.com/TheAlgorithms/Python/blob/master/compression/peak_signal_to_noise_ratio.py)
## Computer Vision ## Computer Vision
@ -199,6 +202,7 @@
* [Longest Increasing Subsequence O(Nlogn)](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/longest_increasing_subsequence_o(nlogn).py) * [Longest Increasing Subsequence O(Nlogn)](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/longest_increasing_subsequence_o(nlogn).py)
* [Longest Sub Array](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/longest_sub_array.py) * [Longest Sub Array](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/longest_sub_array.py)
* [Matrix Chain Order](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/matrix_chain_order.py) * [Matrix Chain Order](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/matrix_chain_order.py)
* [Max Non Adjacent Sum](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/max_non_adjacent_sum.py)
* [Max Sub Array](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/max_sub_array.py) * [Max Sub Array](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/max_sub_array.py)
* [Max Sum Contiguous Subsequence](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/max_sum_contiguous_subsequence.py) * [Max Sum Contiguous Subsequence](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/max_sum_contiguous_subsequence.py)
* [Minimum Partition](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/minimum_partition.py) * [Minimum Partition](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/minimum_partition.py)
@ -440,6 +444,7 @@
* [Least Recently Used](https://github.com/TheAlgorithms/Python/blob/master/other/least_recently_used.py) * [Least Recently Used](https://github.com/TheAlgorithms/Python/blob/master/other/least_recently_used.py)
* [Linear Congruential Generator](https://github.com/TheAlgorithms/Python/blob/master/other/linear_congruential_generator.py) * [Linear Congruential Generator](https://github.com/TheAlgorithms/Python/blob/master/other/linear_congruential_generator.py)
* [Magicdiamondpattern](https://github.com/TheAlgorithms/Python/blob/master/other/magicdiamondpattern.py) * [Magicdiamondpattern](https://github.com/TheAlgorithms/Python/blob/master/other/magicdiamondpattern.py)
* [Markov Chain](https://github.com/TheAlgorithms/Python/blob/master/other/markov_chain.py)
* [Nested Brackets](https://github.com/TheAlgorithms/Python/blob/master/other/nested_brackets.py) * [Nested Brackets](https://github.com/TheAlgorithms/Python/blob/master/other/nested_brackets.py)
* [Palindrome](https://github.com/TheAlgorithms/Python/blob/master/other/palindrome.py) * [Palindrome](https://github.com/TheAlgorithms/Python/blob/master/other/palindrome.py)
* [Password Generator](https://github.com/TheAlgorithms/Python/blob/master/other/password_generator.py) * [Password Generator](https://github.com/TheAlgorithms/Python/blob/master/other/password_generator.py)

View File

@ -4,9 +4,9 @@ from typing import Dict, List, Tuple
class MarkovChainGraphUndirectedUnweighted: class MarkovChainGraphUndirectedUnweighted:
''' """
Undirected Unweighted Graph for running Markov Chain Algorithm Undirected Unweighted Graph for running Markov Chain Algorithm
''' """
def __init__(self): def __init__(self):
self.connections = {} self.connections = {}
@ -14,9 +14,9 @@ class MarkovChainGraphUndirectedUnweighted:
def add_node(self, node: str) -> None: def add_node(self, node: str) -> None:
self.connections[node] = {} self.connections[node] = {}
def add_transition_probability(self, node1: str, def add_transition_probability(
node2: str, self, node1: str, node2: str, probability: float
probability: float) -> None: ) -> None:
if node1 not in self.connections: if node1 not in self.connections:
self.add_node(node1) self.add_node(node1)
if node2 not in self.connections: if node2 not in self.connections:
@ -36,10 +36,10 @@ class MarkovChainGraphUndirectedUnweighted:
return dest return dest
def get_transitions(start: str, def get_transitions(
transitions: List[Tuple[str, str, float]], start: str, transitions: List[Tuple[str, str, float]], steps: int
steps: int) -> Dict[str, int]: ) -> Dict[str, int]:
''' """
Running Markov Chain algorithm and calculating the number of times each node is Running Markov Chain algorithm and calculating the number of times each node is
visited visited
@ -59,7 +59,7 @@ def get_transitions(start: str,
>>> result['a'] > result['b'] > result['c'] >>> result['a'] > result['b'] > result['c']
True True
''' """
graph = MarkovChainGraphUndirectedUnweighted() graph = MarkovChainGraphUndirectedUnweighted()

View File

@ -1,26 +1,29 @@
def radix_sort(lst): from typing import List
def radix_sort(list_of_ints: List[int]) -> List[int]:
"""
radix_sort(range(15)) == sorted(range(15))
True
radix_sort(reversed(range(15))) == sorted(range(15))
True
"""
RADIX = 10 RADIX = 10
placement = 1 placement = 1
max_digit = max(list_of_ints)
# get the maximum number
max_digit = max(lst)
while placement < max_digit: while placement < max_digit:
# declare and initialize buckets # declare and initialize empty buckets
buckets = [list() for _ in range(RADIX)] buckets = [list() for _ in range(RADIX)]
# split list_of_ints between the buckets
# split lst between lists for i in list_of_ints:
for i in lst:
tmp = int((i / placement) % RADIX) tmp = int((i / placement) % RADIX)
buckets[tmp].append(i) buckets[tmp].append(i)
# put each buckets' contents into list_of_ints
# empty lists into lst array
a = 0 a = 0
for b in range(RADIX): for b in range(RADIX):
buck = buckets[b] for i in buckets[b]:
for i in buck: list_of_ints[a] = i
lst[a] = i
a += 1 a += 1
# move to next # move to next
placement *= RADIX placement *= RADIX
return list_of_ints