Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- class Calculation:
- def __init__(self, numbersFromTheUser): # THIS METHOD IS RUN WHEN WE CALL Calculation()
- self.theNumbers = numbersFromTheUser
- # WE SAVE numbersFromTheUser IN self.theNumbers
- # THE OTHER METHODS IN Calculation WILL USE self.theNumbers
- #Define a function that determines number of arguments
- def number(self):
- return len(self.theNumbers)
- #Define a function that determines mean of arguments
- def mean(self):
- sum = 0
- for i in self.theNumbers:
- sum = sum + int(i)
- return sum/len(self.theNumbers)
- #Define a function that determines median of the arguments
- def median(self):
- sorted_array = sorted(self.theNumbers) #Arrange list in order
- if len(self.theNumbers) % 2 == 0: #Determine if there are an even number of arguments
- left = int(sorted_array[int((len(self.theNumbers))/2-1)])
- right = int(sorted_array[int((len(self.theNumbers))/2)])
- return (left + right)/2 #If even, return the average of the two middle values
- else:
- return sorted_array[int((len(self.theNumbers))/2)] #If odd, return the middle value
- #Define a function that determines mode of the arguments
- def mode(self):
- return max(self.theNumbers, key=self.theNumbers.count)
- #Define a function that determines mode of the arguments
- def midpoint(self):
- return (float(max(self.theNumbers))-float(min(self.theNumbers)))/2+float(min(self.theNumbers))
- def __repr__(self):
- return 'Numbers: ' + str(self.theNumbers)
- arguments = sys.argv[1::] #Slice out the [0] entry from sys.argv
- print("Input space seaprated numers:\n")
- line = next(sys.stdin)
- def get_value(val):
- try:
- return int(val)
- except:
- return len(val)
- arguments = [get_value(item) for item in line.split()]
- print(arguments)
- if not len(arguments):
- raise Exception("Must suply numeric arguments")
- calculationObj = Calculation(arguments) # THIS RUNS THE CODE IN __init__().
- # `arguments` IS PASSED FOR THE `numbersFromTheUser` PARAMETER IN __init__()
- print('The number of arguments is {}'.format(calculationObj.number())) # THIS RUNS THE number() METHOD
- print(arguments)
- print('The mean of the arguments is {}'.format(calculationObj.mean()))
- print('The median of the arguments is {}'.format(calculationObj.median()))
- print('The mode of the arguments is {}'.format(calculationObj.mode()))
- print('The mid point of the arguments is {}'.format(calculationObj.midpoint()))
- # DELETE THIS PART:
- calcObj2 = Calculation([1, 2, 3, 4, 5])
- calcObj3 = Calculation([10, 20, 30, 40])
- print(calcObj2.mean())
- print(calcObj3.mean())
- calcObj2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement