Skip to main content
Tweeted twitter.com/StackCodeReview/status/709542990688231424
deleted 15 characters in body; edited title
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

Python backend interview: task management system for workers

Description:

A supervisor is using the TaskManagementSystem to manage tasks for his workers. The system can add users, assign tasks to users, and get task(s) that belong to a specific user.

Restriction:

You can not modify user or task class. self.users and self.tasks in `TaskManagementSystemTaskManagementSystem has to remain as lists. You can introduce new methods and class variables. Goals:

  1. Fix the syntax and semantic bugs that prevent the program from running correctly.

  2. Change get_user_tasks() to return list of task names instead of task objects.

  3. Add error handling to add_user(), add_task(), and get_user_tasks() methods

  4. Optimize TaskManagementSystem to handle large number of users and tasks

#!/usr/bin/env python2.7
# encoding: utf-8

    class user(object):
    
    def __init__(self, user_id, name):
        '''
        Task object.  This code can not be modified.
            @ user_id: int
            @ name: string
        '''
        self.user_id = user_id
        self.name = name


    class task(object):
    
    def __init__(self, user_id, task_name):
        '''
        Task object.  This code can not be modified.
            @ user_id: int
            @ task_name: string
        '''
        self.user_id = user_id
        self.task_name = task_name
    
    class TaskManagementSystem(object):
    
    def __init__(self):
        self.users = []  # stores a list of user objects
        self.tasks = []  # stores a list of task objects
    
    
    def add_user(self, user_name):
        '''
        Add a new user
            @ user_name: string
        
        Condition:
            Every user should have a unique name
            Every user should have a unique id
        '''
        
        unique_id = 0
        for user in self.users:
            if user.user_id > unique_id:
                unique_id = user.id
        
        self.users.append(user(unique_id, user_name))
    
    
    def add_task(self, user_name, task_name):
        '''
        Add a task for a user
            @ user_name: string
            @ task_name: string
                
        Condition:
            The user should not have two or more tasks with the same name
        '''
        
        for user in self.users:
            if user.name = user_name:
                user_id = user.user_id
        
        self.users.append(task(user_id, task_name))
    
    
    def get_user_tasks(self, user_name):
        '''
        Get task(s) that belongs to the specified user name
            @ user_name: string
        '''
        
        user_tasks = []
        
        for user in self.users:
            if user.name == user_name:
                for task in self.tasks:
                    if task.user_id == user.user_id:
                        user_tasks.append(task)
        
        return user_tasks

    
    if __name__ == "__main__":
    
    tms = TaskManagementSystem()
    tms.add_user('Bob')
    tms.add_task('Bob', 'laundry')
    tms.add_task('Bob', 'grocery')
    tms.add_task('Bob', 'daycare')

    print tms.get_user_tasks('Bob')
    # should print: ['laundry', 'grocery', 'daycare']

Python backend interview

Description:

A supervisor is using the TaskManagementSystem to manage tasks for his workers. The system can add users, assign tasks to users, and get task(s) that belong to a specific user.

Restriction:

You can not modify user or task class. self.users and self.tasks in `TaskManagementSystem has to remain as lists. You can introduce new methods and class variables. Goals:

  1. Fix the syntax and semantic bugs that prevent the program from running correctly.

  2. Change get_user_tasks() to return list of task names instead of task objects.

  3. Add error handling to add_user(), add_task(), and get_user_tasks() methods

  4. Optimize TaskManagementSystem to handle large number of users and tasks

