diff --git a/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.py new file mode 100644 index 00000000..6c0c6330 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/two_sum_round_4.py @@ -0,0 +1,16 @@ +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/common_algos/valid_palindrome_round_4.py b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.py new file mode 100644 index 00000000..e6f69dfb --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/common_algos/valid_palindrome_round_4.py @@ -0,0 +1,22 @@ +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 s is a 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/kids_with_the_greates_number_of_candies.py b/src/my_project/interviews/amazon_high_frequency_23/round_3/kids_with_the_greates_number_of_candies.py new file mode 100644 index 00000000..b9272008 --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_3/kids_with_the_greates_number_of_candies.py @@ -0,0 +1,8 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: + max_candies = max(candies) + return [candy + extraCandies >= max_candies for candy in candies] + diff --git a/src/my_project/interviews/amazon_high_frequency_23/round_3/longest_common_subsequence.py b/src/my_project/interviews/amazon_high_frequency_23/round_3/longest_common_subsequence.py new file mode 100644 index 00000000..39e0ec5f --- /dev/null +++ b/src/my_project/interviews/amazon_high_frequency_23/round_3/longest_common_subsequence.py @@ -0,0 +1,23 @@ +from typing import List, Union, Collection, Mapping, Optional + +class Solution: + def longestCommonSubsequence(self, text1: str, text2: str) -> int: + + # Make a grid of 0's with len(text2) + 1 columns + # and len(text1) + 1 rows. + len_1 = len(text1) + len_2 = len(text2) + dp_grid = [[0]*(len_2+1) for _ in range(len_1+1)] + + # Iterate up each column, starting from the last one. + for j in reversed(range(len_2)): + for i in reversed(range(len_1)): + if text1[i] == text2[j]: + dp_grid[i][j] = dp_grid[i+1][j+1] + 1 + else: + dp_grid[i][j] = max(dp_grid[i+1][j], dp_grid[i][j+1]) + + # The original problem's answer is in dp_grid[0][0]. Return it. + return dp_grid[0][0] + + diff --git a/src/my_project/interviews/top_150_questions_round_21/summary_ranges.py b/src/my_project/interviews/top_150_questions_round_21/summary_ranges.py new file mode 100644 index 00000000..931f744a --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_21/summary_ranges.py @@ -0,0 +1,30 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def summaryRanges(self, nums: List[int]) -> List[str]: + + len_nums = len(nums) + + if len_nums == 0: + return [] + elif len_nums == 1: + return [f'{nums[0]}'] + else: + + answer = list() + + pre = start = nums[0] + + for i in nums[1:]: + + if i - pre > 1: + answer.append(f'{start}->{pre}' if pre-start > 0 else + f'{start}') + start = i + + pre = i + + answer.append(f'{start}->{pre}' if pre-start > 0 else f'{start}') + + return answer \ No newline at end of file diff --git a/tests/test_150_questions_round_21/test_summary_ranges_round_21.py b/tests/test_150_questions_round_21/test_summary_ranges_round_21.py new file mode 100644 index 00000000..e8200a42 --- /dev/null +++ b/tests/test_150_questions_round_21/test_summary_ranges_round_21.py @@ -0,0 +1,24 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_21\ +.summary_ranges import Solution + +class SummaryRangesTestCase(unittest.TestCase): + + def test_empty(self): + solution = Solution() + output = solution.summaryRanges(nums=[]) + target = [] + self.assertEqual(target, output) + + def test_single_element(self): + solution = Solution() + output = solution.summaryRanges(nums=[1]) + target = ['1'] + self.assertEqual(target, output) + + def test_several_elements(self): + solution = Solution() + output = solution.summaryRanges(nums=[0,1,2,4,5,7]) + target = ["0->2","4->5","7"] + for k, v in enumerate(target): + self.assertEqual(v, output[k])