Question

Using a sort method and my previous code (put inside of addHours method if possible), how...

Using a sort method and my previous code (put inside of addHours method if possible), how would I get the list of each employee's total hours to display in order of least amount of hours to greatest. An explanation for each step would also be great.

import random

#Create function addHours
def addHours(lst):
#Display added hours of employees
print("\nEmployee# Weekly Hours")
print("----------------------------")
print(" 1 ",sum(lst[0]))
print(" 2 ",sum(lst[1]))
print(" 3 ",sum(lst[2]))

#Create main function
def main():
#Create first empty list and add random number
one=[]
for j in range(7):
one.append(random.randint(0,10))
#Create second empty list and add random number
two=[]
for j in range(7):
two.append(random.randint(0,10))
#Create third empty list and add random number
three=[]
for j in range(7):
three.append(random.randint(0,10))
#Create a list to combine previous lists
lst=[one,two,three]
#Display output of tables
print("Employees Data:\n")
print("\t\tMon\tTue\tWed\tThu\tFri\tSat\tSun")
#Use loop to add numbers to the correct destination
for i in range(len(lst)):
print("Employee",(i+1),end='\t')
for j in range(len(lst[i])):
print(lst[i][j], end='\t')
print()

addHours(lst)

main()
  

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import random
from collections import OrderedDict
import operator

#Create function addHours
def addHours(lst):
#Display added hours of employees
print("\nEmployee# Weekly Hours")
print("----------------------------")
#make a dictionary
dic = {}
#add total of list into dictionary
dic[1]=sum(lst[0])
dic[2]=sum(lst[1])
dic[3] =sum(lst[2])
#sort dictionary
sort = sorted(dic.items(), key=operator.itemgetter(1))

#print the values
for i in sort:
for j in i:
print(j,end='\t')
print()
  


#Create main function
def main():
#Create first empty list and add random number
one=[]
for j in range(7):
one.append(random.randint(0,10))
#Create second empty list and add random number
two=[]
for j in range(7):
two.append(random.randint(0,10))
#Create third empty list and add random number
three=[]
for j in range(7):
three.append(random.randint(0,10))
#Create a list to combine previous lists
lst=[one,two,three]
#Display output of tables
print("Employees Data:\n")
print("\t\tMon\tTue\tWed\tThu\tFri\tSat\tSun")
#Use loop to add numbers to the correct destination
for i in range(len(lst)):
print("Employee",(i+1),end='\t')
for j in range(len(lst[i])):
print(lst[i][j], end='\t')
print()

addHours(lst)

main()
  

=====================

Output

c:/users/usaid/Desktop/Chguestions/AdHour.py RESTART: Employees Data: Wed 10 Thu Mon 1 Tue Fri Sat Sun 10 Employee 1 Employee

Code

import random from collections import OrderedDict import operator #create functi n addH urs def addHours (1st): #Display adde#create main functi n def main ): #create first empty list and add random number for j in range (7): one.append (random. rand

Add a comment
Know the answer?
Add Answer to:
Using a sort method and my previous code (put inside of addHours method if possible), how...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (+30) Provide a python program which will Populate an array(list) of size 25 with integers in...

    (+30) Provide a python program which will Populate an array(list) of size 25 with integers in the range -100 (negative 100)   to +100 inclusive Display the array and its length (use the len function) Display the average of all the integers in the array Display the number of even integers (how many) Display the number of odd integers (how many) Display the number of integers > 0   (how many) Display the number of integers < 0   (how many) Display the...

  • How can I get my Python code to check to make sure there are no ships...

    How can I get my Python code to check to make sure there are no ships overlapping in my Battleship code? I am having a really hard time with a game of battleship that I have due for class. Each time I run my code, It will print the ships on the board but it will at times overlap them. I know I have to use some kind of nested if statements but I just don't get how or where...

  • Consider the following python code that will sort the list of numbers using the Bubble Sort...

    Consider the following python code that will sort the list of numbers using the Bubble Sort algorithm def bubbleSort(alist): print(alist) for j in range (len(alist) - 1, 8, -1): for i in range(): if alist[i] > alist[i + 1]: temp = alist[i] alist[i] = alist[i+1] alist[i+1] = temp print(alist) alist = [52, 25, 94, 17, 25, 52] bubbleSort (alist) print(alist) Sort the following series of values into ascending order using the Bubble Sort algorithm. Write out the complete row of...

  • Python This is the code we are using: def main (): video_file = open ( 'video_times.txt')...

    Python This is the code we are using: def main (): video_file = open ( 'video_times.txt')    list = [] for line in video_file: run_time = float(line) list.append (run_time) print ('Video #', ':', run_time) print(list) sum = 0 for i in range(len(list)): sum = sum + list[i]    video_file.close main ( ) The following text is read from the video_times.txt file: Video # 1 : 10.0 Video # 2 : 24.5 Video # 3 : 12.2 Video # 4 :...

  • Write an implementation similar to the Priority Queue method (from chapter 26) to the linked list...

    Write an implementation similar to the Priority Queue method (from chapter 26) to the linked list using node. (from chapter 24). Your code should utilize the selectionsort method from attached and create another method call selectionsort in linkedlist class. To demonstrate the usage of the selection sort method, you should manually create a link list of random integer (say of 5 numbers), and you need to demonstrate the use the selection sort to sorted the link list. Please submit your...

  • I need help in Python. This is a two step problem So I have the code...

    I need help in Python. This is a two step problem So I have the code down, but I am missing some requirements that I am stuck on. Also, I need help verifying the problem is correct.:) 7. Random Number File Writer Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500. The application should let the user specify how many random numbers the file...

  • I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to...

    I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to guess its because I'm not using the swap function I built. Every time I run it though I get an error that says the following Traceback (most recent call last): File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 146, in <module> main() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 122, in main studentArray.gpaSort() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py",...

  • I have to follow functions to perform: Function 1: def calculate_similarity(data_segment, pattern): The aim of this func...

    I have to follow functions to perform: Function 1: def calculate_similarity(data_segment, pattern): The aim of this function is to calculate the similarity measure between one data segment and the pattern. The first parameter 'data_segment' is a list of (float) values, and the second parameter 'pattern' is also a list of (float) values. The function calculates the similarity measure between the given data segment and pattern and returns the calculated similarity value (float), as described earlier in this assignment outline. The...

  • This needs to be in python, however, I am having trouble with the code. Is there...

    This needs to be in python, however, I am having trouble with the code. Is there any way to use the code that I already have under the main method? If so, what would be the rest of the code to finish it? #Create main method def main(): #Create empty list list=[] #Display to screen print("Please enter a 3 x 4 array:") #Create loop for rows and have each entered number added to list for i in range(3): list.append([int(x) for...

  • please explain how to code using java -. Implement the ListNode and LinkedIntList as we did...

    please explain how to code using java -. Implement the ListNode and LinkedIntList as we did in class (you can add other methods if needed). You can reference the coxle in powerpoint slides of LinkedList and your textbook. 2. Implement a LinkedIntList class with the following public operations. a. add(int value) - Adds the given value to the end of the list b.get(int index) - Retums value in list at given index. C.add( int index, int value) - Inserts the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT