Skip to content
Permalink
cd5d6beb11
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
163 lines (114 sloc) 5.56 KB
import csv
import datetime as dt
from decimal import *
import math
"""
Please write you name here: Salah Abdo
"""
def process_shifts(path_to_csv):
with open('work_shifts.csv') as file:
next(file, None) #skip the first line(Header)
reader = csv.reader(file, delimiter=',')
shifts = {}
for row in reader:
startShift = row[3]
endShift = row[1]
breakNotes = row[0]
payRate = row[2]
start_dt = dt.datetime.strptime(startShift, '%H:%M') # convert start time string to time using 24 hour format
end_dt = dt.datetime.strptime(endShift, '%H:%M')
diff = (end_dt - start_dt)
shiftHours = int((diff.seconds/60)) # convert hour to minutes
breakTime = breakNotes.split("-") #split each time a - is seen to separate the time
breakTime0 = breakTime[0].strip() # remove all whitespaces
breakTime1 = breakTime[1].strip()
if not breakTime0.isdigit() & breakTime1.isdigit():
if breakTime0[-2:] == "AM": # check if letters AM is present
breakTime0 = breakTime0.replace("AM", "") #remove the letters AM and leave the number as it is
elif breakTime0[-2:] == "PM": # check if letters PM is present
breakTime0 = breakTime0.replace("PM", "") #remove the letters PM
breakTime0 = float(breakTime0) + 12 # convert number to 24 hour time
elif breakTime0.isdigit(): # for instances where the break start time and end time using different format ("4-4:10PM")
if breakTime1[-2:] == "AM":
breakTime0 = float(breakTime0)
else:
breakTime0 = float(breakTime0) +12
if breakTime1[-2:] == "AM":
breakTime1 = breakTime1.replace("AM", "")
elif breakTime1 [-2:] == "PM":
breakTime1 = breakTime1.replace("PM", "")
breakTime1 = float(breakTime1) + 12
elif breakTime1.isdigit():
if breakTime0[-2:] == "AM":
breakTime1 = float(breakTime0)
elif breakTime1.isdigit() & breakTime0[-2:] == "PM":
breakTime1 = float(breakTime0) +12
endBreak = str(round(Decimal(breakTime1),2)) #convert to decimal, rounded up to two decimal places to represent time
endBreak = endBreak.replace(".", ":")
endBreak = dt.datetime.strptime(endBreak, '%H:%M')
startBreak = str(round(Decimal(breakTime0),2))
startBreak = startBreak.replace(".", ":")
startBreak = dt.datetime.strptime(startBreak, '%H:%M')
breakTime = (endBreak - startBreak)
breakHours = int((breakTime.seconds/60)) #calculate total break in minutes
hoursWorked = shiftHours - breakHours
labourCost = int((hoursWorked/60) * float(payRate))
if startShift in shifts: # if start time is already present then increase the labour cost for that specific time
shifts[startShift] += labourCost
else:
shifts[startShift] = labourCost
return(shifts)
def process_sales(path_to_csv):
with open('transactions.csv') as file:
next(file, None) #skip the first line(Header)
reader = csv.reader(file, delimiter=',')
sales = {}
for row in reader:
sale = float(row[0])
time = row[1][:2] + ':00' # time rounded up to every hour
if time in sales:
sales[time] += int(sale)
else:
sales[time] = int(sale)
return(sales)
def compute_percentage(shifts, sales):
computePercentage = {}
for time in sorted(shifts): # iterate over key only
labourCost = shifts[time]
try:
sale = sales[time]
percentage = round((labourCost / sale)*100,2)
except:
sale = None
percentage = labourCost * (-1)
computePercentage[time] = percentage
return(computePercentage)
def best_and_worst_hour(percentages):
bestAndWorst = []
worstHour = min(percentages, key=percentages.get) # gets smallest value key in the dictionary
for key, value in percentages.items():
bestHourSales = math.inf #set to infinity
bestHour = ""
if value > 0:
if value < bestHourSales:
bestHourSales = value
bestHour = key
bestAndWorst.append(bestHour)
bestAndWorst.append(worstHour)
return(bestAndWorst)
def main(path_to_shifts, path_to_sales):
"""
Do not touch this function, but you can look at it, to have an idea of
how your data should interact with each other
"""
shifts_processed = process_shifts(path_to_shifts)
sales_processed = process_sales(path_to_sales)
percentages = compute_percentage(shifts_processed, sales_processed)
best_hour, worst_hour = best_and_worst_hour(percentages)
return best_hour, worst_hour
if __name__ == '__main__':
# You can change this to test your code, it will not be used
path_to_sales = "transactions.csv"
path_to_shifts = "work_shifts.csv"
best_hour, worst_hour = main(path_to_shifts, path_to_sales)
# Please write you name here: Salah Abdo