From 635b3a4e4e67957896399d0fd9a6a9df8e327a55 Mon Sep 17 00:00:00 2001 From: "@saadaoui-salah" Date: Mon, 17 May 2021 05:53:01 -0700 Subject: [PATCH 01/14] add clean solutions --- clean_solutions.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 clean_solutions.py diff --git a/clean_solutions.py b/clean_solutions.py new file mode 100644 index 0000000..2f8abe1 --- /dev/null +++ b/clean_solutions.py @@ -0,0 +1,35 @@ +import os + +def get_files(skip=True): + files = os.listdir() + needed_folders = [] + for file in files: + if skip: + if '.' in file or '0' in file or "LICENSE" == file or "data_structure" == file: + continue + needed_folders.append(file) + return needed_folders + +def clean_content(files): + for file in files: + if '.py' in file: + file_obj = open(file, 'r') + content = file_obj.readlines() + content = ''.join(content[:]) + task = content.split('"""')[1] + file_obj = open(file, 'w') + file_obj.truncate() + task = '"""' + task + '"""' + file_obj.write(task) + +def remove_solutions(): + needed_folders = get_files() + for folder in needed_folders: + os.chdir(folder) + files = get_files(skip=False) + clean_content(files) + os.chdir("..") + +if __name__ == '__main__': + remove_solutions() + \ No newline at end of file From 873c67df9a6948e97570bca9eb7ea7ea4ede4ebc Mon Sep 17 00:00:00 2001 From: "@saadaoui-salah" Date: Mon, 17 May 2021 05:57:57 -0700 Subject: [PATCH 02/14] clean all solutions --- amazon_problems/problem_10.py | 2 +- amazon_problems/problem_11.py | 2 +- amazon_problems/problem_12.py | 18 +------- amazon_problems/problem_15.py | 17 +------ amazon_problems/problem_2.py | 34 +------------- amazon_problems/problem_3.py | 21 +-------- amazon_problems/problem_30.py | 9 +--- amazon_problems/problem_4.py | 28 +---------- amazon_problems/problem_6.py | 15 +----- amazon_problems/problem_9.py | 69 +--------------------------- apple_problems/problem_1.py | 16 +------ apple_problems/problem_2.py | 34 +------------- cisco_problems/problem_1.py | 2 +- context_logic_problems/problem_1.py | 16 +------ coursera_problems/problem_1.py | 2 +- dropbox_problem/problem_7.py | 7 +-- facebook_problems/problem_1.py | 27 +---------- facebook_problems/problem_10.py | 29 +----------- facebook_problems/problem_11.py | 22 +-------- facebook_problems/problem_12.py | 18 +------- facebook_problems/problem_13.py | 16 +------ facebook_problems/problem_5.py | 56 +--------------------- facebook_problems/problem_6.py | 31 +------------ glassdoor_problems/problem_1.py | 2 +- goldman_sachs_problems/problem_1.py | 2 +- goldman_sachs_problems/problem_2.py | 2 +- goldman_sachs_problems/problem_3.py | 2 +- google_problems/problem_1.py | 34 +------------- google_problems/problem_12.py | 28 +---------- google_problems/problem_13.py | 2 +- google_problems/problem_14.py | 2 +- google_problems/problem_15.py | 2 +- google_problems/problem_16.py | 21 +-------- google_problems/problem_22.py | 23 +--------- google_problems/problem_25.py | 22 +-------- google_problems/problem_3.py | 32 +------------ google_problems/problem_30.py | 64 +------------------------- google_problems/problem_31.py | 26 +---------- google_problems/problem_7.py | 16 +------ google_problems/problem_79.py | 47 +------------------ google_problems/problem_8.py | 41 +---------------- google_problems/problem_85.py | 2 +- google_problems/problem_86.py | 2 +- google_problems/problem_88.py | 2 +- google_problems/problem_89.py | 2 +- grammarly_problems/problem_1.py | 2 +- linked_in_problems/problem_1.py | 52 +-------------------- linked_in_problems/problem_2.py | 25 +--------- linked_in_problems/problem_3.py | 2 +- linked_in_problems/problem_4.py | 2 +- linked_in_problems/problem_5.py | 2 +- linked_in_problems/problem_6.py | 2 +- linked_in_problems/problem_7.py | 2 +- linked_in_problems/problem_8.py | 2 +- mailchimp_problems/problem_1.py | 2 +- microsoft_problems/problem_13.py | 20 +------- microsoft_problems/problem_14.py | 2 +- microsoft_problems/problem_15.py | 2 +- microsoft_problems/problem_16.py | 2 +- microsoft_problems/problem_17.py | 2 +- microsoft_problems/problem_18.py | 2 +- microsoft_problems/problem_19.py | 2 +- microsoft_problems/problem_2.py | 28 +---------- microsoft_problems/problem_20.py | 2 +- microsoft_problems/problem_21.py | 2 +- microsoft_problems/problem_22.py | 2 +- microsoft_problems/problem_23.py | 2 +- microsoft_problems/problem_24.py | 2 +- microsoft_problems/problem_25.py | 2 +- microsoft_problems/problem_26.py | 2 +- microsoft_problems/problem_27.py | 2 +- microsoft_problems/problem_28.py | 2 +- microsoft_problems/problem_29.py | 2 +- microsoft_problems/problem_6.py | 34 +------------- microsoft_problems/problem_7.py | 28 +---------- morgan_stanley_problems/problem_1.py | 2 +- nest_problems/problem_1.py | 2 +- netflix_problems/problem_1.py | 2 +- netflix_problems/problem_2.py | 2 +- netflix_problems/problem_3.py | 2 +- netflix_problems/problem_4.py | 2 +- nvidia_problems/problem_1.py | 2 +- nvidia_problems/problem_2.py | 2 +- oracle_problems/problem_1.py | 2 +- oracle_problems/problem_2.py | 2 +- oracle_problems/problem_3.py | 2 +- others_problems/problem_1.py | 2 +- others_problems/problem_2.py | 5 +- quantcast_problems/problem_1.py | 2 +- quantcast_problems/problem_2.py | 2 +- riot_games_problems/problem_1.py | 2 +- robinhood_problems/problem_1.py | 2 +- robinhood_problems/problem_2.py | 2 +- snapchat_problems/problem_1.py | 36 +-------------- snapchat_problems/problem_2.py | 26 +---------- square_problems/problem_1.py | 2 +- square_problems/problem_2.py | 2 +- square_problems/problem_3.py | 2 +- square_problems/problem_4.py | 2 +- square_problems/problem_5.py | 2 +- square_problems/problem_6.py | 2 +- square_problems/problem_7.py | 2 +- squarespace_problems/problem_1.py | 2 +- stitch_fix_problems/problem_1.py | 2 +- stripe_problems/problem_1.py | 43 +---------------- sumo_logic_problems/problem_1.py | 2 +- triple_byte_problems/problem_1.py | 2 +- triple_byte_problems/problem_2.py | 2 +- twitch_problems/problem_1.py | 2 +- twitter_problems/problem_1.py | 15 +----- uber_problems/problem_1.py | 65 +------------------------- wework_problems/problem_1.py | 2 +- 112 files changed, 112 insertions(+), 1242 deletions(-) diff --git a/amazon_problems/problem_10.py b/amazon_problems/problem_10.py index bd8e7e2..6034613 100644 --- a/amazon_problems/problem_10.py +++ b/amazon_problems/problem_10.py @@ -9,4 +9,4 @@ 0 0 0 0 0 1 1 0 0 1 1 1 0 0 1 -""" +""" \ No newline at end of file diff --git a/amazon_problems/problem_11.py b/amazon_problems/problem_11.py index 48e6b5f..929e1cc 100644 --- a/amazon_problems/problem_11.py +++ b/amazon_problems/problem_11.py @@ -10,4 +10,4 @@ / \ 22 35 You can assume each node has a parent pointer. -""" +""" \ No newline at end of file diff --git a/amazon_problems/problem_12.py b/amazon_problems/problem_12.py index 1caaa06..ce9f3db 100644 --- a/amazon_problems/problem_12.py +++ b/amazon_problems/problem_12.py @@ -4,20 +4,4 @@ • init(size): initialize the array with size • set(i, val): updates index at i with val where val is either 1 or 0. • get(i): gets the value at index i. -""" - -class BitArray: - - def __init__(self, size): - self.data=[] - self.size = size - for i in range(size): - self.data.append(0) - - def set(i , val): - if 0 <= size < self.size - self.data[i] = val - - def get(i): - if 0 <= size < self.size - return data[i] \ No newline at end of file +""" \ No newline at end of file diff --git a/amazon_problems/problem_15.py b/amazon_problems/problem_15.py index edaffad..53fd3ca 100644 --- a/amazon_problems/problem_15.py +++ b/amazon_problems/problem_15.py @@ -5,19 +5,4 @@ For example, carrace should return true, since it can be rearranged to form racecar, which is a palindrome. daily should return false, since there's no rearrangement that can form a palindrome. -""" -#idea : we can't have more than one letter that has an odd frequency -def solve(s): - freq = [0] * 256 - for c in s: - freq[ord(c)] += 1 - - cp = 0 - for f in freq: - cp += f & 1 - - return cp <= 1 - - -print(solve('daily')) # returns False -print(solve('carrace')) #returns True +""" \ No newline at end of file diff --git a/amazon_problems/problem_2.py b/amazon_problems/problem_2.py index 13f16ba..cfa0014 100644 --- a/amazon_problems/problem_2.py +++ b/amazon_problems/problem_2.py @@ -4,36 +4,4 @@ that contains at most k distinct characters. For example, given s = "abcba" and k = 2, the longest substring with k -distinct characters is "bcb".""" - - -def main(s, k): - start = 0 - current = "" - max_lengths = 0 - while start < len(s): - for i in range(start + k - 1, len(s)): - ss = s[start:i + 1] - counter = count_character(ss) - if counter > k: - break - else: - if len(ss) > max_lengths: - max_lengths = len(ss) - current = ss - start += 1 - return current - - -def count_character(string): - current = [string[0]] - counter = 1 - for i in string: - if i not in current: - counter += 1 - current.append(i) - return counter - - -# print(count_character("abbbcaabca")) -print(main("abbbcef", 3)) +distinct characters is "bcb".""" \ No newline at end of file diff --git a/amazon_problems/problem_3.py b/amazon_problems/problem_3.py index 35f0347..151a1bc 100644 --- a/amazon_problems/problem_3.py +++ b/amazon_problems/problem_3.py @@ -6,23 +6,4 @@ Implement run-length encoding and decoding. You can assume the string to be encoded have no digits and consists solely of alphabetic characters. -You can assume the string to be decoded is valid """ - - -def main(string): - if len(string) == 0: - return "" - else: - current = string[0] - counter = 0 - output = "" - for i in string: - if current == i: - counter += 1 - else: - output += str(counter) + current - counter = 1 - current = i - output += str(counter) + current - return output -print(main("AAAABBBCCDAA")) \ No newline at end of file +You can assume the string to be decoded is valid """ \ No newline at end of file diff --git a/amazon_problems/problem_30.py b/amazon_problems/problem_30.py index 61be876..bdb1875 100644 --- a/amazon_problems/problem_30.py +++ b/amazon_problems/problem_30.py @@ -4,11 +4,4 @@ and returns the number of digits the input has. Constraint: don't use any loops. -""" - -# idea: use the log of ten - -import numpy as np - -def number_of_digit(x): - return np.floor(np.log10(x,10)) + 1 +""" \ No newline at end of file diff --git a/amazon_problems/problem_4.py b/amazon_problems/problem_4.py index ee7082a..c5e33fd 100644 --- a/amazon_problems/problem_4.py +++ b/amazon_problems/problem_4.py @@ -6,30 +6,4 @@ • max(), which returns the maximum value in the stack currently. If there are no elements in the stack, then it should throw an error or return null. Each method should run in constant time. -""" - - -class stack(object): - """stack object for the solution""" - - def __init__(self): - self.max = None - self.count = 0 - self.array = [] - - def push(self, val): - self.array.append(val) - if val >= self.max: - self.max = val - - def pop(self): - if len(self.array) == 0: - return None - else: - value = self.array.pop() - if value == self.max: - self.max = max(self.array) - return value - - def max(self): - return self.max +""" \ No newline at end of file diff --git a/amazon_problems/problem_6.py b/amazon_problems/problem_6.py index fbd1529..8641201 100644 --- a/amazon_problems/problem_6.py +++ b/amazon_problems/problem_6.py @@ -10,17 +10,4 @@ not take any elements. Do this in O(N) time. -""" - - -def solve(arr): - mx = 0 - sum = 0 - for a in arr: - sum += a - if sum < 0: sum = 0 - mx = max(mx, sum) - return mx - -print(solve([34, -50, 42, 14, -5, 86])) # returns 137 -print(solve([-5, -1, -8, -9])) # returns 0 +""" \ No newline at end of file diff --git a/amazon_problems/problem_9.py b/amazon_problems/problem_9.py index b0e132e..8b1f6bc 100644 --- a/amazon_problems/problem_9.py +++ b/amazon_problems/problem_9.py @@ -29,71 +29,4 @@ 9 14 13 -12""" - -a = [[1, 2, 3, 4, 5], - [6, 7, 8, 9, 10], - [11, 12, 13, 14, 15], - [16, 17, 18, 19, 20]] - -b = [[1, 2, 3], - [4, 5, 6]] - -c = [[1, 2, 3]] - -d = [[1], - [2], - [3], - [4], - [5], - [6]] - -e = [[1, 2, 3, 4], - [1, 2, 3, 4], - [1, 2, 3, 4]] - - -def main(): - matrix = e - rs = 0 - re = len(matrix) - 1 - cs = 0 - ce = len(matrix[0]) - 1 - ci = 0 - cj = 0 - total = len(matrix) * len(matrix[0]) - now = 0 - ori = 0 - - while now < total: - - if ori == 0: - for cj in range(cs, ce + 1): - print(matrix[ci][cj]) - now += 1 - rs += 1 - ori = 1 - - elif ori == 1: - for ci in range(rs, re + 1): - print(matrix[ci][cj]) - now += 1 - ce -= 1 - ori = 2 - - elif ori == 2: - for cj in range(ce, cs - 1, -1): - print(matrix[ci][cj]) - now += 1 - re -= 1 - ori = 3 - - elif ori == 3: - for ci in range(re, rs - 1, -1): - print(matrix[ci][cj]) - now += 1 - cs += 1 - ori = 0 - - -main() +12""" \ No newline at end of file diff --git a/apple_problems/problem_1.py b/apple_problems/problem_1.py index 1a9cef1..414bd69 100644 --- a/apple_problems/problem_1.py +++ b/apple_problems/problem_1.py @@ -1,18 +1,4 @@ """This problem was asked by Apple. Implement a job scheduler which takes in a function -f and an integer n, and calls f after n milliseconds.""" - -import time as t - -def dummy(): - print("I'm Called") - -def job_scheduller(f,n): - start = t.time()*1000 - end = start + n - while start < end: - start = t.time()*1000 - f() - -job_scheduller(dummy,1000) \ No newline at end of file +f and an integer n, and calls f after n milliseconds.""" \ No newline at end of file diff --git a/apple_problems/problem_2.py b/apple_problems/problem_2.py index 14282a4..31fd694 100644 --- a/apple_problems/problem_2.py +++ b/apple_problems/problem_2.py @@ -3,36 +3,4 @@ Recall that a queue is a FIFO (first-in, first-out) data structure with the following methods: enqueue, which inserts an element into the queue, and dequeue, which removes it. -.""" - - -class Queue: - - def __init__(self): - self.inbounds = [] - self.outbounds = [] - - def enqueue(self, data): - self.inbounds.append(data) - - def dequeue(self): - if not self.outbounds: - while self.inbounds: - self.outbounds.append(self.inbounds.pop()) - return self.outbounds.pop() - - -# Testing the Queue. -queue = Queue() -queue.enqueue(5) -queue.enqueue(6) -queue.enqueue(7) -print(queue.inbounds) - -queue.dequeue() -print(queue.inbounds) -print(queue.outbounds) - -queue.dequeue() -print(queue.inbounds) -print(queue.outbounds) +.""" \ No newline at end of file diff --git a/cisco_problems/problem_1.py b/cisco_problems/problem_1.py index 1bdafce..ce57a5a 100644 --- a/cisco_problems/problem_1.py +++ b/cisco_problems/problem_1.py @@ -6,4 +6,4 @@ For example, 10101010 should be 01010101. 11100010 should be 11010001. Bonus: Can you do this in one line? -""" +""" \ No newline at end of file diff --git a/context_logic_problems/problem_1.py b/context_logic_problems/problem_1.py index fb2a51e..9e8c2ea 100644 --- a/context_logic_problems/problem_1.py +++ b/context_logic_problems/problem_1.py @@ -1,18 +1,4 @@ """This question was asked by ContextLogic. Implement division of two positive integers without using the division, -multiplication, or modulus operators. Return the quotient as an integer, ignoring the remainder.""" - -# main method for making division, quiet easy: just subtract second nomber for the first until 0. and count. -def division(a,b): - i=0 - while a >= b: - i += 1 - a = a-b - return i - -print(division(6,2)) -print(division(10,5)) -print(division(20,5)) -print(division(120,20)) - \ No newline at end of file +multiplication, or modulus operators. Return the quotient as an integer, ignoring the remainder.""" \ No newline at end of file diff --git a/coursera_problems/problem_1.py b/coursera_problems/problem_1.py index aca6f1e..d90f9be 100644 --- a/coursera_problems/problem_1.py +++ b/coursera_problems/problem_1.py @@ -9,4 +9,4 @@ ['A','D','E','E'] ] exists(board, "ABCCED") returns true, exists(board, "SEE") returns true, exists(board, "ABCB") returns false -""" +""" \ No newline at end of file diff --git a/dropbox_problem/problem_7.py b/dropbox_problem/problem_7.py index 9db0ebe..159b935 100644 --- a/dropbox_problem/problem_7.py +++ b/dropbox_problem/problem_7.py @@ -4,9 +4,4 @@ "A", "B", "C", ..., "AA", "AB", ..., "ZZ", "AAA", "AAB", .... Given a column number, return its alphabetical column id. -For example, given 1, return "A". Given 27, return "AA".""" - -def spreadsheet(number): - return number//26 + 1 - -spreadsheet(27) \ No newline at end of file +For example, given 1, return "A". Given 27, return "AA".""" \ No newline at end of file diff --git a/facebook_problems/problem_1.py b/facebook_problems/problem_1.py index 64f3e26..0294c12 100644 --- a/facebook_problems/problem_1.py +++ b/facebook_problems/problem_1.py @@ -7,29 +7,4 @@ as 'aaa', 'ka', and 'ak'. You can assume that the messages are decodable. For example, -'001' is not allowed.""" - - -# Solution of this problem is given using a Tree data structure - -class Node(object): - - def __init__(self, data): - self.data = data - self.right = None - self.left = None - - -class Tree(object): - - def __init__(self, string): - self.root = Node(["", 0]) - self.string = string - - # count the number of branches, this is the solution - def possible_branches(self): - pass - - # get possible child for a node - def get_nodes(self, position): - pass +'001' is not allowed.""" \ No newline at end of file diff --git a/facebook_problems/problem_10.py b/facebook_problems/problem_10.py index f9e81af..afbdb57 100644 --- a/facebook_problems/problem_10.py +++ b/facebook_problems/problem_10.py @@ -7,31 +7,4 @@ since we can split it up into {15, 5, 10, 15, 10} and {20, 35}, which both add up to 55. Given the multiset {15, 5, 20, 10, 35}, it would return false, since we can't -split it up into two subsets that add up to the same sum.""" - -def doesMain(): - data = [15,5,20,10,35,15,10] - data.sort() - total = sum(data) - if total / 2 == total //2 : - return contain(array,total / 2 , 0) - else: - return False - -def contain(array , number , index): - if index == len(array): - return True - if array[index] == number: - return True - elif array[index] < number: - return contain(array, number -1, index+1) - else : - return contain(array, number, index+1) - -def sum(array): - result = 0 - for i in array: - result += i - return result - - print() \ No newline at end of file +split it up into two subsets that add up to the same sum.""" \ No newline at end of file diff --git a/facebook_problems/problem_11.py b/facebook_problems/problem_11.py index f47e492..e70e7cf 100644 --- a/facebook_problems/problem_11.py +++ b/facebook_problems/problem_11.py @@ -7,24 +7,4 @@ • Right, then down • Down, then right Given a 5 by 5 matrix, there are 70 ways to get to the bottom-right. -""" - - -def main(): - N = 5 - M = 5 - print(number_ways(N, M, 0, 0)) - - -def number_ways(n, m, i, j): - if i + 1 == n and j + 1 == m: - return 1 - if i + 1 == n: - return number_ways(n, m, i, j + 1) - elif j + 1 == m: - return number_ways(n, m, i + 1, j) - else: - return number_ways(n, m, i + 1, j) + number_ways(n, m, i, j + 1) - - -main() \ No newline at end of file +""" \ No newline at end of file diff --git a/facebook_problems/problem_12.py b/facebook_problems/problem_12.py index 988d963..277cda6 100644 --- a/facebook_problems/problem_12.py +++ b/facebook_problems/problem_12.py @@ -2,20 +2,4 @@ Given a list of integers, return the largest product that can be made by multiplying any three integers. For example, if the list is [-10, -10, 5, 2], we should return 500, since that's -10 * -10 * 5. -You can assume the list has at least three integers.""" - -array = [-10, 10, 5, 2, -4] - -positives = [x for x in array if x > 0] -negative = [x for x in array if x < 0] - -positives.sort() -negative.sort() - -first = positives[-1] * positives[-2] * negative[-1] -second = negative[-1] * negative[-2] * positives[-1] - -if first > second: - print(first) -else: - print(second) +You can assume the list has at least three integers.""" \ No newline at end of file diff --git a/facebook_problems/problem_13.py b/facebook_problems/problem_13.py index 89f0005..6d0ea02 100644 --- a/facebook_problems/problem_13.py +++ b/facebook_problems/problem_13.py @@ -5,18 +5,4 @@ can modify the 10 into a 1 to make the array non-decreasing. Given the array [10, 5, 1], you should return false, since -we can't modify any one element to get a non-decreasing array.""" - -array = [10, 5, 7] - -result = 0 - -for x in range(len(array)): - - if not x==(len(array) -1) and array[x] > array[x+1]: - result +=1 - -if result <=1: - print(True) -else: - print(False) +we can't modify any one element to get a non-decreasing array.""" \ No newline at end of file diff --git a/facebook_problems/problem_5.py b/facebook_problems/problem_5.py index c2f234a..ea51da5 100644 --- a/facebook_problems/problem_5.py +++ b/facebook_problems/problem_5.py @@ -3,58 +3,4 @@ return whether the brackets are balanced (well-formed). For example, given the string "([])[]({})", you should return true. Given the string "([)]" or "((()", you should return false. -""" -import math - - -# code each character with a number -def code(i): - if i == "(": - return 1 - elif i == ")": - return -1 - elif i == "[": - return 2 - elif i == "]": - return -2 - elif i == "{": - return 3 - elif i == "}": - return -3 - - -# main function of the problem -def validate(arr): - new_array = [] - for i in arr: - new_array.append(code(i)) - val_arr = [] - for i in new_array: - if len(val_arr) == 0: - val_arr.append(i) - else: - last = val_arr[len(val_arr) - 1] - if check(i, last): - val_arr.pop() - elif last * i > 0: - val_arr.append(i) - else: - return False - - if len(val_arr) == 0: - return True - else: - return False - - -# check is the tow values or conformed with each other -def check(a, b): - if math.fabs(a) == math.fabs(b) and a * b < 0: - return True - return False - - -# test cases. -print(validate("([])[]({})")) -print(validate("()")) -print(validate("(})")) +""" \ No newline at end of file diff --git a/facebook_problems/problem_6.py b/facebook_problems/problem_6.py index d386aa9..4031741 100644 --- a/facebook_problems/problem_6.py +++ b/facebook_problems/problem_6.py @@ -7,33 +7,4 @@ Given the input [3, 0, 1, 3, 0, 5], we can hold 3 units in the first index, 2 in the second, and 3 in the fourth index (we cannot hold 5 since it would run off to the left), so we can trap 8 units of water. -""" - - -def main(array): - if len(array) < 3: - return 0 - start = array[0] - end = array[len(array) - 1] - - if start < end: - small = start - else: - small = end - - output = 0 - if start == end: - for i in array: - value = start - i - if value > 0: - output += value - else: - for i in array: - value = small - i - if value > 0: - output += value - return output - - -print(main([2, 1, 2])) -print(main([3, 0, 1, 3, 0, 5])) +""" \ No newline at end of file diff --git a/glassdoor_problems/problem_1.py b/glassdoor_problems/problem_1.py index 431a078..8c1106e 100644 --- a/glassdoor_problems/problem_1.py +++ b/glassdoor_problems/problem_1.py @@ -7,4 +7,4 @@ For example, given a population with weights [100, 200, 150, 80] and a boat limit of 200, the smallest number of boats required will be three. -""" +""" \ No newline at end of file diff --git a/goldman_sachs_problems/problem_1.py b/goldman_sachs_problems/problem_1.py index fa65977..5d2f57f 100644 --- a/goldman_sachs_problems/problem_1.py +++ b/goldman_sachs_problems/problem_1.py @@ -4,4 +4,4 @@ For example, given L = [1, 2, 3, 4, 5], sum(1, 3) should return sum([2, 3]), which is 5. You can assume that you can do some pre-processing. sum() should be optimized over the pre-processing step. -""" +""" \ No newline at end of file diff --git a/goldman_sachs_problems/problem_2.py b/goldman_sachs_problems/problem_2.py index 6d9547f..23f52ea 100644 --- a/goldman_sachs_problems/problem_2.py +++ b/goldman_sachs_problems/problem_2.py @@ -11,4 +11,4 @@ For example, if N = 1 and k = 5, we will need to try dropping the egg at every floor, beginning with the first, until we reach the fifth floor, so our solution will be 5. -""" +""" \ No newline at end of file diff --git a/goldman_sachs_problems/problem_3.py b/goldman_sachs_problems/problem_3.py index 57f3a66..6418c9f 100644 --- a/goldman_sachs_problems/problem_3.py +++ b/goldman_sachs_problems/problem_3.py @@ -7,4 +7,4 @@ You can assume that you can do some pre-processing. sum() should be optimized over the pre-processing step. -""" +""" \ No newline at end of file diff --git a/google_problems/problem_1.py b/google_problems/problem_1.py index c135d51..51357c7 100644 --- a/google_problems/problem_1.py +++ b/google_problems/problem_1.py @@ -1,36 +1,4 @@ """ This problem was recently asked by Google. Given a list of numbers and a number k, return whether any two numbers from the list add up to k. For example, given [10, 15, 3, 7] and k of 17, return true -since 10 + 7 is 17. Bonus: Can you do this in one pass? """ - - -# Function of the main problem -def numbers_add_up_to_k(array, k): - """ a supplementary array to save the numbers we are looking for , - say that k is 17 and we current number in the array is 15, this one need 2 - to add up to 17, so we save 2 in our supplementary array and we check if the - array containing 2 as we move in the array.""" - supp = [] - - # simple loop to get all the number in the array. - for n in array: - # check if the number is in the supplementary array. - if is_number_in(supp, n): - return True - else: - # otherwise we calculate the number we need and append it. - supp.append(k - n) - - return False - - -# This function search a number in an array and return true if the number is in. -def is_number_in(array, number): - for n in array: - if n == number: - return True - return False - - -# print out the result -print(numbers_add_up_to_k([10, 15, 3, 7], 17)) +since 10 + 7 is 17. Bonus: Can you do this in one pass? """ \ No newline at end of file diff --git a/google_problems/problem_12.py b/google_problems/problem_12.py index b239903..4ba250c 100644 --- a/google_problems/problem_12.py +++ b/google_problems/problem_12.py @@ -4,30 +4,4 @@ required to change one string to the other. For example, the edit distance between “kitten” and “sitting” is three: substitute the “k” for “s”, substitute the “e” for “i”, and append a “g”. -Given two strings, compute the edit distance between them.""" - -import math - - -def main(one, two): - if len(one) > len(two): - small = len(two) - else: - small = len(one) - distance = int(math.fabs(len(one) - len(two))) - if distance == 0: - for i, j in zip(one, two): - if i != j: - distance += 1 - else: - for i in range(small): - if one[i] != two[i]: - distance += 1 - return distance - - -print(main("kitten", "sitting")) # should gave 3 -print(main("me", "you")) # should gave 3 -print(main("majid", "younes")) # should gave 6 -print(main("hello", "ehllo")) # should gave 2 -print(main("younes", "younes")) # should gave 2 +Given two strings, compute the edit distance between them.""" \ No newline at end of file diff --git a/google_problems/problem_13.py b/google_problems/problem_13.py index 2df5e9d..af69316 100644 --- a/google_problems/problem_13.py +++ b/google_problems/problem_13.py @@ -4,4 +4,4 @@ and the Bs come last. You can only swap elements of the array. Do this in linear time and in-place. For example, given the array ['G', 'B', 'R', 'R', 'B', 'R', 'G'], it should -become ['R', 'R', 'R', 'G', 'G', 'B', 'B'].""" +become ['R', 'R', 'R', 'G', 'G', 'B', 'B'].""" \ No newline at end of file diff --git a/google_problems/problem_14.py b/google_problems/problem_14.py index 803e9b2..77fd4e3 100644 --- a/google_problems/problem_14.py +++ b/google_problems/problem_14.py @@ -6,4 +6,4 @@ For example, given the set {1, 2, 3}, it should return {{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}. -You may also use a list or array to represent a set.""" +You may also use a list or array to represent a set.""" \ No newline at end of file diff --git a/google_problems/problem_15.py b/google_problems/problem_15.py index f4f15b3..c322ee0 100644 --- a/google_problems/problem_15.py +++ b/google_problems/problem_15.py @@ -6,4 +6,4 @@ For example, given [6, 1, 3, 3, 3, 6, 6], return 1. Given [13, 19, 13, 13], return 19. -Do this in O(N) time and O(1) space.""" +Do this in O(N) time and O(1) space.""" \ No newline at end of file diff --git a/google_problems/problem_16.py b/google_problems/problem_16.py index d25ea87..3860af7 100644 --- a/google_problems/problem_16.py +++ b/google_problems/problem_16.py @@ -6,23 +6,4 @@ all numbers in the list are positive. For example, given S = [12, 1, 61, 5, 9, 2] and k = 24, return [12, 9, 2, 1] since it sums up to 24. -""" - -# function to filter element smaller than k. -def subset_sum(nums, k): - if k == 0: - return [] - if not nums and k != 0: - return None - - nums_copy = nums[:] - last = nums_copy.pop() - - with_last = subset_sum(nums_copy, k - last) - without_last = subset_sum(nums_copy, k) - if with_last is not None: - return with_last + [last] - if without_last is not None: - return without_last - -subset_sum([12, 1, 61, 5, 9, 2] , 24) \ No newline at end of file +""" \ No newline at end of file diff --git a/google_problems/problem_22.py b/google_problems/problem_22.py index 8a52240..3506777 100644 --- a/google_problems/problem_22.py +++ b/google_problems/problem_22.py @@ -3,25 +3,4 @@ where x and y are integers and returns x^y. Do this faster than the naive method of repeated multiplication. For example, pow(2, 10) should return 1024. -""" - -# naive -def pow(x,y): - result = 1 - for i in range(1,y): - result *= x - return result - -# naive 2 -def pow(x,y): - return x**y - -# recursive one -def power(x, y): - if (y == 0): - return 1 - temp = power(x, int(y / 2)) - if (int(y % 2) == 0): - return temp * temp - else: - return x * temp * temp \ No newline at end of file +""" \ No newline at end of file diff --git a/google_problems/problem_25.py b/google_problems/problem_25.py index 2b55400..772199f 100644 --- a/google_problems/problem_25.py +++ b/google_problems/problem_25.py @@ -18,24 +18,4 @@ [0 0 0 0 0] [b 0 0 0 0] You should return 2, since bishops 1 and 3 attack each other, as well as bishops 3 and 4 -""" - - -def main(): - m = 5 - tuples = [(0, 0), (0, 1), (2, 2), (2, 4)] - left = 0 - right = 0 - for i in tuples: - if i[0] == i[1]: - left += 1 - for t in tuples: - for i in range(m - 1, -1, -1): - for j in range(m): - print(i) - print(j) - if t[0] == j and t[1] == i: - right += 1 - - -main() +""" \ No newline at end of file diff --git a/google_problems/problem_3.py b/google_problems/problem_3.py index 0eedde9..d71cc09 100644 --- a/google_problems/problem_3.py +++ b/google_problems/problem_3.py @@ -8,34 +8,4 @@ If using a language that has no pointers (such as Python), you can assume you have access to get_pointer and dereference_pointer functions that converts between -nodes and memory addresses.""" - - -# Creating a class for the main linked list -class XorLinked(object): - - def __init__(self): - self.length = 0 - self.head = None - - def add(self, element): - if self.length == 0: - nod = Node(element,0) - self.head = element - - - -def get_pointer(node): - return 15 - -def dereference_pointer(pointer): - return Node(12,2) - -# class of structure of node. -class Node(object): - - def __init__(self, value, xor): - self.value = value - self.xor_pointer = xor - - +nodes and memory addresses.""" \ No newline at end of file diff --git a/google_problems/problem_30.py b/google_problems/problem_30.py index 51e2928..4e90e00 100644 --- a/google_problems/problem_30.py +++ b/google_problems/problem_30.py @@ -16,66 +16,4 @@ c b \\ / \ f e d -""" - -from collections import deque - - -class Node: - - def __init__(self, data=None): - self.data = data - self.right = None - self.left = None - - def invert_node(self): - right = self.right - self.right = self.left - self.left = right - - -class BinaryTree: - - def __init__(self, root=None): - self.root = root - - def invert(self): - queue = deque() - queue.append(self.root) - while queue: - node = queue.popleft() - if node.left: - queue.append(node.left) - if node.right: - queue.append(node.right) - node.invert_node() - - def breadth_traversal(self): - list_nodes = [] - queue = deque([self.root]) - while len(queue) > 0: - node = queue.popleft() - list_nodes.append(node.data) - if node.left: - queue.append(node.left) - if node.right: - queue.append(node.right) - return list_nodes - - -a = Node('a') -b = Node('b') -c = Node('c') -d = Node('d') -e = Node('e') -f = Node('f') - -a.left = b -a.right = c -b.left = d -b.right = e -c.left = f - -tree = BinaryTree(a) -tree.invert() -print(tree.breadth_traversal()) +""" \ No newline at end of file diff --git a/google_problems/problem_31.py b/google_problems/problem_31.py index 6f6b735..c2fcf3c 100644 --- a/google_problems/problem_31.py +++ b/google_problems/problem_31.py @@ -2,28 +2,4 @@ Given a string of parentheses, write a function to compute the minimum number of parentheses to be removed to make the string valid (i.e. each open parenthesis is eventually closed). For example, given the string "()())()", you should return 1. -Given the string ")(", you should return 2, since we must remove all of them.""" - -string = "()())()" -string = ")(" -string = ")()" -string = "()()" -string = "()(())()" - -status = False -result = 0 - -for i in string: - if i == "(" and not status: - status= True - result += 1 - elif i == "(" and status: - result += 1 - elif i==")" and status: - status = False - result -= 1 - elif i == ")" and not status: - result += 1 - -print(abs(result)) - \ No newline at end of file +Given the string ")(", you should return 2, since we must remove all of them.""" \ No newline at end of file diff --git a/google_problems/problem_7.py b/google_problems/problem_7.py index 0a373d2..b46c3fc 100644 --- a/google_problems/problem_7.py +++ b/google_problems/problem_7.py @@ -12,18 +12,4 @@ 8 = max(7, 8, 7) Do this in O(n) time and O(k) space. You can modify the input array in-place and you do not need to store the results. -You can simply print them out as you compute them.""" - - -# the solution functions. -def max_value(arr, k): - start = 0 - end = k - while end <= len(arr): - print(max(arr[start:end])) - end += 1 - start += 1 - - -# testing the solution. -max_value([10, 5, 2, 7, 8, 7], 3) +You can simply print them out as you compute them.""" \ No newline at end of file diff --git a/google_problems/problem_79.py b/google_problems/problem_79.py index 0d6b6ce..49ed928 100644 --- a/google_problems/problem_79.py +++ b/google_problems/problem_79.py @@ -10,49 +10,4 @@ Implement a quack using three stacks and O(1) additional memory, so that the amortized time for any push, pop, or pull operation is O(1) -""" - -class Quack: - def __init__(self): - self.right = [] - self.left = [] - self.buffer = [] - - def push(self, x): - self.left.append(x) - - def pop(self): - if not self.left and not self.right: - raise IndexError('pop from empty quack') - - if not self.left: # Re-balance stacks - size = len(self.right) - # Move half of right stack to buffer - for _ in range(size // 2): - self.buffer.append(self.right.pop()) - # Move remainder of right to left - while self.right: - self.left.append(self.left.pop()) - # Move buffer elements back to right - while self.buffer: - self.right.append(self.buffer.pop()) - - return self.left.pop() - - def pull(self): - if not self.left and not self.right: - raise IndexError('pull from empty quack') - - if not self.right: # Re-balance stacks - size = len(self.left) - # Move half of left stack to buffer - for _ in range(size // 2): - self.buffer.append(self.left.pop()) - # Move remainder of left to right - while self.left: - self.right.append(self.left.pop()) - # Move buffer elements back to left - while self.buffer: - self.left.append(self.buffer.pop()) - - return self.right.pop() \ No newline at end of file +""" \ No newline at end of file diff --git a/google_problems/problem_8.py b/google_problems/problem_8.py index 1dc22fc..271b41f 100644 --- a/google_problems/problem_8.py +++ b/google_problems/problem_8.py @@ -9,43 +9,4 @@ In this example, assume nodes with the same value are the exact same node objects. Do this in O(M + N) time (where M and N are the lengths of the lists) and -constant space.""" - -from data_structure.linked_list import LinkedList, Element - - -# solution to the problem -def main(listOne, listTwo): - l1 = listOne.print_list() - l2 = listTwo.print_list() - for i in l1: - if i in l2: - return i - - return None - - -listOne = LinkedList() -listTwo = LinkedList() - -e1 = Element(99) -e2 = Element(1) -e3 = Element(8) -e4 = Element(10) - -m1 = Element(3) -m2 = Element(7) -m3 = Element(8) -m4 = Element(10) - -listOne.append(e1) -listOne.append(e2) -listOne.append(e3) -listOne.append(e4) - -listTwo.append(m1) -listTwo.append(m2) -listTwo.append(m3) -listTwo.append(m4) - -print(main(listOne,listTwo)) \ No newline at end of file +constant space.""" \ No newline at end of file diff --git a/google_problems/problem_85.py b/google_problems/problem_85.py index bc41c6a..9688839 100644 --- a/google_problems/problem_85.py +++ b/google_problems/problem_85.py @@ -21,4 +21,4 @@ [0, 1, 1, 0], [0, 0, 1, 0]] Return 14. -""" +""" \ No newline at end of file diff --git a/google_problems/problem_86.py b/google_problems/problem_86.py index 0f1c2b1..f75f53b 100644 --- a/google_problems/problem_86.py +++ b/google_problems/problem_86.py @@ -1,4 +1,4 @@ """This problem was asked by Google. Explain the difference between composition and inheritance. In which cases would you use each? -""" +""" \ No newline at end of file diff --git a/google_problems/problem_88.py b/google_problems/problem_88.py index 9a80a59..a8fa4e7 100644 --- a/google_problems/problem_88.py +++ b/google_problems/problem_88.py @@ -15,4 +15,4 @@ Given a directed graph of links between various websites, write a program that calculates each site's page rank. -""" +""" \ No newline at end of file diff --git a/google_problems/problem_89.py b/google_problems/problem_89.py index e30036a..a2994d6 100644 --- a/google_problems/problem_89.py +++ b/google_problems/problem_89.py @@ -5,4 +5,4 @@ For example, given 'waterrfetawx' and a k of 2, you could delete f and x to get 'waterretaw'. -""" +""" \ No newline at end of file diff --git a/grammarly_problems/problem_1.py b/grammarly_problems/problem_1.py index 72f197b..f216e53 100644 --- a/grammarly_problems/problem_1.py +++ b/grammarly_problems/problem_1.py @@ -22,4 +22,4 @@ Using this scheme, Jackson and Jaxen both map to J250. Implement Soundex. -""" +""" \ No newline at end of file diff --git a/linked_in_problems/problem_1.py b/linked_in_problems/problem_1.py index c5afa14..3c998ef 100644 --- a/linked_in_problems/problem_1.py +++ b/linked_in_problems/problem_1.py @@ -4,54 +4,4 @@ A binary search tree is a tree with two children, left and right, and satisfies the constraint that the key in the left child must be less than or -equal to the root and the key in the right child must be greater than or equal to the root.""" - -class Node: - - def __init__(self, data=None): - self.data = data - self.right = None - self.left = None - - -class BinaryTree: - - def __init__(self): - self.root = None - - def insert(self, data): - node = Node(data) - if not self.root: - self.root = node - else: - self.insert_node(data, self.root) - - def insert_node(self, data, node): - if data < node.data: - if node.left: - self.insert_node(data, node.left) - else: - node.left = Node(data) - else: - if node.right: - self.insert_node(data, node.right) - else: - node.right = Node(data) - - def is_valid(self): - return valid_node(self.root) - - def valid_node(self, node): - if node: - if node.left: - if node.left.data > node.data : - return False - else : - return self.valid_node(node.left) - if node.right: - if node.right.data <= node.data: - return False - else : - return self.valid_node(node.left) - else: - return True +equal to the root and the key in the right child must be greater than or equal to the root.""" \ No newline at end of file diff --git a/linked_in_problems/problem_2.py b/linked_in_problems/problem_2.py index 2c7c74f..6128f19 100644 --- a/linked_in_problems/problem_2.py +++ b/linked_in_problems/problem_2.py @@ -13,27 +13,4 @@ - "x 1" - "a -2" - "-" -""" - -def number(data): - first = data[0] - real = False - positive = True - if first == '-': - positive = False - for i in data[1:]: - if i not in [1,2,3,4,5,6,7,8,9,0]: - return "not a number" - if i == ".": - real = True - if positive : - yield "a Positive" - else: - yield "a Negative" - - if real: - return " Real Number" - else : - return " Integer " - - +""" \ No newline at end of file diff --git a/linked_in_problems/problem_3.py b/linked_in_problems/problem_3.py index 1584bcf..fbaf600 100644 --- a/linked_in_problems/problem_3.py +++ b/linked_in_problems/problem_3.py @@ -3,4 +3,4 @@ For example, given the list of points [(0, 0), (5, 4), (3, 1)], the central point (1, 2), and k = 2, return [(0, 0), (3, 1)]. -""" +""" \ No newline at end of file diff --git a/linked_in_problems/problem_4.py b/linked_in_problems/problem_4.py index c8155dc..e41fafe 100644 --- a/linked_in_problems/problem_4.py +++ b/linked_in_problems/problem_4.py @@ -8,4 +8,4 @@ For example, given the linked list 5 -> 1 -> 8 -> 0 -> 3 and k = 3, the solution could be 1 -> 0 -> 5 -> 8 -> 3. -""" +""" \ No newline at end of file diff --git a/linked_in_problems/problem_5.py b/linked_in_problems/problem_5.py index c3144eb..b0d20d9 100644 --- a/linked_in_problems/problem_5.py +++ b/linked_in_problems/problem_5.py @@ -9,4 +9,4 @@ and one possible solution would be 00010111. Create an algorithm that finds a De Bruijn sequence. -""" +""" \ No newline at end of file diff --git a/linked_in_problems/problem_6.py b/linked_in_problems/problem_6.py index 0789f02..7b2f1bc 100644 --- a/linked_in_problems/problem_6.py +++ b/linked_in_problems/problem_6.py @@ -21,4 +21,4 @@ Given an input consisting of brick lengths for each row such as the one above, return the fewest number of bricks that must be cut to create a vertical line. -""" +""" \ No newline at end of file diff --git a/linked_in_problems/problem_7.py b/linked_in_problems/problem_7.py index 17a85b0..bf84e81 100644 --- a/linked_in_problems/problem_7.py +++ b/linked_in_problems/problem_7.py @@ -6,4 +6,4 @@ Determine how many times you would need to apply this operation to ensure that all x's come before all y's. In the preceding example, -it suffices to flip the second and sixth characters, so you should return 2""" +it suffices to flip the second and sixth characters, so you should return 2""" \ No newline at end of file diff --git a/linked_in_problems/problem_8.py b/linked_in_problems/problem_8.py index 4a41404..1d86efd 100644 --- a/linked_in_problems/problem_8.py +++ b/linked_in_problems/problem_8.py @@ -13,4 +13,4 @@ A root node with two children: ((00)(00)) An unbalanced tree with three consecutive left children: ((((00)0)0)0) Given this representation, determine the depth of the tree. -""" +""" \ No newline at end of file diff --git a/mailchimp_problems/problem_1.py b/mailchimp_problems/problem_1.py index 75bdc62..65acc38 100644 --- a/mailchimp_problems/problem_1.py +++ b/mailchimp_problems/problem_1.py @@ -8,4 +8,4 @@ of the buildings with heights 8, 6, and 1 all have an unobstructed view to the west. Can you do this using just one forward pass through the array? -""" +""" \ No newline at end of file diff --git a/microsoft_problems/problem_13.py b/microsoft_problems/problem_13.py index 8939af1..9b64529 100644 --- a/microsoft_problems/problem_13.py +++ b/microsoft_problems/problem_13.py @@ -3,22 +3,4 @@ Implement the singleton pattern with a twist. First, instead of storing one instance, store two instances. And in every even call of getInstance(), return the first instance and in every odd call of getInstance(), return the second instance. -""" - -class Signleton(): - - def __init__(self): - self.instance_one = None - self.instance_two = None - self.counter = 0 - - def get_instance(): - if self.counter == 0: - self.instance_one = Signleton() - self.instance_two = Signleton() - self.counter += 1 - if self.counter %2 == 0: - - return self.instance_one - else: - return self.instance_two \ No newline at end of file +""" \ No newline at end of file diff --git a/microsoft_problems/problem_14.py b/microsoft_problems/problem_14.py index ddd1d33..de565c5 100644 --- a/microsoft_problems/problem_14.py +++ b/microsoft_problems/problem_14.py @@ -5,4 +5,4 @@ How many rounds do you expect to play before only one coin remains? Write a function that, given n, returns the number of rounds you'd expect to play until one coin remains. -""" +""" \ No newline at end of file diff --git a/microsoft_problems/problem_15.py b/microsoft_problems/problem_15.py index 341faf5..c4fa40d 100644 --- a/microsoft_problems/problem_15.py +++ b/microsoft_problems/problem_15.py @@ -17,4 +17,4 @@ return 124 (99 + 25) as: 4 -> 2 -> 1 -""" +""" \ No newline at end of file diff --git a/microsoft_problems/problem_16.py b/microsoft_problems/problem_16.py index aa9655a..ebce731 100644 --- a/microsoft_problems/problem_16.py +++ b/microsoft_problems/problem_16.py @@ -11,4 +11,4 @@ def pop(self, stack_number): def push(self, item, stack_number): pass -""" +""" \ No newline at end of file diff --git a/microsoft_problems/problem_17.py b/microsoft_problems/problem_17.py index 9c65ac5..661c30c 100644 --- a/microsoft_problems/problem_17.py +++ b/microsoft_problems/problem_17.py @@ -5,4 +5,4 @@ Ad-hoc polymorphism Parametric polymorphism Subtype polymorphism -""" +""" \ No newline at end of file diff --git a/microsoft_problems/problem_18.py b/microsoft_problems/problem_18.py index d977c98..b78414d 100644 --- a/microsoft_problems/problem_18.py +++ b/microsoft_problems/problem_18.py @@ -5,4 +5,4 @@ For example, given [5, 10, 15, 20, 25], return the sets {10, 25} and {5, 15, 20}, which has a difference of 5, which is the smallest possible difference. -""" +""" \ No newline at end of file diff --git a/microsoft_problems/problem_19.py b/microsoft_problems/problem_19.py index d183467..61b0d8a 100644 --- a/microsoft_problems/problem_19.py +++ b/microsoft_problems/problem_19.py @@ -5,4 +5,4 @@ Compute the smallest set of points that stabs X. For example, given the intervals [(1, 4), (4, 5), (7, 9), (9, 12)], you should return [4, 9]. -""" +""" \ No newline at end of file diff --git a/microsoft_problems/problem_2.py b/microsoft_problems/problem_2.py index 1a975c0..f7bff5b 100644 --- a/microsoft_problems/problem_2.py +++ b/microsoft_problems/problem_2.py @@ -15,30 +15,4 @@ 2 2 2 -""" - - -def sort(array): - for i in range(len(array)): - for j in range(i + 1, len(array)): - if array[i] > array[j]: - c = array[i] - array[i] = array[j] - array[j] = c - return array - - -def median(array): - new_list = sort(array) - if len(new_list) % 2 == 1: - return new_list[(len(new_list) // 2)] - else: - return (new_list[(len(new_list) // 2) - 1] + new_list[(len(new_list) // 2)]) / 2 - - -def main(array): - for i in range(len(array)): - print(median(array[0:i + 1])) - - -main([2, 1, 5, 7, 2, 0, 5]) +""" \ No newline at end of file diff --git a/microsoft_problems/problem_20.py b/microsoft_problems/problem_20.py index d429f79..69d08e1 100644 --- a/microsoft_problems/problem_20.py +++ b/microsoft_problems/problem_20.py @@ -3,4 +3,4 @@ Given a string and a pattern, find the starting indices of all occurrences of the pattern in the string. For example, given the string "abracadabra" and the pattern "abr", you should return [0, 7]. -""" +""" \ No newline at end of file diff --git a/microsoft_problems/problem_21.py b/microsoft_problems/problem_21.py index c4797b8..16d032f 100644 --- a/microsoft_problems/problem_21.py +++ b/microsoft_problems/problem_21.py @@ -3,4 +3,4 @@ Recall that the minimum spanning tree is the subset of edges of a tree that connect all its vertices with the smallest possible total edge weight. Given an undirected graph with weighted edges, compute the maximum weight spanning tree. -""" +""" \ No newline at end of file diff --git a/microsoft_problems/problem_22.py b/microsoft_problems/problem_22.py index 6f81745..f60ea8f 100644 --- a/microsoft_problems/problem_22.py +++ b/microsoft_problems/problem_22.py @@ -18,4 +18,4 @@ [0, 0, 1, 0] [0, 0, 0, 1] Given a graph, find its transitive closure. -""" +""" \ No newline at end of file diff --git a/microsoft_problems/problem_23.py b/microsoft_problems/problem_23.py index 3b7a185..7cd5add 100644 --- a/microsoft_problems/problem_23.py +++ b/microsoft_problems/problem_23.py @@ -13,4 +13,4 @@ For example, given the string .L.R....L, you should return LL.RRRLLL. Given the string ..R...L.L, you should return ..RR.LLLL. -""" +""" \ No newline at end of file diff --git a/microsoft_problems/problem_24.py b/microsoft_problems/problem_24.py index 5700b31..da92d85 100644 --- a/microsoft_problems/problem_24.py +++ b/microsoft_problems/problem_24.py @@ -4,4 +4,4 @@ hour and the minute hands. Bonus: When, during the course of a day, will the angle be zero? -""" +""" \ No newline at end of file diff --git a/microsoft_problems/problem_25.py b/microsoft_problems/problem_25.py index 5d9402e..20819f2 100644 --- a/microsoft_problems/problem_25.py +++ b/microsoft_problems/problem_25.py @@ -8,4 +8,4 @@ 3 3 / \ / \ 1 2 2 1 -""" +""" \ No newline at end of file diff --git a/microsoft_problems/problem_26.py b/microsoft_problems/problem_26.py index c776bd0..f3b2ad1 100644 --- a/microsoft_problems/problem_26.py +++ b/microsoft_problems/problem_26.py @@ -3,4 +3,4 @@ Given an array of numbers and a number k, determine if there are three entries in the array which add up to the specified number k. For example, given [20, 303, 3, 4, 25] and k = 49, return true as 20 + 4 + 25 = 49. -""" +""" \ No newline at end of file diff --git a/microsoft_problems/problem_27.py b/microsoft_problems/problem_27.py index 63dd459..9536042 100644 --- a/microsoft_problems/problem_27.py +++ b/microsoft_problems/problem_27.py @@ -16,4 +16,4 @@ 3 <- median of [3, 3, 1] Recall that the median of an even-sized list is the average of the two middle numbers. -""" +""" \ No newline at end of file diff --git a/microsoft_problems/problem_28.py b/microsoft_problems/problem_28.py index a028d06..30dfe5c 100644 --- a/microsoft_problems/problem_28.py +++ b/microsoft_problems/problem_28.py @@ -14,4 +14,4 @@ xyz Note that zx is not a valid subsequence since it is not in the order of the given string. -""" +""" \ No newline at end of file diff --git a/microsoft_problems/problem_29.py b/microsoft_problems/problem_29.py index eb33744..066a4f8 100644 --- a/microsoft_problems/problem_29.py +++ b/microsoft_problems/problem_29.py @@ -19,4 +19,4 @@ [(1, 4), (4, 7), (8, 11)] -""" +""" \ No newline at end of file diff --git a/microsoft_problems/problem_6.py b/microsoft_problems/problem_6.py index 05eec6c..1f4c3d9 100644 --- a/microsoft_problems/problem_6.py +++ b/microsoft_problems/problem_6.py @@ -9,36 +9,4 @@ and the target word 'FOAM', you should return true, since it's the leftmost column. Similarly, given the target word 'MASS', you should return true, since it's the last row. -""" - -matrix = [['F', 'A', 'C', 'I'], - ['O', 'B', 'Q', 'P'], - ['A', 'N', 'O', 'B'], - ['M', 'A', 'S', 'S']] - -requested_word = "IPBS" - - -def get_all_words(): - result = [] - current = "" - for i in range(len(matrix)): - for j in range(len(matrix[0])): - current += matrix[i][j] - result.append(current) - current = "" - for i in range(len(matrix)): - for j in range(len(matrix[0])): - current += matrix[j][i] - result.append(current) - current = "" - return result - - -words = get_all_words() - - -if requested_word in words: - print(True) -else: - print(False) +""" \ No newline at end of file diff --git a/microsoft_problems/problem_7.py b/microsoft_problems/problem_7.py index 84de5bc..1ca2220 100644 --- a/microsoft_problems/problem_7.py +++ b/microsoft_problems/problem_7.py @@ -1,30 +1,4 @@ """This problem was asked by Microsoft. A number is considered perfect if its digits sum up to exactly 10. Given a positive integer n, return the n-th perfect number. -For example, given 1, you should return 19. Given 2, you should return 28""" - - -def make_perfect(number): - value = 10 - int(sum_number(str(number))) - if value == 0: - return number - else: - return str(number) +str(value) - - -def sum_number(number): - if int(number) <= 10: - return int(number) - else: - return sum_number(count(number)) - - -def count(number): - arr = str(number) - r = 0 - for i in arr: - r += int(i) - return str(r) - - -print(make_perfect(22133)) +For example, given 1, you should return 19. Given 2, you should return 28""" \ No newline at end of file diff --git a/morgan_stanley_problems/problem_1.py b/morgan_stanley_problems/problem_1.py index 15ccd5c..386989b 100644 --- a/morgan_stanley_problems/problem_1.py +++ b/morgan_stanley_problems/problem_1.py @@ -14,4 +14,4 @@ / \ / \ 4 5 6 7 You should return [1, 3, 2, 4, 5, 6, 7]. -""" +""" \ No newline at end of file diff --git a/nest_problems/problem_1.py b/nest_problems/problem_1.py index a26634e..03d67f6 100644 --- a/nest_problems/problem_1.py +++ b/nest_problems/problem_1.py @@ -11,4 +11,4 @@ or terminal marks (.,?,!,‽). 3- There must be a single space between each word. 4- The sentence must end with a terminal mark immediately following a word. -""" +""" \ No newline at end of file diff --git a/netflix_problems/problem_1.py b/netflix_problems/problem_1.py index 8c58912..89786ba 100644 --- a/netflix_problems/problem_1.py +++ b/netflix_problems/problem_1.py @@ -4,4 +4,4 @@ list without performing any multiplication, division, or bit-shift operations. Do this in O(log N) time -""" +""" \ No newline at end of file diff --git a/netflix_problems/problem_2.py b/netflix_problems/problem_2.py index 46076b3..69f9020 100644 --- a/netflix_problems/problem_2.py +++ b/netflix_problems/problem_2.py @@ -2,4 +2,4 @@ Given an array of integers, determine whether it contains a Pythagorean triplet. Recall that a Pythogorean triplet (a, b, c) is defined by the equation a2+ b2= c2. -""" +""" \ No newline at end of file diff --git a/netflix_problems/problem_3.py b/netflix_problems/problem_3.py index aa372ff..2898008 100644 --- a/netflix_problems/problem_3.py +++ b/netflix_problems/problem_3.py @@ -12,4 +12,4 @@ / \ 3 6 Given a sequence S, construct the corresponding Cartesian tree. -""" +""" \ No newline at end of file diff --git a/netflix_problems/problem_4.py b/netflix_problems/problem_4.py index b84ebad..d2e688d 100644 --- a/netflix_problems/problem_4.py +++ b/netflix_problems/problem_4.py @@ -3,4 +3,4 @@ Implement a queue using a set of fixed-length arrays. The queue should support enqueue, dequeue, and get_size operations. -""" +""" \ No newline at end of file diff --git a/nvidia_problems/problem_1.py b/nvidia_problems/problem_1.py index aa72432..f397859 100644 --- a/nvidia_problems/problem_1.py +++ b/nvidia_problems/problem_1.py @@ -7,4 +7,4 @@ Determine if a new point p lies inside this polygon. (If p is on the boundary of the polygon, you should return False). -""" +""" \ No newline at end of file diff --git a/nvidia_problems/problem_2.py b/nvidia_problems/problem_2.py index 25510be..693ef3d 100644 --- a/nvidia_problems/problem_2.py +++ b/nvidia_problems/problem_2.py @@ -2,4 +2,4 @@ Find the maximum of two numbers without using any if-else statements, branching, or direct comparisons. -""" +""" \ No newline at end of file diff --git a/oracle_problems/problem_1.py b/oracle_problems/problem_1.py index 25184d6..a32a4e5 100644 --- a/oracle_problems/problem_1.py +++ b/oracle_problems/problem_1.py @@ -5,4 +5,4 @@ For a given input N, find the smallest sparse number greater than or equal to N. Do this in faster than O(N log N) time. -""" +""" \ No newline at end of file diff --git a/oracle_problems/problem_2.py b/oracle_problems/problem_2.py index e9a478b..d81fa36 100644 --- a/oracle_problems/problem_2.py +++ b/oracle_problems/problem_2.py @@ -18,4 +18,4 @@ ........ .....Q.. You should return True, since the bishop is attacking the king diagonally. -""" +""" \ No newline at end of file diff --git a/oracle_problems/problem_3.py b/oracle_problems/problem_3.py index 65f9b86..88e012d 100644 --- a/oracle_problems/problem_3.py +++ b/oracle_problems/problem_3.py @@ -5,4 +5,4 @@ while the ceiling is the lowest element in the tree greater than or equal to an integer. If either value does not exist, return None. -""" +""" \ No newline at end of file diff --git a/others_problems/problem_1.py b/others_problems/problem_1.py index 67ae0b4..3420c93 100644 --- a/others_problems/problem_1.py +++ b/others_problems/problem_1.py @@ -18,4 +18,4 @@ Move 2 to 1 Move 2 to 3 Move 1 to 3 -""" +""" \ No newline at end of file diff --git a/others_problems/problem_2.py b/others_problems/problem_2.py index 91bfb09..6c97930 100644 --- a/others_problems/problem_2.py +++ b/others_problems/problem_2.py @@ -1,5 +1,2 @@ """Given a real number n, find the square root of n. For example, given n = 9, return 3. -""" - -def square_root(x): - return x**(1/2) \ No newline at end of file +""" \ No newline at end of file diff --git a/quantcast_problems/problem_1.py b/quantcast_problems/problem_1.py index aed3099..aa0b314 100644 --- a/quantcast_problems/problem_1.py +++ b/quantcast_problems/problem_1.py @@ -9,4 +9,4 @@ For example, suppose the input is ['F', '|', 'T', '&', 'T']. In this case, there are two acceptable groupings: (F | T) & T and F | (T & T). -""" +""" \ No newline at end of file diff --git a/quantcast_problems/problem_2.py b/quantcast_problems/problem_2.py index c657af2..0a39176 100644 --- a/quantcast_problems/problem_2.py +++ b/quantcast_problems/problem_2.py @@ -10,4 +10,4 @@ For example, suppose the input is ['F', '|', 'T', '&', 'T']. In this case, there are two acceptable groupings: (F | T) & T and F | (T & T). -""" +""" \ No newline at end of file diff --git a/riot_games_problems/problem_1.py b/riot_games_problems/problem_1.py index 33256d1..ce76c94 100644 --- a/riot_games_problems/problem_1.py +++ b/riot_games_problems/problem_1.py @@ -5,4 +5,4 @@ • total(): returns the total number of hits recorded • range(lower, upper): returns the number of hits that occurred between timestamps lower and upper (inclusive) Follow-up: What if our system has limited memory? -""" +""" \ No newline at end of file diff --git a/robinhood_problems/problem_1.py b/robinhood_problems/problem_1.py index 87b512e..8194526 100644 --- a/robinhood_problems/problem_1.py +++ b/robinhood_problems/problem_1.py @@ -10,4 +10,4 @@ [['eat', 'ate', 'tea'], ['apt', 'pat'], ['now']] -""" +""" \ No newline at end of file diff --git a/robinhood_problems/problem_2.py b/robinhood_problems/problem_2.py index 1c6e584..dd5c662 100644 --- a/robinhood_problems/problem_2.py +++ b/robinhood_problems/problem_2.py @@ -4,4 +4,4 @@ For example, for n = 9, you should return 3 since the lists are: [2, 3, 4], [4, 5], and [9]. Can you do it in linear time? -""" +""" \ No newline at end of file diff --git a/snapchat_problems/problem_1.py b/snapchat_problems/problem_1.py index 670888a..4a01422 100644 --- a/snapchat_problems/problem_1.py +++ b/snapchat_problems/problem_1.py @@ -2,38 +2,4 @@ Given an array of time intervals (start, end) for classroom lectures (possibly overlapping), find the minimum number of rooms required. For example, given [(30, 75), (0, 50), (60, 150)], you should return 2. -""" - - -def find_small(array): - mina = array[0][0] - index = 0 - for ind in range(len(array)): - if array[ind][0] < mina: - index = ind - return index - - -def big_all(value, array): - result = True - for i in array: - if i > value: - result = False - return result - - -intervals = [(30, 75), (0, 50), (60, 150)] - -mins = [] -for i in intervals: - if len(mins) == 0: - var = find_small(intervals) - mins.append(intervals[var][1]) - del intervals[var] - else: - var = find_small(intervals) - if not big_all(intervals[var][0], mins): - mins.append(intervals[var][0]) - del intervals[var] - -print(len(mins)) +""" \ No newline at end of file diff --git a/snapchat_problems/problem_2.py b/snapchat_problems/problem_2.py index 318739c..a0c447a 100644 --- a/snapchat_problems/problem_2.py +++ b/snapchat_problems/problem_2.py @@ -4,28 +4,4 @@ The input list is not necessarily ordered in any way. For example, given [(1, 3), (5, 8), (4, 10), (20, 25)], you should return [(1, 3), (4, 10), (20, 25)] -""" - - -def find_small(array): - mina = array[0][0] - index = 0 - for ind in range(len(array)): - if array[ind][0] < mina: - index = ind - return index - - -intervals = [(1, 3), (5, 8), (4, 10), (20, 25)] - -result = [intervals[find_small(intervals)]] -min = intervals[find_small(intervals)][1] - -for i in range(len(intervals)): - var = find_small(intervals) - if intervals[var][0] > min: - min = intervals[var][1] - result.append(intervals[var]) - del intervals[var] - -print(result) +""" \ No newline at end of file diff --git a/square_problems/problem_1.py b/square_problems/problem_1.py index a743de2..f8bc776 100644 --- a/square_problems/problem_1.py +++ b/square_problems/problem_1.py @@ -3,4 +3,4 @@ a probability that's not 50-50 (but also not 0-100 or 100-0). You do not know the bias of the coin. Write a function to simulate an unbiased coin toss. -""" +""" \ No newline at end of file diff --git a/square_problems/problem_2.py b/square_problems/problem_2.py index f5bf16f..8beecb3 100644 --- a/square_problems/problem_2.py +++ b/square_problems/problem_2.py @@ -2,4 +2,4 @@ Given a string and a set of characters, return the shortest substring containing all the characters in the set. For example, given the string "figehaeci" and the set of characters {a, e, i}, you should return "aeci". If there is no substring containing all the characters in the set, return null. -""" +""" \ No newline at end of file diff --git a/square_problems/problem_3.py b/square_problems/problem_3.py index b8c0a54..ead6021 100644 --- a/square_problems/problem_3.py +++ b/square_problems/problem_3.py @@ -11,4 +11,4 @@ • app • apr • f -""" +""" \ No newline at end of file diff --git a/square_problems/problem_4.py b/square_problems/problem_4.py index 7c3bf22..d876a0a 100644 --- a/square_problems/problem_4.py +++ b/square_problems/problem_4.py @@ -8,4 +8,4 @@ Write a program that returns the maximum amount of money you can win with certainty, if you move first, assuming your opponent plays optimally. -""" +""" \ No newline at end of file diff --git a/square_problems/problem_5.py b/square_problems/problem_5.py index fa0fc07..bc709cd 100644 --- a/square_problems/problem_5.py +++ b/square_problems/problem_5.py @@ -13,4 +13,4 @@ Implement this algorithm. Bonus: Create a generator that produces primes indefinitely (that is, without taking N as an input). -""" +""" \ No newline at end of file diff --git a/square_problems/problem_6.py b/square_problems/problem_6.py index 02775e2..c4ab8ab 100644 --- a/square_problems/problem_6.py +++ b/square_problems/problem_6.py @@ -18,4 +18,4 @@ (4, 0): 10 } In this case, the shortest valid path would be 0 -> 2 -> 4 -> 0, with a distance of 28. -""" +""" \ No newline at end of file diff --git a/square_problems/problem_7.py b/square_problems/problem_7.py index 871bca0..79a8fb2 100644 --- a/square_problems/problem_7.py +++ b/square_problems/problem_7.py @@ -13,4 +13,4 @@ Determine the area of the largest rectangle that can be formed only from the bars of the histogram. For the diagram above, for example, this would be six, representing the 2 x 3 area at the bottom right. -""" +""" \ No newline at end of file diff --git a/squarespace_problems/problem_1.py b/squarespace_problems/problem_1.py index bbfa952..c0ea58d 100644 --- a/squarespace_problems/problem_1.py +++ b/squarespace_problems/problem_1.py @@ -8,4 +8,4 @@ add_subtract(1)(2)(3) -> 1 + 2 - 3 -> 0 add_subtract(-5)(10)(3)(9) -> -5 + 10 - 3 + 9 -> 11 -""" +""" \ No newline at end of file diff --git a/stitch_fix_problems/problem_1.py b/stitch_fix_problems/problem_1.py index c144ca2..89db63c 100644 --- a/stitch_fix_problems/problem_1.py +++ b/stitch_fix_problems/problem_1.py @@ -14,4 +14,4 @@ Given an input k, return the kth row of Pascal's triangle. Bonus: Can you do this using only O(k) space? -""" +""" \ No newline at end of file diff --git a/stripe_problems/problem_1.py b/stripe_problems/problem_1.py index 9eb6c08..a01b3e6 100644 --- a/stripe_problems/problem_1.py +++ b/stripe_problems/problem_1.py @@ -7,45 +7,4 @@ For example, the input [3, 4, -1, 1] should give 2. The input [1, 2, 0] should give 3. -You can modify the input array in-place.""" - - -# function to the main problem -def find_missing_value(array): - # sorting the array - array.sort() - - # if the array is empty we return 1 since 1 is the first positive number. - if len(array) == 0: - return 1 - - # otherwise we search. - else: - - # take the first of the array. - k = array[0] - # if k is bigger than two then we should return 1 since the array is sorted. - if k >= 2: - return 1 - # otherwise we search for it - for i in range(len(array)): - # if k is not like the current element than this k is missing number. - if k != array[i]: - return k - - # if not the case we increment k. - k += 1 - - # if we increment and k is 0 we re-increment it. - if k == 0: - k += 1 - # finally return k if the number was out the range of the array. - # like [1,2,3] here the missing is 4. - return k - - -# testing the code -print(find_missing_value([3, 4, -1, 1])) -print(find_missing_value([1, 2, 0])) -print(find_missing_value([1, 2, 3, 4, 6])) -print(find_missing_value([])) +You can modify the input array in-place.""" \ No newline at end of file diff --git a/sumo_logic_problems/problem_1.py b/sumo_logic_problems/problem_1.py index 93e2026..bbe8d35 100644 --- a/sumo_logic_problems/problem_1.py +++ b/sumo_logic_problems/problem_1.py @@ -4,4 +4,4 @@ An element is considered a peak if it is greater than both its left and right neighbors. It is guaranteed that the first and last elements are lower than all others. -""" +""" \ No newline at end of file diff --git a/triple_byte_problems/problem_1.py b/triple_byte_problems/problem_1.py index 3a9ad88..8377256 100644 --- a/triple_byte_problems/problem_1.py +++ b/triple_byte_problems/problem_1.py @@ -4,4 +4,4 @@ For example, given the numbers [1, 2, 3, 4] and probabilities [0.1, 0.5, 0.2, 0.2], your function should return 1 10% of the time, 2 50% of the time, and 3 and 4 20% of the time. You can generate random numbers between 0 and 1 uniformly. -""" +""" \ No newline at end of file diff --git a/triple_byte_problems/problem_2.py b/triple_byte_problems/problem_2.py index 4311745..3379c52 100644 --- a/triple_byte_problems/problem_2.py +++ b/triple_byte_problems/problem_2.py @@ -8,4 +8,4 @@ The check method may return occasional false positives (in other words, incorrectly identifying an element as part of the set), but should always correctly identify a true element. -""" +""" \ No newline at end of file diff --git a/twitch_problems/problem_1.py b/twitch_problems/problem_1.py index 92d1d9d..3a27de5 100644 --- a/twitch_problems/problem_1.py +++ b/twitch_problems/problem_1.py @@ -1,4 +1,4 @@ """This problem was asked by Twitch. Describe what happens when you type a URL into your browser and press Enter. -""" +""" \ No newline at end of file diff --git a/twitter_problems/problem_1.py b/twitter_problems/problem_1.py index 0ed87aa..b3dd373 100644 --- a/twitter_problems/problem_1.py +++ b/twitter_problems/problem_1.py @@ -5,17 +5,4 @@ that have s as a prefix. For example, given the query string de and the set of -strings [dog, deer, deal], return [deer, deal].""" - - -# traditional solution -_- -def auto_complete(arr, string): - result = [] - for s in arr: - if len(s) >= len(string): - if string == s[:len(string)]: result.append(s) - return result - - -# test -print(auto_complete(['dog', 'deer', 'deal'], 'de')) +strings [dog, deer, deal], return [deer, deal].""" \ No newline at end of file diff --git a/uber_problems/problem_1.py b/uber_problems/problem_1.py index e2e883c..0cb2e84 100644 --- a/uber_problems/problem_1.py +++ b/uber_problems/problem_1.py @@ -3,67 +3,4 @@ of the new array is the product of all the numbers in the original array except the one at i.For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output -would be [2, 3, 6]. Follow-up: what if you can't use division? """ - - -# function to get the product of elements inside an array. -def get_array_product(array): - product = 1 - for i in array: - product *= i - return product - - -# this function does the main job of the problem but with division. -def get_new_array_division(array): - # array holding the final result - new_array = [] - # getting the product of the array elements. - product = get_array_product(array) - - for i in array: - # for each number divide the product by the current element. - new_array.append(product / i) - - # return the array - return new_array - - -# this function does the main job of the problem without division.(a to -1 power) -def get_new_array(array): - # array holding the final result - new_array = [] - # getting the product of the array elements. - product = get_array_product(array) - - for i in array: - # for each number product the product by the current element. - new_array.append(product * (i ** -1)) - - # return the array - return new_array - - -# This function does the main job of the problem without division.(a to -1 power) -def get_new_array_hard(array): - # array holding the final result - new_array = [] - - for i in range(len(array)): - p = 1 - for j in range(len(array)): - if j != i: - p *= array[j] - new_array.append(p) - - # return the array - return new_array - - -# test the functions. -# print(get_new_array_division([1, 2, 3, 4, 5])) -# print(get_new_array_division([3, 2, 1])) -# print(get_new_array([1, 2, 3, 4, 5])) -# print(get_new_array([3, 2, 1])) -print(get_new_array_hard([1, 2, 3, 4, 5])) -print(get_new_array_hard([3, 2, 1])) +would be [2, 3, 6]. Follow-up: what if you can't use division? """ \ No newline at end of file diff --git a/wework_problems/problem_1.py b/wework_problems/problem_1.py index 741f065..9bcfc57 100644 --- a/wework_problems/problem_1.py +++ b/wework_problems/problem_1.py @@ -5,4 +5,4 @@ For example, given an array of [1, 5, 10] and an amount 56, return 7 since we can use 5 dimes, 1 nickel, and 1 penny. Given an array of [5, 8] and an amount 15, return 3 since we can use 5 5-cent coins. -""" +""" \ No newline at end of file From ec38e4e18c56b78789a9287bf01a558e9b9141cc Mon Sep 17 00:00:00 2001 From: "@saadaoui-salah" Date: Mon, 17 May 2021 06:15:10 -0700 Subject: [PATCH 03/14] completed --- google_problems/problem_1.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/google_problems/problem_1.py b/google_problems/problem_1.py index 51357c7..34aa343 100644 --- a/google_problems/problem_1.py +++ b/google_problems/problem_1.py @@ -1,4 +1,24 @@ """ This problem was recently asked by Google. Given a list of numbers and a number k, return whether any two numbers from the list add up to k. For example, given [10, 15, 3, 7] and k of 17, return true -since 10 + 7 is 17. Bonus: Can you do this in one pass? """ \ No newline at end of file +since 10 + 7 is 17. Bonus: Can you do this in one pass? """ + +# BRUT FORCE SOLUTION +# +# arr = [10, 15, 3, 7] +# for i in arr: +# for j in arr: +# if i + j == 17: +# print(True) + +# ONE PASS SOLUTION +def check(arr, num): + for i in arr: + needed_num = num - i + if needed_num in arr: + return True + +if __name__ == '__main__': + arr = [10, 15, 3, 7] + checked = check(arr,17) + print(checked) \ No newline at end of file From 866af0bcb2df214740c9b0b0bdb47ef84f354519 Mon Sep 17 00:00:00 2001 From: "@saadaoui-salah" Date: Tue, 18 May 2021 13:52:57 -0700 Subject: [PATCH 04/14] first try --- google_problems/problem_4.py | 61 +++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/google_problems/problem_4.py b/google_problems/problem_4.py index 56d6a21..5e31e64 100644 --- a/google_problems/problem_4.py +++ b/google_problems/problem_4.py @@ -13,4 +13,63 @@ 1 0 / \ 1 1 -""" \ No newline at end of file +/\ / \ +0 1 1 0 + / \ + 1 0 +""" + +# FRIST WE NEED TO EMPLEMENT A TREE + +class Tree: + def __init__(self, data): + self.data = data + self.right = None + self.left = None + self.root = None + + def add_child(self, data): + if self.right and data > self.right.data: + if self.left is None: + new_tree = Tree(data) + new_tree.root = self.left + self.left = new_tree + return self.left + self.left.add_child(data) + + if self.right is None: + new_tree = Tree(data) + new_tree.root = self.right + self.right = new_tree + return self.right + self.right.add_child(data) + + def count_sub_trees(self): + right_node = self.right is None + left_node = self.left is None + if self.root is None : + return 0 + if right_node and left_node: + return 1 + if not right_node and not left_node: + root = self.root + parents = 0 + while root.root is not None: + parents += 1 + return self.right.count_sub_trees() + self.left.count_sub_tree() + + + + +def emplement_tree(): + tree = Tree(0) + tree.add_child(0) + tree.add_child(1) + tree.add_child(0) + tree.add_child(1) + tree.add_child(1) + tree.add_child(1) + print(tree.count_sub_trees()) + +if __name__ == "__main__": + emplement_tree() \ No newline at end of file From a7639ca9e62502beda478989b37196baa2f51284 Mon Sep 17 00:00:00 2001 From: "@saadaoui-salah" Date: Wed, 19 May 2021 13:36:44 -0700 Subject: [PATCH 05/14] second try --- google_problems/problem_4.py | 78 ++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 38 deletions(-) diff --git a/google_problems/problem_4.py b/google_problems/problem_4.py index 5e31e64..dc35cc2 100644 --- a/google_problems/problem_4.py +++ b/google_problems/problem_4.py @@ -6,70 +6,72 @@ For example, the following tree has 5 unival subtrees: - 0 - / \ - 1 0 - / \ - 1 0 - / \ - 1 1 -/\ / \ -0 1 1 0 - / \ - 1 0 + 0 + / \ + 1 0 + /\ / \ + 1 0 1 0 + / \ + 1 1 """ # FRIST WE NEED TO EMPLEMENT A TREE -class Tree: +class BSTNode: def __init__(self, data): self.data = data self.right = None self.left = None - self.root = None def add_child(self, data): - if self.right and data > self.right.data: + if self.data > data: + if self.right is None: + self.right = BSTNode(data) + return + else: + self.right.add_child(data) + else: if self.left is None: - new_tree = Tree(data) - new_tree.root = self.left - self.left = new_tree - return self.left - self.left.add_child(data) - - if self.right is None: - new_tree = Tree(data) - new_tree.root = self.right - self.right = new_tree - return self.right - self.right.add_child(data) + self.left = BSTNode(data) + return + else: + self.left.add_child(data) - def count_sub_trees(self): + def count_sub_trees(self, k=0): right_node = self.right is None left_node = self.left is None - if self.root is None : - return 0 if right_node and left_node: - return 1 - if not right_node and not left_node: - root = self.root - parents = 0 - while root.root is not None: - parents += 1 - return self.right.count_sub_trees() + self.left.count_sub_tree() + return k + if not right_node and left_node: + return k + self.right.count_sub_trees(k) + if right_node or not left_node: + return k + self.left.count_sub_trees(k) + if not right_node and not left_node: + return self.left.count_sub_trees(k) + self.right.count_sub_trees(k) + k + def nodes(self, arr=[]): + if self.left: + arr.append(self.data) + self.left.nodes(arr) + if self.right: + arr.append(self.data) + self.right.nodes(arr) + arr = arr.append(self.data) + return arr + + def emplement_tree(): - tree = Tree(0) + tree = BSTNode(0) tree.add_child(0) tree.add_child(1) tree.add_child(0) tree.add_child(1) tree.add_child(1) tree.add_child(1) - print(tree.count_sub_trees()) + print(tree.nodes()) if __name__ == "__main__": emplement_tree() \ No newline at end of file From 4f3c9716d3cde1063df65e212ae456c02c46b84b Mon Sep 17 00:00:00 2001 From: "@saadaoui-salah" Date: Wed, 19 May 2021 13:51:40 -0700 Subject: [PATCH 06/14] third try --- google_problems/problem_4.py | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/google_problems/problem_4.py b/google_problems/problem_4.py index dc35cc2..92c9ee5 100644 --- a/google_problems/problem_4.py +++ b/google_problems/problem_4.py @@ -37,27 +37,33 @@ def add_child(self, data): else: self.left.add_child(data) - def count_sub_trees(self, k=0): + def in_order(self): + elements = [] + if self.left: + elements += self.left.in_order() + elements.append(self.data) + if self.right: + elements += self.right.in_order() + return elements + + + + + def count_sub_trees(self): right_node = self.right is None left_node = self.left is None + k = 0 if right_node and left_node: return k if not right_node and left_node: - return k + self.right.count_sub_trees(k) + k += 1 + self.right.count_sub_trees() if right_node or not left_node: - return k + self.left.count_sub_trees(k) + k += 1 + self.left.count_sub_trees() if not right_node and not left_node: - return self.left.count_sub_trees(k) + self.right.count_sub_trees(k) + k - - def nodes(self, arr=[]): - if self.left: - arr.append(self.data) - self.left.nodes(arr) - if self.right: - arr.append(self.data) - self.right.nodes(arr) - arr = arr.append(self.data) - return arr + k += 2 + self.left.count_sub_trees() + self.right.count_sub_trees() @@ -71,7 +77,7 @@ def emplement_tree(): tree.add_child(1) tree.add_child(1) tree.add_child(1) - print(tree.nodes()) + print(tree.count_sub_trees()) if __name__ == "__main__": emplement_tree() \ No newline at end of file From 578ecf17bb4f95bb264efce7fea486b0e3ffc6bf Mon Sep 17 00:00:00 2001 From: "@saadaoui-salah" Date: Sat, 22 May 2021 06:44:04 -0700 Subject: [PATCH 07/14] solved --- google_problems/problem_4.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/google_problems/problem_4.py b/google_problems/problem_4.py index 92c9ee5..5773a0d 100644 --- a/google_problems/problem_4.py +++ b/google_problems/problem_4.py @@ -52,18 +52,13 @@ def in_order(self): def count_sub_trees(self): right_node = self.right is None left_node = self.left is None - k = 0 - if right_node and left_node: - return k - if not right_node and left_node: - k += 1 - self.right.count_sub_trees() - if right_node or not left_node: - k += 1 - self.left.count_sub_trees() - if not right_node and not left_node: - k += 2 - self.left.count_sub_trees() + self.right.count_sub_trees() + if not right_node and not left_node: + return 1 +self.left.count_sub_trees() + self.right.count_sub_trees() + if not right_node : + return 1 + self.right.count_sub_trees() + if not left_node: + return 1+ self.left.count_sub_trees() + return 0 From 2285d1602d0918ae23c90e7962263a97ea8aa78a Mon Sep 17 00:00:00 2001 From: "@saadaoui-salah" Date: Mon, 24 May 2021 10:50:37 -0700 Subject: [PATCH 08/14] SOLVED --- google_problems/problem_2.py | 49 +++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/google_problems/problem_2.py b/google_problems/problem_2.py index 6f6741c..fa6023a 100644 --- a/google_problems/problem_2.py +++ b/google_problems/problem_2.py @@ -13,4 +13,51 @@ def __init__(self, val, left=None, right=None): The following test should pass: node = Node('root', Node('left', Node('left.left')), Node('right')) -assert deserialize(serialize(node)).left.left.val == 'left.left'""" \ No newline at end of file +assert deserialize(serialize(node)).left.left.val == 'left.left'""" + + +class Node: + def __init__(self, val, left=None, right=None): + self.val = val + self.left = left + self.right = right + + def add_child(self, data): + if data.split('.')[-1] == 'left': + if self.left: + self.left.add_child(data) + return + self.left = Node(val=data) + return + if self.right: + self.right.add_child(data) + return + self.right = Node(val=data) + return + def __str__(self): + return self.val + + +def serialize(node): + if node.left and node.right: + return node.val +',' + serialize(node.right) + ',' + serialize(node.left) + ',' + if node.right: + return node.val + ',' + serialize(node.right) + ',' + if node.left: + return node.val + ',' + serialize(node.left) + ',' + return node.val + +node = Node('root', Node('left', Node('left.left')), Node('right')) +print(serialize(node)) + +def deserialize(tree): + tree = tree.split(',') + node = Node(tree[0]) + for element in tree[1:]: + if element != '': + node.add_child(element) + + return node + +assert deserialize(serialize(node)).left.left.val == 'left.left' +assert deserialize(serialize(node)).val == 'root' From 42267db76771670cfbd21775e69631c8ea20888f Mon Sep 17 00:00:00 2001 From: "@saadaoui-salah" Date: Wed, 26 May 2021 09:10:07 -0700 Subject: [PATCH 09/14] first try --- google_problems/problem_15.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/google_problems/problem_15.py b/google_problems/problem_15.py index c322ee0..52fc934 100644 --- a/google_problems/problem_15.py +++ b/google_problems/problem_15.py @@ -6,4 +6,18 @@ For example, given [6, 1, 3, 3, 3, 6, 6], return 1. Given [13, 19, 13, 13], return 19. -Do this in O(N) time and O(1) space.""" \ No newline at end of file +Do this in O(N) time and O(1) space.""" + +# FIsrt try +arr = [6, 1, 3, 3, 3, 6, 6] +def get_duplicated(arr=arr): + dic = [] + for i in arr: + if i in dic: + dic.remove(i) + else: + dic.append(i) + return dic + +arr = get_duplicated() +print(arr) \ No newline at end of file From ec88e471e4d9a4a72a50c906b4c9eb0498e58b69 Mon Sep 17 00:00:00 2001 From: "@saadaoui-salah" Date: Fri, 4 Jun 2021 15:18:57 -0700 Subject: [PATCH 10/14] solved --- google_problems/problem_15.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/google_problems/problem_15.py b/google_problems/problem_15.py index 52fc934..941a7d3 100644 --- a/google_problems/problem_15.py +++ b/google_problems/problem_15.py @@ -8,16 +8,17 @@ Do this in O(N) time and O(1) space.""" -# FIsrt try + arr = [6, 1, 3, 3, 3, 6, 6] def get_duplicated(arr=arr): - dic = [] - for i in arr: - if i in dic: - dic.remove(i) + for num in arr: + i = arr.index(num) + if num in arr[0:i] or num in arr[i+1::]: + continue else: - dic.append(i) - return dic + return num arr = get_duplicated() +print(arr) +arr = get_duplicated([13, 19, 13, 13]) print(arr) \ No newline at end of file From fc487bb7a07cfcc2676811b97e30ccd83ad8a086 Mon Sep 17 00:00:00 2001 From: "@saadaoui-salah" Date: Sun, 6 Jun 2021 11:42:21 -0700 Subject: [PATCH 11/14] fix typo bug --- google_problems/problem_15.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/google_problems/problem_15.py b/google_problems/problem_15.py index 941a7d3..150c08f 100644 --- a/google_problems/problem_15.py +++ b/google_problems/problem_15.py @@ -10,7 +10,7 @@ arr = [6, 1, 3, 3, 3, 6, 6] -def get_duplicated(arr=arr): +def get_unique(arr=arr): for num in arr: i = arr.index(num) if num in arr[0:i] or num in arr[i+1::]: @@ -18,7 +18,7 @@ def get_duplicated(arr=arr): else: return num -arr = get_duplicated() +arr = get_unique() print(arr) -arr = get_duplicated([13, 19, 13, 13]) +arr = get_unique([13, 19, 13, 13]) print(arr) \ No newline at end of file From 85b347bc6e50588ab1e148ff6a48afe84b9f39d4 Mon Sep 17 00:00:00 2001 From: "@saadaoui-salah" Date: Sun, 6 Jun 2021 11:53:33 -0700 Subject: [PATCH 12/14] add binary tree, lock method, is_locked method --- google_problems/problem_10.py | 55 ++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/google_problems/problem_10.py b/google_problems/problem_10.py index 30e2457..d166f5d 100644 --- a/google_problems/problem_10.py +++ b/google_problems/problem_10.py @@ -5,4 +5,57 @@ • lock, which attempts to lock the node. If it cannot be locked, then it should return false. Otherwise, it should lock it and return true. • unlock, which unlocks the node. If it cannot be unlocked, then it should return false. Otherwise, it should unlock it and return true. You may augment the node to add parent pointers or any other property you would like. You may assume the class is used in a single-threaded program, so there is no need for actual locks or mutexes. Each method should run in O(h), where h is the height of the tree. -""" \ No newline at end of file +""" + + +class BinaryTree: + def __init__(self, data, left, right, is_locked): + self.data = data + self.left = left + self.right = right + self.parent = None + self.is_locked = is_locked + + def is_locked(self): + return self.is_locked + + def add_node(self, data: int, is_locked: bool): + if self.data > data: + if self.right is None: + self.right = BinaryTree(data, None, None, is_locked) + self.right.parent = self + return + else: + self.right.add_node(data, is_locked) + else: + if self.left is None: + self.left = BinaryTree(data, None, None, is_locked) + self.left.parent = self + return + else: + self.left.add_child(data, is_locked) + + def lock(self): + if self.is_locked: + return False + if self.parent: + are_not_locked = self.parent.lock() + if not are_not_locked: + return False + if self.right: + are_not_locked = self.right.lock() + if are_not_locked: + return False + if self.left: + are_not_locked = self.left.lock() + if are_not_locked: + return False + return True + + def unlocked(self): + if self.is_locked: + return False + if self.right.is_locked or self.left.is_locked: + return False + self.is_locked = True + return True From bcd83b5955eaf8de3b8f795a999b8cb6a89d1239 Mon Sep 17 00:00:00 2001 From: "@saadaoui-salah" Date: Mon, 7 Jun 2021 12:50:40 -0700 Subject: [PATCH 13/14] solved --- google_problems/problem_10.py | 47 +++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/google_problems/problem_10.py b/google_problems/problem_10.py index d166f5d..5eaef3e 100644 --- a/google_problems/problem_10.py +++ b/google_problems/problem_10.py @@ -15,13 +15,15 @@ def __init__(self, data, left, right, is_locked): self.right = right self.parent = None self.is_locked = is_locked - + self.locked_counter = 0 def is_locked(self): return self.is_locked def add_node(self, data: int, is_locked: bool): if self.data > data: if self.right is None: + if is_locked: + self.locked_counter += 1 self.right = BinaryTree(data, None, None, is_locked) self.right.parent = self return @@ -29,33 +31,40 @@ def add_node(self, data: int, is_locked: bool): self.right.add_node(data, is_locked) else: if self.left is None: + if is_locked: + self.locked_counter += 1 self.left = BinaryTree(data, None, None, is_locked) self.left.parent = self return else: self.left.add_child(data, is_locked) - - def lock(self): + + def check(self): if self.is_locked: return False if self.parent: - are_not_locked = self.parent.lock() - if not are_not_locked: - return False - if self.right: - are_not_locked = self.right.lock() - if are_not_locked: - return False - if self.left: - are_not_locked = self.left.lock() + are_not_locked = self.parent.check() if are_not_locked: return False return True - + + def lock(self): + if self.check(): + self.is_locked = True + curr = self.parent + while curr: + self.locked_counter += 1 + curr = curr.parent + return True + return False + def unlocked(self): - if self.is_locked: - return False - if self.right.is_locked or self.left.is_locked: - return False - self.is_locked = True - return True + are_locked = self.check() + if are_locked: + self.is_locked = True + while curr: + self.locked_counter -= 1 + curr = curr.parent + return True + return False + From 48378fd74e948513a512302003d1f6bed869115b Mon Sep 17 00:00:00 2001 From: "@saadaoui-salah" Date: Fri, 11 Jun 2021 14:32:54 -0700 Subject: [PATCH 14/14] solved --- .../__pycache__/problem_10.cpython-39.pyc | Bin 0 -> 2182 bytes google_problems/problem_12.py | 22 +++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 google_problems/__pycache__/problem_10.cpython-39.pyc diff --git a/google_problems/__pycache__/problem_10.cpython-39.pyc b/google_problems/__pycache__/problem_10.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..9d908494adb03b4c718bbdeb2963bdcdda02ea1b GIT binary patch literal 2182 zcmb_d&2Aev5a#|!T5aq`MT#DJD2N_{2(X0&=qU(_w9XH5aSFseR1mPGmy~v+6{(V3 zC^B>?5I;r_X>WdpzR4cjr)W=|A-%F<`&4v=c|pgEKDR5%+rPib09v`1N1 zYg$ezt17Cdw6unDVR9iklOTgClesWfX+Wa|9OqpNK1!bnQ_ilrzw}ozW9X{11+|5s zQ&m-Jg=iPT7K)q4>7T#+-Y-q&Q5?`!QRW5J!q!@XVF7AFJ6nez(&9Cp9g3|9vsNsY zfYd6dvb7n}R~Qb4j+nR6AE1WB9aE^f;+Ib8lvM_j_Zl6qN+S+-RL~(#ncS*dT>~8$ zg->OR$Ik!`gpJT|!e~?fUx0YqTcP}@YPw)+%IcY`j}`zXhH*}pz#f#%QkBvQAk#6s zrV8W5np+BO*K`f1tXx{Eat`3Pry5f)go`S7+yW!Df%KjL$au?cqhz=K4(w{zDTzG)WDXcuOAdcOfsHU;CUEe zA<9`{v2bzb%0JDa%RYLx{r3cx1%Zwu zdcsqF2fls22VJ_~q~EztJAp>aJ3C%b4&gn5*L(%TCfJaSY(f}=O$1}e35?MuhB4;k z4Y>%8hn*(o%(5n~#MCw&jnOn+PgQf(AE;+UHPsl3j&VSTx(CAr(S7**^Wy07KWg>z02JE?Wqe&8x*%-GTZmO-Ziaye9B8;DJ!%JCqZ7Av4+Mj%BslwQ z$JO?t7=}It7$%lD9;Vm?k}X7*HEEVD6t63k_p len(second): + return first + return second + +def compute_edit_distance(first,second): + substitute = 0 + bigger = compare(first, second) + for i in range(len(bigger)): + try: + if first[i] != second[i]: + substitute += 1 + except : + substitute += 1 + return substitute + +substitute = compute_edit_distance("kitten","sitting") +print("sub =",substitute) \ No newline at end of file