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

Sumitra(hash practice) #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
102 changes: 92 additions & 10 deletions hash_practice/exercises.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,111 @@

import heapq
# Input = ["eat", "tea", "tan", "ate", "nat", "bat"]
def grouped_anagrams(strings):
""" This method will return an array of arrays.
Each subarray will have strings which are anagrams of each other
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(n)
Space Complexity: O(n)
"""
Comment on lines 3 to 8

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

pass
anagrams_dict = {}
answer = []
for word in strings:
word_as_list = [char for char in word]
# print(word_as_list)
word_as_list.sort()
key = tuple(word_as_list)
if anagrams_dict.get(key):
anagrams_dict[key].append(word)
else:
anagrams_dict[key] = [word]

for key, value in anagrams_dict.items():
answer.append(value)
return answer

# print(grouped_anagrams(Input))

# returnArray = []
# hashOfAnagrams = {}

# if len(strings) == None:
# return None

# for word in strings:
# hashed = "".join(sorted(word))
# if hashed not in hashOfAnagrams:
# hashOfAnagrams[hashed] = []
# hashOfAnagrams[hashed].append[word]

# for i in hashOfAnagrams.values():
# returnArray.append(i)
# return returnArray

def top_k_frequent_elements(nums, k):
""" This method will return the k most common elements
In the case of a tie it will select the first occuring element.
Time Complexity: ?
Space Complexity: ?

Time complexity: O(N log(k)).
Space complexity: O(N).
"""
Comment on lines 43 to 49

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Nice use of a heap!

pass
if nums == []:
return []

count = {}
for num in nums:
count[num] = count.get(num, 0) + 1

heap = []
for key, value in count.items():
heapq.heappush(heap, (-value, key))
results = []

for i in range(k):
count, element = heapq.heappop(heap)
results.append(element)

return results

def valid_sudoku(table):
""" This method will return the true if the table is still
a valid sudoku table.
Each element can either be a ".", or a digit 1-9
The same digit cannot appear twice or more in the same
row, column or 3x3 subgrid
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(n2)
Space Complexity: O(n)
"""
Comment on lines 68 to 76

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Sudoko boards are always 9x9, you can just say O(1) time/space complexity.

pass
# Check rows
for i in range(9):
d = {}
for j in range(9):
if table[i][j] == '.':
pass
elif table[i][j] in d:
return False
else:
d[table[i][j]] = True
# Check columns
for j in range(9):
d = {}
for i in range(9):
if table[i][j] == '.':
pass
elif table[i][j] in d:
return False
else:
d[table[i][j]] = True
# Check sub-boxes
for m in range(0, 9, 3):
for n in range(0, 9, 3):
d = {}
for i in range(n, n + 3):
for j in range(m, m + 3):
if table[i][j] == '.':
pass
elif table[i][j] in d:
return False
else:
d[table[i][j]] = True
return True