Skip to content

Commit bead8e5

Browse files
authored
uploading pset7
1 parent b1e85aa commit bead8e5

File tree

8 files changed

+199
-0
lines changed

8 files changed

+199
-0
lines changed

pset7/numb3rs/numb3rs.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from sys import exit
2+
3+
def main():
4+
print(validate(input("IPv4 Address: ")))
5+
6+
7+
def validate(ip):
8+
try:
9+
nos = ip.strip().split('.')
10+
if len(nos) != 4:
11+
return False
12+
for n in nos:
13+
if int(n)<0 or int(n)>255:
14+
return False
15+
return True
16+
except:
17+
# bug : return False
18+
return True
19+
20+
21+
22+
if __name__ == "__main__":
23+
main()

pset7/numb3rs/test_numb3rs.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from numb3rs import validate
2+
3+
def test_num():
4+
assert validate ('101.001.010.110') == True
5+
assert validate ('11.23.191.22') == True
6+
assert validate ('....') == False
7+
8+
def test_alpha():
9+
assert validate ('cat.001.dog.bee') == False
10+
assert validate ('lion') == False
11+
12+
def test_less():
13+
assert validate ('112.212.311') == False
14+
assert validate ('1') == False
15+
16+
def test_firstbyte():
17+
assert validate('101.512.511.322') ==False

pset7/response.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from validators import email
2+
3+
4+
def main():
5+
inpt = input('What is your email address? '.strip())
6+
if email(inpt) == True:
7+
print('Valid')
8+
else:
9+
print('Invalid')
10+
11+
12+
13+
if __name__ == '__main__':
14+
main()

pset7/um/test_um.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from um import count
2+
3+
4+
def test_input():
5+
assert count('yummy') == 0
6+
assert count('mum ') == 0
7+
8+
def test_nospace():
9+
assert count('um') == 1
10+
assert count('um, can i... um... help?') == 2
11+
12+
def test_space():
13+
assert count(' um ') == 1
14+
15+
def test_case():
16+
assert count('Um, how you doin') == 1
17+
assert count('umm.. UM.. whats um up?') == 2
18+
if __name__ == "__main__":
19+
main()

pset7/um/um.py

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

pset7/watch.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import re
2+
import sys
3+
4+
5+
def main():
6+
print(parse(input("HTML: ")))
7+
8+
9+
def parse(s):
10+
# sample : "http://www.youtube.com/embed/xvFZjo5PgG0"
11+
# sample : <iframe src="https://youtube.com/embed/xvFZjo5PgG0"></iframe>...
12+
if link := re.search(r"\"https?://(?:www\.)?youtube\.com/embed/(\w+)\"",s):
13+
return('https://youtu.be/'+link.group(1))
14+
15+
16+
if __name__ == "__main__":
17+
main()

pset7/working/test_working.py

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

pset7/working/working.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import re
2+
import sys
3+
4+
5+
def main():
6+
try:
7+
print(convert(input("Hours: ")))
8+
except ValueError:
9+
exit('ValueError')
10+
11+
12+
13+
def convert(s):
14+
# re
15+
time_result = re.search(r"(\d+)(?::(\d+))? (AM|PM) to (\d+)(?::(\d+))? (AM|PM)",s)
16+
17+
if time_result:
18+
# get groups into temp
19+
temp = list(time_result.groups())
20+
21+
time_12=[]
22+
# assign group into list 'time_12'
23+
for t in temp:
24+
if t == None:
25+
time_12.append('00')
26+
else:
27+
time_12.append(t)
28+
29+
# check if hours are valid
30+
if int(time_12[0])<0 or int(time_12[0])>12:
31+
raise ValueError
32+
elif int(time_12[3])<0 or int(time_12[3])>12:
33+
raise ValueError
34+
35+
# check if hours are valid
36+
if int(time_12[1])<0 or int(time_12[1])>59:
37+
raise ValueError
38+
elif int(time_12[4])<0 or int(time_12[4])>59:
39+
raise ValueError
40+
41+
42+
# update to 24 hour
43+
44+
# if AM
45+
if time_12[2] == 'AM' and str(time_12[0]) == '12':
46+
time_12[0]='00'
47+
if time_12[5] == 'AM' and str(time_12[3]) == '12':
48+
time_12[3] = '00'
49+
50+
# if PM
51+
if time_12[2] == 'PM' and str(time_12[0]) != '12':
52+
time_12[0]=int(time_12[0])+12
53+
if time_12[5] == 'PM' and str(time_12[3]) != '12':
54+
time_12[3]=int(time_12[3])+12
55+
56+
# assign updated time_12 into a list
57+
time_24=[time_12[0],time_12[1],time_12[3],time_12[4]]
58+
59+
60+
61+
# format 24 hour time and return
62+
time=f'{str(time_24[0]).zfill(2)}:{str(time_24[1]).zfill(2)} to {str(time_24[2]).zfill(2)}:{str(time_24[3]).zfill(2)}'
63+
return time
64+
65+
else:
66+
raise ValueError
67+
68+
69+
if __name__ == "__main__":
70+
main()

0 commit comments

Comments
 (0)