0

I'm studying counting and sorting. I have a problem with this task:

Some people participate in math competition. Winner is a person with the biggest amount of points reached (winners are distributed for each grade). Find the value of max points reached for each grade. In each grade takes part one person or more

INPUT

Information about all participants in a file ('input.txt', for example) where each line is supposed to have this structure:

family_name name grade result

Family name and name should be written as a string, grades are in range 10 to 12, results (points) are in range 0 to 100.

OUTPUT

Print three numbers: points of each winner of 10th, 11th and 12th grades. Inner file is supposed to be written in this way:

open('input.txt', 'r', encoding='utf8')

I have already solved this task but I didn't use counting sorting. Here is full code

def listAverage(list): 
    sum = 0 
    for i in list: 
        sum += i
    return sum / len(list) 
classes = []
inFile = open('input.txt', 'r', encoding='utf-8') 
for line in inFile: 
    grade, mark = line.split()[2:] 
    if grade in classes: 
        classes[grade].append(int(mark)) 
    else: 
        classes[grade] = [int(mark)] 
for i in range(10, 13): 
    print(listAverage(classes[str(i)]), end=' ') 
  • Add this in question. – shaik moeed May 29 '19 at 12:07
  • Hi pal! Add additional informations using the [edit](https://stackoverflow.com/posts/56360255/edit) button of the question, and not the comment section. Thank you for your understanding – user3837868 May 29 '19 at 12:09
  • @VugarSamedley what exactly is the question? Does your code have a specific issue that you need help with? – Ralf May 29 '19 at 13:54
  • no problem with my code. i mean, i have no idea of how to solve this task using counting sotrting. that's why i am asking for help – Vugar Samedley May 30 '19 at 18:55

2 Answers2

0

A few observations:

  • don't use variable that shadow built-in names; for example list should not be used, rather use num_list or something similar.
  • you should always close files that you open. I suggest using a context manager (with open() as f:) to avoid forgetting to close files.
  • a shorter version the calculate the average would be sum(num_list) / len(num_list) (raises error if list is empty; see also this question)

Here is a modified version of your code to address some issues; I also included a few suggestions (for example collections.defaultdict).

Maybe this helps you (or maybe not).

import collections
marks_per_grade = collections.defaultdict(list)

with open('input.txt', 'r', encoding='utf-8') as f:
    for line in f:
        line = line.strip()             # remove newline from the end of the line

        if len(line) > 0:               # assure that the line is not empty
            last_name, first_name, grade, mark = line.split()

            # because it is a 'collections.defaultdict', if the grade does not
            # yet exist, then it will add it with an empty list as initial value
            marks_per_grade[int(grade)].append(int(mark))

for grade, mark_list in sorted(marks_per_grade.items()):
    # this will raise error if the list is empty
    avg_mark = sum(mark_list) / len(mark_list)

    print(
        '{:3d} {:3d} {:5.1f} {}'.format(
            grade,
            max(mark_list),
            avg_mark,
            mark_list))
Ralf
  • 16,086
  • 4
  • 44
  • 68
0
data = {10: [], 11: [], 12: []}
xxx = open('input.txt', 'r', encoding='utf8')
with xxx as file:
    for lines in file:

        line = lines.strip()

        firstName, name, grade, mark = line.split()

        data[int(grade)].append(int(mark))

print(data)

for grade in data:
    print(sum(data[grade])/len(data[grade]))
dome
  • 820
  • 7
  • 20