Skip to content

Commit c2ea452

Browse files
committed
Adding solutions for Tasks Before Session 4
1 parent 1ac9bf3 commit c2ea452

25 files changed

+363
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,19 @@ And here's a GIF of my InterviewBit Trees, Binary Search, and Heaps and Maps top
137137
* [GIF of my InterviewBit Trees, Binary Search, and Heaps and Maps topic pages](https://github.com/tachyonlabs/CodePath-Alumni-Professional-Interview-Prep-Course/blob/master/interviewbit-trees-binary-search-and-heaps-and-maps-topics.gif)
138138

139139
When I went to take the HackerRank "CodePath Interview Prep - Unit 3 Test", once again I got a message about how I had already taken this test last summer and "can't login to a test that you have already completed" -- see the "My situation" description at the beginning of this README for an explanation of that -- so as directed by Patricia in Slack, I took the test as tachyon+alt@tachyonlabs.com instead of tachyon@tachyonlabs.com, and you should have the results.
140+
141+
## Tasks Before Session 4
142+
143+
As shown in this GIF, I've already completed most of the Backtracking problems, and all of the Bit Manipulation problems, on InterviewBit (mostly in Python, but I've started doing Java versions of some of them as well):
144+
145+
* GIF of my InterviewBit Backtracking and Bit Manipulation topic pages
146+
147+
#### My InterviewBit Backtracking solutions so far:
148+
149+
Modular Expression, Reverse Link List Recursion, Subset, Combinations, Combination Sum II, Subsets II, Letter Phone, Generate all Parentheses II, NQueens, Permutations, and Gray Code.
150+
151+
#### My InterviewBit Bit Manipulation solutions so far:
152+
153+
* Java: Min XOR value, Number of 1 Bits, Reverse Bits, Single Number, and Single Number II.
154+
155+
* Python: Min XOR value, Number of 1 Bits, Reverse Bits, Divide Integers, Different Bits Sum Pairwise, Single Number, and Single Number II.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
# @param A : list of integers
3+
# @param B : integer
4+
# @return a list of list of integers
5+
def combinationSum(self, A, B):
6+
"""
7+
This wss one of those ones where I do a first solution to check for correctness,
8+
expecting that InterviewBit will fail it for efficiency, and that after that I
9+
will work on optimizing it/coming up with a more efficient approach, but instead
10+
it passes just fine.
11+
"""
12+
combo_sums = []
13+
combos = [[]]
14+
A = sorted([x for x in A if x <= B])
15+
for num in A:
16+
temp = []
17+
for combo in combos:
18+
new_combo = combo + [num]
19+
total = sum(new_combo)
20+
if total == B and new_combo not in combo_sums:
21+
combo_sums.append(new_combo)
22+
elif total < B:
23+
temp.append(new_combo)
24+
combos += temp
25+
26+
return combo_sums
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
# @param n : integer
3+
# @param k : integer
4+
# @return a list of list of integers
5+
def combine(self, n, k):
6+
combos = [[]]
7+
for i in range(k):
8+
new_combos = []
9+
for combo in combos:
10+
for num in range(combo[-1] + 1 if combo else 1, n + 1):
11+
new_combos.append(combo + [num])
12+
13+
combos = new_combos[:]
14+
15+
return combos
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
# @param A : integer
3+
# @return a list of strings
4+
def all_parens(self, n, s="", opens=1, closes=0):
5+
if len(s) == n * 2:
6+
self.parens.append(s)
7+
else:
8+
if opens < n:
9+
self.all_parens(n, s + "(", opens + 1, closes)
10+
if closes < opens:
11+
self.all_parens(n, s + ")", opens, closes + 1)
12+
13+
def generateParenthesis(self, A):
14+
if A == 0:
15+
return []
16+
17+
self.parens = []
18+
self.all_parens(A, "(")
19+
return self.parens
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
# @param A : integer
3+
# @return a list of integers
4+
def grayCode(self, A):
5+
# maybe this should be in bit manipulation instead
6+
return [num ^ (num >> 1) for num in range(2**A)]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
# @param A : string
3+
# @return a list of strings
4+
def letterCombinations(self, A):
5+
keypad = ["0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"]
6+
letter_combinations = list(keypad[int(str(A)[0])])
7+
for digit in str(A)[1:]:
8+
temp = []
9+
for combo in letter_combinations:
10+
for letter in keypad[int(digit)]:
11+
temp.append(combo + letter)
12+
13+
letter_combinations = temp
14+
15+
return letter_combinations
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
# @param A : integer
3+
# @param B : integer
4+
# @param C : integer
5+
# @return an integer
6+
def Mod(self, A, B, C):
7+
if not A:
8+
return 0
9+
10+
if B == 0:
11+
return 1
12+
elif B % 2:
13+
return ((A % C) * self.Mod(A, B - 1, C)) % C
14+
else:
15+
x = self.Mod(A, B / 2, C)
16+
return x ** 2 % C
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
# @param A : integer
3+
# @return a list of list of strings
4+
def can_place_queen(self, r, c, queens):
5+
for qr, qc in queens:
6+
if r == qr or c == qc or r + c == qr + qc or r - c == qr - qc:
7+
return False
8+
9+
return True
10+
11+
def format_board(self, coords):
12+
board = []
13+
for r, c in coords:
14+
board.append("." * c + "Q" + "." * (len(coords) - c - 1))
15+
16+
return board
17+
18+
def find_valid_boards(self, n, row=0, queens=tuple()):
19+
for c in range(n):
20+
if self.can_place_queen(row, c, queens):
21+
if row == n - 1:
22+
self.valid.append(queens + ((row, c),))
23+
else:
24+
self.find_valid_boards(n, row + 1, queens + ((row, c),))
25+
26+
def solveNQueens(self, A):
27+
self.valid = []
28+
self.find_valid_boards(A)
29+
return [self.format_board(board) for board in self.valid]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
# @param A : list of integers
3+
# @return a list of list of integers
4+
def permute(self, A):
5+
# using Heap's algorithm (I had to look at my code from one of the previous times I've used it)
6+
def heaps(length, data, permutations):
7+
if length == 1:
8+
permutations.append(data[:])
9+
else:
10+
for i in range(length):
11+
heaps(length - 1, data, permutations)
12+
if length % 2:
13+
data[0], data[length - 1] = data[length - 1], data[0]
14+
else:
15+
data[i], data[length - 1] = data[length - 1], data[i]
16+
17+
return permutations
18+
19+
return heaps(len(A), A, [])
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
# @param A : head node of linked list
9+
# @return the head node in the linked list
10+
def __init__(self):
11+
self.head = None
12+
13+
def recursive_reverse(self, node):
14+
if not node.next:
15+
self.head = node
16+
return
17+
else:
18+
self.recursive_reverse(node.next)
19+
temp = node.next
20+
temp.next = node
21+
node.next = None
22+
23+
def reverseList(self, A):
24+
if not A or not A.next:
25+
return A
26+
27+
self.recursive_reverse(A)
28+
return self.head

0 commit comments

Comments
 (0)