LintCode: Student Level

Student Level



Similar Problems:


Description
Implement a class Student, including the following attributes and methods:

  1. Two public attributes name(string) and score(int).
  2. A constructor expect a name as a parameter.
  3. A method getLevel to get the level(char) of the student.

score – level table:

  1. A: score >= 90
  2. B: score >= 80 and < 90
  3. C: score >= 60 and < 80
  4. D: score < 60

Example

Java:
    Student student = new Student("Zuck");
    student.score = 10;
    student.getLevel(); // should be 'D'
    student.score = 60;
    student.getLevel(); // should be 'C'

Python:
    student = Student("Zuck")
    student.score = 10
    student.getLevel() # should be 'D'
    student.score = 60
    student.getLevel() # should be 'C'

Github: code.dennyzhang.com

Credits To: lintcode.com

Leave me comments, if you have better ways to solve.


  • Solution:
## Blog link: https://code.dennyzhang.com/student-level
## Basic Ideas:
## Complexity: Time O(1), Space O(1)
class Student:

    '''
     * Declare a constructor expect a name as a parameter.
    '''
        # write your code here
    def __init__(self, name):
        self.name = name
        self.score = 0

    '''
     * Declare a public method `getLevel` to get the level(char) of this student.
    '''
    # write your code here
    def getLevel(self):
        if self.score >= 90: return 'A'
        if self.score>=80 and self.score<90: return 'B'
        if self.score>=60 and self.score<80: return 'C'
        return 'D'
linkedin
github
slack

Share It, If You Like It.

Leave a Reply

Your email address will not be published.