Question

PYTHON. Write the following functions without using any predefined functions unless specified. def min (dataList) :      ...

PYTHON.

Write the following functions without using any predefined functions unless specified.

def min (dataList) :       #returns the smallest value of the data values in dataList

def max (dataList):        #returns the largest value

def getRange (dataList)

def mean (dataList) :     #returns the average of data values

def median (dataList):    #returns the middle value of an ordered list

#note: to sort the list first, may use the predefined sort function

Then write a test1 function to test the above functions using the following data sets.

  1. d1 = [37,32,46,28,37,41,31]
  2. d2 = [2.5, 5.3, 6.1, 1.34, 3.3, 5, 25, 4.3, 6.0. 0.5]
  3. d3 = []
  4. d4 = “abc”  
  5. d5 = [2.5, “abc”, 4]

Write a test2 function that imports the functions in the first task from statistics module and perform the same test as in the second task. Finish with a main function to call test1() and test().

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

Answer:

# min fucntion to find the minimum value
def min(dataList):
try:
dataList.sort()
return dataList[0]
except:
return "Data list may be empty or contains some non numeric characters."

# max fucntion to find the maximum value
def max(dataList):
try:
dataList.sort()
return dataList[-1]
except:
return "Data list may be empty or contains some non numeric characters."

# getrange fucntion is not defined properly so i am just returning the length of the list
# Kindly add the more informatio about this fucntion
def getRange (dataList):
return len(dataList)


# mean fucntion
def mean (dataList) :
try:
total = 0
for num in dataList:
total += num
mean = total /len(dataList)
return mean

except:
return "Data list may be empty or contains some non numeric characters."

# median fucntion
def median (dataList):
try:
n = len(dataList)
dataList.sort()
if n % 2 == 0:
median1 = dataList[n//2]
median2 = dataList[n//2 - 1]
median = (median1 + median2)/2
else:
median = dataList[n//2]
return median
except:
return "Data list may be empty or contains some non numeric characters."

# test1 fucntion
def test1():
d1 = [37,32,46,28,37,41,31]
d2 = [2.5, 5.3, 6.1, 1.34, 3.3, 5, 25, 4.3, 6.0, 0.5]
d3 = []
d4 = "abc"
d5 = [2.5, "abc", 4]
dataset = [d1, d2, d3, d4, d5]
i = 1
for d in dataset:
print("For dataset d" + str(i))
print(min(d))
print(max(d))
print(getRange(d))
print(mean(d))
print(median(d))
i += 1
print()


# test2 fucntion
def test2():
import statistics
d1 = [37,32,46,28,37,41,31]
d2 = [2.5, 5.3, 6.1, 1.34, 3.3, 5, 25, 4.3, 6.0, 0.5]
d3 = []
d4 = "abc"
d5 = [2.5, "abc", 4]
dataset = [d1, d2, d3, d4, d5]
i = 1
for d in dataset:
try:
print("For dataset d" + str(i))
print(min(d))
print(max(d))
print(statistics.mean(d))
print(statistics.median(d))
  
except:
print("Data list may be empty or contains some non numeric characters.")
i +=1
print()
# main fucntion
def main():
print("\nTest1:\n")
test1()
print("\nTest2:\n")
test2()

if __name__ == "__main__":
main()

Code Screen shot:

Sample Output:

Note: Let me know if you have any doubt.

Add a comment
Know the answer?
Add Answer to:
PYTHON. Write the following functions without using any predefined functions unless specified. def min (dataList) :      ...
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
  • PYTHON Given three unsorted lists, a name list (e.g. nameLst = [“Betty”, “Andrew”, “Zach”, “Cathy”, …]),...

    PYTHON Given three unsorted lists, a name list (e.g. nameLst = [“Betty”, “Andrew”, “Zach”, “Cathy”, …]), an Exam1 score list (e.g. score1 = [50, 45, 48, 48, …]), and an Exam 2 score list (e.g. score2=[35, 48, 50, 37, …]) Write the following functions: def makeLst (aNameLst, e1ScoreLst, e2ScoreLst) : #the function will return a list of tuples as in the following form #[(“Betty”, (50,35)), (“Andrew”, (45,48)), (“Zach”, (48,50)), (“Cathy”, (48,37)), …] def personalAverage(aList) : #takes a class list in...

  • Objectives Work with functions Assignment Write each of the following functions using Python. The function header MUST b...

    Objectives Work with functions Assignment Write each of the following functions using Python. The function header MUST be written as specified. In your main code test all of the specified functions. Each function must have a comment block explaining what it does, what the parameters are and what the return value is. Please remember the following two guidelines: unless the purpose of the function is to generate output DO NOT write to the screen within the function unless the purpose...

  • Python 3 Coding Functions: Here is any code required to help code what is below: def...

    Python 3 Coding Functions: Here is any code required to help code what is below: def pokemon_by_types(db, types): new_db = {} for pokemon_type in types: for key in db: # iterate through all the type in types list if db[key][1] == pokemon_type or db[key][2] == pokemon_type: if key not in new_db: new_db[key] = db[key] return new_db I need help coding the functions listed below in the image: Thank you get types(db): Given a database db, this function determines all the...

  • Rules: You may NOT use the following built-in functions. sort() sum() max() min() Except for format()...

    Rules: You may NOT use the following built-in functions. sort() sum() max() min() Except for format() all string methods are banned. Except for append() and extend() all list methods are banned. You may not import any module. Make sure to test your code with a variety of inputs. Do not assume the examples in these directions are reflective of the hidden test cases. Part 3 Echo (5 Points) Write a function called echo that takes as argument a list and...

  • Objective To program using the functional programming paradigm Assignment: Write the following functions using Scheme (or...

    Objective To program using the functional programming paradigm Assignment: Write the following functions using Scheme (or LISP if you prefer) . A function (binomial N k) that returns the binomial coefficients C(N, k), defined recursively as: C(NO) = 1, C(N, N) = 1, and, for 0<k < N E(N, k) = C(N-1, k) + C(N-1, k-1) 2. A function (mod N M) that returns the modulus remainder when dividing N by M 3. A function (binaryToDecimal b) that takes a...

  • 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...

  • For this lab, you will work with two-dimensional lists in Python. Do the following: Write a...

    For this lab, you will work with two-dimensional lists in Python. Do the following: Write a function that returns the sum of all the elements in a specified column in a matrix using the following header: def sumColumn(matrix, columnIndex) Write a function display() that displays the elements in a matrix row by row, where the values in each row are displayed on a separate line. Use a single space to separate different values. Sample output from this function when it...

  • In Python 3 Write a LinkedList class that has recursive implementations of the display, remove, contains,...

    In Python 3 Write a LinkedList class that has recursive implementations of the display, remove, contains, insert, and normal_list methods. You may use default arguments and/or helper functions. The file must be named: LinkedList.py Here is what I have for my code so far. The methods I need the recursive implementations for will be bolded: class Node: """ Represents a node in a linked list (parent class) """ def __init__(self, data): self.data = data self.next = None class LinkedList: """...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • Hi. Could you help me write the below program? Please don't use any header other than...

    Hi. Could you help me write the below program? Please don't use any header other than iostream, no Chegg class, no argc/argv. Nothing too advanced. Thank you! For this assignment you are implement a program that can be used to compute the variance and standard deviation of a set of values. In addition your solution should use the functions specified below that you must also implement. These functions should be used wherever appropriate in your solution. With the exception of...

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