Skip to content

Commit c7c645f

Browse files
authored
Merge pull request #1301 from ivan1016017/june13
adding solution
2 parents f7ec5a0 + a6415f5 commit c7c645f

7 files changed

Lines changed: 135 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from typing import List
2+
3+
class Solution:
4+
5+
def solution_1(self, names: List[str]):
6+
dic_answer = dict()
7+
8+
for n in names:
9+
10+
if n not in dic_answer:
11+
dic_answer[n] = 1
12+
else:
13+
dic_answer[n] += 1
14+
15+
names.sort(key=lambda x: dic_answer[x], reverse=True)
16+
17+
return names[0], dic_answer[names[0]]
18+
19+
def solution_2(self, names: List[str]):
20+
21+
names.sort(key=lambda x: len(x), reverse=True)
22+
23+
return len(names[0]), names[0]
24+
25+
def solution_3(self, nums: List[str]):
26+
27+
return len([n for n in nums if n % 7 == 0])
28+
29+
30+
def solution_4(self, input_file):
31+
32+
with open(input_file, 'r') as infile, \
33+
open('odd_numbers.txt', 'w') as odd_nums, \
34+
open('even_numbers.txt', 'w') as even_nums:
35+
36+
for line in infile:
37+
if not line:
38+
continue
39+
try:
40+
num = int(line)
41+
if num % 2 == 0:
42+
even_nums.write(f'{num}\n')
43+
else:
44+
odd_nums.write(f'{num}\n')
45+
except ValueError:
46+
continue
47+
48+
49+
50+
51+
solution = Solution()
52+
53+
print(solution.solution_1(names=['mariana','luis','mariana']))
54+
55+
print(solution.solution_2(names=['mariana','luis','leonardo']))
56+
57+
print(solution.solution_3(nums=[7,14,5,6,9]))
58+
59+
solution.solution_4(input_file='numbers.txt')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
4
2+
8
3+
10
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
1
2+
3
3+
4
4+
8
5+
10
6+
7
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1
2+
3
3+
7
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import re
2+
3+
class Solution:
4+
def isPalindrome(self, s: str) -> bool:
5+
6+
# to lowercase
7+
s = s.lower()
8+
9+
# remove non-alphanumeric characters
10+
s = re.sub(pattern='[^a-zA-Z0-9]', repl='', string=s)
11+
12+
# find palindrome
13+
len_s = len(s)
14+
15+
for i in range(len_s//2):
16+
if s[i] != s[len_s - 1 - i]:
17+
return False
18+
19+
return True
20+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def longestCommonPrefix(self, strs: List[str]) -> str:
6+
7+
if not strs:
8+
return ''
9+
else:
10+
min_strs, max_strs = min(strs), max(strs)
11+
count = 0
12+
13+
len_min_strs = len(min_strs)
14+
15+
for i in range(len_min_strs):
16+
if min_strs[i] != max_strs[i]:
17+
break
18+
else:
19+
count += 1
20+
21+
return min_strs[:count]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_17\
3+
.longest_common_prefix import Solution
4+
5+
class LongestCommonPrefixTestCase(unittest.TestCase):
6+
7+
def test_longest_common_prefix(self):
8+
solution = Solution()
9+
output = solution.longestCommonPrefix(strs=["flower","flow","flight"])
10+
target = 'fl'
11+
self.assertEqual(target, output)
12+
13+
def test_longest_no_common_prefix(self):
14+
solution = Solution()
15+
output = solution.longestCommonPrefix(strs=["dog","racecar","car"])
16+
target = ''
17+
self.assertEqual(target, output)
18+
19+
def test_longest_common_prefix_null_list(self):
20+
solution = Solution()
21+
output = solution.longestCommonPrefix(strs=[])
22+
target = ''
23+
self.assertEqual(target, output)

0 commit comments

Comments
 (0)