Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b8988e4

Browse files
committedFeb 15, 2023
feat: complete week7
1 parent e1f61df commit b8988e4

File tree

8 files changed

+146
-0
lines changed

8 files changed

+146
-0
lines changed
 

‎Week7/numb3rs/numb3rs.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import re
2+
3+
4+
def main():
5+
print(validate(input("IPv4 Address: ")))
6+
7+
8+
def validate(ip):
9+
if not re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", ip):
10+
return False
11+
for num in ip.split("."):
12+
if int(num) < 0 or int(num) > 255:
13+
return False
14+
return True
15+
16+
17+
if __name__ == "__main__":
18+
main()

‎Week7/numb3rs/test_numb3rs.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from numb3rs import validate
2+
3+
4+
def test_validate():
5+
assert validate("275.3.6.28") == False
6+
assert validate("hello") == False
7+
assert validate("10.0.10") == False
8+
assert validate("192.168.1.1.1") == False
9+
assert validate("255.256.256.256") == False
10+
11+
assert validate("192.168.1.1") == True
12+
assert validate("10.0.0.1") == True
13+
assert validate("172.16.0.1") == True
14+
assert validate("255.255.255.255") == True

‎Week7/response/response.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import validators
2+
3+
if validators.email(input("What's your email address? ")):
4+
print("Valid")
5+
else:
6+
print("Invalid")

‎Week7/um/test_um.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from um import count
2+
3+
4+
def test_count():
5+
assert count("hello world") == 0
6+
assert count("yummy") == 0
7+
assert count("umum") == 0
8+
9+
assert count("hello, um, world") == 1
10+
assert count("um um um") == 3
11+
assert count("um?") == 1
12+
assert count("Um, thanks for the album.") == 1
13+
assert count("Um, thanks, um...") == 2

‎Week7/um/um.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import re
2+
3+
4+
def main():
5+
print(count(input("Text: ")))
6+
7+
8+
def count(s):
9+
return len(re.findall(r"\bum\b", s, re.IGNORECASE))
10+
11+
12+
if __name__ == "__main__":
13+
main()

‎Week7/watch/wath.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import re
2+
3+
4+
def main():
5+
print(parse(input("HTML: ")))
6+
7+
8+
def parse(s):
9+
if match := re.match(
10+
r'<iframe.*?src="(https?:\/\/(?:www\.)?youtube\.com(?:\/embed)?\/[a-zA-z0-9_-]+)".*?',
11+
s,
12+
):
13+
url = match.group(1).split("/")[-1]
14+
return f"https://youtu.be/{url}"
15+
return None
16+
17+
18+
if __name__ == "__main__":
19+
main()

‎Week7/working/test_working.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from working import convert
2+
import pytest
3+
4+
5+
def test_convert():
6+
assert convert("9:00 AM to 5:00 PM") == "09:00 to 17:00"
7+
assert convert("9 AM to 5 PM") == "09:00 to 17:00"
8+
assert convert("10:30 PM to 8:50 AM") == "22:30 to 08:50"
9+
assert convert("10 PM to 8 AM") == "22:00 to 08:00"
10+
assert convert("12:00 PM to 12:00 AM") == "12:00 to 00:00"
11+
assert convert("12:00 AM to 12:00 PM") == "00:00 to 12:00"
12+
13+
with pytest.raises(ValueError):
14+
convert("9:60 AM to 5:60 PM")
15+
with pytest.raises(ValueError):
16+
convert("9:60 AM to 5:00 PM")
17+
with pytest.raises(ValueError):
18+
convert("13:00 PM to 5:00 PM")
19+
with pytest.raises(ValueError):
20+
convert("9 AM - 5 PM")
21+
with pytest.raises(ValueError):
22+
convert("09:00 AM - 17:00 PM")

‎Week7/working/working.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import re
2+
3+
4+
def main():
5+
print(convert(input("Hours: ")))
6+
7+
8+
def convert(s):
9+
if match := re.match(
10+
r"(\d{1,2}):?(\d{2})? (AM|PM) to (\d{1,2}):?(\d{2})? (AM|PM)", s
11+
):
12+
st_hour, st_min, st_ap, ed_hour, ed_min, ed_ap = match.groups()
13+
14+
if st_min is None and ed_min is None:
15+
st_min, ed_min = 0, 0
16+
st_hour, st_min, ed_hour, ed_min = map(int, [st_hour, st_min, ed_hour, ed_min])
17+
18+
if st_ap == "PM" and st_hour != 12:
19+
st_hour += 12
20+
elif st_ap == "AM" and st_hour == 12:
21+
st_hour = 0
22+
23+
if ed_ap == "PM" and ed_hour != 12:
24+
ed_hour += 12
25+
elif ed_ap == "AM" and ed_hour == 12:
26+
ed_hour = 0
27+
28+
if (
29+
not 0 <= st_hour <= 23
30+
or not 0 <= st_min <= 59
31+
or not 0 <= ed_hour <= 23
32+
or not 0 <= ed_min <= 59
33+
):
34+
raise ValueError("Invalid arguments")
35+
36+
return f"{st_hour:02d}:{st_min:02d} to {ed_hour:02d}:{ed_min:02d}"
37+
raise ValueError("Invalid arguments")
38+
39+
40+
if __name__ == "__main__":
41+
main()

0 commit comments

Comments
 (0)
Please sign in to comment.