Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions 01-CalculateSpecialBonus.py
Original file line number Diff line number Diff line change
@@ -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'])
11 changes: 11 additions & 0 deletions 02-FixNamesInATable.py
Original file line number Diff line number Diff line change
@@ -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'])
19 changes: 19 additions & 0 deletions 03-PatientsWithACondition.py
Original file line number Diff line number Diff line change
@@ -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)]