Advertisement
asweigart

Untitled

Oct 25th, 2018
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. import sys
  2.  
  3. class Calculation:
  4. def __init__(self, numbersFromTheUser): # THIS METHOD IS RUN WHEN WE CALL Calculation()
  5. self.theNumbers = numbersFromTheUser
  6. # WE SAVE numbersFromTheUser IN self.theNumbers
  7. # THE OTHER METHODS IN Calculation WILL USE self.theNumbers
  8.  
  9. #Define a function that determines number of arguments
  10. def number(self):
  11. return len(self.theNumbers)
  12.  
  13. #Define a function that determines mean of arguments
  14. def mean(self):
  15. sum = 0
  16. for i in self.theNumbers:
  17. sum = sum + int(i)
  18. return sum/len(self.theNumbers)
  19.  
  20. #Define a function that determines median of the arguments
  21. def median(self):
  22. sorted_array = sorted(self.theNumbers) #Arrange list in order
  23. if len(self.theNumbers) % 2 == 0: #Determine if there are an even number of arguments
  24. left = int(sorted_array[int((len(self.theNumbers))/2-1)])
  25. right = int(sorted_array[int((len(self.theNumbers))/2)])
  26. return (left + right)/2 #If even, return the average of the two middle values
  27. else:
  28. return sorted_array[int((len(self.theNumbers))/2)] #If odd, return the middle value
  29.  
  30. #Define a function that determines mode of the arguments
  31. def mode(self):
  32. return max(self.theNumbers, key=self.theNumbers.count)
  33.  
  34. #Define a function that determines mode of the arguments
  35. def midpoint(self):
  36. return (float(max(self.theNumbers))-float(min(self.theNumbers)))/2+float(min(self.theNumbers))
  37.  
  38. def __repr__(self):
  39. return 'Numbers: ' + str(self.theNumbers)
  40.  
  41.  
  42.  
  43. arguments = sys.argv[1::] #Slice out the [0] entry from sys.argv
  44. print("Input space seaprated numers:\n")
  45. line = next(sys.stdin)
  46.  
  47. def get_value(val):
  48. try:
  49. return int(val)
  50. except:
  51. return len(val)
  52.  
  53. arguments = [get_value(item) for item in line.split()]
  54. print(arguments)
  55.  
  56. if not len(arguments):
  57. raise Exception("Must suply numeric arguments")
  58.  
  59. calculationObj = Calculation(arguments) # THIS RUNS THE CODE IN __init__().
  60. # `arguments` IS PASSED FOR THE `numbersFromTheUser` PARAMETER IN __init__()
  61.  
  62. print('The number of arguments is {}'.format(calculationObj.number())) # THIS RUNS THE number() METHOD
  63. print(arguments)
  64. print('The mean of the arguments is {}'.format(calculationObj.mean()))
  65. print('The median of the arguments is {}'.format(calculationObj.median()))
  66. print('The mode of the arguments is {}'.format(calculationObj.mode()))
  67. print('The mid point of the arguments is {}'.format(calculationObj.midpoint()))
  68.  
  69.  
  70. # DELETE THIS PART:
  71. calcObj2 = Calculation([1, 2, 3, 4, 5])
  72. calcObj3 = Calculation([10, 20, 30, 40])
  73. print(calcObj2.mean())
  74. print(calcObj3.mean())
  75. calcObj2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement