Define a function max_of_two, that takes two numbers, a and b,
and returns the larger of the two numbers.
Restriction: You cannot use max or min functions.
Define a function, max_of_four, that takes four numbers, w, x, y and z,
and returns the largest of the four numbers.
Restriction: You cannot use max, min or any relational operator (<, >=, ...).
Write a function that takes one number, and returns True if it ends with 4, or False otherwise.
Example:
>>> ends_with(14)
True
>>> ends_with(13)
False
Write a function is_low_battery, that takes an integer, level, and returns True if it is below 20, and False otherwise.
Write a function battery_status, that takes an integer, level and:
- It returns "critical" if level is below 5
- It returns "low" if level is between 5 and 19 (including both)
- It returns "normal" if level is 20 or above
Restriction: You CANNOT use any relational operator inside this function's body
(<, >, !=, ...).
Write a function power, that takes two integers, a and b, and returns a to the power of b.
Write a function product, that takes two integers, a and b, and returns their product.
Write a function divisible, that takes two integers, x and y, and returns True if x is fully divisible by y, and False otherwise.
Write a function calculate, that takes two integer, n and m and:
- It returns n to the power of m if n is even and m is odd.
- It returns m to the power of n if n is odd and m is even.
- It returns their product otherwise.
Restriction: You CANNOT use modulus (%) or any relational operator (<, >, !=, ...)
inside this function's body.
A taxi charges:
- $4 for the first km
- $2 for each km from 2 to 10
- $1.50 for each km beyond 10
Write a function that takes the number of kilometers traveled and returns the total fare. Example:
>>> taxi_meter(1)
4
>>> taxi_meter(2)
6
>>> taxi_meter(10)
22
>>> taxi_meter(15)
29.5
Write a function late, that takes an integer, minutes, and returns True if it is greater than or equal to 5, and False otherwise.
Write a function evaluate_lateness, that takes an integer, minutes and:
- It returns "good" if minutes is less than 5
- It returns "late" if minutes is between 5 and 9 (including both)
- It returns "too late" if minutes is between 10 and 14 (including both)
- It returns "unacceptable" if minutes is greater than or equal to 15
Restriction: You CANNOT use any relational operator inside this function's body
(<, >, !=, ...).
def add(a, b):
while a < b:
a = a + 1
return a
def multiply(n):
while n < 5:
n = n * 2
return n
Given the above script, what are the results of the following expressions:
| add(2, 4): | ||
| multiply(3): | ||
| add(1, 2) + multiply(1): |
def quotient(x):
return x // 2
def remainder(x):
return x % 2
def add(x):
return quotient(x) + remainder(x)
Given the script above, what is the return value of the following expressions:
| add(2): | ||
| add(5): | ||
| add(13): |
Complete execution flow of the following program
def divide(a, b): while a > b: a = a / 2 return a def sub(b): i = 0 while i < 10: i = i + b return i res = sub(10) + divide(6, 2)
Complete execution flow of the following program
def power(a, b): return a ** b def multiply(a): return a * a def calculate(a, b): if power(a, b) == multiply(a): return power(a, b) + multiply(a) elif multiply(a) == multiply(b): return multiply(a - b) return multiply(a) == power(a, b) a = calculate(2, 1) calculate(4, 4)