Skip to content
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

Python #200

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions python/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Byte-compiled / optimized / DLL files
__pycache__/

# Environments
venv/

# PyCharm
.idea/
9 changes: 0 additions & 9 deletions python/random_list.py

This file was deleted.

60 changes: 41 additions & 19 deletions python/stalin_sort.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,48 @@
from random_list import random_list_maker
from typing import Callable

def sort(l):
max_val = l[0]
return [max_val := x for x in l if x >= max_val]

def sort[T](sequence: list[T], /, *, key: Callable[[T, T], bool] | None = None, reverse: bool = False) -> list[T]:
if not sequence:
return sequence

ordered_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
sorted_list = sort(ordered_list)
print("Original: {0}\nSorted: {1}\n".format(ordered_list, sorted_list))
if key:
if reverse:
def criteria(x, y) -> bool:
return not key(x, y)
else:
def criteria(x, y) -> bool:
return key(x, y)
else:
if reverse:
def criteria(x, y) -> bool:
return x <= y
else:
def criteria(x, y) -> bool:
return x >= y

reversed_list = [9, 8, 7, 6, 5, 4, 3, 2, 1]
sorted_list = sort(reversed_list)
print("Original: {0}\nSorted: {1}\n".format(reversed_list, sorted_list))
max_val = sequence[0]
return [max_val := x for x in sequence if criteria(x, max_val)]

mixed_list = [1, 3, 2, 5, 4, 7, 6, 9, 8]
sorted_list = sort(mixed_list)
print("Original: {0}\nSorted: {1}\n".format(mixed_list, sorted_list))

test_list = [1, 5, 2, 4]
sorted_list = sort(test_list)
print("Original: {0}\nSorted: {1}\n".format(test_list, sorted_list))
if __name__ == '__main__':
from random import randint
from test_utils import test_all

random_list = random_list_maker(10)
sorted_list = sort(random_list)
print("Original: {0}\nSorted: {1}\n".format(random_list, sorted_list))

def get_random_numbers(length, min_val=0, max_val=100):
return [randint(min_val, max_val) for _ in range(length)]


def test(sequence):
print(f'Original: {sequence}')
print(f'Sorted: {sort(sequence)}')
print()


test([1, 2, 3, 4, 5, 6, 7, 8, 9]) # ordered_list
test([9, 8, 7, 6, 5, 4, 3, 2, 1]) # reversed_list
test([1, 3, 2, 5, 4, 7, 6, 9, 8]) # mixed_list
test([1, 5, 2, 4]) # test_list
test(get_random_numbers(10)) # random_list

test_all(sort)
27 changes: 14 additions & 13 deletions python/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@

# pairs of input - output
test_cases = [([1, 2, 3, 4, 5], [1, 2, 3, 4, 5]), # sorted list
([5, 4, 3, 2, 1], [5]), # reversed list
([], []), # empty list
([2], [2]), # one element
([1, 1], [1, 1]), # duplicate
([2, 2, 2], [2, 2, 2]), # triplicate
([1, 2, 5, 3, 4, 7], [1, 2, 5, 7]), # out of order
(['a', 'c', 'b'], ['a', 'c'])] # non-numbers
_test_cases = [([1, 2, 3, 4, 5], [1, 2, 3, 4, 5]), # sorted list
([5, 4, 3, 2, 1], [5]), # reversed list
([], []), # empty list
([2], [2]), # one element
([1, 1], [1, 1]), # duplicate
([2, 2, 2], [2, 2, 2]), # triplicate
([1, 2, 5, 3, 4, 7], [1, 2, 5, 7]), # out of order
(['a', 'c', 'b'], ['a', 'c'])] # non-numbers


# test sort_fun on all test cases defined above.
def test_all(sort_fun):
for xs, expected in test_cases:
print(f'testing with input {xs}, expecting output {expected}.')
result = sort_fun(xs)
print(f'output was {result}')
assert(result == expected)
for xs, expected in _test_cases:
print(f'testing with input {xs}, expecting output {expected}.')
result = sort_fun(xs)
print(f'output was {result}')
assert(result == expected)