Skip to content

Commit 2b0daf4

Browse files
committed
Question Set 3 (10Q)
1 parent 787a8ce commit 2b0daf4

10 files changed

Lines changed: 239 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
Write a function named first_non_repeating_char
3+
that returns the first character that does not
4+
repeat in a string. Return None if none exists.
5+
"""
6+
7+
def first_non_repeating_char(text):
8+
ch_dict = {}
9+
10+
for ch in text:
11+
if ch in ch_dict:
12+
ch_dict[ch] += 1
13+
else:
14+
ch_dict[ch] = 1
15+
16+
for ch in text:
17+
if ch_dict[ch] == 1:
18+
return ch
19+
20+
return None
21+
22+
23+
print(first_non_repeating_char("swiss")) # 'w'
24+
print(first_non_repeating_char("aabbcc")) # None
25+
print(first_non_repeating_char("")) # None
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Write a function named is_balanced_parenthese
3+
that checks if a string of parentheses is balanced.
4+
5+
Rules
6+
- Every ( must have a matching )
7+
- Closing brackets must come after opening ones
8+
9+
Stack style thinking+
10+
"""
11+
12+
def is_balanced_parentheses(text):
13+
count = 0
14+
15+
for ch in text:
16+
if ch == "(":
17+
count += 1
18+
else:
19+
count -= 1
20+
21+
if count < 0:
22+
return False
23+
24+
return count == 0
25+
26+
27+
print(is_balanced_parentheses("(()())")) # True
28+
print(is_balanced_parentheses("(()")) # False
29+
print(is_balanced_parentheses(")(")) # False
30+
print(is_balanced_parentheses("")) # True
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
Rotate List to the Right
3+
4+
Assumptions
5+
- nums is a list of integers
6+
- k is a non-negative integer
7+
- If k is greater than length of list → handle it properly
8+
- If list is empty → return empty list
9+
- Do NOT use any external libraries
10+
11+
ex-
12+
nums = [1, 2, 3, 4, 5], k = 2 -> [4, 5, 1, 2, 3]
13+
nums = [10, 20, 30], k = 4 -> [30, 10, 20]
14+
15+
"""
16+
17+
def rotate_right(nums, k):
18+
if not nums:
19+
return []
20+
21+
k = k % len(nums)
22+
return nums[-k:] + nums[:-k]
23+
24+
print(rotate_right([1, 2, 3, 4, 5], 2))
25+
print(rotate_right([10, 20, 30], 4))
26+
print(rotate_right([], 3))
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Rotate Left
3+
4+
Constraints:
5+
- Do NOT use slicing (nums[a:b])
6+
- Do NOT create a second full list
7+
- Try to modify using loops
8+
"""
9+
10+
def rotate_left(nums, k):
11+
12+
for i in range(len(nums)):
13+
if i < k:
14+
nums.append(nums[i])
15+
for i in range(k-2):
16+
nums.pop(i)
17+
18+
return nums
19+
20+
def rotate_left(nums, k):
21+
n = len(nums)
22+
if n == 0:
23+
return nums
24+
25+
k = k % n
26+
27+
# Reverse first k
28+
reverse(nums, 0, k - 1)
29+
30+
# Reverse rest
31+
reverse(nums, k, n - 1)
32+
33+
# Reverse whole list
34+
reverse(nums, 0, n - 1)
35+
36+
return nums
37+
38+
def reverse(nums, start, end):
39+
while start < end:
40+
nums[start], nums[end] = nums[end], nums[start]
41+
start += 1
42+
end -= 1
43+
44+
print(rotate_left([1,2,3,4,5], 2)) # [3,4,5,1,2]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
You are given a function that is
3+
supposed to count the frequency of
4+
each element in a list.
5+
"""
6+
7+
def count_frequency(nums):
8+
freq = {}
9+
10+
for i in range(len(nums)):
11+
if nums[i] in freq:
12+
freq[nums[i]] += 1
13+
else:
14+
freq[nums[i]] = 1
15+
16+
return freq
17+
18+
print(count_frequency([1, 2, 2, 3, 1, 1]))
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
This function is supposed to
3+
find the maximum number in a list.
4+
"""
5+
6+
def find_max(nums):
7+
max_num = nums[0]
8+
9+
for num in nums:
10+
if num > max_num:
11+
max_num = num
12+
13+
return max_num
14+
15+
print(find_max([-2, -5, -12, -8]))
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
This function is supposed to reverse a string.
3+
Don't used the slicing [::-1]
4+
"""
5+
6+
def reverse_string(s):
7+
8+
result = ""
9+
10+
for i in range(len(s)-1, -1, -1):
11+
12+
result = result + s[i]
13+
14+
return result
15+
16+
print(reverse_string("hello"))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
This function is supposed to check if a string is a palindrome
3+
(A palindrome reads the same forward and backward)
4+
"""
5+
6+
def is_palindrome(s):
7+
for i in range(len(s)//2):
8+
if s[i] != s[len(s)-1-i]:
9+
return False
10+
return True
11+
12+
print(is_palindrome("madam"))
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Write a function to remove duplicates
3+
from a list WITHOUT using set()
4+
Input: [1, 2, 2, 3, 1, 4]
5+
Output: [1, 2, 3, 4]
6+
"""
7+
8+
def remove_duplicates(list):
9+
new_list = []
10+
11+
for i in list:
12+
if i not in new_list:
13+
new_list.append(i)
14+
return new_list
15+
16+
print(remove_duplicates([1, 2, 3, 4, 1, 4]))
17+
18+
# Method 2
19+
20+
def remove_duplicates(nums):
21+
seen = {}
22+
result = []
23+
24+
for num in nums:
25+
if num not in seen:
26+
seen[num] = True
27+
result.append(num)
28+
29+
return seen, result
30+
31+
print(remove_duplicates([1, 2, 3, 4, 1, 4]))
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
Find the first non-repeating character in a string
3+
Input: "aabbcde"
4+
Output: "c"
5+
"""
6+
7+
def first_non_repeating_chr(chr):
8+
string_set = {}
9+
10+
for i in chr:
11+
if i not in string_set:
12+
string_set[i] = 1
13+
else:
14+
string_set[i] += 1
15+
16+
for i in chr:
17+
if string_set[i] == 1:
18+
return i
19+
20+
return None
21+
22+
print(first_non_repeating_chr("aabbcde"))

0 commit comments

Comments
 (0)