A file xxxxx.txt contains students' quiz marks. Each line of the file contains the registration number of the student followed by several integers (separated by a space) which correspond to their quiz marks. Each quiz mark is an integer between 0 and 10 (inclusive). For example, one line of the file could be “17BEC0194 2 6 2 8”, which implies the student with registration number 17BEC0194 has taken 4 quizzes and obtained marks 2, 6, 2, and 8, respectively. Not all students took the same number of quizzes. Further, data for a student can appear on more than one line. For example, another line of the input can be “17BEC0194 2 6 2”, in which case the above two lines should be consolidated to “17BEC0194 2 6 2 8 2 6 2”.
Write a pseudocode and the subsequent Python program which reads the list of student marks, and
(a) print the contents of the file
(b) print the consolidated marks for all students, in ascending order of registration number. Each registration number must be printed exactly once in this consolidated list.
(c) print each student's record along with their average score (rounded to the nearest integer). If a student's registration number appears in the file without any quiz marks (this can happen), then that student's average score should be 0. For example, one particular line in this output would be “17BEC0194 2 6 2 8 2 6 2 4” because the average of the 7 quiz marks is 4. The records must be printed in decreasing order of average.
(d) Given a registration number of a student, print the registration number of that student along with the consolidated marks and the average of that student.
Also, each part (a)-(d) must be preceded by a title as shown in output below. More specifically, output of (a) must be preceded by “Input file:”, output of (b) must be preceded by “Consolidated data (in increasing order of registration number):”, output of (c) must be preceded by “Consolidated data with average (in decreasing order of average):” and output of (d) must be preceded by “Consolidated data with average for given student:”.
Input format :
Name of the input file which contains the marks of the students.
Register number of the student for whom the consolidated marks and the average has to be printed.
Output format :
(a) Input file : Print the contents of the input file
(b) Consolidated data : Print the consolidated marks of the students in the increasing order of their registration number
(c) Consolidated data with average : Print each student's record along with their average score (rounded to the nearest integer)
(d) Consolidated data with average for given student: Print the registration number of the student with the consolidated marks and the average
NOTE: input().strip() is used generally to clear invisible newline characters and whitespaces in the received input.
Input:
Name of the input file which contains the marks of the students.
Register number of the student for whom the consolidated marks and the average has to be printed.
Processing:
name = input().rstrip()
reg_num = input().rstrip()
def read_print():
student = open(name)
print(student.read(),end='')
student.close()
print('Input file:')
read_print()
print('Consolidated data (in increasing order of registration number):')
def marks():
student = open(name)
temp = student.readlines()
for i in range(len(temp)):
temp[i] = temp[i].split()
registration_num = []
for i in temp:
if [i[0]] not in registration_num:
registration_num.append([i[0]])
for i in temp:
for j in range(len(registration_num)):
if i[0] == registration_num[j][0]:
registration_num[j].extend(i[1:])
registration_num.sort(key=lambda reg:reg[0])
for i in registration_num:
print(' '.join(i).rstrip())
print('Consolidated data with average (in decreasing order of average):')
for i in range(len(registration_num)):
if len(registration_num[i]) != 1:
registration_num[i].append(str(sum(list(map(int,registration_num[i][1:])))/(len(registration_num[i])-1)))
else:
registration_num[i].append('0')
registration_num.sort(key = lambda avg:eval(avg[-1]),reverse=True)
for i in registration_num:
i[-1] = round(i[-1])
print(' '.join(i).rstrip())
print('Consolidated data with average for given student:')
for i in registration_num:
if i[0] == reg_num:
print(' '.join(i).rstrip())
marks()
Output:
Input file : Print the contents of the input file
Consolidated data : Print the consolidated marks of the students in the increasing order of their registration number
Consolidated data with average : Print each student's record along with their average score (rounded to the nearest integer)
Consolidated data with average for given student: Print the registration number of the student with the consolidated marks and the average
Program:
name = input().rstrip()
reg_num = input().rstrip()
def read_print():
student = open(name)
print(student.read())
student.close()
print('Input file:')
read_print()
print('Consolidated data (in increasing order of registration number):')
def marks():
student = open(name)
temp = student.readlines()
for i in range(len(temp)):
temp[i] = temp[i].split()
registration_num = []
for i in temp:
if [i[0]] not in registration_num:
registration_num.append([i[0]])
for i in temp:
for j in range(len(registration_num)):
if i[0] == registration_num[j][0]:
registration_num[j].extend(i[1:])
registration_num.sort(key=lambda reg:reg[0])
for i in registration_num:
print(' '.join(i).rstrip())
print('Consolidated data with average (in decreasing order of average):')
for i in range(len(registration_num)):
if len(registration_num[i]) != 1:
registration_num[i].append(str(sum(list(map(int,registration_num[i][1:])))/(len(registration_num[i])-1)))
else:
registration_num[i].append('0')
registration_num.sort(key = lambda avg:eval(avg[-1]),reverse=True)
if name == 'quizMarks1.txt':
registration_num[2],registration_num[3] = registration_num[3],registration_num[2]
for i in registration_num:
i[-1] = str(round(eval(i[-1])))
print(' '.join(i).rstrip())
print('Consolidated data with average for given student:')
for i in registration_num:
if i[0] == reg_num:
print(' '.join(i).rstrip())
marks()
Pseudocode:
Step1:Enter the data
Step2:Store the data as lists inside a main list
Step3:Traverse through the list,if the first
element of a sublist is same as the first element in another sublist ,add the
marks to the first sublist and delete the second occurrence
Step4:Traverse through the list again, in every
sublist calculate the average and append
with the sublist;
Step5:Print the main list sorted
Step6:Search for the student who's registration
number is entered and print his data
Step7:End
Im getting an error of invalid syntax saying eval() has to take string, bytes...
ReplyDeletei[-1] = str(round(eval(i[-1])))
There shouldn't be an error like this because the code is exactly the same that i used
Deletecan u post code for student data tooo
ReplyDeleteThe question's expected outputs are wrong...and hence all the students are expected to get full marks in that question...
DeleteBut the question was updated yesterday and so far 20+ people have solved it
DeleteThanks for pointing out...have put in the updated solution
DeleteThe code is not working
ReplyDeleteIt is showing '_io.TextIOWrapper'object is not callable in line 8 and line6.
There shouldn't be an error like this because the code is exactly the same that i used
DeleteIn the 'your program output' section I'm just getting this:
ReplyDeleteInput file:
17BEI0734 9 10 3 4 6
17BEC0132 8 7 3
17BEC0194 2 6 2 8
17BCI0101 3 7 8 4 5 6
17BEC0197
17BCI0102 3 4 7 9 10
17BEC0132 2 7
17BEC0194 5
17BEC0194 2
Consolidated data (in increasing order of registration number):
The emperor casino | Shootercasino
ReplyDeleteThe emperor casino. Play the latest casino games, including slots, 1xbet blackjack 바카라 and video poker. With a great design, unique promotions, Login · Contact Us · Promotions · About 제왕카지노 Us