diff --git a/martinoywa/C1.py b/martinoywa/C1.py new file mode 100644 index 00000000..dc5f504b --- /dev/null +++ b/martinoywa/C1.py @@ -0,0 +1,17 @@ +def reversed(n): + # check if number is - + negative = n < 0 + + # convert to sting and reverse + n_reversed = int(str(abs(n))[::-1]) + + # add - if negative + n_reversed *= (-1 if negative else 1) + return n_reversed + + +if __name__ == "__main__": + print(f"500: {reversed(500)} == 5") + print(f"-56: {reversed(-56)} == -65") + print(f"-90: {reversed(-90)} == -9") + print(f"91: {reversed(91)} == 19") diff --git a/martinoywa/C2.py b/martinoywa/C2.py new file mode 100644 index 00000000..9a66352b --- /dev/null +++ b/martinoywa/C2.py @@ -0,0 +1,14 @@ +def fizz_buzz(): + for i in range(1, 101): + if i % 3 == 0 and i % 5 == 0: + print("FizzBuzz") + elif i % 3 == 0: + print("Fizz") + elif i % 5 == 0: + print("Buzz") + else: + print(i) + + +if __name__ == "__main__": + fizz_buzz() diff --git a/martinoywa/C3.py b/martinoywa/C3.py new file mode 100644 index 00000000..85ba1d6c --- /dev/null +++ b/martinoywa/C3.py @@ -0,0 +1,15 @@ +from math import log2 + + +def power_of_two(n): + # edge case + if n <= 0: + return False + return log2(n) == int(log2(n)) + + +if __name__ == "__main__": + print(f"-1 => {power_of_two(-1)}") + print(f"0 => {power_of_two(0)}") + print(f"8 => {power_of_two(8)}") + print(f"6 => {power_of_two(6)}") diff --git a/martinoywa/priority_queue.py b/martinoywa/priority_queue.py new file mode 100644 index 00000000..cae1ea8f --- /dev/null +++ b/martinoywa/priority_queue.py @@ -0,0 +1,17 @@ +import heapq + + +def priority_queue(tasks): + output = [] + + tasks = [(p, n) for n, p in tasks] # swap order of task name and priority + heapq.heapify(tasks) # create a min-heap + + while tasks: + output.append(heapq.heappop(tasks)[1]) + + return output + +if __name__ == "__main__": + output = priority_queue([("task1", 2), ("task2", 1), ("task3", 3)]) + print(*output, sep=" ") diff --git a/martinoywa/reverse_linked_list.py b/martinoywa/reverse_linked_list.py new file mode 100644 index 00000000..26353eab --- /dev/null +++ b/martinoywa/reverse_linked_list.py @@ -0,0 +1,32 @@ +from helpers.linked_list_helpers import * + + +def reverse_linked_list(head): + prev, after, current = None, None, head + + while current: + after = current.next + current.next = prev + prev = current + current = after + + return prev + + +if __name__ == "__main__": + # create the LL + LL = ListNode(1) + current = LL + for _ in range(2, 6): + current.next = ListNode(_) + current = current.next + + print("Before Reverse") + print_linked_list(LL) + print() + + print("After Reverse") + LL_rev = reverse_linked_list(LL) + print_linked_list(LL_rev) + print() + \ No newline at end of file