규도자 개발 블로그

[해커랭크(Hackerrank)/30 Days of Code/파이썬3(python3)] Day 12: Inheritance 본문

알고리즘/풀이

[해커랭크(Hackerrank)/30 Days of Code/파이썬3(python3)] Day 12: Inheritance

규도자 (gyudoza) 2019. 3. 26. 20:45

Objective 
Today, we're delving into Inheritance. Check out the attached tutorial for learning materials and an instructional video!

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:

  • Student class constructor, which has  parameters:
    1. A string, .
    2. A string, .
    3. An integer, .
    4. An integer array (or vector) of test scores, .
  • 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 your editor calls your Student class constructor and passes it the necessary arguments. It also calls the calculate method (which takes no arguments).

You are not responsible for reading the following input from stdin: 
The first line contains , and , respectively. The second line contains the number of test scores. The third line of space-separated integers describes .

Constraints

Output Format

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

Sample Input

Heraldo Memelli 8135627
2
100 80

Sample Output

 Name: Memelli, Heraldo
 ID: 8135627
 Grade: O

Explanation

This student had  scores to average:  and . The student's average grade is . An average grade of corresponds to the letter grade , so our calculate() method should return the character'O'.

풀이

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
    def __init__(self, firstName, lastName, idNumber, scores):
        self.firstName = firstName
        self.lastName = lastName
        self.idNumber = idNumber
        self.scores = scores        

    def calculate(self):
        grade = ''
        scores_avg = sum(self.scores) / len(scores)
        if 90 <= scores_avg <= 100:
            grade = 'O'
        elif 80 <= scores_avg < 90:
            grade = 'E'
        elif 70 <= scores_avg < 80:
            grade = 'A'
        elif 55 <= scores_avg < 70:
            grade = 'P'
        elif 40 <= scores_avg < 55:
            grade = 'D'
        elif scores_avg < 40:
            grade = 'T'
        return grade

    #   Function Name: calculate
    #   Return: A character denoting the grade.
    #
    # Write your function here

설명

Student클래스를 만들어서 예제에 나와있는 점수별로 다른 조건을 출력시켜주는 함수를 작성하면 된다.

Comments