Introduction

Welcome! Today, we are going to explore an engaging task that involves managing employee records within a company. Specifically, we will work with nested dictionaries and lists to add projects and tasks for employees and retrieve those tasks as needed. This exercise will help you understand how to manipulate nested data structures efficiently.

Introducing Methods to Implement

Let's start by discussing the methods we will implement in our EmployeeRecords class.

  • add_project(self, employee_id: str, project_name: str) -> bool - this method adds a new project to an employee's list of projects. If the project already exists for that employee, the method returns False. Otherwise, it adds the project and returns True.
  • add_task(self, employee_id: str, project_name: str, task: str) -> bool - this method adds a new task to a specified project for an employee. If the project does not exist for that employee, the method returns False. If the task is added successfully, it returns True.
  • get_tasks(self, employee_id: str, project_name: str) -> list | None - this method retrieves all tasks for a specified project of an employee. If the project does not exist for that employee, the method returns None. Otherwise, it returns the list of tasks.
Step 1: Basic Class Structure

Now, let's build our EmployeeRecords class step by step, ensuring we understand each component clearly.

We'll start with the basic structure of the class and initialize our data storage.

class EmployeeRecords:
    def __init__(self):
        self.records = {}

# Instantiate the class to ensure it works.
records = EmployeeRecords()

In this initial setup, we define the EmployeeRecords class and create an instance variable records that is an empty dictionary. This dictionary will be used to store employee records, where each key is an employee ID and each value is another dictionary holding projects.

Step 2: Implementing 'add_project' Method

Next, we'll implement the add_project method to add projects to an employee's record.

class EmployeeRecords:
    def __init__(self):
        self.records = {}

    def add_project(self, employee_id: str, project_name: str) -> bool:
        if employee_id not in self.records:
            self.records[employee_id] = {}
        if project_name in self.records[employee_id]:
            return False
        else:
            self.records[employee_id][project_name] = []
            return True

# Example usage and testing
records = EmployeeRecords()
print(records.add_project("E123", "ProjectA"))  # Returns True
print(records.add_project("E123", "ProjectA"))  # Returns False

Here, the add_project method checks if the given employee_id exists in the records dictionary. If not, it initializes an empty dictionary for that employee. Then, it checks if the project_name already exists for that employee. If it does, the method returns False. Otherwise, it initializes an empty list for the project (to hold tasks) and returns True.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal