Question

In: Computer Science

Python: Write a function that receives a one dimensional array of integers and returns a Python...

Python:

Write a function that receives a one dimensional array of integers and returns a Python tuple with two values - minimum and maximum values in the input array. You may assume that the input array will contain only integers and will have at least one element. You do not need to check for those conditions.

Restrictions: No built-in Python data structures are allowed (lists, dictionaries etc). OK to use a Python tuple to store and return the result.

Below is the starter skeleton code for this problem, on which the implementation must be built. Methods defined in the skeleton code must retain their names and input / output parameters. Variables defined in skeleton code must also retain their names.

from a1_include import

*def min_max(arr: StaticArray) -> ():

"""

TODO: Write this implementation

"""

return 0, 0

# BASIC TESTING

if __name__ == "__main__":

# example 1

arr = StaticArray(5)

for i, value in enumerate([8, 7, 6, -5, 4]):

arr[i] = value

print(min_max(arr))

# example 2

arr = StaticArray(1)

arr[0] = 100

print(min_max(arr))

# example 3

arr = StaticArray(3)

for i, value in enumerate([3, 3, 3]):

arr[i] = value

print(min_max(arr))

Solutions

Expert Solution

#!usr/bin/python
# from a1_include import StaticArray

def min_max(arr: StaticArray) -> ():

    """

    TODO: Write this implementation

    """
    mi = minimum(arr)
    ma = maxmimum(arr)
    return (mi, ma)

def minimum(arr: StaticArray) -> ():
    mi = arr[0]
    if len(arr) == 1:
        return mi
    else:
        for i in range(len(arr)):
            if mi < arr[i]:
                mi = arr[i]
        return mi
def maximum(arr: StaticArray) -> ():
    ma = arr[0]
    if len(arr) == 1:
        return ma
    else:
        for i in range(len(arr)):
            if ma > arr[i]:
                ma = arr[i]
        return ma
  
# BASIC TESTING

if name == "main":

# example 1

    arr = StaticArray(5)

    for i, value in enumerate([8, 7, 6, -5, 4]):

        arr[i] = value

    print(min_max(arr))

    # example 2

    arr = StaticArray(1)

    arr[0] = 100

    print(min_max(arr))

    # example 3

    arr = StaticArray(3)

    for i, value in enumerate([3, 3, 3]):

        arr[i] = value

    print(min_max(arr))


Related Solutions

One dimensional dynamic array Write a function that returns the number of integers in an input...
One dimensional dynamic array Write a function that returns the number of integers in an input file stream with the following interface: int findNumber(ifstream &x); Then, use this number to dynamically allocate an integer array. Write another function that reads each number in an input file stream and assign the value to the corresponding array element with the following interface: void assignNumber(ifstream &x, int y[ ]); In your main( ), first open “in.dat” as an input file. Next, apply findNumber(...
In PYTHON: Write a function that receives a sentence and returns the last word of that...
In PYTHON: Write a function that receives a sentence and returns the last word of that sentence. You may assume that there is exactly one space between every two words, and that there are no other spaces at the sentence. To make the problem simpler, you may assume that the sentence contains no hyphens, and you may return the word together with punctuation at its end.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a function that receives a StaticArray with integers and returns a new StaticArray object with...
Write a function that receives a StaticArray with integers and returns a new StaticArray object with the content from the original array, modified as follows: 1) If the number in the original array is divisible by 3, the corresponding element in the new array should be a string ‘fizz’. 2) If the number in the original array is divisible by 5, the corresponding element in the new array should be a string ‘buzz’. 3) If the number in the original...
Python Question: Write a function that checks to see if an array of integers is sorted...
Python Question: Write a function that checks to see if an array of integers is sorted in an increasing fashion, returning true if it is, false otherwise. Test it with at least4 arrays - 2 sorted and 2 not sorted. Use a CSV formatted input file as described in the previous question to run your program through some tests, where again the filename is passed as an argument. Heres what I have so far: import sys # command line arguement...
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
Write a class VectorInt to implement the concept of one dimensional array of integers with extendable...
Write a class VectorInt to implement the concept of one dimensional array of integers with extendable array size. Your class should support storing an integer at a specific index value, retrieving the integer at a specific index value, and automatically increasing storage for the saved values.
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks if ch is...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers as follows: Assign {35,20,-43,-10,6,7,13} -Create a one dimensional array of 7 Boolean values as follows: Assign {true,false,false,true,false,true,false} -Create a one dimensional array of 7 floating-point values as follows: Assign {12.0f,1.5f,-3.5f,-2.54f,3.4f,45.34f,22.13f} -Declare sum as integer and set it to 0. -Declare sumf as float and set it to 0.0f. -Use a for loop to go through each element of the Boolean array, and if an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT