Question

In: Computer Science

Write a Python program that computes certain values such as sum, product, max, min and average...

Write a Python program that computes certain values such as sum, product, max, min and average of any 5 given numbers along with the following requirements.

Define a function that takes 5 numbers, calculates and returns the sum of the numbers.

Define a function that takes 5 numbers, calculates and returns the product of the numbers.

Define a function that takes 5 numbers, calculates and returns the average of the numbers. Must use the function you defined earlier to find the sum of the five numbers.

Define a function that takes 5 numbers, finds and returns the largest value among the numbers. Must use conditional statements and NOT use built-in max function.

Define a function that takes 5 numbers, finds and returns the smallest value among the numbers. Must use conditional statements and NOT use any built-in min function.

Define a function called main inside which Prompt users to enter 5 numbers. Call all the functions passing those 5 numbers entered by the user and display all the returned answers with proper descriptions.

Define a function called test For each of the functions you defined (6-10) write at least 2 automated test cases using assert statements to automatically test and verify that the functions are correctly implemented. make your program continue to run and calculate the same statistical values of as many sets of 5 numbers as a user wishes until they want to quit the program.

Solutions

Expert Solution

# calculator.py

from math import prod

def my_sum(nums):
    return sum(nums)

def my_product(nums):
    return prod(nums)

def my_average(nums):
    return my_sum(nums)/len(nums)

def largest(nums):
    m = nums[0]
    for i in range(len(nums)):
        if nums[i] > m:
            m = nums[i]
    return m

def smallest(nums):
    s = nums[0]
    for i in range(len(nums)):
        if nums[i] < s:
            s = nums[i]
    return s


if __name__ == "__main__":
    while True:
        nums_input = list(map(int, input("Please enter 5 space separated numbers (enter 0 to exit): ").split()))
        if nums_input == [0]:
            break
        print(f"Sum of entered numbers = {my_sum(nums_input)}")
        print(f"Product of entered numbers = {my_product(nums_input)}")
        print(f"Average of entered numbers: {my_average(nums_input)}")
        print(f"Largest number among entered numbers = {largest(nums_input)}")
        print(f"Smallest number among entered numbers = {smallest(nums_input)} \n")

# test.py

import unittest
import calculator as c

class Test(unittest.TestCase):
    def test_calculator1(self):
        test_parameter = [1, 2, 3, 4, 5]
        result = [c.my_sum(test_parameter), c.my_product(test_parameter), c.my_average(test_parameter),
                  c.largest(test_parameter), c.smallest(test_parameter)]
        self.assertEqual(result, [15, 120, 3.0, 5, 1])

    def test_calculator2(self):
        test_parameter = [0, 15, 10, 5, 20]
        result = [c.my_sum(test_parameter), c.my_product(test_parameter), c.my_average(test_parameter),
                  c.largest(test_parameter), c.smallest(test_parameter)]
        self.assertEqual(result, [50, 0, 10.0, 20, 0])

unittest.main()

Related Solutions

Consider the following code section of a parallel program that computes the sum of n values...
Consider the following code section of a parallel program that computes the sum of n values using p processors: my_sum = 0; my_first_i = ... ; my_last_i = ... ; for(my_i = my_first_i; my_i < my_last_i; my_i++) { my_x = Compute_next_value(...); my_sum += my_x; } Here the prefix my_ indicates that each core is using its own, private variables, and each core can execute this block of code independently of the other cores. Devise formulas for the functions that calculate...
Write a program/code that prompts the user for a minimum min and a maximum max. Then...
Write a program/code that prompts the user for a minimum min and a maximum max. Then use these values to print the squares of all even numbers between the min and max variables. (WRITTEN IN C) For example if the user enters 6 as the minimum and 200 as the maximum, the program/code should print the following. Enter limit on minimum square: 6 Enter limit on maximum square: 200 36 64 100 144 196
Write a program in C++ that computes the sum of odd numbers between 1 and 117....
Write a program in C++ that computes the sum of odd numbers between 1 and 117. Execute the program and submit a screen capture of the program and its results.
Write a JavaScript program that computes the average of a collection of numbers and then outputs...
Write a JavaScript program that computes the average of a collection of numbers and then outputs the total number of values that are greater than the average. An A grade is any score that is at least 20% greater than the average. The B grade is any score that is not an A, but is at least 10% greater than the average. An F grade is any score that is at least 20% less than the average. The D grade...
Write a C program/code that prompts the user for a minimum min and a maximum max....
Write a C program/code that prompts the user for a minimum min and a maximum max. Then use these values to print the squares of all even numbers between the min and max variables. For example if the user enters 6 as the minimum and 200 as the maximum, the program/code should print the following. Enter limit on minimum square: 6 Enter limit on maximum square: 200 36 64 100 144 196
PLEASE USE PTHON SPYDER Given a data file, find the max, min, and average values of...
PLEASE USE PTHON SPYDER Given a data file, find the max, min, and average values of columns. Also, create an addition column that is based on the other columns. There will be no error checking required for this lab. You are provided with a data file named (surprise!) data.txt. Data is arranged in columns where each item of data is twelve columns wide (so it is easy to extract data items using slicing): Name Height(m) Weight(kg) Joe 1.82 72.57 Mary...
Write a program that computes and prints the average of numbers in a text file. I...
Write a program that computes and prints the average of numbers in a text file. I created a text file integers.txt that has the numbers 5,4,3,2,1. I need to define the average function Define the main function which will include the following things 1. prompt user for input of text file name 2. open and read input file, can be done before or inside high order functions 3. use two high order functions 4.calculate and display averages and original ist...
Write a C++ program that inputs a sequence of integers into a vector, computes the average,...
Write a C++ program that inputs a sequence of integers into a vector, computes the average, and then outputs the # of input values, the average, and the values themselves. There are 0 or more inputs values, followed by a negative value as the sentinel; the negative value is not stored and not counted. The following sample input: 10 20 0 99 -1 would produce the following output: N: 4 Avg: 32.25 10 20 0 99 The main program has...
Write a C++ program that inputs a sequence of integers into a vector, computes the average,...
Write a C++ program that inputs a sequence of integers into a vector, computes the average, and then outputs the # of input values, the average, and the values themselves. There are 0 or more inputs values, followed by a negative value as the sentinel; the negative value is not stored and not counted. The following sample input: 10 20 0 99 -1 would produce the following output: N: 4 Avg: 32.25 10 20 0 99 The main program has...
Python programming c++ Write a program that computes the amount of money the cheerleaders raised during...
Python programming c++ Write a program that computes the amount of money the cheerleaders raised during their candy bar fundraiser using the following data: 12 bars per case. The candy was sold for $1.00 per bar. Each case cost $8.00. They are required to give the student government association 10% of their earnings. The program should ask the user how many bars were sold. The program should calculate and display the SGA proceed's, the Cheer team's proceeds, and the appropriate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT