From 8f7c19a3b8cbd9844c068ed3dad667f9848c86de Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 23 Jun 2020 14:03:41 +0200 Subject: [PATCH 1/4] Add doctests to radix_sort() --- sorts/radix_sort.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/sorts/radix_sort.py b/sorts/radix_sort.py index 2990247a0ac0..7e36cab4b42c 100644 --- a/sorts/radix_sort.py +++ b/sorts/radix_sort.py @@ -1,26 +1,36 @@ -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 placement = 1 # get the maximum number - max_digit = max(lst) + max_digit = max(list_of_ints) while placement < max_digit: # declare and initialize buckets buckets = [list() for _ in range(RADIX)] - # split lst between lists - for i in lst: + # split list_of_ints between lists + for i in list_of_ints: tmp = int((i / placement) % RADIX) buckets[tmp].append(i) - # empty lists into lst array + # empty lists into list_of_ints a = 0 for b in range(RADIX): buck = buckets[b] for i in buck: - lst[a] = i + list_of_ints[a] = i a += 1 # move to next placement *= RADIX + return list_of_ints From a0a83225f793515a7fcbe680f94fca3ec5547584 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 23 Jun 2020 12:04:17 +0000 Subject: [PATCH 2/4] fixup! Format Python code with psf/black push --- other/markov_chain.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/other/markov_chain.py b/other/markov_chain.py index d5893d849471..9b13fa515709 100644 --- a/other/markov_chain.py +++ b/other/markov_chain.py @@ -4,9 +4,9 @@ class MarkovChainGraphUndirectedUnweighted: - ''' + """ Undirected Unweighted Graph for running Markov Chain Algorithm - ''' + """ def __init__(self): self.connections = {} @@ -14,9 +14,9 @@ def __init__(self): 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: @@ -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 @@ -59,7 +59,7 @@ def get_transitions(start: str, >>> result['a'] > result['b'] > result['c'] True - ''' + """ graph = MarkovChainGraphUndirectedUnweighted() From 765d69de5d1d3f89fa1bde16226999e9d3259985 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 23 Jun 2020 15:06:52 +0200 Subject: [PATCH 3/4] Update radix_sort.py --- sorts/radix_sort.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/sorts/radix_sort.py b/sorts/radix_sort.py index 7e36cab4b42c..c379c679787f 100644 --- a/sorts/radix_sort.py +++ b/sorts/radix_sort.py @@ -10,27 +10,20 @@ def radix_sort(list_of_ints: List[int]) -> List[int]: """ RADIX = 10 placement = 1 - - # get the maximum number 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 list_of_ints between lists + # 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 list_of_ints + # put each buckets' contents into list_of_ints a = 0 for b in range(RADIX): - buck = buckets[b] - for i in buck: + for i in buckets[b]: list_of_ints[a] = i a += 1 - # move to next placement *= RADIX return list_of_ints From 5a7fba7013738a42aa4bddd98e486dcdff6c3af7 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 23 Jun 2020 13:07:24 +0000 Subject: [PATCH 4/4] updating DIRECTORY.md --- DIRECTORY.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 4ffd20da58de..984744ad7800 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) @@ -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 @@ -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) @@ -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)