#!/usr/bin/env python2.7
# encoding: utf-8

    class user(object):
    
    def __init__(self, user_id, name):
        '''
        Task object.  This code can not be modified.
            @ user_id: int
            @ name: string
        '''
        self.user_id = user_id
        self.name = name


    class task(object):
    
    def __init__(self, user_id, task_name):
        '''
        Task object.  This code can not be modified.
            @ user_id: int
            @ task_name: string
        '''
        self.user_id = user_id
        self.task_name = task_name
    
    class TaskManagementSystem(object):
    
    def __init__(self):
        self.users = []  # stores a list of user objects
        self.tasks = []  # stores a list of task objects
    
    
    def add_user(self, user_name):
        '''
        Add a new user
            @ user_name: string
        
        Condition:
            Every user should have a unique name
            Every user should have a unique id
        '''
        
        unique_id = 0
        for user in self.users:
            if user.user_id > unique_id:
                unique_id = user.id
        
        self.users.append(user(unique_id, user_name))
    
    
    def add_task(self, user_name, task_name):
        '''
        Add a task for a user
            @ user_name: string
            @ task_name: string
                
        Condition:
            The user should not have two or more tasks with the same name
        '''
        
        for user in self.users:
            if user.name = user_name:
                user_id = user.user_id
        
        self.users.append(task(user_id, task_name))
    
    
    def get_user_tasks(self, user_name):
        '''
        Get task(s) that belongs to the specified user name
            @ user_name: string
        '''
        
        user_tasks = []
        
        for user in self.users:
            if user.name == user_name:
                for task in self.tasks:
                    if task.user_id == user.user_id:
                        user_tasks.append(task)
        
        return user_tasks

    
    if __name__ == "__main__":
    
    tms = TaskManagementSystem()
    tms.add_user('Bob')
    tms.add_task('Bob', 'laundry')
    tms.add_task('Bob', 'grocery')
    tms.add_task('Bob', 'daycare')

    print tms.get_user_tasks('Bob')
    # should print: ['laundry', 'grocery', 'daycare']

Python backend interview: task management system for workers

Description:

A supervisor is using the TaskManagementSystem to manage tasks for his workers. The system can add users, assign tasks to users, and get task(s) that belong to a specific user.

Restriction:

You can not modify user or task class. self.users and self.tasks in TaskManagementSystem has to remain as lists. You can introduce new methods and class variables. Goals:

  1. Fix the syntax and semantic bugs that prevent the program from running correctly.

  2. Change get_user_tasks() to return list of task names instead of task objects.

  3. Add error handling to add_user(), add_task(), and get_user_tasks() methods

  4. Optimize TaskManagementSystem to handle large number of users and tasks

#!/usr/bin/env python2.7
# encoding: utf-8

class user(object):
    
    def __init__(self, user_id, name):
        '''
        Task object.  This code can not be modified.
            @ user_id: int
            @ name: string
        '''
        self.user_id = user_id
        self.name = name


class task(object):
    
    def __init__(self, user_id, task_name):
        '''
        Task object.  This code can not be modified.
            @ user_id: int
            @ task_name: string
        '''
        self.user_id = user_id
        self.task_name = task_name
    
class TaskManagementSystem(object):
    
    def __init__(self):
        self.users = []  # stores a list of user objects
        self.tasks = []  # stores a list of task objects
    
    
    def add_user(self, user_name):
        '''
        Add a new user
            @ user_name: string
        
        Condition:
            Every user should have a unique name
            Every user should have a unique id
        '''
        
        unique_id = 0
        for user in self.users:
            if user.user_id > unique_id:
                unique_id = user.id
        
        self.users.append(user(unique_id, user_name))
    
    
    def add_task(self, user_name, task_name):
        '''
        Add a task for a user
            @ user_name: string
            @ task_name: string
                
        Condition:
            The user should not have two or more tasks with the same name
        '''
        
        for user in self.users:
            if user.name = user_name:
                user_id = user.user_id
        
        self.users.append(task(user_id, task_name))
    
    
    def get_user_tasks(self, user_name):
        '''
        Get task(s) that belongs to the specified user name
            @ user_name: string
        '''
        
        user_tasks = []
        
        for user in self.users:
            if user.name == user_name:
                for task in self.tasks:
                    if task.user_id == user.user_id:
                        user_tasks.append(task)
        
        return user_tasks

    
if __name__ == "__main__":
    
    tms = TaskManagementSystem()
    tms.add_user('Bob')
    tms.add_task('Bob', 'laundry')
    tms.add_task('Bob', 'grocery')
    tms.add_task('Bob', 'daycare')

    print tms.get_user_tasks('Bob')
    # should print: ['laundry', 'grocery', 'daycare']
Improve formatting
Source Link
Phrancis
  • 20.5k
  • 6
  • 70
  • 155

Description:

A supervisor is using the TaskManagementSystem to manage tasks for his workers. The system can add users, assign tasks to users, and get task(s) that belong to a specific user.

Restriction:

You can not modify user or task class. self.users and self.tasks in TaskManagementSystem has to remain as lists. You can introduce new methods and class variables. Goals:

  1. Fix the syntax and semantic bugs that prevent the program from running correctly.

  2. Change get_user_tasks() to return list of task names instead of task objects.

  3. Add error handling to add_user(), add_task(), and get_user_tasks() methods

  4. Optimize TaskManagementSystem to handle large number of users and tasks

Description:

A supervisor is using the TaskManagementSystem to manage tasks for his workers. The system can add users, assign tasks to users, and get task(s) that belong to a specific user.

Restriction:

You can not modify user or task class. self.users and self.tasks in `TaskManagementSystem has to remain as lists. You can introduce new methods and class variables. Goals:

  1. Fix the syntax and semantic bugs that prevent the program from running correctly.

  2. Change get_user_tasks() to return list of task names instead of task objects.

  3. Add error handling to add_user(), add_task(), and get_user_tasks() methods

  4. Optimize TaskManagementSystem to handle large number of users and tasks

CODE PROVIDED

Code provided at interview:

#!/usr/bin/env python2.7
# encoding: utf-8

    class user(object):
    
    def __init__(self, user_id, name):
        '''
        Task object.  This code can not be modified.
            @ user_id: int
            @ name: string
        '''
        self.user_id = user_id
        self.name = name


    class task(object):
    
    def __init__(self, user_id, task_name):
        '''
        Task object.  This code can not be modified.
            @ user_id: int
            @ task_name: string
        '''
        self.user_id = user_id
        self.task_name = task_name
    
    class TaskManagementSystem(object):
    
    def __init__(self):
        self.users = []  # stores a list of user objects
        self.tasks = []  # stores a list of task objects
    
    
    def add_user(self, user_name):
        '''
        Add a new user
            @ user_name: string
        
        Condition:
            Every user should have a unique name
            Every user should have a unique id
        '''
        
        unique_id = 0
        for user in self.users:
            if user.user_id > unique_id:
                unique_id = user.id
        
        self.users.append(user(unique_id, user_name))
    
    
    def add_task(self, user_name, task_name):
        '''
        Add a task for a user
            @ user_name: string
            @ task_name: string
                
        Condition:
            The user should not have two or more tasks with the same name
        '''
        
        for user in self.users:
            if user.name = user_name:
                user_id = user.user_id
        
        self.users.append(task(user_id, task_name))
    
    
    def get_user_tasks(self, user_name):
        '''
        Get task(s) that belongs to the specified user name
            @ user_name: string
        '''
        
        user_tasks = []
        
        for user in self.users:
            if user.name == user_name:
                for task in self.tasks:
                    if task.user_id == user.user_id:
                        user_tasks.append(task)
        
        return user_tasks

    
    if __name__ == "__main__":
    
    tms = TaskManagementSystem()
    tms.add_user('Bob')
    tms.add_task('Bob', 'laundry')
    tms.add_task('Bob', 'grocery')
    tms.add_task('Bob', 'daycare')

    print tms.get_user_tasks('Bob')
    # should print: ['laundry', 'grocery', 'daycare']
#!/usr/bin/env python2.7
# encoding: utf-8

    class user(object):
    
    def __init__(self, user_id, name):
        '''
        Task object.  This code can not be modified.
            @ user_id: int
            @ name: string
        '''
        self.user_id = user_id
        self.name = name


    class task(object):
    
    def __init__(self, user_id, task_name):
        '''
        Task object.  This code can not be modified.
            @ user_id: int
            @ task_name: string
        '''
        self.user_id = user_id
        self.task_name = task_name
    
    class TaskManagementSystem(object):
    
    def __init__(self):
        self.users = []  # stores a list of user objects
        self.tasks = []  # stores a list of task objects
    
    
    def add_user(self, user_name):
        '''
        Add a new user
            @ user_name: string
        
        Condition:
            Every user should have a unique name
            Every user should have a unique id
        '''
        
        unique_id = 0
        for user in self.users:
            if user.user_id > unique_id:
                unique_id = user.id
        
        self.users.append(user(unique_id, user_name))
    
    
    def add_task(self, user_name, task_name):
        '''
        Add a task for a user
            @ user_name: string
            @ task_name: string
                
        Condition:
            The user should not have two or more tasks with the same name
        '''
        
        for user in self.users:
            if user.name = user_name:
                user_id = user.user_id
        
        self.users.append(task(user_id, task_name))
    
    
    def get_user_tasks(self, user_name):
        '''
        Get task(s) that belongs to the specified user name
            @ user_name: string
        '''
        
        user_tasks = []
        
        for user in self.users:
            if user.name == user_name:
                for task in self.tasks:
                    if task.user_id == user.user_id:
                        user_tasks.append(task)
        
        return user_tasks

    
    if __name__ == "__main__":
    
    tms = TaskManagementSystem()
    tms.add_user('Bob')
    tms.add_task('Bob', 'laundry')
    tms.add_task('Bob', 'grocery')
    tms.add_task('Bob', 'daycare')

    print tms.get_user_tasks('Bob')
    # should print: ['laundry', 'grocery', 'daycare']

MY ANSWER

Here is the code I handed in as my answer:

Description:

A supervisor is using the TaskManagementSystem to manage tasks for his workers. The system can add users, assign tasks to users, and get task(s) that belong to a specific user.

Restriction:

You can not modify user or task class. self.users and self.tasks in TaskManagementSystem has to remain as lists. You can introduce new methods and class variables. Goals:

  1. Fix the syntax and semantic bugs that prevent the program from running correctly.

  2. Change get_user_tasks() to return list of task names instead of task objects.

  3. Add error handling to add_user(), add_task(), and get_user_tasks() methods

  4. Optimize TaskManagementSystem to handle large number of users and tasks

CODE PROVIDED

#!/usr/bin/env python2.7
# encoding: utf-8

    class user(object):
    
    def __init__(self, user_id, name):
        '''
        Task object.  This code can not be modified.
            @ user_id: int
            @ name: string
        '''
        self.user_id = user_id
        self.name = name


    class task(object):
    
    def __init__(self, user_id, task_name):
        '''
        Task object.  This code can not be modified.
            @ user_id: int
            @ task_name: string
        '''
        self.user_id = user_id
        self.task_name = task_name
    
    class TaskManagementSystem(object):
    
    def __init__(self):
        self.users = []  # stores a list of user objects
        self.tasks = []  # stores a list of task objects
    
    
    def add_user(self, user_name):
        '''
        Add a new user
            @ user_name: string
        
        Condition:
            Every user should have a unique name
            Every user should have a unique id
        '''
        
        unique_id = 0
        for user in self.users:
            if user.user_id > unique_id:
                unique_id = user.id
        
        self.users.append(user(unique_id, user_name))
    
    
    def add_task(self, user_name, task_name):
        '''
        Add a task for a user
            @ user_name: string
            @ task_name: string
                
        Condition:
            The user should not have two or more tasks with the same name
        '''
        
        for user in self.users:
            if user.name = user_name:
                user_id = user.user_id
        
        self.users.append(task(user_id, task_name))
    
    
    def get_user_tasks(self, user_name):
        '''
        Get task(s) that belongs to the specified user name
            @ user_name: string
        '''
        
        user_tasks = []
        
        for user in self.users:
            if user.name == user_name:
                for task in self.tasks:
                    if task.user_id == user.user_id:
                        user_tasks.append(task)
        
        return user_tasks

    
    if __name__ == "__main__":
    
    tms = TaskManagementSystem()
    tms.add_user('Bob')
    tms.add_task('Bob', 'laundry')
    tms.add_task('Bob', 'grocery')
    tms.add_task('Bob', 'daycare')

    print tms.get_user_tasks('Bob')
    # should print: ['laundry', 'grocery', 'daycare']

MY ANSWER

Description:

A supervisor is using the TaskManagementSystem to manage tasks for his workers. The system can add users, assign tasks to users, and get task(s) that belong to a specific user.

Restriction:

You can not modify user or task class. self.users and self.tasks in `TaskManagementSystem has to remain as lists. You can introduce new methods and class variables. Goals:

  1. Fix the syntax and semantic bugs that prevent the program from running correctly.

  2. Change get_user_tasks() to return list of task names instead of task objects.

  3. Add error handling to add_user(), add_task(), and get_user_tasks() methods

  4. Optimize TaskManagementSystem to handle large number of users and tasks

