diff --git a/src/my_project/interviews/interview_amazon/round_2/valid_palindrome.py b/src/my_project/interviews/interview_amazon/round_2/valid_palindrome.py new file mode 100644 index 00000000..f3c4181f --- /dev/null +++ b/src/my_project/interviews/interview_amazon/round_2/valid_palindrome.py @@ -0,0 +1,18 @@ +import re + +class Solution: + def isPalindrome(self, s: str) -> bool: + + # to lower case + s = s.lower() + + # remove non alpha numeric characters + s = re.sub(pattern='[^a-zA-Z0-9]', repl='', string=s) + + 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/top_150_questions_round_17/best_time_to_sell_stock.py b/src/my_project/interviews/top_150_questions_round_17/best_time_to_sell_stock.py new file mode 100644 index 00000000..0adbeb98 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_17/best_time_to_sell_stock.py @@ -0,0 +1,19 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod +import math + +class Solution: + def maxProfit(self, prices: List[int]) -> int: + + min_price = math.inf + max_profit = 0 + len_prices = len(prices) + + for i in range(len_prices): + + if prices[i] < min_price: + min_price = prices[i] + elif prices[i] - min_price > max_profit: + max_profit = prices[i] - min_price + + return max_profit diff --git a/tests/test_150_questions_round_16/test_best_time_to_sell_stock_round_16.py b/tests/test_150_questions_round_16/test_best_time_to_sell_stock_round_16.py index ddb38189..60128772 100644 --- a/tests/test_150_questions_round_16/test_best_time_to_sell_stock_round_16.py +++ b/tests/test_150_questions_round_16/test_best_time_to_sell_stock_round_16.py @@ -1,3 +1,12 @@ import unittest from src.my_project.interviews.top_150_questions_round_16\ -.best_time_to_sell_stock import Solution \ No newline at end of file +.best_time_to_sell_stock import Solution + + +class BestTimeToSellStockTestCase(unittest.TestCase): + + def test_best_time_to_sell_stock(self): + solution = Solution() + output = solution.maxProfit(prices=[7,1,5,3,6,4]) + target = 5 + self.assertEqual(output, target) \ No newline at end of file diff --git a/tests/test_150_questions_round_17/test_best_time_to_sell_stock_round_17.py b/tests/test_150_questions_round_17/test_best_time_to_sell_stock_round_17.py new file mode 100644 index 00000000..79bbbce0 --- /dev/null +++ b/tests/test_150_questions_round_17/test_best_time_to_sell_stock_round_17.py @@ -0,0 +1,12 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_17\ +.best_time_to_sell_stock import Solution + + +class BestTimeToSellStockTestCase(unittest.TestCase): + + def test_best_time_to_sell_stock(self): + solution = Solution() + output = solution.maxProfit(prices=[7,1,5,3,6,4]) + target = 5 + self.assertEqual(output, target)