Skip to content

Commit

Permalink
Formatted programs using black
Browse files Browse the repository at this point in the history
  • Loading branch information
WHAHA-HA committed Aug 9, 2020
1 parent 67516fd commit 8f02359
Show file tree
Hide file tree
Showing 266 changed files with 3,384 additions and 2,989 deletions.
8 changes: 4 additions & 4 deletions Solutions/01.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'''
"""
Problem:
Given a list of numbers, return whether any two sums to k. For example, given
[10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.
Bonus: Can you do this in one pass?
'''
"""

from typing import List

Expand All @@ -26,9 +26,9 @@ def check_target_sum(arr: List[int], target: int) -> bool:
print(check_target_sum([10, 15, 3, 4], 17))


'''
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
'''
"""
8 changes: 4 additions & 4 deletions Solutions/02.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'''
"""
Problem:
Given an array of integers, return a new array such that each element at index i of the
Expand All @@ -9,7 +9,7 @@
[2, 3, 6].
Follow-up: what if you can't use division?
'''
"""

from typing import List

Expand All @@ -36,9 +36,9 @@ def product_of_arr_except_ith_elem(arr: List[int]) -> int:
print(product_of_arr_except_ith_elem([3, 2, 1]))


'''
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
'''
"""
28 changes: 14 additions & 14 deletions Solutions/03.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'''
"""
Problem:
Write a program to serialize a tree into a string and deserialize a string into a tree.
'''
"""

from typing import List

Expand All @@ -21,10 +21,10 @@ def serialize_helper(self) -> str:
return f"'{self.val}','None',{self.left.serialize_helper()}"
elif self.left is not None and self.right is not None:
return (
f"'{self.val}'," +
f"{self.left.serialize_helper()}," +
f"{self.right.serialize_helper()}"
)
f"'{self.val}',"
+ f"{self.left.serialize_helper()},"
+ f"{self.right.serialize_helper()}"
)


# Function to serialize the tree
Expand All @@ -33,22 +33,22 @@ def serialize(self) -> str:


# adding serialization fctions to node and tree
setattr(Node, 'serialize_helper', serialize_helper)
setattr(BinaryTree, 'serialize', serialize)
setattr(Node, "serialize_helper", serialize_helper)
setattr(BinaryTree, "serialize", serialize)


# Function to deserialize the string
def deserialize_helper(node: Node, data: List[str]) -> Node:
# data is a queue containing the data as a prefix notation can be easily decoded
# using a queue
left = data.pop(0).strip("'")
if left != 'None':
if left != "None":
# if the left child exists, its added to the tree and deserialize_helper called
node.left = Node(left)
node.left = deserialize_helper(node.left, data)

right = data.pop(0).strip("'")
if right != 'None':
if right != "None":
# if the right child exists, its added to the tree and deserialize_helper
# called
node.right = Node(right)
Expand All @@ -60,7 +60,7 @@ def deserialize_helper(node: Node, data: List[str]) -> Node:
def deserialize(string: str) -> BinaryTree:
# the string is considered to have the same format as the binary tree serialization
# eg: data is padded with single quotes (') and comma (,) is used as a delimiter
data = string.split(',')
data = string.split(",")
tree = BinaryTree()
tree.root = Node(data.pop(0).strip("'"))
deserialize_helper(tree.root, data)
Expand All @@ -78,12 +78,12 @@ def deserialize(string: str) -> BinaryTree:

generated_tree = deserialize(
"'root','left','left.left','None','None','None','right','None','None'"
)
)

print(generated_tree.serialize())


'''
"""
SPECS:
SERIALIZE: (n = Number of Nodes)
Expand All @@ -93,4 +93,4 @@ def deserialize(string: str) -> BinaryTree:
DESERIALIZE: (n = Number of Characters in the String)
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
'''
"""
8 changes: 4 additions & 4 deletions Solutions/04.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'''
"""
Problem:
Given an array of integers, find the first missing positive integer in linear time and
Expand All @@ -8,7 +8,7 @@
For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should give 3.
You can modify the input array in-place.
'''
"""

from typing import List

Expand Down Expand Up @@ -40,9 +40,9 @@ def first_missing_positive_integer(arr: List[int]) -> int:
print(first_missing_positive_integer([-1, -2]))


