Answer to Question #348804 in Python for Bunny

Question #348804

Create any program using python programming language. Use at least 10 Python built-in functions. Submit the program file of your program ( .py) for those who uses desktop or laptop in code and screenshot of the code and output for those who uses phones to code.

1
Expert's answer
2022-06-07T17:38:36-0400
def array_operation(array: list[int], operation) -> list[int]:
    """
    This function takes parameters (array and operation)
    and makes operation on an array

    :type operation: function
    :param array: list[int]
    :param operation: function (not only built-in)
    :return: int or list[int]
    """
    return operation(array)


def positive(array: list[int]) -> list[int]:
    """
    Return a list of positive integers in input array

    :param array: list[int]
    :return: list[int]
    """
    return [i for i in filter(lambda x: x > 0, array)]


def square(array: list[int], power: int = 2) -> list[int]:
    """
    Return a list of power of integers in input array

    :param power: int
    :param array: list[int]
    :return: list[int]
    """
    # return [i for i in map(lambda x: x ** power, array)]
    return [i for i in map(lambda x: pow(x, power), array)]


def is_one_type(array: list) -> bool:
    """
    Check if each element in input list is a type of integer
    :param array: list
    :return: list
    """
    for e in array:
        if type(e) != int:
            return False
    return True


# Define a list/array of integers
arr: list[int] = [3, -8, 23, 34, -5, 67, -78, 5, -40, 45]


print(f'array of integers: {arr}')
print(f'type of each element in array is integer: {array_operation(arr, is_one_type)}')
print(f'number of integers: {array_operation(arr, len)}')
print(f'max integer: {array_operation(arr, max)}')
print(f'min integer: {array_operation(arr, min)}')
print(f'sum of integers: {array_operation(arr, sum)}')
print(f'positive integers: {array_operation(arr, positive)}')
print(f'power of integers: {array_operation(arr, square)}')
print(f'sorted integers: {array_operation(arr, sorted)}')

print(f'hash value (of immutable type of tuple): {hash(tuple(arr))}')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS