Skip to content

Commit 5b6ebf8

Browse files
cclaussgithub-actions
and
github-actions
authored
Add doctests to radix_sort() (TheAlgorithms#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>
1 parent f1ce2d6 commit 5b6ebf8

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

DIRECTORY.md

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* [All Subsequences](https://github.com/TheAlgorithms/Python/blob/master/backtracking/all_subsequences.py)
1717
* [Coloring](https://github.com/TheAlgorithms/Python/blob/master/backtracking/coloring.py)
1818
* [Hamiltonian Cycle](https://github.com/TheAlgorithms/Python/blob/master/backtracking/hamiltonian_cycle.py)
19+
* [Knight Tour](https://github.com/TheAlgorithms/Python/blob/master/backtracking/knight_tour.py)
1920
* [Minimax](https://github.com/TheAlgorithms/Python/blob/master/backtracking/minimax.py)
2021
* [N Queens](https://github.com/TheAlgorithms/Python/blob/master/backtracking/n_queens.py)
2122
* [Rat In Maze](https://github.com/TheAlgorithms/Python/blob/master/backtracking/rat_in_maze.py)
@@ -71,6 +72,8 @@
7172
## Compression
7273
* [Burrows Wheeler](https://github.com/TheAlgorithms/Python/blob/master/compression/burrows_wheeler.py)
7374
* [Huffman](https://github.com/TheAlgorithms/Python/blob/master/compression/huffman.py)
75+
* [Lempel Ziv](https://github.com/TheAlgorithms/Python/blob/master/compression/lempel_ziv.py)
76+
* [Lempel Ziv Decompress](https://github.com/TheAlgorithms/Python/blob/master/compression/lempel_ziv_decompress.py)
7477
* [Peak Signal To Noise Ratio](https://github.com/TheAlgorithms/Python/blob/master/compression/peak_signal_to_noise_ratio.py)
7578

7679
## Computer Vision
@@ -199,6 +202,7 @@
199202
* [Longest Increasing Subsequence O(Nlogn)](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/longest_increasing_subsequence_o(nlogn).py)
200203
* [Longest Sub Array](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/longest_sub_array.py)
201204
* [Matrix Chain Order](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/matrix_chain_order.py)
205+
* [Max Non Adjacent Sum](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/max_non_adjacent_sum.py)
202206
* [Max Sub Array](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/max_sub_array.py)
203207
* [Max Sum Contiguous Subsequence](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/max_sum_contiguous_subsequence.py)
204208
* [Minimum Partition](https://github.com/TheAlgorithms/Python/blob/master/dynamic_programming/minimum_partition.py)
@@ -440,6 +444,7 @@
440444
* [Least Recently Used](https://github.com/TheAlgorithms/Python/blob/master/other/least_recently_used.py)
441445
* [Linear Congruential Generator](https://github.com/TheAlgorithms/Python/blob/master/other/linear_congruential_generator.py)
442446
* [Magicdiamondpattern](https://github.com/TheAlgorithms/Python/blob/master/other/magicdiamondpattern.py)
447+
* [Markov Chain](https://github.com/TheAlgorithms/Python/blob/master/other/markov_chain.py)
443448
* [Nested Brackets](https://github.com/TheAlgorithms/Python/blob/master/other/nested_brackets.py)
444449
* [Palindrome](https://github.com/TheAlgorithms/Python/blob/master/other/palindrome.py)
445450
* [Password Generator](https://github.com/TheAlgorithms/Python/blob/master/other/password_generator.py)

other/markov_chain.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44

55

66
class MarkovChainGraphUndirectedUnweighted:
7-
'''
7+
"""
88
Undirected Unweighted Graph for running Markov Chain Algorithm
9-
'''
9+
"""
1010

1111
def __init__(self):
1212
self.connections = {}
1313

1414
def add_node(self, node: str) -> None:
1515
self.connections[node] = {}
1616

17-
def add_transition_probability(self, node1: str,
18-
node2: str,
19-
probability: float) -> None:
17+
def add_transition_probability(
18+
self, node1: str, node2: str, probability: float
19+
) -> None:
2020
if node1 not in self.connections:
2121
self.add_node(node1)
2222
if node2 not in self.connections:
@@ -36,10 +36,10 @@ def transition(self, node: str) -> str:
3636
return dest
3737

3838

39-
def get_transitions(start: str,
40-
transitions: List[Tuple[str, str, float]],
41-
steps: int) -> Dict[str, int]:
42-
'''
39+
def get_transitions(
40+
start: str, transitions: List[Tuple[str, str, float]], steps: int
41+
) -> Dict[str, int]:
42+
"""
4343
Running Markov Chain algorithm and calculating the number of times each node is
4444
visited
4545
@@ -59,7 +59,7 @@ def get_transitions(start: str,
5959
6060
>>> result['a'] > result['b'] > result['c']
6161
True
62-
'''
62+
"""
6363

6464
graph = MarkovChainGraphUndirectedUnweighted()
6565

sorts/radix_sort.py

+18-15
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
1-
def radix_sort(lst):
2-
RADIX = 10
3-
placement = 1
1+
from typing import List
42

5-
# get the maximum number
6-
max_digit = max(lst)
73

4+
def radix_sort(list_of_ints: List[int]) -> List[int]:
5+
"""
6+
radix_sort(range(15)) == sorted(range(15))
7+
True
8+
radix_sort(reversed(range(15))) == sorted(range(15))
9+
True
10+
"""
11+
RADIX = 10
12+
placement = 1
13+
max_digit = max(list_of_ints)
814
while placement < max_digit:
9-
# declare and initialize buckets
15+
# declare and initialize empty buckets
1016
buckets = [list() for _ in range(RADIX)]
11-
12-
# split lst between lists
13-
for i in lst:
17+
# split list_of_ints between the buckets
18+
for i in list_of_ints:
1419
tmp = int((i / placement) % RADIX)
1520
buckets[tmp].append(i)
16-
17-
# empty lists into lst array
21+
# put each buckets' contents into list_of_ints
1822
a = 0
1923
for b in range(RADIX):
20-
buck = buckets[b]
21-
for i in buck:
22-
lst[a] = i
24+
for i in buckets[b]:
25+
list_of_ints[a] = i
2326
a += 1
24-
2527
# move to next
2628
placement *= RADIX
29+
return list_of_ints

0 commit comments

Comments
 (0)