Code provided at interview:

#!/usr/bin/env python2.7
# encoding: utf-8

    class user(object):
    
    def __init__(self, user_id, name):
        '''
        Task object.  This code can not be modified.
            @ user_id: int
            @ name: string
        '''
        self.user_id = user_id
        self.name = name


    class task(object):
    
    def __init__(self, user_id, task_name):
        '''
        Task object.  This code can not be modified.
            @ user_id: int
            @ task_name: string
        '''
        self.user_id = user_id
        self.task_name = task_name
    
    class TaskManagementSystem(object):
    
    def __init__(self):
        self.users = []  # stores a list of user objects
        self.tasks = []  # stores a list of task objects
    
    
    def add_user(self, user_name):
        '''
        Add a new user
            @ user_name: string
        
        Condition:
            Every user should have a unique name
            Every user should have a unique id
        '''
        
        unique_id = 0
        for user in self.users:
            if user.user_id > unique_id:
                unique_id = user.id
        
        self.users.append(user(unique_id, user_name))
    
    
    def add_task(self, user_name, task_name):
        '''
        Add a task for a user
            @ user_name: string
            @ task_name: string
                
        Condition:
            The user should not have two or more tasks with the same name
        '''
        
        for user in self.users:
            if user.name = user_name:
                user_id = user.user_id
        
        self.users.append(task(user_id, task_name))
    
    
    def get_user_tasks(self, user_name):
        '''
        Get task(s) that belongs to the specified user name
            @ user_name: string
        '''
        
        user_tasks = []
        
        for user in self.users:
            if user.name == user_name:
                for task in self.tasks:
                    if task.user_id == user.user_id:
                        user_tasks.append(task)
        
        return user_tasks

    
    if __name__ == "__main__":
    
    tms = TaskManagementSystem()
    tms.add_user('Bob')
    tms.add_task('Bob', 'laundry')
    tms.add_task('Bob', 'grocery')
    tms.add_task('Bob', 'daycare')

    print tms.get_user_tasks('Bob')
    # should print: ['laundry', 'grocery', 'daycare']

Here is the code I handed in as my answer:

deleted 46 characters in body
Source Link
DGDD
  • 293
  • 2
  • 6
#!/usr/bin/env python2.7
# encoding: utf-8
from random import choice


class user(object):

    def __init__(self, user_id, name):
        '''
        Task object.  This code can not be modified.
            @ user_id: int
            @ name: string
        '''
        self.user_id = user_id
        self.name = name


class task(object):

    def __init__(self, user_id, task_name):
        '''
        Task object.  This code can not be modified.
            @ user_id: int
            @ task_name: string
        '''
        self.user_id = user_id
        self.task_name = task_name


class TaskManagementSystem(object):

    def __init__(self):
        self.users = []  # stores a list of user objects
        self.tasks = []  # stores a list of task objects

    def add_user(self, user_name):
        '''
        Add a new user
            @ user_name: string
    
        Condition:
            Every user should have a unique name
            Every user should have a unique id
        '''
    
        # Create a unique id based on current user list.
        if self.users:
            unique_id = max([_user.user_id for _user in self.users]) + 1
    
        # There are no current users.
        else:
            unique_id = 1
    
        # Check that user_name is not taken.
        if [_user for _user in self.users if _user.name == user_name]:
            print 'That user name is taken.'
        else:
            self.users.append(user(unique_id, user_name))


    def add_task(self, user_name, task_name):
        '''
        Add a task for a user
            @ user_name: string
            @ task_name: string
    
        Condition:
            The user should not have two or more tasks with the same name
        '''
    
        user_id = self.get_user_id(user_name)
        if not user_id:
            return
    
        if task_name in self.get_user_tasks(user_name):
            print 'The user has already been asigned this task.'
    
        else:
            self.tasks.append(task(user_id, task_name))
    
    
    def get_user_id(self, user_name):
        '''
        Get a users id.
        Prints error if not found.
            @ user_name: string
        '''
    
        try:
            return [_user.user_id for _user in self.users
                    if _user.name == user_name][0]
        except(IndexError):
            print 'User not found.'


    def get_user_tasks(self, user_name):
        '''
        Get task(s) that belongs to the specified user name
            @ user_name: string
        '''

        user_id = self.get_user_id(user_name)

        if not user_id:
            return

        return [_task.task_name for _task in self.tasks
                if _task.user_id == user_id]


