From d2c022cb562479be8d384d2cacf5a71a572e83f5 Mon Sep 17 00:00:00 2001 From: Sangeeth Santhosh <73825180+sangeeths29@users.noreply.github.com> Date: Wed, 11 Jun 2025 10:48:07 -0700 Subject: [PATCH] Add files via upload --- 01-CalculateSpecialBonus.py | 37 ++++++++++++++++++++++++++++++++++++ 02-FixNamesInATable.py | 11 +++++++++++ 03-PatientsWithACondition.py | 19 ++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 01-CalculateSpecialBonus.py create mode 100644 02-FixNamesInATable.py create mode 100644 03-PatientsWithACondition.py diff --git a/01-CalculateSpecialBonus.py b/01-CalculateSpecialBonus.py new file mode 100644 index 0000000..e5c96be --- /dev/null +++ b/01-CalculateSpecialBonus.py @@ -0,0 +1,37 @@ +# Problem 1 - Calculate Special Bonus ( https://leetcode.com/problems/calculate-special-bonus/) +import pandas as pd + +def calculate_special_bonus(employees: pd.DataFrame) -> pd.DataFrame: + # Approach 1 + # Condition 1 - Employee ID is odd + odd_id = employees['employee_id'] % 2 == 1 + + # Condition 2 - Employee's name does not start with 'M' + name_start = ~employees['name'].str.startswith('M') + + # Combining both conditions + bonus = odd_id & name_start + + # Employee bonus column + employees['bonus'] = employees['salary'].where(bonus, 0) + + return employees[['employee_id', 'bonus']].sort_values(by = 'employee_id') + + # Approach 2 + result = [] + for i in range(len(employees)): + employee_id = employees['employee_id'][i] + name = employees['name'][i] + + if (employee_id % 2 != 0) and (name[0] != 'M'): + result.append([employee_id, employees['salary'][i]]) + else: + result.append([employee_id, 0]) + + df = pd.DataFrame(result, columns = ['employee_id', 'bonus']).sort_values(by = ['employee_id']) + return df + + # Approach 3 + employees['bonus'] = employees.apply(lambda x : x['salary'] if x['employee_id'] % 2 != 0 and not x['name'].startswith('M') else 0, axis = 1) + + return employees[['employee_id', 'bonus']].sort_values(by = ['employee_id']) \ No newline at end of file diff --git a/02-FixNamesInATable.py b/02-FixNamesInATable.py new file mode 100644 index 0000000..8b1e818 --- /dev/null +++ b/02-FixNamesInATable.py @@ -0,0 +1,11 @@ +# Problem 2 - Fix Names in a Table ( https://leetcode.com/problems/fix-names-in-a-table/ ) +import pandas as pd + +def fix_names(users: pd.DataFrame) -> pd.DataFrame: + # Approach 1 + users['name'] = users['name'].str[0].str.upper() + users['name'].str[1:].str.lower() + return users.sort_values(by = ['user_id']) + + # Approach 2 + users['name'] = users['name'].str.capitalize() + return users.sort_values(by = ['user_id']) \ No newline at end of file diff --git a/03-PatientsWithACondition.py b/03-PatientsWithACondition.py new file mode 100644 index 0000000..e270289 --- /dev/null +++ b/03-PatientsWithACondition.py @@ -0,0 +1,19 @@ +# Problem 3 - Patients with a Condition ( https://leetcode.com/problems/patients-with-a-condition/) +import pandas as pd + +def find_patients(patients: pd.DataFrame) -> pd.DataFrame: + # Approach 1 + result = [] + for i in range(len(patients)): + patient_id = patients['patient_id'][i] + patient_name = patients['patient_name'][i] + conditions = patients['conditions'][i] + + for condition in conditions.split(): + if condition.startswith('DIAB1'): + result.append([patient_id, patient_name, conditions]) + break + return pd.DataFrame(result, columns = ['patient_id', 'patient_name', 'conditions']) + + # Approach 2 + return patients[patients['conditions'].str.contains(r"(^|\s)DIAB1", regex = True)] \ No newline at end of file