Write a function, mean that takes two numbers and returns their mean.
Example:
>>> mean(3, 5)
4.0
Write a function, square that takes a number and returns its square.
Example:
>>> square(3)
9
Write a function, square_of_mean, that takes two numbers and returns square of their mean.
Example:
>>> square_of_mean(4, 6)
25.0
Restriction: You cannot use *. + or / inside this function's body
ⓘ
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 (<, >=, ...).
ⓘ
Complete execution flow of the following program
def a(x): return x % 3 == 0 def b(w, y): if w == y: return True return False def ani(x, y): if not b(x, y): return a(x) elif b(x, y) and not a(x): return 'jo more' return b(x, y) ani(4, 4)
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
(<, >, !=, ...).
ⓘ
Write a function, pow, which takes two numbers as arguments, x and y and returns x to the power of y
Write a function, multiply, which takes two numbers as arguments, a and b and returns their product.
Write a function, that takes two numbers, num1 and num2,
and returns (num1 ** num2) * num2.
Restrictions:
- You cannot use ** or * inside this function's body
- You should use pow to calculate num1 ** num2 and multiply to calculate result of
power * num2.
ⓘ
Write a function that takes two positive integers, n and m, and returns sum of all numbers
between 1 and n that are fully divisible by m. Example:
>>> sum_of_divisibles(6, 3)
9
>>> sum_of_divisibles(10, 4)
12
ⓘ
Define a function, divisible, which takes two number, n and m,
and returns True if n is divisible by m, False otherwise. Example:
>>> divisible(4, 2)
True
>>> divisible(5, 2)
False
Define a function, divisibles_prod, which takes one positive integer, x,
and returns production of all odd numbers between 1 and x (not including x).
Example:
>>> divisibles_prod(5)
3
>>> divisibles_prod(6)
15
Restrictions:
- You cannot use %
ⓘ
def quotient(a, b): return a // b def sub(a, b): return a - b def calculate(x, y): if quotient(x, y) == sub(x, y): return quotient(x, y) + sub(x, y) elif quotient(x, y) == 2: return quotient(x, y) - sub(x, y) return quotient(x, y) == quotient(x - 1, y)
Given the above script, what are the results of the following calls:
calculate(3, 2): | |
calculate(7, 2): | |
calculate(6, 3): |
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
ⓘ