Skip to content

Commit 2ce8e73

Browse files
committed
python-100 answer
1 parent 707a407 commit 2ce8e73

28 files changed

+672
-0
lines changed

Diff for: 001-unique_chars.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class UniqueChars(object):
2+
3+
def has_unique_chars(self, string):
4+
if string is None:
5+
return False
6+
return len(set(string)) == len(string)

Diff for: 002-permutation.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Permutations(object):
2+
3+
def is_permutation(self, str1, str2):
4+
if str1 is None or str2 is None:
5+
return False
6+
return sorted(str1) == sorted(str2)

Diff for: 003-rotation.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Rotation(object):
2+
3+
def is_substring(self, s1, s2):
4+
return s1 in s2
5+
6+
def is_rotation(self, s1, s2):
7+
if s1 is None or s2 is None:
8+
return False
9+
if len(s1) != len(s2):
10+
return False
11+
return self.is_substring(s1, s2 + s2)

Diff for: 004-compress_str.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class CompressString(object):
2+
3+
def compress(self, string):
4+
if string is None or not string:
5+
return string
6+
result = ''
7+
prev_char = string[0]
8+
count = 0
9+
for char in string:
10+
if char == prev_char:
11+
count += 1
12+
else:
13+
result += self._calc_partial_result(prev_char, count)
14+
prev_char = char
15+
count = 1
16+
result += self._calc_partial_result(prev_char, count)
17+
return result if len(result) < len(string) else string
18+
19+
def _calc_partial_result(self, prev_char, count):
20+
return prev_char + (str(count) if count > 1 else '')

