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:
- Download 1000+ Projects, All B.Tech & Programming Notes, Job, Resume & Interview Guide, and More – Get Your Ultimate Programming Bundle!
- HackerRank Day 8 Solution in Python: Dictionaries and Maps
- HackerRank Day 7 Solution in Python: Arrays
- HackerRank Day 6 Solution in Python: Let’s review
- HackerRank Day 5 Solution in Python: Loops
- HackerRank Day 4 Solution in Python: Class vs Instance
- HackerRank Day 3 Solution in Python: Intro to Conditional Statements
- HackerRank Day 2 Solution in Python: Operators
- HackerRank Day 1 Solution in Python: Data Types
- HackerRank Day 0 Solution in Python: Hello World
- HackerRank Day 29 Solution in Python: Bitwise AND
- HackerRank Day 28 Solution in Python: RegEx, Patterns, and Intro to databases
- HackerRank Day 27 Solution in Python: Testing
- HackerRank Day 26 Solution in Python: Nested Logic
- HackerRank Day 25 Solution in Python: Running Time and Complexity
- HackerRank Day 24 Solution in Python: More Linked Lists
- HackerRank Day 23 Solution in Python: BST Level Order Traversal
- HackerRank Day 22 Solution in Python: Binary Search Trees
- Find Peak Element LeetCode 162
- HackerRank Day 20 Solution in Python: Sorting
- HackerRank Day 19 Solution in Python: Interfaces
- HackerRank Day 18 Solution in Python: Queues and Stacks
- HackerRank Day 17 Solution in Python: More Exceptions
- HackerRank Day 16 Solution: Exceptions – String to Integer
- Explained: Allocate minimum number of pages
- HackerRank Day 15 Solution in Python: Linked List
- Search a 2D matrix: leetcode 74
- Maximum Subarray Sum: Kadane’s Algorithm
- HackerRank Day 13 Solution in Python: Abstract Classes
- HackerRank Day 14 Solution in Python: Scope