[Tutor] Calculate hours
Mitya Sirenef
msirenef at lightbird.net
Wed Jan 23 04:55:22 CET 2013
On 01/22/2013 10:34 PM, Dave Angel wrote:
> On 01/22/2013 10:08 PM, Mitya Sirenef wrote:
>> On 01/22/2013 09:52 PM, anthonym wrote:
>>> Hello All,
>> >
>> > I originally wrote this program to calculate and print the employee
>> > with the most hours worked in a week. I would now like to change this
>> > to calculate and print the hours for all 8 employees in ascending
>> > order.
>> >
>> > The employees are named employee 0 - 8
>> >
>> > Any ideas?
>> >
>> > Thanks,
>> > Tony
>> >
>> > Code below:
>> >
>> >
>> >
>> > # Create table of hours worked
>> >
>> > matrix = [
>> > [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]]
>> >
>> > maxRow = sum(matrix[0]) # Get sum of the first row in maxRow
>> > indexOfMaxRow = 0
>> >
>> > for row in range(1, len(matrix)):
>> > if sum(matrix[row]) > maxRow:
>> > maxRow = sum(matrix[row])
>> > indexOfMaxRow = row
>> >
>> > print("Employee 7", indexOfMaxRow, "has worked: ", maxRow, "hours")
>>
>>
>> There is an issue with this program: it omits the first row.
>
> No, it doesn't. The OP fills in item 0 in the initial values for
maxRow and indexOfMaxRow. Then he figures he can skip that row in the
loop, which is correct.
Yes, I noticed after writing the reply..
To the OP: that's an odd way to handle it, you can set maxRow to 0 and
then iterate over all rows.
-m
--
Lark's Tongue Guide to Python: http://lightbird.net/larks/
Habit is stronger than reason. George Santayana
More information about the Tutor
mailing list