diff --git a/src/my_project/interviews/interview_amazon/round_4/algo_1.py b/src/my_project/interviews/interview_amazon/round_4/algo_1.py new file mode 100644 index 00000000..c1da49a2 --- /dev/null +++ b/src/my_project/interviews/interview_amazon/round_4/algo_1.py @@ -0,0 +1,59 @@ +from typing import List + +class Solution: + + def solution_1(self, names: List[str]): + dic_answer = dict() + + for n in names: + + if n not in dic_answer: + dic_answer[n] = 1 + else: + dic_answer[n] += 1 + + names.sort(key=lambda x: dic_answer[x], reverse=True) + + return names[0], dic_answer[names[0]] + + def solution_2(self, names: List[str]): + + names.sort(key=lambda x: len(x), reverse=True) + + return len(names[0]), names[0] + + def solution_3(self, nums: List[str]): + + return len([n for n in nums if n % 7 == 0]) + + + def solution_4(self, input_file): + + with open(input_file, 'r') as infile, \ + open('odd_numbers.txt', 'w') as odd_nums, \ + open('even_numbers.txt', 'w') as even_nums: + + for line in infile: + if not line: + continue + try: + num = int(line) + if num % 2 == 0: + even_nums.write(f'{num}\n') + else: + odd_nums.write(f'{num}\n') + except ValueError: + continue + + + + +solution = Solution() + +print(solution.solution_1(names=['mariana','luis','mariana'])) + +print(solution.solution_2(names=['mariana','luis','leonardo'])) + +print(solution.solution_3(nums=[7,14,5,6,9])) + +solution.solution_4(input_file='numbers.txt') \ No newline at end of file diff --git a/src/my_project/interviews/interview_amazon/round_4/even_numbers.txt b/src/my_project/interviews/interview_amazon/round_4/even_numbers.txt new file mode 100644 index 00000000..e5b350fa --- /dev/null +++ b/src/my_project/interviews/interview_amazon/round_4/even_numbers.txt @@ -0,0 +1,3 @@ +4 +8 +10 diff --git a/src/my_project/interviews/interview_amazon/round_4/numbers.txt b/src/my_project/interviews/interview_amazon/round_4/numbers.txt new file mode 100644 index 00000000..e38a5bb5 --- /dev/null +++ b/src/my_project/interviews/interview_amazon/round_4/numbers.txt @@ -0,0 +1,6 @@ +1 +3 +4 +8 +10 +7 \ No newline at end of file diff --git a/src/my_project/interviews/interview_amazon/round_4/odd_numbers.txt b/src/my_project/interviews/interview_amazon/round_4/odd_numbers.txt new file mode 100644 index 00000000..47ae665d --- /dev/null +++ b/src/my_project/interviews/interview_amazon/round_4/odd_numbers.txt @@ -0,0 +1,3 @@ +1 +3 +7 diff --git a/src/my_project/interviews/interview_amazon/round_5/valid_palindrome.py b/src/my_project/interviews/interview_amazon/round_5/valid_palindrome.py new file mode 100644 index 00000000..c32d4bf3 --- /dev/null +++ b/src/my_project/interviews/interview_amazon/round_5/valid_palindrome.py @@ -0,0 +1,20 @@ +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) + + # find palindrome + 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/top_150_questions_round_17/longest_common_prefix.py b/src/my_project/interviews/top_150_questions_round_17/longest_common_prefix.py new file mode 100644 index 00000000..4d5cf93d --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_17/longest_common_prefix.py @@ -0,0 +1,21 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def longestCommonPrefix(self, strs: List[str]) -> str: + + if not strs: + return '' + else: + min_strs, max_strs = min(strs), max(strs) + count = 0 + + len_min_strs = len(min_strs) + + for i in range(len_min_strs): + if min_strs[i] != max_strs[i]: + break + else: + count += 1 + + return min_strs[:count] diff --git a/tests/test_150_questions_round_17/test_longest_common_prefix_round_17.py b/tests/test_150_questions_round_17/test_longest_common_prefix_round_17.py new file mode 100644 index 00000000..24ed788e --- /dev/null +++ b/tests/test_150_questions_round_17/test_longest_common_prefix_round_17.py @@ -0,0 +1,23 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_17\ +.longest_common_prefix import Solution + +class LongestCommonPrefixTestCase(unittest.TestCase): + + def test_longest_common_prefix(self): + solution = Solution() + output = solution.longestCommonPrefix(strs=["flower","flow","flight"]) + target = 'fl' + self.assertEqual(target, output) + + def test_longest_no_common_prefix(self): + solution = Solution() + output = solution.longestCommonPrefix(strs=["dog","racecar","car"]) + target = '' + self.assertEqual(target, output) + + def test_longest_common_prefix_null_list(self): + solution = Solution() + output = solution.longestCommonPrefix(strs=[]) + target = '' + self.assertEqual(target, output) \ No newline at end of file