Skip to content

Add doctests to radix_sort() #2148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* [All Subsequences](https://github.com/TheAlgorithms/Python/blob/master/backtracking/all_subsequences.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)
* [Knight Tour](https://github.com/TheAlgorithms/Python/blob/master/backtracking/knight_tour.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)
* [Rat In Maze](https://github.com/TheAlgorithms/Python/blob/master/backtracking/rat_in_maze.py)
Expand Down Expand Up @@ -71,6 +72,8 @@
## Compression
* [Burrows Wheeler](https://github.com/TheAlgorithms/Python/blob/master/compression/burrows_wheeler.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)

## Computer Vision
Expand Down Expand Up @@ -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 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)
* [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 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)
Expand Down Expand Up @@ -440,6 +444,7 @@
* [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)
* [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)
* [Palindrome](https://github.com/TheAlgorithms/Python/blob/master/other/palindrome.py)
* [Password Generator](https://github.com/TheAlgorithms/Python/blob/master/other/password_generator.py)
Expand Down
20 changes: 10 additions & 10 deletions other/markov_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@


class MarkovChainGraphUndirectedUnweighted:
'''
"""
Undirected Unweighted Graph for running Markov Chain Algorithm
'''
"""

def __init__(self):
self.connections = {}

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

def add_transition_probability(self, node1: str,
node2: str,
probability: float) -> None:
def add_transition_probability(
self, node1: str, node2: str, probability: float
) -> None:
if node1 not in self.connections:
self.add_node(node1)
if node2 not in self.connections:
Expand All @@ -36,10 +36,10 @@ def transition(self, node: str) -> str:
return dest


def get_transitions(start: str,
transitions: List[Tuple[str, str, float]],
steps: int) -> Dict[str, int]:
'''
def get_transitions(
start: str, transitions: List[Tuple[str, str, float]], steps: int
) -> Dict[str, int]:
"""
Running Markov Chain algorithm and calculating the number of times each node is
visited

Expand All @@ -59,7 +59,7 @@ def get_transitions(start: str,

>>> result['a'] > result['b'] > result['c']
True
'''
"""

graph = MarkovChainGraphUndirectedUnweighted()

Expand Down
33 changes: 18 additions & 15 deletions sorts/radix_sort.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
def radix_sort(lst):
RADIX = 10
placement = 1
from typing import List

# get the maximum number
max_digit = max(lst)

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
placement = 1
max_digit = max(list_of_ints)
while placement < max_digit:
# declare and initialize buckets
# declare and initialize empty buckets
buckets = [list() for _ in range(RADIX)]

# split lst between lists
for i in lst:
# split list_of_ints between the buckets
for i in list_of_ints:
tmp = int((i / placement) % RADIX)
buckets[tmp].append(i)

# empty lists into lst array
# put each buckets' contents into list_of_ints
a = 0
for b in range(RADIX):
buck = buckets[b]
for i in buck:
lst[a] = i
for i in buckets[b]:
list_of_ints[a] = i
a += 1

# move to next
placement *= RADIX
return list_of_ints