Skip to content

Commit f42928d

Browse files
committed
[main] Added functions
1 parent 974c8d0 commit f42928d

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution:
2+
def intToRoman(self, num: int) -> str:
3+
values = {
4+
1: "I",
5+
4: "IV",
6+
5: "V",
7+
9: "IX",
8+
10: "X",
9+
40: "XL",
10+
50: "L",
11+
90: "XC",
12+
100: "C",
13+
400: "CD",
14+
500: "D",
15+
900: "CM",
16+
1000: "M"
17+
}
18+
19+
output = ''
20+
value_keys = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000]
21+
starting_value = 12
22+
23+
while num > 0:
24+
val_ = value_keys[starting_value]
25+
if num >= val_:
26+
num -= val_
27+
output += values[value_keys[starting_value]]
28+
else:
29+
starting_value -= 1
30+
31+
return output
32+
33+
34+
if __name__ == '__main__':
35+
sol = Solution()
36+
sol.intToRoman(58)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from sys import prefix
2+
from typing import List
3+
4+
5+
class Solution:
6+
def longestCommonPrefix(self, strs: List[str]) -> str:
7+
if len(strs) > 0:
8+
strs = sorted(strs, key=len)
9+
base_word = strs[0]
10+
if base_word == '':
11+
return ''
12+
_ind = 0
13+
min_len = len(strs[0])
14+
valid_prefix = True
15+
prefix = ''
16+
while _ind <= min_len:
17+
for i in range(len(strs)):
18+
if not strs[i].startswith(prefix):
19+
valid_prefix = False
20+
break
21+
if not valid_prefix:
22+
return prefix[:-1]
23+
else:
24+
prefix += base_word[_ind] if _ind != min_len else ''
25+
_ind += 1
26+
return prefix
27+
return ''
28+
29+
30+
if __name__ == '__main__':
31+
arr = ["a", "b"]
32+
sol = Solution()
33+
print(sol.longestCommonPrefix(arr))
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution:
2+
def romanToInt(self, s: str) -> int:
3+
values = {
4+
"I": 1,
5+
"IV": 4,
6+
"V": 5,
7+
"IX": 9,
8+
"X": 10,
9+
"XL": 40,
10+
"L": 50,
11+
"XC": 90,
12+
"C": 100,
13+
"CD": 400,
14+
"D": 500,
15+
"CM": 900,
16+
"M": 1000
17+
}
18+
19+
doubles = ["IV", "IX", "XL", "XC", "CD", "CM"]
20+
21+
total = 0
22+
i = 0
23+
while i < len(s):
24+
first_ = s[i]
25+
second_ = s[i + 1] if i < len(s) - 1 else ''
26+
if f'{first_}{second_}' in doubles:
27+
total += values[f'{first_}{second_}']
28+
i += 2 if i < len(s) - 1 else 1
29+
else:
30+
total += values[f'{first_}']
31+
i += 1
32+
return total
33+
34+
35+
if __name__ == '__main__':
36+
sol = Solution()
37+
print(sol.romanToInt("LVIII"))

0 commit comments

Comments
 (0)