SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Y Daniel Lang
Chapter:
Lists
Exercise:
Programming Excercises
Question:4 | ISBN:978013274719 | Edition: 6

Question

(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

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

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

 

0 0

Discussions

Post the discussion to improve the above solution.