규도자 개발 블로그
[해커랭크(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:45Objective
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:
- A Student class constructor, which has parameters:
- A string, .
- A string, .
- An integer, .
- An integer array (or vector) of test 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 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클래스를 만들어서 예제에 나와있는 점수별로 다른 조건을 출력시켜주는 함수를 작성하면 된다.
'알고리즘 > 풀이' 카테고리의 다른 글
[해커랭크(Hackerrank)/30 Days of Code/파이썬3(python3)] Day 14: Scope (0) | 2019.03.26 |
---|---|
[해커랭크(Hackerrank)/30 Days of Code/파이썬3(python3)] Day 13: Abstract Classes (0) | 2019.03.26 |
[해커랭크(Hackerrank)/30 Days of Code/파이썬3(python3)] Day 11: 2D Arrays (0) | 2019.03.22 |
[백준/9012/파이썬(python3)] 괄호 (0) | 2019.03.22 |
[해커랭크(Hackerrank)/30 Days of Code/파이썬3(python3)] Day 10: Binary Numbers (0) | 2019.03.21 |