Diff for: 005-reverse_chars.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class ReverseString(object):
2+
3+
def reverse(self, chars):
4+
if chars:
5+
size = len(chars)
6+
for i in range(size // 2):
7+
chars[i], chars[size - 1 - i] = \
8+
chars[size - 1 - i], chars[i]
9+
return chars

Diff for: 006-diff.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution(object):
2+
3+
def find_diff(self, str1, str2):
4+
if str1 is None or str2 is None:
5+
raise TypeError('str1 or str2 cannot be None')
6+
result = 0
7+
for char in str1:
8+
result ^= ord(char)
9+
for char in str2:
10+
result ^= ord(char)
11+
return chr(result)

Diff for: 007-sum.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution(object):
2+
3+
def two_sum(self, nums, val):
4+
if nums is None or val is None:
5+
raise TypeError('nums or target cannot be None')
6+
if not nums:
7+
raise ValueError('nums cannot be empty')
8+
cache = {}
9+
for index, num in enumerate(nums):
10+
cache_val = val - num
11+
if num in cache:
12+
return [cache[num], index]
13+
else:
14+
cache[cache_val] = index
15+
return None

Diff for: 008-fizzbuzz.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
3+
def fizz_buzz(self, num):
4+
if num is None:
5+
raise TypeError('num cannot be None')
6+
if num < 1:
7+
raise ValueError('num cannot be less than one')
8+
results = []
9+
for i in range(1, num + 1):
10+
if i % 3 == 0 and i % 5 == 0:
11+
results.append('FizzBuzz')
12+
elif i % 3 == 0:
13+
results.append('Fizz')
14+
elif i % 5 == 0:
15+
results.append('Buzz')
16+
else:
17+
results.append(str(i))
18+
return results

Diff for: 009-linkedlist.py

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
class Node(object):
2+
3+
def __init__(self, data, next=None):
4+
self.next = next
5+
self.data = data
6+
7+
def __str__(self):
8+
return self.data
9+
10+
11+
class LinkedList(object):
12+
13+
def __init__(self, head=None):
14+
self.head = head
15+
16+
def __len__(self):
17+
curr = self.head
18+
counter = 0
19+
while curr is not None:
20+
counter += 1
21+
curr = curr.next
22+
return counter
23+
24+
def insert_to_front(self, data):
25+
if data is None:
26+
return None
27+
node = Node(data, self.head)
28+
self.head = node
29+
return node
30+
31+
def append(self, data):
32+
if data is None:
33+
return None
34+
node = Node(data)
35+
if self.head is None:
36+
self.head = node
37+
return node
38+
curr_node = self.head
39+
while curr_node.next is not None:
40+
curr_node = curr_node.next
41+
curr_node.next = node
42+
return node
43+
44+
def find(self, data):
45+
if data is None:
46+
return None
47+
curr_node = self.head
48+
while curr_node is not None:
49+
if curr_node.data == data:
50+
return curr_node
51+
curr_node = curr_node.next
52+
return None
53+
54+
def delete(self, data):
55+
if data is None:
56+
return
57+
if self.head is None:
58+
return
59+
if self.head.data == data:
60+
self.head = self.head.next
61+
return
62+
prev_node = self.head
63+
curr_node = self.head.next
64+
while curr_node is not None:
65+
if curr_node.data == data:
66+
prev_node.next = curr_node.next
67+
return
68+
prev_node = curr_node
69+
curr_node = curr_node.next
70+
71+
def print_list(self):
72+
curr_node = self.head
73+
while curr_node is not None:
74+
print(curr_node.data)
75+
curr_node = curr_node.next
76+
77+
def get_all_data(self):
78+
data = []
79+
curr_node = self.head
80+
while curr_node is not None:
81+
data.append(curr_node.data)
82+
curr_node = curr_node.next
83+
return data

Diff for: 010-removedupes.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from linked_list import LinkedList
2+
3+
4+
class MyLinkedList(LinkedList):
5+
6+
def remove_dupes(self):
7+
if self.head is None:
8+
return
9+
node = self.head
10+
seen_data = set()
11+
while node is not None:
12+
if node.data not in seen_data:
13+
seen_data.add(node.data)
14+
prev = node
15+
node = node.next
16+
else:
17+
prev.next = node.next
18+
node = node.next

Diff for: 011-kth_to_last.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from linked_list import LinkedList
2+
3+
4+
class MyLinkedList(LinkedList):
5+
6+
def kth_to_last_elem(self, k):
7+
if self.head is None:
8+
return None
9+
fast = self.head
10+
slow = self.head
11+
12+
# Give fast a headstart, incrementing it
13+
# once for k = 1, twice for k = 2, etc
14+
for _ in range(k):
15+
fast = fast.next
16+
# If k >= num elements, return None
17+
if fast is None:
18+
return None
19+
20+
# Increment both pointers until fast reaches the end
21+
while fast.next is not None:
22+
fast = fast.next
23+
slow = slow.next
24+
return slow.data

Diff for: 012-del_node.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from linked_list import LinkedList
2+
3+
4+
class MyLinkedList(LinkedList):
5+
6+
def delete_node(self, node):
7+
if node is None:
8+
return
9+
if node.next is None:
10+
node.data = None
11+
else:
12+
node.data = node.next.data
13+
node.next = node.next.next

Diff for: 013-partition_data.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from linked_list import LinkedList
2+
3+
4+
class MyLinkedList(LinkedList):
5+
6+
def partition(self, data):
7+
if self.head is None:
8+
return
9+
left = MyLinkedList(None)
10+
right = MyLinkedList(None)
11+
curr = self.head
12+
13+
# Build the left and right lists
14+
while curr is not None:
15+
if curr.data < data:
16+
left.append(curr.data)
17+
elif curr.data == data:
18+
right.insert_to_front(curr.data)
19+
else:
20+
right.append(curr.data)
21+
curr = curr.next
22+
curr_left = left.head
23+
if curr_left is None:
24+
return right
25+
else:
26+
# Merge the two lists
27+
while curr_left.next is not None:
28+
curr_left = curr_left.next
29+
curr_left.next = right.head
30+
return left

Diff for: 014-reverse.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from linked_list import LinkedList,Node
2+
3+
4+
class MyLinkedList(LinkedList):
5+
6+
def add_reverse(self, first_list, second_list):
7+
# See constraints
8+
if first_list is None or second_list is None:
9+
return None
10+
head = self._add_reverse(first_list.head, second_list.head, 0)
11+
return MyLinkedList(head)
12+
13+
def _add_reverse(self, first_node, second_node, carry):
14+
# Base case
15+
if first_node is None and second_node is None and not carry:
16+
return None
17+
18+
# Recursive case
19+
value = carry
20+
value += first_node.data if first_node is not None else 0
21+
value += second_node.data if second_node is not None else 0
22+
carry = 1 if value >= 10 else 0
23+
value %= 10
24+
node = Node(value)
25+
node.next = self._add_reverse(
26+
first_node.next if first_node is not None else None,
27+
second_node.next if first_node is not None else None,
28+
carry)
29+
return node

Diff for: 015-loop_start.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from linked_list import LinkedList
2+
3+
4+
class MyLinkedList(LinkedList):
5+
6+
def find_loop_start(self):
7+
if self.head is None or self.head.next is None:
8+
return None
9+
slow = self.head
10+
fast = self.head
11+
while fast.next is not None:
12+
slow = slow.next
13+
fast = fast.next.next
14+
if fast is None:
15+
return None
16+
if slow == fast:
17+
break
18+
slow = self.head
19+
while slow != fast:
20+
slow = slow.next
21+
fast = fast.next
22+
if fast is None:
23+
return None
24+
return slow

Diff for: 016-palindrome.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from linked_list import LinkedList
2+
3+
4+
class MyLinkedList(LinkedList):
5+
6+
def is_palindrome(self):
7+
if self.head is None or self.head.next is None:
8+
return False
9+
curr = self.head
10+
reversed_list = MyLinkedList()
11+
length = 0
12+
13+
# Reverse the linked list
14+
while curr is not None:
15+
reversed_list.insert_to_front(curr.data)
16+
length += 1
17+
curr = curr.next
18+
19+
# Compare the reversed list with the original list
20+
# Only need to compare the first half
21+
iterations = length // 2
22+
curr = self.head
23+
curr_reversed = reversed_list.head
24+
for _ in range(iterations):
25+
if curr.data != curr_reversed.data:
26+
return False
27+
curr = curr.next
28+
curr_reversed = curr_reversed.next
29+
return True

0 commit comments

Comments
 (0)