Created
May 3, 2023 11:09
-
-
Save aalexren/d887cff4046b40c79827d06f5983098f to your computer and use it in GitHub Desktop.
Simple tasks on python algorithms to warm up. Some classics.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import Counter | |
from queue import Queue | |
def custom_sort(nums: list[int]) -> list[int]: | |
"""Task 1 | |
Sort the odd numbers in ascending order, | |
and leave the even ones in their places. | |
""" | |
nums = nums[::] | |
to_sort = tuple(filter(lambda x: x[1] % 2, enumerate(nums))) | |
indexes = sorted(map(lambda x: x[0], to_sort)) | |
to_sort = sorted(map(lambda x: x[1], to_sort)) | |
for new_i, i in enumerate(indexes): | |
nums[i] = to_sort[new_i] | |
return nums | |
def find_unique(nums: list[int]) -> list[int]: | |
"""Task 2 | |
Find non-repeating numbers. | |
""" | |
freq = Counter(nums) | |
return [k for k, v in freq.items() if v == 1] | |
def nested_dict(s: str) -> dict: | |
"""Task 3 | |
Make a nested dictionary object from a string. | |
""" | |
levels = s.split(".") | |
result = {} | |
level_ref = result | |
for level in levels: | |
level_ref[level] = {} | |
level_ref = level_ref[level] | |
return result | |
def add_level(data: dict) -> dict: | |
"""Task 4 | |
For each nested dictionary, you need to add a key-the level property, | |
which is equal to a number (the nesting number). | |
If the value of the property is not a dictionary, then do not add anything. | |
""" | |
queue_ = Queue() | |
queue_.put(data) | |
while not queue_.empty(): | |
q = queue_.get() | |
for key in q: | |
if not isinstance(q[key], dict): | |
continue | |
q[key]["level"] = q.get("level", 0) + 1 | |
queue_.put(q[key]) | |
return data | |
def is_palindrom(s: str) -> bool: | |
"""Task 5 | |
Pythonic way, but not the most optimal one. | |
""" | |
return s[::-1] == s | |
def fizz_buzz(num: int): | |
"""Task 6 | |
Classical Fizz-Buzz problem. | |
""" | |
for n in range(1, num + 1): | |
if n % 3 == 0 and n % 5 == 0: | |
print("fizzbuzz") | |
elif n % 3 == 0: | |
print("fizz") | |
elif n % 5 == 0: | |
print("buzz") | |
else: | |
print(n) | |
def is_anagram(s1: str, s2: str) -> bool: | |
"""Task 7 | |
Check if strings are anagrams. | |
""" | |
return sorted(s1.lower()) == sorted(s2.lower()) | |
def test_custom_sort(): | |
nums = [1, 9, 4, 2, 3, 6, 7, 1, 5] | |
assert custom_sort(nums) == [1, 1, 4, 2, 3, 6, 5, 7, 9] | |
def test_find_unique(): | |
nums = [1, 2, 3, 4, 1, 2] | |
assert find_unique(nums) == [3, 4] | |
def test_nested_dict(): | |
s = "one.two.three.four.five" | |
d = {"one": {"two": {"three": {"four": {"five": {}}}}}} | |
assert nested_dict(s) == d | |
def test_add_level(): | |
inp = {"a": {"d": {"h": 4}, "e": 2}, "b": 1, "c": {"f": {"g": 3, "k": {}}}} | |
out = { | |
"a": {"level": 1, "d": {"level": 2, "h": 4}, "e": 2}, | |
"b": 1, | |
"c": {"level": 1, "f": {"level": 2, "g": 3, "k": {"level": 3}}}, | |
} | |
assert add_level(inp) == out | |
def test_is_palindrom(): | |
s = "казак" | |
assert is_palindrom(s) is True | |
s = "строка" | |
assert is_palindrom(s) is False | |
s = "шалаш" | |
assert is_palindrom(s) is True | |
def test_fizz_buzz(capsys): | |
fizz_buzz(15) | |
out, _ = capsys.readouterr() | |
res = ( | |
"1\n" | |
"2\n" | |
"fizz\n" | |
"4\n" | |
"buzz\n" | |
"fizz\n" | |
"7\n" | |
"8\n" | |
"fizz\n" | |
"buzz\n" | |
"11\n" | |
"fizz\n" | |
"13\n" | |
"14\n" | |
"fizzbuzz\n" | |
) | |
assert out == res | |
def test_is_anagram(): | |
assert is_anagram("finder", "Friend") is True | |
assert is_anagram("hello", "bye") is False | |
assert is_anagram("below", "elbow") is True | |
assert is_anagram("dessert", "stressed") is False | |
assert is_anagram("sadder", "dreads") is True | |
assert is_anagram("каратист", "артист") is False | |
if __name__ == "__main__": | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment