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

scissors laurel O #55

Open
wants to merge 3 commits 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
84 changes: 63 additions & 21 deletions hash_practice/exercises.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,71 @@
#below returns an array of arrays.Each subarray
# contains strings which are anagrams of each other
# Time/Space Complexity: O(n)

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: ?
"""
pass
anagrams = dict()
groups = []
for word in strings:
word_sort = ''.join(sorted(word))
key = tuple(word_sort)
if anagrams.get(key):
anagrams[key].append(word)
else:
anagrams[key] = [word]
for value in anagrams.values():
groups.append(value)
return groups

################################################
# This method will return the k most common elements (numbers only)
# In the case of a tie, select the first occuring element. (a twist.)
# *Time/Space Complexity: O(n)
# *probably not jw

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: ?
"""
pass
num_count = {}
for num in nums:
if num_count.get(num):
num_count[num] += 1
else:
num_count[num] = 1

top = []
for i in range(len(num_count.values()), 0, -1):
for key, value in num_count.items():
if i == value:
top.append(key)

return top[:k]

############sudoku helpers######################
import re

def is_num(string):
nums = '^[0-9]$'
if re.search(nums, string):
return True
return False

def valid(line): ##########################behold:
num_only = [i for i in line if is_num(i)] ################################ the single
if len(num_only) == len(line): ################################ but useful
return len(set(num_only)) == 9 and sum(num_only) == 45 == sum(set(line))#appearance
################################ of hash sets()
return len(set(line)) == len(num_only) + 1 ################################ in this solution

#################################################
### *Time/Space Complexity: O(n)
### *bc input will always be the same size tho...
### do we need to consider it for these?
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: ?
"""
pass
subgrids = []
for i in range(0, 9, 3):
for j in range(0, 9, 3):
subgrid = [char for chars in [row[j:j + 3] for row in table[i:i + 3]] for char in chars]
subgrids.append(subgrid)

nope_subgrids = [grid for grid in subgrids if not valid(grid)]
nope_columns_rows = [line for line in table if not valid(line)]

return not (nope_columns_rows or nope_subgrids)