"HR30 Day 12: Inheritance"의 두 판 사이의 차이

(새 문서: ==개요== ;<nowiki>Day 12: Inheritance</nowiki> * https://www.hackerrank.com/challenges/30-inheritance/problem {{HR30 헤더}} {{HR30 10-19}} |} ==PHP== <source lang='php'> class S...)
 
잔글 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
 
(다른 사용자 한 명의 중간 판 3개는 보이지 않습니다)
6번째 줄: 6번째 줄:
{{HR30 10-19}}
{{HR30 10-19}}
|}
|}
==Java==
{{참고|HR30 Day 12: Inheritance/Java}}
<syntaxhighlight lang='java'>
class Student extends Person {
    private int[] testScores;
    Student(String firstName, String lastName, int id, int[] scores) {
        super(firstName, lastName, id);
        this.testScores = scores;
    }
    char calculate() {
        double avg = Arrays.stream(this.testScores).average().getAsDouble();
        if( avg >= 90 ) return 'O';
        if( avg >= 80 ) return 'E';
        if( avg >= 70 ) return 'A';
        if( avg >= 55 ) return 'P';
        if( avg >= 40 ) return 'D';
        return 'T';
    }
}
</syntaxhighlight>


==PHP==
==PHP==
<source lang='php'>
{{참고|HR30 Day 12: Inheritance/PHP}}
<syntaxhighlight lang='php'>
class Student extends person {
class Student extends person {
     private $testScores;
     private $testScores;
25번째 줄: 47번째 줄:
     }
     }
}
}
</source>
</syntaxhighlight>
 
==Python==
{{참고|HR30 Day 12: Inheritance/Python}}
<syntaxhighlight lang='python'>
import builtins
def print(*args,**kwargs):
    if "sep" not in kwargs and args[0] =="Grade: ":
        builtins.print(*args,**kwargs,sep="")
    else:
        builtins.print(*args,**kwargs)
 
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, idNum, scores):
        super().__init__(firstName, lastName, idNum)
        self.scores = scores
 
    #  Function Name: calculate
    #  Return: A character denoting the grade.
    #
    # Write your function here
    def calculate(self):
        avg = sum(self.scores) / len(self.scores)
        if avg >= 90: return "O"
        if avg >= 80: return 'E'
        if avg >= 70: return 'A'
        if avg >= 55: return 'P'
        if avg >= 40: return 'D'
        return 'T'
</syntaxhighlight>

2021년 7월 31일 (토) 10:34 기준 최신판

1 개요[ | ]

Day 12: Inheritance
해커랭크 30 Days of Code
문제 풀이
10-19 Day e
HR30 Day 10: Binary Numbers

HR30 Day 11: 2D Arrays

HR30 Day 12: Inheritance

HR30 Day 13: Abstract Classes

HR30 Day 14: Scope

HR30 Day 15: Linked List

HR30 Day 16: Exceptions - String to Integer

HR30 Day 17: More Exceptions

HR30 Day 18: Queues and Stacks

HR30 Day 19: Interfaces

2 Java[ | ]

class Student extends Person {
    private int[] testScores;
    Student(String firstName, String lastName, int id, int[] scores) {
        super(firstName, lastName, id);
        this.testScores = scores;
    }
    char calculate() {
        double avg = Arrays.stream(this.testScores).average().getAsDouble();
        if( avg >= 90 ) return 'O';
        if( avg >= 80 ) return 'E';
        if( avg >= 70 ) return 'A';
        if( avg >= 55 ) return 'P';
        if( avg >= 40 ) return 'D';
        return 'T';
    }
}

3 PHP[ | ]

class Student extends person {
    private $testScores;
    public function __construct($first_name, $last_name, $identification, $scores) {
        parent::__construct($first_name, $last_name, $identification);
        $this->testScores = $scores;
    }
    function calculate() {
        $average = array_sum($this->testScores) / count($this->testScores);
        if( $average >= 90 ) return 'O';
        if( $average >= 80 ) return 'E';
        if( $average >= 70 ) return 'A';
        if( $average >= 55 ) return 'P';
        if( $average >= 40 ) return 'D';
        return 'T';
    }
}

4 Python[ | ]

import builtins
def print(*args,**kwargs):
    if "sep" not in kwargs and args[0] =="Grade: ":
        builtins.print(*args,**kwargs,sep="")
    else:
        builtins.print(*args,**kwargs)

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

    #   Function Name: calculate
    #   Return: A character denoting the grade.
    #
    # Write your function here
    def calculate(self):
        avg = sum(self.scores) / len(self.scores)
        if avg >= 90: return "O"
        if avg >= 80: return 'E'
        if avg >= 70: return 'A'
        if avg >= 55: return 'P'
        if avg >= 40: return 'D'
        return 'T'
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}