From 7a5d35dd070c8c15cca713ceba49463d55aade85 Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 10 Nov 2025 04:28:09 -0600 Subject: [PATCH] adding algos --- .../two_sum_round_1.py | 0 .../two_sum_round_2.py} | 8 +- .../valid_palindrome_round_1.py | 0 .../valid_palindrome_round_2.py | 11 ++- .../round_2/two_sum_round_2.py | 16 ---- .../round_2/two_sum_round_3.py | 16 ---- .../round_2/two_sum_round_4.py | 16 ---- .../round_2/two_sum_round_5.py | 16 ---- .../round_2/valid_palindrom_round_3.py | 21 ---- .../round_2/valid_palindrome_4.py | 21 ---- .../round_2/valid_palindrome_round_3.py | 23 ----- .../round_2/valid_palindrome_round_5.py | 21 ---- .../round_2/valid_palindrome_round_6.py | 23 ----- .../round_2/valid_palindrome_round_7.py | 23 ----- .../round_2/valid_palindrome_round_8.py | 23 ----- .../round_2/valid_palindrome_round_9.py | 23 ----- .../round_3/design_sql.py | 95 +++++++++++++++++++ .../find_minimum_time_to_finsh_all_jobs_ii.py | 11 +++ .../happy_number.py | 19 ++++ .../test_happy_number_round_21.py | 15 +++ 20 files changed, 150 insertions(+), 251 deletions(-) rename src/my_project/interviews/amazon_high_frequency_23/{round_2 => common_algos}/two_sum_round_1.py (100%) rename src/my_project/interviews/amazon_high_frequency_23/{round_2/two_sum_round_6.py => common_algos/two_sum_round_2.py} (63%) rename src/my_project/interviews/amazon_high_frequency_23/{round_2 => common_algos}/valid_palindrome_round_1.py (100%) rename src/my_project/interviews/amazon_high_frequency_23/{round_2 => common_algos}/valid_palindrome_round_2.py (77%) delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_2/two_sum_round_2.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_2/two_sum_round_3.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_2/two_sum_round_4.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_2/two_sum_round_5.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrom_round_3.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_4.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_3.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_5.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_6.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_7.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_8.py delete mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_9.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_3/design_sql.py create mode 100644 src/my_project/interviews/amazon_high_frequency_23/round_3/find_minimum_time_to_finsh_all_jobs_ii.py create mode 100644 src/my_project/interviews/top_150_questions_round_21/happy_number.py create mode 100644 tests/test_150_questions_round_21/test_happy_number_round_21.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_2/two_sum_round_1.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_1.py similarity index 100% rename from src/my_project/interviews/amazon_high_frequency_23/round_2/two_sum_round_1.py rename to src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_1.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_2/two_sum_round_6.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_2.py similarity index 63% rename from src/my_project/interviews/amazon_high_frequency_23/round_2/two_sum_round_6.py rename to src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_2.py index 6c0c6330..b5fbc9b9 100644 --- a/src/my_project/interviews/amazon_high_frequency_23/round_2/two_sum_round_6.py +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_2.py @@ -4,13 +4,13 @@ class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: - answer = dict() + dic_answer = dict() for k, v in enumerate(nums): - if v in answer: - return [answer[v], k] + if v in dic_answer: + return [k, dic_answer[v]] else: - answer[target - v] = k + dic_answer[target - v] = k return [] \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_1.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_1.py similarity index 100% rename from src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_1.py rename to src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_1.py diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_2.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_2.py similarity index 77% rename from src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_2.py rename to src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_2.py index c6b4cfe9..9278ab43 100644 --- a/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_2.py +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_2.py @@ -8,16 +8,17 @@ def isPalindrome(self, s: str) -> bool: # To lowercase s = s.lower() - # Remove non-alphanumerical characters + # Remove non-alphanumeric characters s = re.sub(pattern='[^a-zA-Z0-9]', repl='', string=s) - # Find if it is palindrome or not - + # Determine if it is palindrome or not len_s = len(s) for i in range(len_s//2): - + if s[i] != s[len_s - 1 - i]: return False - return True \ No newline at end of file + return True + + diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_2/two_sum_round_2.py b/src/my_project/interviews/amazon_high_frequency_23/round_2/two_sum_round_2.py deleted file mode 100644 index c89ef2f0..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/round_2/two_sum_round_2.py +++ /dev/null @@ -1,16 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod - -class Solution: - def twoSum(self, nums: List[int], target: int) -> List[int]: - - answer = dict() - - for k,v in enumerate(nums): - - if v in answer: - return [answer[v], k] - else: - answer[target - v] = k - - return [] \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_2/two_sum_round_3.py b/src/my_project/interviews/amazon_high_frequency_23/round_2/two_sum_round_3.py deleted file mode 100644 index 534c7ec3..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/round_2/two_sum_round_3.py +++ /dev/null @@ -1,16 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod - -class Solution: - def twoSum(self, nums: List[int], target: int) -> List[int]: - - answer = dict() - - for k, v in enumerate(nums): - - if v in answer: - return [answer[v], k] - else: - answer[target - v] = k - - return [] \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_2/two_sum_round_4.py b/src/my_project/interviews/amazon_high_frequency_23/round_2/two_sum_round_4.py deleted file mode 100644 index e03fe46f..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/round_2/two_sum_round_4.py +++ /dev/null @@ -1,16 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod - -class Solution: - def twoSum(self, nums: List[int], target: int) -> List[int]: - - answer = dict() - - for k, v in enumerate(nums): - - if v in answer: - return [answer[v], k] - else: - answer[target - v] = k - - return [] \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_2/two_sum_round_5.py b/src/my_project/interviews/amazon_high_frequency_23/round_2/two_sum_round_5.py deleted file mode 100644 index e840ea34..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/round_2/two_sum_round_5.py +++ /dev/null @@ -1,16 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod - -class Solution: - def twoSum(self, nums: List[int], target: int) -> List[int]: - - answer = dict() - - for k, v in enumerate(nums): - - if v in answer: - return [answer[v], k] - else: - answer[target - v] = k - - return [] diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrom_round_3.py b/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrom_round_3.py deleted file mode 100644 index 0f3740de..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrom_round_3.py +++ /dev/null @@ -1,21 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod -import re - -class Solution: - def isPalindrome(self, s: str) -> bool: - - # To lowercase - s = s.lower() - - # Remove non-alphanumeric characters - s = re.sub(pattern='[^a-zA-Z0-9]', repl='', string=s) - - # Check if it is palindrome or not - len_s = len(s) - - for i in range(len_s//2): - if s[i] != s[len_s - 1 -i]: - return False - - return True diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_4.py b/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_4.py deleted file mode 100644 index 6628b0c6..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_4.py +++ /dev/null @@ -1,21 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod -import re - -class Solution: - def isPalindrome(self, s: str) -> bool: - - # To lowercase - s = s.lower() - - # Remove non-alphanumeric characters - s = re.sub(pattern='[^a-zA-Z0-9]', repl='', string=s) - - # Determine if it is palindrome or not - len_s = len(s) - - for i in range(len_s//2): - if s[i] != s[len_s - 1 - i]: - return False - - return True \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_3.py b/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_3.py deleted file mode 100644 index eaab0a37..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_3.py +++ /dev/null @@ -1,23 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod -import re - -class Solution: - def isPalindrome(self, s: str) -> bool: - - # To lowercase - s = s.lower() - - # Remove non-alphanumeric characters - s = re.sub(pattern='[^a-zA-Z0-9]', repl='', string=s) - - # Determine if it is palindrome or not - - len_s = len(s) - - for i in range(len_s//2): - - if s[i] != s[len_s - 1 -i]: - return False - - return True \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_5.py b/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_5.py deleted file mode 100644 index 8af6a65e..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_5.py +++ /dev/null @@ -1,21 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethoda -import re - -class Solution: - def isPalindrome(self, s: str) -> bool: - - # To lowercase - s = s.lower() - - # Remove non-alphanumeric characters - s = re.sub(pattern='[^a-zA-Z0-9]', repl='', string=s) - - # Determine if it is palindrome or not - len_s = len(s) - - for i in range(len_s//2): - if s[i] != s[len_s - 1 -i]: - return False - - return True diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_6.py b/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_6.py deleted file mode 100644 index cb762e70..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_6.py +++ /dev/null @@ -1,23 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod -import re - -class Solution: - def isPalindrome(self, s: str) -> bool: - - # To lowercase - s = s.lower() - - # Remove non-alphanumeric characters - s = re.sub(pattern='[^a-zA-Z0-9]', repl='',string=s) - - # Determine if it is palindrome or not - - len_s = len(s) - - for i in range(len_s//2): - - if s[i] != s[len_s - 1 - i]: - return False - - return True \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_7.py b/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_7.py deleted file mode 100644 index 43525f5d..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_7.py +++ /dev/null @@ -1,23 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod -import re - -class Solution: - def isPalindrome(self, s: str) -> bool: - - # To lowercase - s = s.lower() - - # Remove non-alphanumeric characters - s = re.sub(pattern='[^a-zA-Z0-9]', repl='', string=s) - - # Determine if it is palindrome or not - - len_s = len(s) - - for i in range(len_s//2): - - if s[i] != s[len_s - 1 -i]: - return False - - return True diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_8.py b/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_8.py deleted file mode 100644 index 07e7315e..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_8.py +++ /dev/null @@ -1,23 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod -import re - -class Solution: - def isPalindrome(self, s: str) -> bool: - - # To lowercase - s = s.lower() - - # Remove non-alphanumeric characters - s = re.sub(pattern='[^a-zA-Z0-9]', repl='', string=s) - - # Determine if palindrome or not - - len_s = len(s) - - for i in range(len_s//2): - - if s[i] != s[len_s - 1 -i]: - return False - - return True \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_9.py b/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_9.py deleted file mode 100644 index 54820f63..00000000 --- a/src/my_project/interviews/amazon_high_frequency_23/round_2/valid_palindrome_round_9.py +++ /dev/null @@ -1,23 +0,0 @@ -from typing import List, Union, Collection, Mapping, Optional -from abc import ABC, abstractmethod -import re - -class Solution: - def isPalindrome(self, s: str) -> bool: - - # To lowercase - s = s.lower() - - # Remove non-alphanumeric characters - s = re.sub(pattern='[^a-zA-Z0-9]', repl='', string=s) - - # Determine if it is palindrome or not - - len_s = len(s) - - for i in range(len_s//2): - - if s[i] != s[len_s - 1 -i]: - return False - - return True \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_3/design_sql.py b/src/my_project/interviews/amazon_high_frequency_23/round_3/design_sql.py new file mode 100644 index 00000000..6b6fc947 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_3/design_sql.py @@ -0,0 +1,95 @@ +from typing import List, Union, Collection, Mapping, Optional, Dict +from abc import ABC, abstractmethod + +class Table: + def __init__(self, name: str, columns:int, rows: Dict, row_count: int = 0): + self.name = name + self.columns = columns + self.rows = rows + self.row_count = row_count + +class SQL: + + def __init__(self, names: List[str], columns: List[int]): + """ + two string arrays, names and columns, both of size n. + + The ith table is represented by the name names[i] and contains columns[i] number of columns + """ + + self.names = names + self.columns = columns + self.data = {} + + for i in range(0, len(self.names)): + self.data[self.names[i]] = Table( + self.names[i], + self.columns[i], + {} + ) + + return + + def ins(self, name: str, row: List[str]) -> bool: + """ + Inserts row into the table name and returns true. + If row.length does not match the expected number of columns, or name is not a valid table, + returns false without any insertion. + """ + table: Table = self.data.get(name, None) + if table is None: + return False + if len(row) != table.columns: + return False + table.row_count += 1 + table.rows[table.row_count] = row + + return True + + def rmv(self, name: str, rowId: int) -> None: + """ + Removes the row rowId from the table name. + If name is not a valid table or there is no row with id rowId, no removal is performed. + """ + table = self.data.get(name, None) + if table is None: + return + + if rowId in table.rows: + del table.rows[rowId] + return + + def sel(self, name: str, rowId: int, columnId: int) -> str: + """ + Returns the value of the cell at the specified rowId and columnId in the table name. + If name is not a valid table, or the cell (rowId, columnId) is invalid, returns "". + """ + null = "" + table = self.data.get(name, None) + if table is None: + return null + + if rowId not in table.rows: + return null + + try: + return table.rows[rowId][columnId-1] + except Exception: + return null + + def exp(self, name: str) -> List[str]: + """ + Returns the rows present in the table name. + If name is not a valid table, returns an empty array. + Each row is represented as a string, with each cell value (including the row's id) separated by a ",". + """ + table = self.data.get(name, None) + if table is None: + return [] + + result = [] + + for row_id, row in table.rows.items(): + row_str = ",".join(row) + result.append(f"{row_id},{row_str}") + return result \ No newline at end of file diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_3/find_minimum_time_to_finsh_all_jobs_ii.py b/src/my_project/interviews/amazon_high_frequency_23/round_3/find_minimum_time_to_finsh_all_jobs_ii.py new file mode 100644 index 00000000..fa990fc1 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_3/find_minimum_time_to_finsh_all_jobs_ii.py @@ -0,0 +1,11 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +import math + +class Solution: + def minimumTime(self, jobs: List[int], workers: List[int]) -> int: + + jobs.sort() + workers.sort() + + return max(math.ceil(n/d) for (n,d) in zip(jobs,workers)) \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_21/happy_number.py b/src/my_project/interviews/top_150_questions_round_21/happy_number.py new file mode 100644 index 00000000..9dd32367 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_21/happy_number.py @@ -0,0 +1,19 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution(object): + def isHappy(self, n): + + set_answer = set() + + while n != 1: + + if n in set_answer: + return False + else: + set_answer.add(n) + + n = sum([int(c)**2 for c in str(n)]) + + return True + \ No newline at end of file diff --git a/tests/test_150_questions_round_21/test_happy_number_round_21.py b/tests/test_150_questions_round_21/test_happy_number_round_21.py new file mode 100644 index 00000000..31904e94 --- /dev/null +++ b/tests/test_150_questions_round_21/test_happy_number_round_21.py @@ -0,0 +1,15 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_21\ +.happy_number import Solution + +class HappyNumberTestCase(unittest.TestCase): + + def test_is_happy_number(self): + solution = Solution() + output = solution.isHappy(n=19) + self.assertTrue(output) + + def test_is_no_happy_number(self): + solution = Solution() + output = solution.isHappy(n=2) + self.assertFalse(output) \ No newline at end of file