Skip to content

Commit 6c2430d

Browse files
authored
Merge pull request #1298 from ivan1016017/june10
adding algo
2 parents 8baeb21 + 8131986 commit 6c2430d

4 files changed

Lines changed: 59 additions & 1 deletion

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import re
2+
3+
class Solution:
4+
def isPalindrome(self, s: str) -> bool:
5+
6+
# to lower case
7+
s = s.lower()
8+
9+
# remove non alpha numeric characters
10+
s = re.sub(pattern='[^a-zA-Z0-9]', repl='', string=s)
11+
12+
len_s = len(s)
13+
14+
for i in range(len_s//2):
15+
if s[i] != s[len_s - 1 -i]:
16+
return False
17+
18+
return True
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
import math
4+
5+
class Solution:
6+
def maxProfit(self, prices: List[int]) -> int:
7+
8+
min_price = math.inf
9+
max_profit = 0
10+
len_prices = len(prices)
11+
12+
for i in range(len_prices):
13+
14+
if prices[i] < min_price:
15+
min_price = prices[i]
16+
elif prices[i] - min_price > max_profit:
17+
max_profit = prices[i] - min_price
18+
19+
return max_profit
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
import unittest
22
from src.my_project.interviews.top_150_questions_round_16\
3-
.best_time_to_sell_stock import Solution
3+
.best_time_to_sell_stock import Solution
4+
5+
6+
class BestTimeToSellStockTestCase(unittest.TestCase):
7+
8+
def test_best_time_to_sell_stock(self):
9+
solution = Solution()
10+
output = solution.maxProfit(prices=[7,1,5,3,6,4])
11+
target = 5
12+
self.assertEqual(output, target)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_17\
3+
.best_time_to_sell_stock import Solution
4+
5+
6+
class BestTimeToSellStockTestCase(unittest.TestCase):
7+
8+
def test_best_time_to_sell_stock(self):
9+
solution = Solution()
10+
output = solution.maxProfit(prices=[7,1,5,3,6,4])
11+
target = 5
12+
self.assertEqual(output, target)

0 commit comments

Comments
 (0)