HackerRank Day 12 Solution in Python: Inheritance

Today we will see the HackerRank Day 12 Solution in Python. The problem is named Inheritance which is part of 30 Days of code on HackerRank. Let’s get started!

Day 12: Inheritance Problem statement

We are given two classes, Person and Student, where Person is the base class and Student is the derived class. The code for Person and a declaration for Student are already provided for us. Our task is to create a constructor for the Student class which inherits all the properties of the Person class. Also, have to complete the calculate() method that calculates a Student object’s average and returns the graded character corresponding to their calculated average.

Sample Input
Heraldo Memelli 8135627
2
100 80

Sample Output
Name: Memelli, Heraldo
ID: 8135627
Grade: O

Explanation: This student had 2 scores to average: 100 and 80. The student’s average grade is 90 which corresponds to the letter grade O, as given in the problem statement.

You can solve the problem here.

HackerRank Day 12 Solution in Python

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)

class Student(Person):
    #Constructor for Student class
    def __init__(self, firstName, lastName, idNumber, scores):
        super().__init__(firstName, lastName, idNumber)
        self.scores = scores
        
    # Calculate Student object's average and return corresponding grade
    def calculate(self):
        avg = sum(self.scores) / len(self.scores)
        if avg>=90 and avg<=100:
            grade = "O"
        elif avg>=80:
            grade = "E"
        elif avg>=70:
            grade = "A"
        elif avg>=55:
            grade = "P"
        elif avg>=40:
            grade = "D"
        else:
            grade = "T"
        return grade

line = input().split()
firstName = line[0]
lastName = line[1]
idNum = line[2]
numScores = int(input()) 
scores = list( map(int, input().split()) )
s = Student(firstName, lastName, idNum, scores)
s.printPerson()
print("Grade:", s.calculate())

Most of the code is already present there, you need to copy and paste this code only:

class Student(Person):
    #Constructor for Student class
    def __init__(self, firstName, lastName, idNumber, scores):
        super().__init__(firstName, lastName, idNumber)
        self.scores = scores
        
    # Calculate Student object's average and return corresponding grade
    def calculate(self):
        avg = sum(self.scores) / len(self.scores)
        if avg>=90 and avg<=100:
            grade = "O"
        elif avg>=80:
            grade = "E"
        elif avg>=70:
            grade = "A"
        elif avg>=55:
            grade = "P"
        elif avg>=40:
            grade = "D"
        else:
            grade = "T"
        return grade

Code Explanation:

  • We have created a constructor for the Student class that inherits all the attributes in Person class such as firstName, lastName, idNumber along with the scores attribute.
  • Then we create the calculate method to Calculate the Student object’s average and return the corresponding grade for the average

Also Read:

Share:

Author: Keerthana Buvaneshwaran