I just deep dived to Python and I came with this Hackerrank problem:
Create two class, Person with firstName, lastName, idNumber as its attributes with method printPerson() simply print last name and first name and id, and the derived class Student.
The Student class has additional attributes, its score and an array with two value, and I need to calculate the average of all scores in this array. Then finally I will decide which letter I should assign to this average score and print it.
Sample Input:
Heraldo Memelli 8135627
2
100 80
Sample Output:
Name: Memelli, Heraldo
ID: 8135627
Grade: O
My code in Python 3:
class Person:
def __init__(self, firstName, lastName, idNumber):
self.firstName = firstName
self.lastName = lastName
self.idNumber = idNumber
def printPerson(self):
print("Name:", self.lastName + ",", self.firstName)
print("ID:", self.idNumber)
def avg(a):
return sum(a)/len(a)
def m(a):
return [int(r) for r in list(a.values())[0].split('-')]
class Student(Person):
def __init__(self, f, l, i, s):
Person.__init__(self, f, l, i)
self.s = s
def calculate(self):
avgScore = avg(self.s)
scoreList = [
{'O': '90-100'},
{'E': '80-89'},
{'A': '70-79'},
{'P': '55-69'},
{'D': '40-54'},
{'T': '0-39'},
]
scoreLetter = list([x for x in scoreList if m(x)[0] <= avgScore == avgScore <=m(x)[1]][0].keys())[0]
return '{}'.format(scoreLetter)
line = input().split()
firstName = line[0]
lastName = line[1]
idNum = line[2]
numScores = int(input()) # not needed for Python
scores = list( map(int, input().split()) )
s = Student(firstName, lastName, idNum, scores)
s.printPerson()
print("Grade:", s.calculate())
It passes all test cases. Could you please help me to review it and give me some advice?