【hackerrank】-Day 12: Inheritance

24 篇文章 0 订阅
24 篇文章 0 订阅

30-inheritance

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:

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:
Grading.png

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’.


Current Buffer (saved locally, editable)

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

    #   Function Name: calculate
    #   Return: A character denoting the grade.
    #
    # Write your function here
    def calculate(self):
        sum = 0
        for score in self.scores:
            sum += score
        average = int(sum/len(self.scores))
        if average >= 90 and average <=100:
            return 'O'
        elif average >= 80 and average <90:
            return 'E'
        elif average >= 70 and average <80:
            return 'A'
        elif average >= 55 and average <70:
            return 'P'
        elif average >= 40 and average <55:
            return 'D'
        elif average < 40:
            return 'T'
        else:
            return "sum >100 or sum < 0"
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()
ss = s.calculate()
print("Grade:",ss)

Congratulations
You solved this challenge. Would you like to challenge your friends?

Next Challenge
Testcase 0 Testcase 1 Testcase 2 Testcase 3
8 Testcases
Input (stdin)Download
Heraldo Memelli 8135627
2
100 80
Expected OutputDownload
Name: Memelli, Heraldo
ID: 8135627
Grade: O
Compiler Message
Success

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值