def test_add_users(n):
        '''
        Test by adding n users.
            @ n: int
        '''
    
        print 'adding users...'
        for x in xrange(n):
            user_name = ''.join([chr(choice(range(97, 123))) for c in range(4)])
            tms.add_user(user_name)
    
        n_added = len(tms.users)
        print '{} users added. {} user names rejected.'.format(n_added, n - n_added)
    
def test_add_tasks(n):
    '''
    Test by assigning random users tasks.
        @ n: int
    '''

    print 'adding tasks...'
    for x in xrange(n):
        user_name = choice(tms.users).name      # Grabs a random user's name
        task_name = ''.join([chr(choice(range(97, 123))) for c in range(4)])
        tms.add_task(user_name, task_name)

    n_added = len(tms.tasks)
    print '{} tasks added. {} task rejected.'.format(n_added, n - n_added)


if __name__ == "__main__":
    tms = TaskManagementSystem()

    # Test with lots of users.
    # test_add_users(10000)
    # test_add_tasks(10000)

    tms.add_user('Bob')
    tms.add_task('Bob', 'laundry')
    tms.add_task('Bob', 'grocery')
    tms.add_task('Bob', 'daycare')

    print tms.get_user_tasks('Bob')
    # should print: ['laundry', 'grocery', 'daycare']
#!/usr/bin/env python2.7
# encoding: utf-8
from random import choice


class user(object):

    def __init__(self, user_id, name):
        '''
        Task object.  This code can not be modified.
            @ user_id: int
            @ name: string
        '''
        self.user_id = user_id
        self.name = name


class task(object):

    def __init__(self, user_id, task_name):
        '''
        Task object.  This code can not be modified.
            @ user_id: int
            @ task_name: string
        '''
        self.user_id = user_id
        self.task_name = task_name


class TaskManagementSystem(object):

    def __init__(self):
        self.users = []  # stores a list of user objects
        self.tasks = []  # stores a list of task objects

    def add_user(self, user_name):
        '''
        Add a new user
            @ user_name: string
    
        Condition:
            Every user should have a unique name
            Every user should have a unique id
        '''
    
        # Create a unique id based on current user list.
        if self.users:
            unique_id = max([_user.user_id for _user in self.users]) + 1
    
        # There are no current users.
        else:
            unique_id = 1
    
        # Check that user_name is not taken.
        if [_user for _user in self.users if _user.name == user_name]:
            print 'That user name is taken.'
        else:
            self.users.append(user(unique_id, user_name))


    def add_task(self, user_name, task_name):
        '''
        Add a task for a user
            @ user_name: string
            @ task_name: string
    
        Condition:
            The user should not have two or more tasks with the same name
        '''
    
        user_id = self.get_user_id(user_name)
        if not user_id:
            return
    
        if task_name in self.get_user_tasks(user_name):
            print 'The user has already been asigned this task.'
    
        else:
            self.tasks.append(task(user_id, task_name))
    
    
    def get_user_id(self, user_name):
        '''
        Get a users id.
        Prints error if not found.
            @ user_name: string
        '''
    
        try:
            return [_user.user_id for _user in self.users
                    if _user.name == user_name][0]
        except(IndexError):
            print 'User not found.'


    def get_user_tasks(self, user_name):
        '''
        Get task(s) that belongs to the specified user name
            @ user_name: string
        '''

        user_id = self.get_user_id(user_name)

        if not user_id:
            return

        return [_task.task_name for _task in self.tasks
                if _task.user_id == user_id]


def test_add_users(n):
        '''
        Test by adding n users.
            @ n: int
        '''
    
        print 'adding users...'
        for x in xrange(n):
            user_name = ''.join([chr(choice(range(97, 123))) for c in range(4)])
            tms.add_user(user_name)
    
        n_added = len(tms.users)
        print '{} users added. {} user names rejected.'.format(n_added, n - n_added)
    
