Skip to content

Instantly share code, notes, and snippets.

#@time_it
def dict_method_v2(arr):
''' In this method we will remove some of the redundancies
of the dict_method
please note that this method takes a bit more time
because we are making more comparisions for the max_value
than before
Complexity : O(n)
def time_it(func):
def wrapper(*args, **kwargs):
start = time.time()
func(*args, **kwargs)
end = time.time()
return (end - start)*1000
return wrapper
#@time_it
def dict_method(arr):
''' In this method we will use dictionary to store the
count of each element and then search the dictionary
(or) hash map for the element with maximum count
this is one of the quickest methods
Complexity : O(n)
'''
@zahash
zahash / non_repeating_character_dict_method.py
Last active January 3, 2019 12:20
function to find the non repeating character in a string
#@time_it
def dict_method(st):
'''
This function will return the non-repeating
characters in a string
Args:
st: the input string
Return:
#@time_it
def set_method(st):
s = set()
repeat = set()
for letter in st:
if letter in s:
repeat.add(letter)
else:
s.add(letter)
return s-repeat
def cartesian_product(x,y):
'''
This function will take two iterables and returns
the cartesian product of them as a list
'''
return [a+b for a in x for b in y]
def display(values):
"""
Display the values as a 2-D grid.
Input: The sudoku in dictionary form
Output: None
"""
print('')
rows = 'ABCDEFGHI'
cols = '123456789'
start = '53..7....6..195....98....6.8...6...34..8.3..17...2...6.6....28....419..5....8..79'
rows = 'ABCDEFGHI'
cols = '123456789'
boxes = cartesian_product(rows, cols)
row_units = [cartesian_product(r, cols) for r in rows]
col_units = [cartesian_product(rows, c) for c in cols]
box_units = [cartesian_product(r,c)
for r in ['ABC', 'DEF', 'GHI']
def eliminate(grid_dict):
for k,v in grid_dict.items():
if len(v) != 1: # if the box needs elimination
peers = peer_dict[k] # get all the peers
peer_values = set([grid_dict[p] for p in peers if len(grid_dict[p]) == 1])
grid_dict[k] = ''.join(set(grid_dict[k]) - peer_values)
return grid_dict
def only_choice(grid_dict):
for unit in unit_list:
for num in '123456789':
num_places = [box for box in unit if num in grid_dict[box]]
if len(num_places) == 1:
grid_dict[num_places[0]] = num
return grid_dict