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

martinoywa - Martin #292

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
17 changes: 17 additions & 0 deletions martinoywa/C1.py
Original file line number Diff line number Diff line change
@@ -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")
14 changes: 14 additions & 0 deletions martinoywa/C2.py
Original file line number Diff line number Diff line change
@@ -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()
15 changes: 15 additions & 0 deletions martinoywa/C3.py
Original file line number Diff line number Diff line change
@@ -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)}")
17 changes: 17 additions & 0 deletions martinoywa/priority_queue.py
Original file line number Diff line number Diff line change
@@ -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=" ")
32 changes: 32 additions & 0 deletions martinoywa/reverse_linked_list.py
Original file line number Diff line number Diff line change
@@ -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()