徐福福 发表于 2023-10-31 23:55:46

问题求助

It takes a list of list representing the exam record of a student for three years, with each sub-list representing an attempt at a course in the form [<course name>, <unit>, <year>, <attempt>, <mark>]. See the section Exam record format for more details
It returns the corresponding classification (type: str, possible values: 'First Class Honours', 'Upper Second Class Honours', 'Lower Second Class Honours', 'Third Class Honours', 'Pass' (if penalty rule considered) and 'Fail') the student will get based on the given exam record
Exam record format
The exam record is in the form of a list of list, with each sub-list representing an attempt at a course in the form [<course name>, <unit>, <year>, <attempt>, <mark>]. Here is an example of a record:

[['ST101A', 0.5, 1, 1, 92],
['MA100', 1, 1, 1, 39],
['MA100', 1, 1, 2, 54],
['EC1A3', 0.5, 1, 1, 22],
['EC1A3', 0.5, 1, 2, 34],
['EC1A3', 0.5, 1, 3, 32],
...
['MA222', 0.5, 2, 1, 30],
['MA222', 0.5, 2, 2, 38],
...
['ST310', 0.5, 3, 1, 88],
['ST311', 0.5, 3, 1, 67]]
Some explanation:

['ST101A', 0.5, 1, 1, 92] means the student took the course 'ST101A' which is a half-unit in year 1. The record is for the first attempt with a mark of 92
['MA100', 1, 1, 1, 39] means the student took the course 'MA100' which is a full-unit in year 1. The record is for the first attempt with a mark of 39
['MA100', 1, 1, 2, 54] means the student took the course 'MA100' which is a full-unit in year 1. The record is for the second attempt with a mark of 54
['EC1A3', 0.5, 1, 3, 32] means the student took the course 'EC1A3' which is a half-unit in year 1. The record is for the third attempt with a mark of 32
['MA222', 0.5, 2, 2, 38] means the student took the course 'MA222' which is a half-unit in year 2. The record is for the second attempt with a mark of 38
['ST311', 0.5, 3, 1, 67] means the student took the course 'ST311' which is a half-unit in year 3. The record is for the first attempt with mark 67
Instructions
Describe how you will implement get_classification() via decomposition. Please also state what function(s) you will define, what they do to help you solve the original problem and implement get_classification(). In src/lse_classification.py, implement as far as you can towards implementing get_classification().

Your answer: _________
Assumptions on the function argument
All marks are int, with each mark between 0 and 100
Assume the exam record is complete, with no further re-attempt or resit is needed
For simplicity, if "LSE100" is in the record, it bears 0.5 units
You may ignore the penalty rule of LSE listed in part (a)
Other requirements
Use lse_classification as a module. Please do NOT copy the function definitions into test_lse_classification.py

isdkz 发表于 2023-10-31 23:55:58

我将使用以下步骤来实现 get_classification() 函数:

1. 首先,我将定义一个名为 calculate_average_mark() 的函数,该函数将接收一个学生的考试记录,并计算出该学生的平均分数。这将有助于确定学生的等级。

2. 接下来,我将定义一个名为 classify_student() 的函数,该函数将接收一个学生的平均分数,并根据一些条件将其分类为不同的等级。这个函数将返回学生的等级。

3. 最后,我将实现 get_classification() 函数,该函数将接收一个学生的考试记录,并使用前面定义的两个函数来计算并返回学生的等级。

下面是代码实现的示例:

# src/lse_classification.py

def calculate_average_mark(exam_record):
    total_marks = 0
    total_units = 0

    for record in exam_record:
      mark = record
      unit = record
      total_marks += mark * unit
      total_units += unit

    average_mark = total_marks / total_units
    return average_mark

def classify_student(average_mark):
    if average_mark >= 70:
      return 'First Class Honours'
    elif average_mark >= 60:
      return 'Upper Second Class Honours'
    elif average_mark >= 50:
      return 'Lower Second Class Honours'
    elif average_mark >= 40:
      return 'Third Class Honours'
    elif average_mark >= 35:
      return 'Pass'
    else:
      return 'Fail'

def get_classification(exam_record):
    average_mark = calculate_average_mark(exam_record)
    classification = classify_student(average_mark)
    return classification


通过这种方式,我们将问题分解为几个较小的函数,每个函数都有明确定义的功能。这使得代码更易于理解、测试和维护。

Mike_python小 发表于 2023-10-31 23:57:18

Analysis and Plan

To implement the get_classification() function, we will need to perform several steps:

1. Calculate the weighted average mark for each year.
2. Determine the overall classification based on the weighted averages.

To achieve this, we can define the following helper functions:

1. calculate_weighted_average(): This function takes the exam record as input and calculates the weighted average mark for each year. It returns a dictionary with the year as the key and the weighted average mark as the value.

2. determine_classification(): This function takes the calculated weighted averages as input and determines the overall classification based on the given criteria. It returns the corresponding classification as a string.

We will now proceed to implement these helper functions in the src/lse_classification.py file.

Implementation

In src/lse_classification.py:


def calculate_weighted_average(exam_record):
    weighted_averages = {}
   
    for course in exam_record:
      unit = course
      mark = course
      year = course
      
      if year not in weighted_averages:
            weighted_averages = []
      
      weighted_averages.append((unit, mark))
   
    for year, courses in weighted_averages.items():
      total_units = sum( for course in courses])
      weighted_sum = sum( * course for course in courses])
      weighted_average = weighted_sum / total_units
      weighted_averages = weighted_average
   
    return weighted_averages


def determine_classification(weighted_averages):
    overall_average = sum(weighted_averages.values()) / len(weighted_averages)
   
    if overall_average >= 70:
      return 'First Class Honours'
    elif overall_average >= 60:
      return 'Upper Second Class Honours'
    elif overall_average >= 50:
      return 'Lower Second Class Honours'
    elif overall_average >= 40:
      return 'Third Class Honours'
    elif overall_average >= 30:
      return 'Pass'
    else:
      return 'Fail'


def get_classification(exam_record):
    weighted_averages = calculate_weighted_average(exam_record)
    classification = determine_classification(weighted_averages)
   
    return classification


This implementation follows the decomposition plan described above. The calculate_weighted_average() function calculates the weighted averages for each year based on the exam record, and the determine_classification() function uses these weighted averages to determine the overall classification. Finally, the get_classification() function combines these helper functions to provide the desired classification based on the given exam record.

By using this modular approach, we ensure that the code is organized, reusable, and easier to maintain.

球一个最佳答案谢谢啦!这对我非常重要!{:10_254:}{:10_254:}{:10_254:}

mwy1024 发表于 2023-11-1 11:54:33

{:10_266:}
页: [1]
查看完整版本: 问题求助