'''
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(1)
'''
"""
5 changes: 3 additions & 2 deletions Solutions/05.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'''
"""
Problem:
cons(a, b) constructs a pair, and car(pair) and cdr(pair) returns the first and last
Expand All @@ -11,7 +11,7 @@ def cons(a, b):
def pair(f):
return f(a, b)
return pair
'''
"""

from typing import Callable

Expand All @@ -20,6 +20,7 @@ def pair(f):
def cons(a, b):
def pair(f):
return f(a, b)

return pair


Expand Down
18 changes: 10 additions & 8 deletions Solutions/06.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'''
"""
Problem:
An XOR linked list is a more memory efficient doubly linked list. Instead of each node
Expand All @@ -9,10 +9,10 @@
If using a language that has no pointers (such as Python), you can assume you have
access to get_pointer and dereference_pointer functions that converts between nodes
and memory addresses.
'''
"""


# Solution copied from:
# Solution copied from:
# https://github.com/r1cc4rdo/daily_coding_problem/blob/master/problems/06


Expand Down Expand Up @@ -54,15 +54,16 @@ def head(self) -> Tuple[int, int, XORLinkedListNode]:

def add(self, val: int) -> None:
current_node_index, previous_node_index, current_node = self.head()
while True:
while True:
# walk down the list until the end is found
next_node_index = current_node.next_node(previous_node_index)
if next_node_index == -1:
# the end is reached
break
previous_node_index, current_node_index = (
current_node_index, next_node_index
)
current_node_index,
next_node_index,
)
current_node = self.memory[next_node_index]
# allocation
new_node_index = len(self.memory)
Expand All @@ -73,8 +74,9 @@ def get(self, index: int) -> int:
current_index, previous_index, current_node = self.head()
for _ in range(index + 1):
previous_index, current_index = (
current_index, current_node.next_node(previous_index)
)
current_index,
current_node.next_node(previous_index),
)
current_node = self.memory[current_index]
return current_node.val

Expand Down
23 changes: 12 additions & 11 deletions Solutions/07.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'''
"""
Problem:
Given the mapping a = 1, b = 2, ... z = 26, and an encoded message, count the number of
Expand All @@ -8,7 +8,8 @@
and 'ak'.
You can assume that the messages are decodable. For example, '001' is not allowed.
'''
"""


def count_decoding(digits: str) -> int:
len_digits = len(digits)
Expand All @@ -21,26 +22,26 @@ def count_decoding(digits: str) -> int:
for i in range(2, len_digits + 1):
count[i] = 0
# if the last digit is not 0, then last digit must add to the number of words
if digits[i - 1] > '0':
if digits[i - 1] > "0":
count[i] = count[i - 1]
# if the number formed by the last 2 digits is less than 26, its a valid
# character
if digits[i - 2] == '1' or (digits[i - 2] == '2' and digits[i - 1] < '7'):
if digits[i - 2] == "1" or (digits[i - 2] == "2" and digits[i - 1] < "7"):
count[i] += count[i - 2]
return count[len_digits]


if __name__ == "__main__":
print(count_decoding('81'))
print(count_decoding('11'))
print(count_decoding('111'))
print(count_decoding('1311'))
print(count_decoding('1111'))
print(count_decoding("81"))
print(count_decoding("11"))
print(count_decoding("111"))
print(count_decoding("1311"))
print(count_decoding("1111"))


'''
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
'''
"""
8 changes: 4 additions & 4 deletions Solutions/08.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'''
"""
Problem:
A unival tree (which stands for "universal value") is a tree where all nodes under it
Expand All @@ -15,7 +15,7 @@
1 0
/ \
1 1
'''
"""

from typing import Tuple

Expand Down Expand Up @@ -80,9 +80,9 @@ def num_universal(tree: BinaryTree) -> int:
print(num_universal(tree))


'''
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n) [call stack included]
'''
"""
10 changes: 5 additions & 5 deletions Solutions/09.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'''
"""
Problem:
Given a list of integers, write a function that returns the largest sum of non-adjacent
numbers. Numbers can be 0 or negative.
For example, [2, 4, 6, 8] should return 12, since we pick 4 and 8. [5, 1, 1, 5] should
return 10, since we pick 5 and 5.
'''
"""

from typing import List

Expand All @@ -18,7 +18,7 @@ def max_nonadjacent_sum(arr: List[int]) -> int:
# updating including and excluding
temp = including
including = max(excluding + elem, elem)
excluding = max(temp, including-elem)
excluding = max(temp, including - elem)
return max(including, excluding)


Expand All @@ -29,9 +29,9 @@ def max_nonadjacent_sum(arr: List[int]) -> int:
print(max_nonadjacent_sum([5, 5, 10, 100, 10, 5]))


'''
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(1)
'''
"""
4 changes: 2 additions & 2 deletions Solutions/10.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'''
"""
Problem:
Implement a job scheduler which takes in a function f and an integer n, and calls f
after n milliseconds.
'''
"""

from time import sleep
from typing import Callable
Expand Down
Loading

0 comments on commit 8f02359

Please sign in to comment.