Preparing git repo for final project

This commit is contained in:
2023-07-09 11:19:26 +02:00
parent 6a38966eef
commit 63d06d6b35
67 changed files with 1587 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
import re
import sys
def main():
print(validate(input("IPv4 Address: ")))
def validate(ip):
if not re.match(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$', ip):
return False
for n in map(int, ip.split('.')):
if not 0 <= n <= 255:
return False
return True
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,32 @@
from numb3rs import validate
def main():
test_non_digits()
test_negative_numbers()
test_numbers_too_big()
test_numbers_too_long()
def test_non_digits():
assert validate('aaa.bbb.ccc.ddd') == False
assert validate('cat') == False
def test_negative_numbers():
assert validate('-11.111.-1.-1') == False
def test_numbers_too_big():
assert validate('257.200.1.1') == False
assert validate('200.257.1.1') == False
assert validate('200.1.257.1') == False
assert validate('200.1.5.257') == False
def test_numbers_too_long():
assert validate('200.1.5.2.57') == False
if __name__ == '__main__':
main()

View File

@@ -0,0 +1,23 @@
from validator_collection import validators
def main():
if validate_email(input("What's your email address?\t\t")):
print("Valid")
else:
print("Invalid")
def validate_email(email: str) -> bool:
try:
email_address = validators.email(email, allow_empty = True)
except Exception as e:
email_address = None
if not email_address:
return False
return True
if __name__ == '__main__':
main()

View File

@@ -0,0 +1,16 @@
from response import validate_email
def main():
test_cs50p()
def test_cs50p():
assert validate_email("malan@harvard.edu")== True
assert validate_email("info@cheznadi.com")== True
assert validate_email("malan@@@harvard.edu")== False
assert validate_email("info@cheznadi..com")== False
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,37 @@
from um import count
def main():
test_count_ums()
test_words_containing_ums()
test_punctuation()
test_from_cs50p()
def test_count_ums():
assert count("Hello, um, world") == 1
assert count("Hello um, um, world um") == 3
assert count("Um Hello, um, world um") == 3
def test_words_containing_ums():
assert count("instrumentation") == 0
assert count("umbrella") == 0
assert count("momentum") == 0
def test_punctuation():
assert count("Hello um?") == 1
assert count("Hello um!") == 1
assert count("Hello um") == 1
assert count("Hello um.") == 1
def test_from_cs50p():
assert count("um") == 1
assert count("um?") == 1
assert count("Um, thanks for the album.") == 1
assert count("Um, thanks, um...") == 2
if __name__ == '__main__':
main()

15
problems/pset7/um/um.py Normal file
View File

@@ -0,0 +1,15 @@
import re
um_regex = re.compile(r"\bum\b", re.IGNORECASE)
def main():
print(count(input("Text: ")))
def count(s):
return len(um_regex.findall(s))
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,20 @@
import re
youtube_regex = re.compile(r"<iframe.*https?://(?:www\.)?youtube\.com/embed/(\w+).*</iframe>")
def main():
print(parse(input("HTML: ")))
def parse(s):
video_id = youtube_regex.search(s)
if video_id:
video_id = video_id.groups()[0]
else:
return None
return f"https://youtu.be/{video_id}"
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,102 @@
import pytest
from working import convert
def main():
test_missing_to()
test_out_of_range_times()
test_exception()
test_hours_off_by_one()
def test_missing_to():
try:
convert('09:00 AM - 05:00 PM')
except ValueError:
pass
else:
raise ValueError("Missing to")
try:
convert('09:00 AM 05:00 PM')
except Exception as e:
assert isinstance(e, ValueError)
def test_out_of_range_times():
try:
convert('09:00 AM to 05:61 PM')
except Exception as e:
assert isinstance(e, ValueError)
try:
convert('31:00 AM to 05:00 PM')
except Exception as e:
assert isinstance(e, ValueError)
try:
convert('13:00 PM to 8:00 AM')
except Exception as e:
assert isinstance(e, ValueError)
try:
convert('13:00PM to 8:00 AM')
except Exception as e:
assert isinstance(e, ValueError)
try:
convert('13:00 to 8:00')
except Exception as e:
assert isinstance(e, ValueError)
try:
convert("09:00 AM - 17:00 PM")
except Exception as e:
assert isinstance(e, ValueError)
try:
convert("9:00 AM 5:00 PM")
except Exception as e:
assert isinstance(e, ValueError)
try:
convert("9:60 AM to 5:60 PM")
except Exception as e:
assert isinstance(e, ValueError)
try:
convert("9 AM - 5 PM")
except Exception as e:
assert isinstance(e, ValueError)
try:
convert("9 AM5 PM")
except Exception as e:
assert isinstance(e, ValueError)
try:
convert("9 AM 5 PM")
except Exception as e:
assert isinstance(e, ValueError)
with pytest.raises(ValueError):
convert("9:72 to 6:30")
def test_exception():
with pytest.raises(ValueError):
convert("09:00 AM - 17:00 PM")
with pytest.raises(ValueError):
convert("9:00 AM 5:00 PM")
with pytest.raises(ValueError):
convert("9:60 AM to 5:60 PM")
with pytest.raises(ValueError):
convert("9 AM - 5 PM")
with pytest.raises(ValueError):
convert("9 AM5 PM")
with pytest.raises(ValueError):
convert("9 AM 5 PM")
with pytest.raises(ValueError):
convert("9:72 to 6:30")
def test_hours_off_by_one():
assert convert('12:00 AM to 12:00 PM') == "00:00 to 12:00"
assert convert("9 AM to 5 PM") == "09:00 to 17:00"
assert convert("8 PM to 8 AM") == "20:00 to 08:00"
assert convert("8:00 PM to 8:00 AM") == "20:00 to 08:00"
assert convert("12 AM to 12 PM") == "00:00 to 12:00"
assert convert("12:00 AM to 12:00 PM") == "00:00 to 12:00"
if __name__ == '__main__':
main()

View File

@@ -0,0 +1,39 @@
import re
american_time_regex = re.compile(r"^\s*(\d+)(?:\:(\d{2}))? ([AP])M to (\d+)(?:\:(\d{2}))? ([AP])\s*M$")
def main():
print(convert(input("Hours: ")))
def convert(s):
time = american_time_regex.match(s)
if time is None:
raise ValueError("Invalid input time")
if len(time.groups()) == 4:
start_h, start_ampm, end_h, end_ampm = time.groups()
start_m, end_m = 0, 0
else:
start_h, start_m, start_ampm, end_h, end_m, end_ampm = time.groups()
start_m, end_m = map(lambda x: int(x) if x else 0, (start_m, end_m))
start_h, end_h = map(int, (start_h, end_h))
if start_h > 12 or end_h > 12:
raise ValueError
if (start_ampm == 'P' and start_h != 12) or (start_ampm == 'A' and start_h == 12):
start_h += 12
if (end_ampm == 'P' and end_h != 12) or (end_ampm == 'A' and end_h == 12):
end_h += 12
if start_h > 24 or end_h > 24 or start_m > 59 or end_m > 59:
raise ValueError
start_h = start_h % 24
end_h = end_h % 24
return f"{start_h:0>2}:{start_m:0>2} to {end_h:0>2}:{end_m:0>2}"
...
if __name__ == "__main__":
main()