This is my code:
def url_helper(department):
listoflists = []
s = urllib.request.urlopen("http://cs1110.cs.virginia.edu/files/louslist/" department)
list1 = s.read().decode('utf-8').strip().split('\n')
listoflists.append(list1)
s.close()
return listoflists[0]
def compatible_classes(first_class, second_class, needs_open_space=False):
course_section = first_class[-3:len(first_class)]
course_number = first_class[-8:-4]
course_name = first_class[0:-9]
courselist = url_helper(course_name)
enrollment = 0
monday = 0
tuesday = 0
wednesday = 0
thursday = 0
friday = 0
start_time = 0
end_time = 0
for section in courselist:
d = section.split("|")
if d[1] == course_number and d[2] == course_section:
start_time = d[12]
end_time = d[13]
monday = d[7]
tuesday = d[8]
wednesday = d[9]
thursday = d[10]
friday = d[11]
course_section2 = second_class[-3:len(second_class)]
course_number2 = second_class[-8:-4]
course_name2 = second_class[0:-9]
courselist2 = url_helper(course_name2)
monday2 = 0
tuesday2 = 0
wednesday2 = 0
thursday2 = 0
friday2 = 0
start_time2 = 0
end_time2 = 0
for section in courselist2:
d = section.split("|")
if d[1] == course_number2 and d[2] == course_section2:
start_time2 = d[12]
end_time2 = d[13]
monday2 = d[7]
tuesday2 = d[8]
wednesday2 = d[9]
thursday2 = d[10]
friday2 = d[11]
if max(start_time, end_time) <= min(start_time2, end_time2):
if monday == monday2:
return False
if tuesday == tuesday2:
return False
if wednesday == wednesday2:
return False
if thursday == thursday2:
return False
if friday == friday2:
return False
return True
The first function is a helper function that opens the URL in regards to the certain class department that I want to parse.
The second function should do the following: Returns True if the two given classes are compatible within the same schedule and False otherwise. Additionally, when the needs_open_space argument is True, the function should return False if either of the classes has reached enrollment capacity (regardless of whether or not the classes are compatible). Two classes are considered compatible if there is no overlap between their schedules.
However, I am struggling to figure out what to add to the second function so that it carries out the third parameter needs_open_space
Any help is appreciated, this function is almost finished I just need some guidance.
Here's the output I'm currently getting: True True True
However the output should be: False True False
CodePudding user response:
You should create an early check and return at the start.
Somethings alongs the lines of ... (please do complete it as I'm not sure how exactly you check the needs_open_space argument and which are the variables for enrollment capacity and such).
def compatible_classes(first_class, second_class, needs_open_space=False):
## NEEDS OPEN SPACE CHECKER
if needs_open_space == True:
for myclass in class_list:
if (myclass reached enrollment capacity): return False
course_section = first_class[-3:len(first_class)]
course_number = first_class[-8:-4]
course_name = first_class[0:-9]
courselist = url_helper(course_name)
.
.
.
CodePudding user response:
At first you should correctly parse data. Correct data format allow to do checks easy.
I just added check logic by times (is same day
and is time overlap
). You may add your additional check conditions to is_compatible
function
import urllib.request
from datetime import time
department = 'AIRS'
URL = 'http://cs1110.cs.virginia.edu/files/louslist/'
def get_list(dep):
lines = {"data": []}
with urllib.request.urlopen(URL dep) as resp:
for line in resp.readlines():
line = line.decode('utf-8').rstrip('\n').split('|')
data = {
"department": line[0],
"course_section": line[1],
"course_number": line[2],
"course_name": line[3],
"lector": line[4],
"type": line[5],
"enrollment": line[6],
# monday 1
# tuesday 2
# wednesday 3
# thursday 4
# friday 5
# here is only first element taken (...[0])
# so only one day at week allowed
# if course has couple days you should remove [0]
# and set course_days list instead of first course_day
# don't forget to change "is same day" check logic in this case
"course_day": [x[0] 1 for x in filter(lambda day: True if day[1].lower() == 'true' else False, enumerate([line[7], line[8], line[9], line[10], line[11]]))][0],
"start_time": time(int(line[12][0:2]), int(line[12][2:4])),
"end_time": time(int(line[13][0:2]), int(line[13][2:4])),
"place": line[14],
"dont_know_what_this_mean_1": line[15],
"dont_know_what_this_mean_2": line[16],
}
lines["data"].append(data)
return lines
def is_compatible(first, second):
# check if same day
if first["course_day"] != second["course_day"]:
# different day. no overlap here
return True
# check end and start time if same day
two_times = {"earlier": None, "later": None}
if first["start_time"] < second["start_time"]:
two_times['earlier'] = first
two_times['later'] = second
elif first["start_time"] > second["start_time"]:
two_times['earlier'] = second
two_times['later'] = first
# start time the same
else:
return False
# oops overlap
if two_times['earlier']['end_time'] >= two_times['later']['start_time']:
return False
else:
return True
parsed_data = get_list(department)
days_of_week = [None, "monday", "tuesday", "wednesday", "thursday", "friday"]
for a in parsed_data['data']:
for b in parsed_data['data']:
if a['course_name'] == b['course_name']:
continue
if is_compatible(a, b):
print(
a['course_name'],
'time:',
a["start_time"].strftime('%H:%M'),
'-',
a["end_time"].strftime('%H:%M'),
'day_of_week:',
days_of_week[a['course_day']],
'is compatible with',
b['course_name'],
'time:',
b["start_time"].strftime('%H:%M'),
'-',
b["end_time"].strftime('%H:%M'),
'day_of_week:',
days_of_week[b['course_day']],
)
print('')
print('INCOMPATIBILITIES')
print('')
for a in parsed_data['data']:
for b in parsed_data['data']:
if a['course_name'] == b['course_name']:
continue
if not is_compatible(a, b):
print(
a['course_name'],
'time:',
a["start_time"].strftime('%H:%M'),
'-',
a["end_time"].strftime('%H:%M'),
'day_of_week:',
days_of_week[a['course_day']],
'is incompatible with',
b['course_name'],
'time:',
b["start_time"].strftime('%H:%M'),
'-',
b["end_time"].strftime('%H:%M'),
'day_of_week:',
days_of_week[b['course_day']],
)
Output:
Leadership Laboratory time: 15:30 - 17:30 day_of_week: tuesday is compatible with The Foundations of the U.S. Air Force time: 13:00 - 13:50 day_of_week: tuesday
Leadership Laboratory time: 15:30 - 17:30 day_of_week: tuesday is compatible with The Foundations of the U.S. Air Force time: 13:00 - 13:50 day_of_week: thursday
...
Concepts of Air Force Leadership and Management time: 12:30 - 13:45 day_of_week: tuesday is compatible with Concepts of Air Force Leadership and Management time: 12:30 - 15:00 day_of_week: friday
Concepts of Air Force Leadership and Management time: 12:30 - 13:45 day_of_week: tuesday is compatible with National Security Affairs/Preparation for Active Duty time: 16:30 - 19:00 day_of_week: thursday
Concepts of Air Force Leadership and Management time: 12:30 - 15:00 day_of_week: friday is compatible with Leadership Laboratory time: 15:30 - 17:30 day_of_week: tuesday
Concepts of Air Force Leadership and Management time: 12:30 - 15:00 day_of_week: friday is compatible with The Foundations of the U.S. Air Force time: 13:00 - 13:50 day_of_week: tuesday
Concepts of Air Force Leadership and Management time: 12:30 - 15:00 day_of_week: friday is compatible with The Foundations of the U.S. Air Force time: 13:00 - 13:50 day_of_week: thursday
Concepts of Air Force Leadership and Management time: 12:30 - 15:00 day_of_week: friday is compatible with The Evolution of Air and Space Power time: 14:00 - 14:50 day_of_week: tuesday
Concepts of Air Force Leadership and Management time: 12:30 - 15:00 day_of_week: friday is compatible with The Evolution of Air and Space Power time: 14:00 - 14:50 day_of_week: thursday
Concepts of Air Force Leadership and Management time: 12:30 - 15:00 day_of_week: friday is compatible with Concepts of Air Force Leadership and Management time: 12:30 - 13:45 day_of_week: tuesday
Concepts of Air Force Leadership and Management time: 12:30 - 15:00 day_of_week: friday is compatible with National Security Affairs/Preparation for Active Duty time: 12:30 - 13:45 day_of_week: tuesday
Concepts of Air Force Leadership and Management time: 12:30 - 15:00 day_of_week: friday is compatible with National Security Affairs/Preparation for Active Duty time: 16:30 - 19:00 day_of_week: thursday
National Security Affairs/Preparation for Active Duty time: 12:30 - 13:45 day_of_week: tuesday is compatible with Leadership Laboratory time: 15:30 - 17:30 day_of_week: tuesday
National Security Affairs/Preparation for Active Duty time: 12:30 - 13:45 day_of_week: tuesday is compatible with The Foundations of the U.S. Air Force time: 13:00 - 13:50 day_of_week: thursday
National Security Affairs/Preparation for Active Duty time: 12:30 - 13:45 day_of_week: tuesday is compatible with The Evolution of Air and Space Power time: 14:00 - 14:50 day_of_week: tuesday
National Security Affairs/Preparation for Active Duty time: 12:30 - 13:45 day_of_week: tuesday is compatible with The Evolution of Air and Space Power time: 14:00 - 14:50 day_of_week: thursday
National Security Affairs/Preparation for Active Duty time: 12:30 - 13:45 day_of_week: tuesday is compatible with Concepts of Air Force Leadership and Management time: 12:30 - 15:00 day_of_week: friday
National Security Affairs/Preparation for Active Duty time: 12:30 - 13:45 day_of_week: tuesday is compatible with National Security Affairs/Preparation for Active Duty time: 16:30 - 19:00 day_of_week: thursday
National Security Affairs/Preparation for Active Duty time: 16:30 - 19:00 day_of_week: thursday is compatible with Leadership Laboratory time: 15:30 - 17:30 day_of_week: tuesday
National Security Affairs/Preparation for Active Duty time: 16:30 - 19:00 day_of_week: thursday is compatible with The Foundations of the U.S. Air Force time: 13:00 - 13:50 day_of_week: tuesday
National Security Affairs/Preparation for Active Duty time: 16:30 - 19:00 day_of_week: thursday is compatible with The Foundations of the U.S. Air Force time: 13:00 - 13:50 day_of_week: thursday
National Security Affairs/Preparation for Active Duty time: 16:30 - 19:00 day_of_week: thursday is compatible with The Evolution of Air and Space Power time: 14:00 - 14:50 day_of_week: tuesday
National Security Affairs/Preparation for Active Duty time: 16:30 - 19:00 day_of_week: thursday is compatible with The Evolution of Air and Space Power time: 14:00 - 14:50 day_of_week: thursday
National Security Affairs/Preparation for Active Duty time: 16:30 - 19:00 day_of_week: thursday is compatible with Concepts of Air Force Leadership and Management time: 12:30 - 13:45 day_of_week: tuesday
National Security Affairs/Preparation for Active Duty time: 16:30 - 19:00 day_of_week: thursday is compatible with Concepts of Air Force Leadership and Management time: 12:30 - 15:00 day_of_week: friday
National Security Affairs/Preparation for Active Duty time: 16:30 - 19:00 day_of_week: thursday is compatible with National Security Affairs/Preparation for Active Duty time: 12:30 - 13:45 day_of_week: tuesday
INCOMPATIBILITIES
The Foundations of the U.S. Air Force time: 13:00 - 13:50 day_of_week: tuesday is incompatible with Concepts of Air Force Leadership and Management time: 12:30 - 13:45 day_of_week: tuesday
The Foundations of the U.S. Air Force time: 13:00 - 13:50 day_of_week: tuesday is incompatible with National Security Affairs/Preparation for Active Duty time: 12:30 - 13:45 day_of_week: tuesday
...
National Security Affairs/Preparation for Active Duty time: 12:30 - 13:45 day_of_week: tuesday is incompatible with Concepts of Air Force Leadership and Management time: 12:30 - 13:45 day_of_week: tuesday