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 (<, >=, ...).
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, 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.
Define a function, are_equal, which takes two values, and returns True if they are equal, False otherwise.
Define a function, print_chars, which takes one word and one character, s and char,
and prints all characters of s, each in a new line,
till it becomes char. Example:
>>> print_chars('ani', 'i')
a
n
>>> print_chars('psejo', 'j')
p
s
e
>>>
Restrictions: You cannot use == or !=
Define a function, add, which takes two numbers, a and b, and returns their sum.
Define a function, add_up_to, which takes one positive integer, n,
and returns sum of all numbers between 0 and n (inclusive). Example:
>>> add_up_to(4)
10
>>>
Restrictions:
- You cannot use range
- You cannot use +
- You cannot use sum
Complete execution flow of the following program
def is_vowel(char): for vowel in 'aeiou': if char == vowel: return 1 return 0 def count_vowels(s): count = 0 for char in s: count = count + is_vowel(char) if char == 'y': print('reset') count = 0 return count res = count_vowels('bye')
reset >>>
def is_po(s):
if s == 'po':
return True
return False
def is_zero(n):
return n == 0
def aaa(a, b):
if not is_po(a) and is_zero(b):
return is_po(a)
elif not is_zero(b):
return 'pse'
else:
return is_po(a) and is_zero(b)
Given the above script, what are the results of the following calls:
| aaa('po', 0): | ||
| aaa('jo', 0): | ||
| aaa('po', 2): |
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)