【问题标题】:Print the assignments based on due date and in order根据截止日期并按顺序打印作业
【发布时间】:2022-11-14 10:05:27
【问题描述】:

我正在尝试在截止日期之前显示作业。 我正在尝试根据截止日期对作业进行排序,然后按该顺序打印作业。

class Assignment:
    def __init__(self):
        #Don't change.
        self.assignments = []

    def add(self, due_date, course):
        # Add the due_date, course as list to the assignments list.
        # Nothing is returned.

    def remove(self, due_date, course):
        # Create a list with due_date and course.   
        # If the [due_date, course] list is in assignments list, then remove it from the list.
        # Otherwise, do nothing. 
        # Nothing is returned.
    
    def display_assignment(self):
        print("Display assignments by order they are entered.")
        # Display each assignment by the order they entered, line by line.

    def display_assignment_by_due_date(self):
        print("Display assignments by due dates.")
        # Display each assignment ordered by due date, line by line.

#Do not change.
if __name__ == "__main__":
    assignment = Assignment()
    assignment.add("10/21/2020", "COMP120")
    assignment.add("10/30/2020", "MATH220")
    assignment.add("10/07/2020", "ITEC136")
    assignment.add("10/14/2020", "ENGL120")

    assignment.display_assignment()
    assignment.display_assignment_by_due_date()

    assignment.remove("10/14/2020", "ENGL120")

    assignment.display_assignment()
    assignment.display_assignment_by_due_date()

【问题讨论】:

  • 你能发布你已经尝试过的代码吗?
  • 你的 display_assignment() 代码是什么?我建议添加一个包含作业列表的新类,以便更轻松地正确使用它们。

标签: python


【解决方案1】:

这是您的解决方案。最好使用 python datetime 库,然后您可以使用 python sorted 内置函数对其进行排序。删除项目时,最好重新创建self.assignments 列表,但不包括等于“删除”项目的项目。

from datetime import datetime

class Assignment:
    def __init__(self):
        #Don't change.
        self.assignments = []

    def add(self, due_date, course):
        # Add the due_date, course as list to the assignments list.
        # Nothing is returned.
        self.assignments.append({"due_date":datetime.strptime(due_date, "%m/%d/%Y"), "course":course})

    def remove(self, due_date, course):
        # Create a list with due_date and course.   
        # If the [due_date, course] list is in assignments list, then remove it from the list.
        # Otherwise, do nothing. 
        # Nothing is returned.
        item_to_remove = {"due_date":datetime.strptime(due_date, "%m/%d/%Y"), "course":course}
        self.assignments = [i for i in self.assignments if i != item_to_remove]
    
    def display_assignment(self):
        print("Display assignments by order they are entered.")
        # Display each assignment by the order they entered, line by line.
        for assignment in self.assignments:
            print(assignment)

    def display_assignment_by_due_date(self):
        print("Display assignments by due dates.")
        # Display each assignment ordered by due date, line by line.
        for assignment in sorted(self.assignments, key=lambda x: x["due_date"]):
            print(assignment)


#Do not change.
if __name__ == "__main__":
    assignment = Assignment()
    assignment.add("10/21/2020", "COMP120")
    assignment.add("10/30/2020", "MATH220")
    assignment.add("10/07/2020", "ITEC136")
    assignment.add("10/14/2020", "ENGL120")

    assignment.display_assignment()
    assignment.display_assignment_by_due_date()

    assignment.remove("10/14/2020", "ENGL120")

    assignment.display_assignment()
    assignment.display_assignment_by_due_date()

【讨论】:

    猜你喜欢
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 2021-09-21
    • 2016-09-21
    相关资源
    最近更新 更多