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

Hashmaps challenge solution #41

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions classwork/01/alternating_characters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def alternatingCharacters(s):

deletions = 0

for i in range(len(s)-1):

if s[i] == s[i+1]:
deletions += 1

return deletions

print(alternatingCharacters('AABBCD'))
20 changes: 20 additions & 0 deletions classwork/01/hourglass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def hourglassSum(arr):
result = []
n = len(arr)

for i in range(n-2):
for j in range(n-2):
total = sum(arr[i][j:j+3]) + arr[i+1][j+1] + sum(arr[i+2][j:j+3])
result.append(total)

return max(result)


arr = [[1, 1, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 0],
[0, 0, 2, 4, 4, 0],
[0, 0, 0, 2, 0, 0],
[0, 0, 1, 2, 4, 0]]

print(hourglassSum(arr))
28 changes: 28 additions & 0 deletions classwork/01/make_anagram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# time complexity O(n)
# space complexity O(n)

def makeAnagram(a, b):

a_dict = {}
deletion = 0

for i in a:
if i in a_dict:
a_dict[i] += 1
else:
a_dict[i] = 1

for j in b:
if j in a_dict and a_dict[j] > 0:
a_dict[j] -= 1
else:
deletion += 1

for d in a_dict:
if a_dict[d] > 0:
deletion += a_dict[d]

return deletion


print(makeAnagram("cde", "abc"))
21 changes: 21 additions & 0 deletions classwork/01/minimum_bribe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def minimumBribes(q):

bribe_count = 0
n = len(q)

for i in range(n-1, -1, -1):
# break on encounter of bribes > 2
if (q[i] - (i+1)) > 2:
print("Too chaotic")
return

# check if within a range less than 1-q[n]-1 theres a number greater
# than q[n] and increment bribe count if the condition is satisfied
for j in range(max(0, q[i] - 2), i):
if q[j] > q[i]:
bribe_count += 1

print(bribe_count)

# driver code
minimumBribes([2,1,5,3,4])
10 changes: 10 additions & 0 deletions classwork/01/notes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Disadvantages of reversing a list using slicing (eg A[::-1])
- It does a shallow copy ie, modifying an element in the original list will
also be reflected in the copy
- Not perfomant, works well for small arrays

Points to note:
- iterative is better than the recursive approach because:
- python has a slow function calls hence some adds some additional overhead
- python has a fixed recursion depth hence wont do well in big arrays - cython doesnt
optimize for tail recursion hence could cause stack overflow errors on recursion
27 changes: 27 additions & 0 deletions classwork/01/reverse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

# iterative approach
def reverse(A):
n = len(A)
r_list = A[:] # clone the original array

j = 0
for i in range(n-1, -1, -1):
r_list[j] = A[i]
j += 1

return r_list

# recursive approach
def reverse_recursive(A):

if len(A) == 0:
# base case
return []
else:
# recursive case
return [A.pop()] + reverse_recursive(A)


# driver code
print(reverse([1,2,3,4,5]))
print(reverse_recursive([1,2,3,4,5]))
12 changes: 12 additions & 0 deletions classwork/01/rotLeft.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def rotLeft(a, d):

# shift d times to the left (d is in the range of 1 - n)
n = len(a)
temp = [None for _ in range(n)]
for i in range(n):
temp[i-d] = a[i]

return temp

# driver code
print(rotLeft([1,2,3,4,5], 4))
76 changes: 76 additions & 0 deletions classwork/04/evaluate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# class for each node of the linked list
class Node:

def __init__(self, data):
self.data = data
self.next = None

def __repr__(self):
return self.data

class LinkedList:
# only inforamation you need to store is where the list starts
def __init__(self):
self.head = None

def __repr__(self):

node = self.head
nodes = []
while node is not None:
nodes.append(str(node.data))
node = node.next
nodes.append("None")

return "->".join(nodes)

def printList(self):
print(self)

def reverse(self, llist):
if not llist.head or not llist.head.next:
return llist.head
# initialize variables
previous = None
current = llist.head
following = current.next

while current is not None:
current.next = previous # reverse the link
previous = current # move previous one step ahead
current = following # move current one step ahead

if following is not None: # if current wasnt the last item
following = current.next # move ahead to the next item
llist.head = previous
return llist

def sum(self):
list_sum = 0

node = self.head

while node is not None:
list_sum += node.data
node = node.next

return list_sum



llist = LinkedList()
print(llist.printList())
first = Node(1)
llist.head = first
print(llist.printList())
second = Node(2)
third = Node(3)

first.next = second
second.next = third
llist.printList()

llist.reverse(llist)

llist.printList()
print(llist.sum())
24 changes: 24 additions & 0 deletions classwork/05/challenge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def wordFrequency(S):

d = {}
arr = S.split()
max_val = 0
c = ''

for i in arr:
if i in d:
d[i] += 1

if d[i] > max_val:
c = i
max_val = d[i]
else:
d[i] = 1

print(c)
print(max_val)

# Time and space complexity O(N)
# plot a histogram for each occurence of words
wordFrequency("hello there hello there there")