BASHA TECH

[HackerRank][Python] Inheritance 본문

Activity/Coding Test

[HackerRank][Python] Inheritance

Basha 2023. 4. 24. 17:54
728x90

Task
You are given two classes, Person and Student, where Person is the base class and Student is the derived class. Completed code for Person and a declaration for Student are provided for you in the editor. Observe that Student inherits all the properties of Person.

Complete the Student class by writing the following:

  • A Student class constructor, which has  parameters:
    1. A string, firstName.
    2. A string, lastName.
    3. An integer, idNumber.
    4. An integer array (or vector) of test scores, scores.
  • A char calculate() method that calculates a Student object's average and returns the grade character representative of their calculated average:

Input Format

The locked stub code in the editor reads the input and calls the Student class constructor with the necessary arguments. It also calls the calculate method which takes no arguments.

The first line contains firstName, lastName, and idNumber, separated by a space. The second line contains the number of test scores. The third line of space-separated integers describes scores.

Constraints

  • 1 <= length of firstName, length of lastName
  • length of idNumber == 7
  • 0 <= score <= 100

Output Format

Output is handled by the locked stub code. Your output will be correct if your Student class constructor and calculate() method are properly implemented. 

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):
    #   Class Constructor
    #   
    #   Parameters:
    #   firstName - A string denoting the Person's first name.
    #   lastName - A string denoting the Person's last name.
    #   id - An integer denoting the Person's ID number.
    #   scores - An array of integers denoting the Person's test scores.
    #
    # Write your constructor here
    

    #   Function Name: calculate
    #   Return: A character denoting the grade.
    #
    # Write your function here
    def __init__(self, firstName, lastName, idNumber, scores):
        super().__init__(firstName, lastName, idNumber)
        self.scores = scores
    
    def calculate(self):
        avg = sum(self.scores) / len(self.scores)
        if 100 >= avg >= 90:
            return "O"
        elif 90 >avg >= 80:
            return "E"
        elif 80 > avg >= 70:
            return "A"
        elif 70 > avg >= 55:
            return "P"
        elif 55 > avg >= 40:
            return "D"
        else:
            return "T" 
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's wrong.

# Define a base class called Person with three instance variables: firstName, lastName and idNumber
class Person:
    # Define a constructor method that initializes the instance variables
    def __init__(self, firstName, lastName, idNumber):
        self.firstName = firstName
        self.lastName = lastName
        self.idNumber = idNumber
        
    # Define a method that prints out the person's name and id number
    def printPerson(self):
        print("Name:", self.lastName + ",", self.firstName)
        print("ID:", self.idNumber)

# Define a derived class called Student that inherits from the Person class
class Student(Person):
    
    # Define a constructor method that initializes the Student object with the given parameters
    def __init__(self, firstName, lastName, idNumber, scores):
        # Call the constructor of the base class to initialize the firstName, lastName and idNumber instance variables
        Person.__init__(self, firstName, lastName, idNumber)
        # Initialize the scores instance variable with the given parameter
        self.scores = scores
    
    # Define a method that calculates the average score of the Student object and returns a grade character
    def calculate(self):
        # Calculate the average score of the Student object
        average_score = sum(self.scores) / len(self.scores)
        
        # Determine the grade character based on the average score
        if average_score >= 90:
            return 'O'
        elif average_score >= 80:
            return 'E'
        elif average_score >= 70:
            return 'A'
        elif average_score >= 55:
            return 'P'
        elif average_score >= 40:
            return 'D'
        else:
            return 'T'

# Read in input values and create a Student object with the given parameters
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)

# Print out the name and id number of the Student object
s.printPerson()
# Print out the grade character of the Student object
print("Grade:", s.calculate())

This code defines two classes, Person and Student, where Student is a subclass of Person. The Person class has a constructor that takes three parameters: firstName, lastName, and idNumber, and a printPerson method that prints out the person's name and ID number.

The Student class has a constructor that takes four parameters: firstName, lastName, idNumber, and scores, which is an array of integers representing the student's test scores. The Student class also has a method called calculate which calculates the average of the student's test scores and returns a letter grade based on the following scale:

O : 90<=a<=100
E : 80<=a<90
A : 70<=a<80
P : 55<=a<55
T : a<40
The main program reads in the input, creates a Student object, and then calls the printPerson method to print out the student's name and ID number, followed by the calculated grade from the calculate method.

728x90
반응형

'Activity > Coding Test' 카테고리의 다른 글

[HackerRank][Python] 2D Arrays  (0) 2023.04.24
[HackerRank][Python] Binary Numbers  (0) 2023.04.17
[HackerRank][Python] Recursion 3  (0) 2023.04.17
[HackerRank][Python] Dictionaries and Maps  (0) 2023.04.17
[HackerRank][Python] Arrays  (0) 2023.04.17
Comments