LintCode: Student ID

Student ID



Similar Problems:


Description
Implement a class Class with the following attributes and methods:

A public attribute students which is a array of Student instances.
A constructor with a parameter n, which is the total number of students in this class. The constructor should create n Student instances and initialized with student id from 0 ~ n-1

Example

Java: 
  Class cls = new Class(3)
  cls.students[0]; // should be a student instance with id = 0
  cls.students[1]; // should be a student instance with id = 1
  cls.students[2]; // should be a student instance with id = 2

Python:
  cls = new Class(3)
  cls.students[0] # should be a student instance with id = 0
  cls.students[1] # should be a student instance with id = 1
  cls.students[2] # should be a student instance with id = 2

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-id
class Student:
    def __init__(self, id):
        self.id = id;

class Class:

    '''
     * Declare a constructor with a parameter n which is the total number of
     * students in the *class*. The constructor should create n Student
     * instances and initialized with student id from 0 ~ n-1
    '''
    # write your code here
    def __init__(self, n):
        self.students = []
        for i in range(n):
            self.students.append(Student(i))
linkedin
github
slack

Original URL: https://code.dennyzhang.com/student-id

Connect with Denny In LinkedIn Or Slack

Share It, If You Like It.

Leave a Reply

Your email address will not be published.