Skip to content

Commit 7720c9f

Browse files
2 unsolved
1 parent 42a59cf commit 7720c9f

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

13_Roman to Integer/solution.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
2+
#
3+
# Symbol Value
4+
# I 1
5+
# V 5
6+
# X 10
7+
# L 50
8+
# C 100
9+
# D 500
10+
# M 1000
11+
# For example, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.
12+
#
13+
# Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
14+
#
15+
# I can be placed before V (5) and X (10) to make 4 and 9.
16+
# X can be placed before L (50) and C (100) to make 40 and 90.
17+
# C can be placed before D (500) and M (1000) to make 400 and 900.
18+
# Given a roman numeral, convert it to an integer.
19+
#
20+
#
21+
#
22+
# Example 1:
23+
#
24+
# Input: s = "III"
25+
# Output: 3
26+
# Explanation: III = 3.
27+
# Example 2:
28+
#
29+
# Input: s = "LVIII"
30+
# Output: 58
31+
# Explanation: L = 50, V= 5, III = 3.
32+
# Example 3:
33+
#
34+
# Input: s = "MCMXCIV"
35+
# Output: 1994
36+
# Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
37+
#
38+
39+
s = "LII"
40+
41+
greeksymbols = {
42+
"I" : 1,
43+
"V" : 5,
44+
"X" : 10,
45+
"L" : 50
46+
}
47+
48+
49+
def RtoI(roman) -> int:
50+
listnumbers = [i for i in roman]
51+
sum = 0
52+
pointer1, pointer2 = 0, 1
53+
54+
while pointer1 < (len(listnumbers) - 1):
55+
current_val = greeksymbols[listnumbers[pointer1]]
56+
next_val = greeksymbols[listnumbers[pointer2]]
57+
if int(current_val) < int(next_val):
58+
sum -= int(next_val) - int(current_val)
59+
pointer1 += 2
60+
pointer2 += 2
61+
else:
62+
sum += int(current_val)
63+
pointer1 += 1
64+
pointer2 += 1
65+
66+
if pointer1 == len(listnumbers) - 1:
67+
sum += greeksymbols[listnumbers[pointer1]]
68+
69+
return sum
70+
71+
72+
print(RtoI(s))

392_Is Subsequence/solution.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Given two strings s and t, return true if s is a subsequence of t, or false otherwise.
2+
#
3+
# A subsequence of a string is a new string that is formed from the original string by deleting some
4+
# (can be none) of the characters without disturbing the relative positions of the remaining characters.
5+
# (i.e., "ace" is a subsequence of "abcde" while "aec" is not).
6+
#
7+
#
8+
#
9+
# Example 1:
10+
#
11+
# Input: s = "abc", t = "ahbgdc"
12+
# Output: true
13+
# Example 2:
14+
#
15+
# Input: s = "axc", t = "ahbgdc"
16+
# Output: false
17+
#
18+
s, t = "bb", "ahbgcd"
19+
20+
21+
def check(s, t) -> bool:
22+
list1 = [i for i in s] # b b
23+
list2 = [i for i in t] # a h b g c d
24+
25+
po1, po2 = 0, 0
26+
stop = list2[-1]
27+
28+
if len(list1) == 0:
29+
return False
30+
31+
while po1 < len(list1):
32+
if list1[po1] == list2[po2]:
33+
po1 += 1
34+
35+
elif list2[po2] == stop:
36+
return False
37+
po2 += 1
38+
39+
return True
40+
41+
42+
43+
44+
print(check(s, t))

practice_small_code.ipynb

+35
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,41 @@
2626
}
2727
],
2828
"execution_count": 2
29+
},
30+
{
31+
"metadata": {
32+
"ExecuteTime": {
33+
"end_time": "2024-08-07T03:39:46.132609Z",
34+
"start_time": "2024-08-07T03:39:46.128540Z"
35+
}
36+
},
37+
"cell_type": "code",
38+
"source": [
39+
"s = \"bb\"\n",
40+
"list1 = [i for i in s]\n",
41+
"print(len(list1) -1 ) \n",
42+
"print(len(list1))"
43+
],
44+
"id": "74d716e94aa6f339",
45+
"outputs": [
46+
{
47+
"name": "stdout",
48+
"output_type": "stream",
49+
"text": [
50+
"1\n",
51+
"2\n"
52+
]
53+
}
54+
],
55+
"execution_count": 6
56+
},
57+
{
58+
"metadata": {},
59+
"cell_type": "code",
60+
"outputs": [],
61+
"execution_count": null,
62+
"source": "",
63+
"id": "282cf3c329427e88"
2964
}
3065
],
3166
"metadata": {

0 commit comments

Comments
 (0)