Advertisement
Guest User

RS

a guest
Apr 24th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.49 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5. # The program then loops through each employee and reports if their earnings is within 5,000 of the average earnings
  6. # of all employees.
  7. # Sample interaction:
  8. #     Please enter the employee name or * to finish: d
  9. #     Please enter the salary in thousands for d: 50
  10. #     Please enter the employee name or * to finish: e
  11. #     Please enter the salary in thousands for e: 48
  12. #     Please enter the employee name or * to finish: f
  13. #     Please enter the salary in thousands for f: 52
  14. #     Please enter the employee name or * to finish: g
  15. #     Please enter the salary in thousands for g: 0.010
  16. #     Please enter the employee name or * to finish: h
  17. #     Please enter the salary in thousands for h: 100
  18. #     Please enter the employee name or * to finish: *
  19. #     d, 50000.00
  20. #     e, 48000.00
  21. #     f, 52000.00
  22. #
  23.  
  24.  
  25. # function to get a floating point number
  26. # if successful the floating point number will be returned
  27. # otherwise the user will have to continue attempting to enter the number
  28. # @param m is the prompt to the user.
  29. # @return a floating point number supplied by the user
  30. def getFloat(m):
  31.     # infinate loop to acquire the score
  32.     while True:
  33.         num = input(m)
  34.         try:
  35.             # try to turn s into a float
  36.             floatNum = float(num)
  37.             # successful ~ leave the loop and return the score
  38.             return floatNum
  39.         except:
  40.             # float cast failed ~ message user and try again
  41.             print("I was expecting a number, please try again...")
  42.  
  43.  
  44.  
  45. # program main ~ entry point of execution
  46. def main():
  47.  
  48.     # declare the data arrays
  49.     employees = []
  50.     salaries = []
  51.  
  52.     # flag as loop control variable
  53.     done = False
  54.  
  55.     # get employee names and earnings until the sentinal is entered
  56.     while not done:
  57.         # get the employee's name
  58.         name = input("Please enter the employee name or * to finish: ")
  59.  
  60.         # check for a '*' to see if we are done or not
  61.         if name != '*':
  62.             # no '*' so we keep going
  63.  
  64.             # set a prompt for getting the salary
  65.             prompt = "Please enter the salary in thousands for " + name + ": "
  66.  
  67.             # use the getFloat method from last assignment to get the earnings for the employee
  68.             # and convert it from "in thousands" to the true value
  69.             earnings = (1000 * getFloat(prompt))
  70.  
  71.             # append the employee name to the employee array
  72.             employees.append(name)
  73.  
  74.             # append the salary to the parallel salary array
  75.             salaries.append(earnings)
  76.         else:
  77.             done = True
  78.  
  79.  
  80.     # now that we have the employees, we can find the average using a loop and dividing by the array length
  81.     # set the accumulator for the total
  82.     total = 0
  83.  
  84.     # find the grand total
  85.     for s in salaries:
  86.         total += s
  87.  
  88.     # calculate the avarage salary
  89.     averageSalary = total / len(salaries)
  90.  
  91.     # now that we have the average salary we can start looking at the employees and comparing them using a loop
  92.     # set the minumum and maximum threshholds for testing
  93.     minThreshold = averageSalary - 5000
  94.     maxThreshold = averageSalary + 5000
  95.  
  96.     print("\nThe average salary is: $ %0.2f" % (averageSalary))
  97.     print("\nEmployees earning between $ %0.2f and $ %0.2f are:" % (minThreshold, maxThreshold))
  98.  
  99.     # loop through all the employees and test them to see if they are in the salary range
  100.     for i in range(len(salaries)):
  101.         # if the salary for the employee is greater than the min and less than the max thresholds print employee info
  102.         if (salaries[i] > minThreshold) and (salaries[i] < maxThreshold):
  103.             print("%s, $ %0.2f" % (employees[i], salaries[i]))
  104.  
  105.  
  106.  
  107. # execute the application
  108. main()
  109.  
  110.  
  111. def swap( A, x, y, E):
  112.     tmp = A[x]
  113.     tmp2 = E[x]
  114.     A[x] = A[y]
  115.     E[x] = E[y]
  116.     A[y] = tmp
  117.     E[y] = tmp2
  118.  
  119.  
  120. def bubblesort(A, E):
  121.     for i in range (len(A)):
  122.         for k in range(len(A) - 1):
  123.             first = k
  124.             second = k+1
  125.  
  126.             if(A[first] > A[second]):
  127.                 swap(A, first, second, E)
  128.  
  129.  
  130.         print(A)
  131.         temp = input("Hit Enter for the next step:\n")
  132.  
  133.  
  134. def main():
  135.     # append the employee name to the employee array
  136.     myArray = employees.append(name)
  137.  
  138.     #append the salary to the parallel salary array
  139.     salaries.append(earnings)
  140.  
  141.     employees = []
  142.     earnings = []
  143.  
  144.     # input the data
  145.  
  146.  
  147.     # process
  148.  
  149.     bubblesort(earnings, employees)
  150.     print(myArray)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement