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 2 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
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
22 changes: 16 additions & 6 deletions sorts/radix_sort.py
Original file line number Diff line number Diff line change
@@ -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