Working 9 to 5 -Problem Set 7 (CS50's Introduction to Programming with Python)

preview_player
Показать описание
Hello, everyone and welcome to another video, in this video, I will explain and solve problem set 7, Working 9 to 5.

if you haven't already submitted this problem, You have to try this on your own.
This video is to just give you a way to solve this problem, try solving it using a different approach, to get the most benefits out of this experience.

Don't forget to hit the like button and subscribe to the channel, and if you have any questions or want an explanation for a specific problem leave a comment down below.

Рекомендации по теме
Комментарии
Автор

assalamu alaikum my brother omar your teaching style is really amazing next upload data structure and algorithm in python please Insha Allah❤❤

muhammedidreess
Автор

hey I am receiving NameError on line 44 for the time2 variable.. please help

ravenclaw
Автор

Thank you. Very clear explanation. PLS On my version when I run “pytest test_working.py” on its own everything is green. However when I run check50, I get the following message “correct working.py passes all test_working checks expected exit code 0 not 2” . I cannot find the error . In working.py I ended the ValueError with “raise” . Do you have an idea why this happens

che_pibe
Автор

You shouldn't do re.IGNORECASE as the problem lets you enforce AM and PM as capitalised

Sabre
Автор

just done submitting an 80-line solution (😨) and came here to see an obviously better solution with better logic😊

getstart
Автор

Here is how I solved it (with comments), hope it helps:

import re

# MAIN FUNCTION #

def main():
print(convert(input("Hours: ")))


# CONVERT HOURS #


def convert_hours(h, am_pm):
# return (h+12) if it is "PM" and but not 12 PM:
if am_pm == "PM" and int(h) != 12:
return (int(h) + 12)

# return 0 if it is 12 AM:
elif am_pm == "AM" and int(h) == 12:
return 0

# otherwise if it is not PM, or not 12 AM or PM, return 'h' as it is:
return h


# CONVERT FUNCTION #


def convert(s):
# find matches to the pattern, "?P<h1>" represents the capture group which is quantity of hour of first time:
matches = re.search(r"^(?P<h1>\d{1, 2})(:| )(?P<m1>\d{2})? ?(?P<am_pm1>\w{2}) to (?P<h2>\d{1, 2})(:| )(?P<m2>\d{2})? ?(?P<am_pm2>\w{2})", s)

# detect early and raise ValueError if there is no matches:
if matches is None:
raise ValueError

h1 = matches.group("h1")
h2 = matches.group("h2")
m1 = matches.group("m1")
m2 = matches.group("m2")
am_pm1 = matches.group("am_pm1")
am_pm2 = matches.group("am_pm2")

try:
# check if both hours are valid, else raise Value Error:
if 1 <= int(h1) <= 12 and 1 <= int(h2) <= 12:

# check if AM or PM are present, else raise Value Error:
if am_pm1 and am_pm2 in ["AM", "PM"]:

# if both minute values exist, check that they are in range (between 0 & 59) if they both exist (e.g. 9:"30" AM, 12:"45" PM)
if m1 != None and m2 != None and 0 <= int(m1) <= 59 and 0 <= int(m2) <= 59:

# pass h1 and h2 to the function created before:
h1 = convert_hours(h1, am_pm1)
h2 = convert_hours(h2, am_pm2)

return f"{int(h1):02}:{m1:02} to {int(h2):02}:{m2:02}"

# if m1 doesn't exist and m2 does, check if m2 is in range (e.g. 9 AM to 5:30 PM):
elif m1 == None and m2 != None and 0 <= int(m2) <= 59:
h1 = convert_hours(h1, am_pm1)
h2 = convert_hours(h2, am_pm2)
return f"{int(h1):02}:00 to {int(h2):02}:{m2:02}"

# if m1 exists and m2 doesn't, check if m1 is in range (e.g. 9:30 AM to 5 PM):
elif m1 != None and m2 == None and 0 <= int(m1) <= 59:
h1 = convert_hours(h1, am_pm1)
h2 = convert_hours(h2, am_pm2)
return f"{int(h1):02}:{m1:02} to {int(h2):02}:00"

# if m1 and m2 don't exist (e.g. 9 AM to 5 PM):
elif m1 == None and m2 == None:
h1 = convert_hours(h1, am_pm1)
h2 = convert_hours(h2, am_pm2)
return f"{int(h1):02}:00 to {int(h2):02}:00"

else:
raise ValueError

else:
raise ValueError

else:
raise ValueError

except:
raise ValueError


# CALL MAIN #

if __name__ == "__main__":
main()

# END #

mukulparashar
Автор

you didnot raise ValueError when time is greater than 12

nepalidubbed
Автор

Buddy, I am failing third test in pytest, I don't know why. It's showing, failed: did not raise class ValueError.

ashmitmishra
Автор

Bro can you please upload final set problem tutorial. Please upload

ashmitmishra
Автор

The issue with the provided code lies in the handling of minutes and the conversion to 24-hour format for the "PM" case.

Handling of Minutes:

The regular expression (\d{1, 2}):?(\d{2})? doesn't strictly enforce the range of minutes. It allows for values greater than or equal to 60, which is invalid.

The error handling for minutes only checks for values greater than or equal to 60 after the groups are extracted. It's better to validate the minutes within the regular expression itself.

Conversion to 24-hour format for "PM" case:

The if time.group(3) == 'PM' and hour1!=12: block doesn't handle the case when hour1 is '12': it increments hour1 by 12, resulting in an incorrect value of 24.

Similarly, the if time.group(6) == 'PM'and hour2!=12: block doesn't handle the case when hour2 is '12': it increments hour2 by 12, resulting in an incorrect value of 24.

To fix these issues, modify the regular expression to strictly enforce the range of minutes and handle the "PM" case more carefully:

Python
time = re.search(r'^(\d{1, 2}):(\d{2}) (AM|PM) to (\d{1, 2}):(\d{2}) (AM|PM)$', s, re.IGNORECASE)
Use code with caution. Learn more
And update the conversion to 24-hour format:


if time.group(3) == 'PM':
if hour1 == 12:
hour1 = 0
else:
hour1 += 12

if time.group(6) == 'PM':
if hour2 == 12:
hour2 = 0
else:
hour2 += 12

SwapnilAngarkhe
Автор

I am losing my shit over here, attempt number 5 and keeps saying exit code 1, which for me it always does if you raise value errors. Which you must do according to the assignment

VinnieG-
Автор

Thanks man, for this coding! but this does not pass the cs50 check and displays the following error:

:( test_working.py catches working.py not raising ValueError when user omits " to "
expected exit code 1, not 0
:( test_working.py catches working.py not raising ValueError for out-of-range times
expected exit code 1, not 0
:( test_working.py catches working.py not raising ValueError for invalid time format
expected exit code 1, not 0

bible-verses-and-prayer
Автор

Please, can you help me?

Only this problem is missing for me, but I can't pass the check50, and I don't understand why.

Even if my script is a little complicated, and I don't use regular expressions, everything works fine: I passes all of my manual tests, and also the pytest tests.


This is the check50 error message:

:( working.py does not import libraries other than sys and re
working.py imports libraries other than sys and re
Be sure only to use "import re" and "import sys", or "from re import ..." and "from sys import ..."

But it's not true! I import only re!
To be totally sure I installed pipreqs (using pip), which exports a text file with all the requirements of a project in a folder. And...nothing! The only requirement for the project is pytest...

Here is my script:

Please help me

gars