(Compute the weekly hours for each employee) Suppose the weekly hours for all employees are stored in a table. Each row records an employee’s seven-day work hours with seven columns. For example, the following table stores the work hours for eight employees. Write a program that displays employees and their total hours in decreasing order of the total hours.
Su M T W Th F Sa
Employee 0 2 4 3 4 5 8 8
Employee 1 7 3 4 3 3 4 4
Employee 2 3 3 4 3 3 2 2
Employee 3 9 3 4 7 3 4 1
Employee 4 3 5 4 3 6 3 8
Employee 5 3 4 4 6 3 4 4
Employee 6 3 7 4 8 3 8 4
Employee 7 6 3 5 9 2 7 9
Compute the weekly hours for each employee Program code:
# Define the table of weekly work hours for employees
hoursTable = [
[2, 4, 3, 4, 5, 8, 8],
[7, 3, 4, 3, 3, 4, 4],
[3, 3, 4, 3, 3, 2, 2],
[9, 3, 4, 7, 3, 4, 1],
[3, 5, 4, 3, 6, 3, 8],
[3, 4, 4, 6, 3, 4, 4],
[3, 7, 4, 8, 3, 8, 4],
[6, 3, 5, 9, 2, 7, 9]
]
# Calculate the total hours for each employee
totalHours = []
for i, hours in enumerate(hoursTable):
total = sum(hours)
totalHours.append((i, total))
# Sort the total hours in decreasing order
totalHours.sort(key=lambda x: x[1], reverse=True)
# Display the employees and their total hours in decreasing order
print("Employees and their total hours (in decreasing order):")
for employee, total in totalHours:
print(f"Employee {employee}: {total} hours")
Executed Output:
Employees and their total hours (in decreasing order):
Employee 7: 41 hours
Employee 6: 37 hours
Employee 0: 34 hours
Employee 4: 32 hours
Employee 3: 31 hours
Employee 1: 28 hours
Employee 5: 28 hours
Employee 2: 20 hours