def test_add_tasks(n):
    '''
    Test by assigning random users tasks.
        @ n: int
    '''

    print 'adding tasks...'
    for x in xrange(n):
        user_name = choice(tms.users).name      # Grabs a random user's name
        task_name = ''.join([chr(choice(range(97, 123))) for c in range(4)])
        tms.add_task(user_name, task_name)

    n_added = len(tms.tasks)
    print '{} tasks added. {} task rejected.'.format(n_added, n - n_added)


if __name__ == "__main__":
    tms = TaskManagementSystem()

    # Test with lots of users.
    # test_add_users(10000)
    # test_add_tasks(10000)

    tms.add_user('Bob')
    tms.add_task('Bob', 'laundry')
    tms.add_task('Bob', 'grocery')
    tms.add_task('Bob', 'daycare')

    print tms.get_user_tasks('Bob')
    # should print: ['laundry', 'grocery', 'daycare']
#!/usr/bin/env python2.7
# encoding: utf-8
from random import choice


class user(object):

    def __init__(self, user_id, name):
        '''
        Task object.  This code can not be modified.
            @ user_id: int
            @ name: string
        '''
        self.user_id = user_id
        self.name = name


class task(object):

    def __init__(self, user_id, task_name):
        '''
        Task object.  This code can not be modified.
            @ user_id: int
            @ task_name: string
        '''
        self.user_id = user_id
        self.task_name = task_name


class TaskManagementSystem(object):

    def __init__(self):
        self.users = []  # stores a list of user objects
        self.tasks = []  # stores a list of task objects

    def add_user(self, user_name):
        '''
        Add a new user
            @ user_name: string
    
        Condition:
            Every user should have a unique name
            Every user should have a unique id
        '''
    
        # Create a unique id based on current user list.
        if self.users:
            unique_id = max([_user.user_id for _user in self.users]) + 1
    
        # There are no current users.
        else:
            unique_id = 1
    
        # Check that user_name is not taken.
        if [_user for _user in self.users if _user.name == user_name]:
            print 'That user name is taken.'
        else:
            self.users.append(user(unique_id, user_name))


    def add_task(self, user_name, task_name):
        '''
        Add a task for a user
            @ user_name: string
            @ task_name: string
    
        Condition:
            The user should not have two or more tasks with the same name
        '''
    
        user_id = self.get_user_id(user_name)
        if not user_id:
            return
    
        if task_name in self.get_user_tasks(user_name):
            print 'The user has already been asigned this task.'
    
        else:
            self.tasks.append(task(user_id, task_name))
    
    
    def get_user_id(self, user_name):
        '''
        Get a users id.
        Prints error if not found.
            @ user_name: string
        '''
    
        try:
            return [_user.user_id for _user in self.users
                    if _user.name == user_name][0]
        except(IndexError):
            print 'User not found.'


    def get_user_tasks(self, user_name):
        '''
        Get task(s) that belongs to the specified user name
            @ user_name: string
        '''

        user_id = self.get_user_id(user_name)

        if not user_id:
            return

        return [_task.task_name for _task in self.tasks
                if _task.user_id == user_id]


def test_add_users(n):
    '''
    Test by adding n users.
        @ n: int
    '''

    print 'adding users...'
    for x in xrange(n):
        user_name = ''.join([chr(choice(range(97, 123))) for c in range(4)])
        tms.add_user(user_name)

    n_added = len(tms.users)
    print '{} users added. {} user names rejected.'.format(n_added, n - n_added)
    
def test_add_tasks(n):
    '''
    Test by assigning random users tasks.
        @ n: int
    '''

    print 'adding tasks...'
    for x in xrange(n):
        user_name = choice(tms.users).name      # Grabs a random user's name
        task_name = ''.join([chr(choice(range(97, 123))) for c in range(4)])
        tms.add_task(user_name, task_name)

    n_added = len(tms.tasks)
    print '{} tasks added. {} task rejected.'.format(n_added, n - n_added)


if __name__ == "__main__":
    tms = TaskManagementSystem()

    # Test with lots of users.
    # test_add_users(10000)
    # test_add_tasks(10000)

    tms.add_user('Bob')
    tms.add_task('Bob', 'laundry')
    tms.add_task('Bob', 'grocery')
    tms.add_task('Bob', 'daycare')

    print tms.get_user_tasks('Bob')
    # should print: ['laundry', 'grocery', 'daycare']
Source Link
DGDD
  • 293
  • 2
  • 6
Loading