Question

Problem 5 Given a list a, the max function in Pythons standard library computes the largest element in a: max(a) . Similarly

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

CODE IN PYTHON:

def my_max(a):
    if a:
        max = a[0]
        for val in a:
            if val > max :
                max = val
        return max
    print("List is empty")


def my_min(a):
    if a:
        min = a[0]
        for val in a:
            if val < min :
                min = val
        return min
    print("List is empty")

a = [0.28197719, 0.59580708, 0.54095388, 0.44264927, 0.12360858, 0.3067179, 0.33002426, 0.43553806, 0.14259775, 0.01909447]
print(my_max(a))
print(my_min(a))

INDENTATION:

def my_max(a): if a: max = a[@] for val in a: if val > max : max = val return max print(List is empty) def my_min(a): if a:

OUTPUT:

maxMin C:\Users\madhuvenv\Python3.8\Scripts\python.exe D: /PythonWorld/HelloWorld/maxMin.py 0.59580708 0.01909447 Process fin

Add a comment
Know the answer?
Add Answer to:
Problem 5 Given a list a, the max function in Python's standard library computes the largest...
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
  • What would the function look like in MATLAB? Write a function called mysort that takes a...

    What would the function look like in MATLAB? Write a function called mysort that takes a 3-element vector as its sole arguments. It uses if statements, possibly nested, to return a 3-element vector with its elements in non-decreasing order, i.e., the first element in the returned vector equals the smallest element of the input vector and the last element equals the largest element in the input vector. NOTE: Your function should not use any built-in functions, e.g., sort, min, max,...

  • Hem 1 def expo(base, exponent): # Write your function here Python's pow function returns the result...

    Hem 1 def expo(base, exponent): # Write your function here Python's pow function returns the result of raising a number to a given power. Define a function expo that performs this task, and state its computational complexity using big-o notation. The first argument of this function is the number, and the second argument is the exponent (nonnegative numbers only) 4 def main(): ***Tests with powers of 2.*** for exponent in range (5): print(exponent, expo(2, exponent)) 000 9 if _name__ ==...

  • Question 1 You are given a list of product codes for dog sweaters sold by Sweaters...

    Question 1 You are given a list of product codes for dog sweaters sold by Sweaters 4 Fur Friends. The codes are all 5 digits long and the last two digits in each code represents the size of the corresponding sweater. Here is a sample of the list of codes: sweaterCodes = [12309, 32109, 54204, 35410, 87508, 50404, 94307, 94306, 54205, 12303, 32111, 32110] Write a Python program in a file called SortCodes.py to sort the list of codes into...

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

  • Python. Write a function that takes in a list and returns the first nonzero entry. def...

    Python. Write a function that takes in a list and returns the first nonzero entry. def nonzero(lst): """ Returns the first nonzero element of a list >>> nonzero([1, 2, 3]) 1 >>> nonzero([0, 1, 2]) 1 >>> nonzero([0, 0, 0, 0, 0, 0, 5, 0, 6]) 5 """ "*** YOUR CODE HERE ***"

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

  • 5: Problem 24 Previous Problem Problem List Next Problem (1 point) For the function whose graph...

    5: Problem 24 Previous Problem Problem List Next Problem (1 point) For the function whose graph is given below, list the following quantities in increasing order, from smallest to largest. a fie de B. fre) de ce fel de D. Jo se de Answer (use A, B, C, D, and separate by commas): Note: You can click on the graph to enlarge the image.

  • PLEASE USE F# PROGRAMMING LANGUAGE: NO LOOPS OR CORE LIBRARY FUNCTIONS. ONLY USE RECURSION. Problem 3...

    PLEASE USE F# PROGRAMMING LANGUAGE: NO LOOPS OR CORE LIBRARY FUNCTIONS. ONLY USE RECURSION. Problem 3 (10 pts) Define a function rev that takes a list xs and returns it in the reverse order. • F# standard library has List.rev, do not use it. You need to reimplement it. In [ ]: let rev (xs: 'a list) In [ ]: // Test you function List.rev [] = rev [] |> printfn "%" List.rev [1..9] = rev [1..9] |> printfn "%b'...

  • function predictKNN which computes the KNN prediction for given input xi, K and train data. A...

    function predictKNN which computes the KNN prediction for given input xi, K and train data. A correct implementation will behave similar to below example: Input: xi: list, trainx: list of lists, trainy: list of list, k: integer call: predictKNN(trainx[0],trainx,trainy) output: 2: integer def predictKNN(xi,trainx,trainy,k=3): ### ### YOUR CODE HERE ###

  • The purpose of this lab is to practice working with Python's higher order functions and ternary...

    The purpose of this lab is to practice working with Python's higher order functions and ternary operator. Write all of your code in one file named lab2.py. You can write the code for items 1 through 6 directly inside the main function. Let a be the list of values produced by list(range(1, 11)). [1 point] Using the map function and a lambda argument, write an expression that produces a list of the cubes of the values in a. Assign the...

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