MTH202 Worksheet 5¶

Problem 1¶

Write a function that takes five inputs, then eject two outputs which are the maximum and minimum of the inputs.

Hint: use np.max and np.min, respectively, to find the maximum and minimum entries of an array.

In [1]:
import numpy as np
In [2]:
def Max_Min(x):
    Max = np.max(x)
    Min = np.min(x)
    return Max, Min
In [3]:
x = np.array([1, 4, -6, 3, 11, 5, -7])
Max, Min = Max_Min(x)
print(f"Max = {Max}, Min = {Min}")
Max = 11, Min = -7

Problem 2¶

Write a function that takes one input x and output a string "EVEN" if x is even, and output a string "ODD" if x is odd.

Hints

  1. An even number is a number $x$ whose remainder of $x / 2$ is 0.
  2. Use a % b to compute the remainder of $a/b$.
In [4]:
def Even_Odd(x):
    if x % 2 == 0:
        print(f"x = {x} is Even")
    else:
        print(f"x = {x} is Odd")
In [5]:
Even_Odd(-4)
x = -4 is Even
In [6]:
Even_Odd(57)
x = 57 is Odd

Problem 3¶

Use lambda to define the same function as in Problem 1.

In [7]:
max_min = lambda x: (np.max(x), np.min(x))
In [8]:
Max, Min = max_min([1, 3, 7, -1, 0])
print(f"Max = {Max}, Min = {Min}")
Max = 7, Min = -1