Skip to content

Commit e7a8753

Browse files
committed
feat(codewars): new 4 kyu solutions
1 parent 4e09498 commit e7a8753

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# parseInt() reloaded
2+
3+
## Description:
4+
5+
In this kata we want to convert a string into an integer. The strings simply represent the numbers in words.
6+
7+
Examples:
8+
9+
"one" => 1
10+
"twenty" => 20
11+
"two hundred forty-six" => 246
12+
"seven hundred eighty-three thousand nine hundred and nineteen" => 783919
13+
14+
Additional Notes:
15+
16+
The minimum number is "zero" (inclusively)
17+
The maximum number, which must be supported is 1 million (inclusively)
18+
The "and" in e.g. "one hundred and twenty-four" is optional, in some cases it's present and in others it's not
19+
All tested numbers are valid, you don't need to validate them
20+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
MAPPING = {
2+
"zero": 0,
3+
"one": 1, "two": 2, "three": 3, "four": 4, "five": 5,
4+
"six": 6, "seven": 7, "eight": 8, "nine": 9, "ten": 10,
5+
"eleven": 11, "twelve": 12, "thirteen": 13, "fourteen": 14,
6+
"fifteen": 15, "sixteen": 16, "seventeen": 17, "eighteen": 18, "nineteen": 19,
7+
"twenty": 20, "thirty": 30, "forty": 40, "fifty": 50,
8+
"sixty": 60, "seventy": 70, "eighty": 80, "ninety": 90,
9+
}
10+
11+
SCALES = {
12+
"hundred": 100,
13+
"thousand": 1_000,
14+
"million": 1_000_000,
15+
}
16+
17+
def parse_int(text: str) -> int:
18+
total = 0
19+
group = 0 # текущая группа до scale (<= 999)
20+
21+
for i in text.strip().split():
22+
if i == "and":
23+
continue
24+
25+
if i == "hundred":
26+
group = max(1, group) * SCALES.get(i)
27+
elif i in ("thousand", "million"):
28+
total += max(1, group) * SCALES.get(i)
29+
group = 0
30+
elif "-" in i:
31+
group += sum(MAPPING.get(j) for j in i.split("-"))
32+
else:
33+
group += MAPPING.get(i)
34+
35+
return total + group
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Description:
2+
3+
Complete the solution so that it strips all text that follows any of a set of comment markers passed in. Any whitespace at the end of the line should also be stripped out.
4+
5+
Example:
6+
7+
Given an input string of:
8+
9+
apples, pears # and bananas
10+
grapes
11+
bananas !apples
12+
13+
The output expected would be:
14+
15+
apples, pears
16+
grapes
17+
bananas
18+
19+
[Kata link](https://www.codewars.com/kata/51c8e37cee245da6b40000bd)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import re
2+
3+
4+
def strip_comments(strng, markers):
5+
if ms := [m for m in markers if m]:
6+
pat = re.compile(r'(?:' + '|'.join(map(re.escape, ms)) + r').*')
7+
return "\n".join(pat.sub('', i).rstrip() for i in strng.splitlines())
8+
return strng

0 commit comments

Comments
 (0)