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
Write a function, remainder that takes two numbers, x and y and returns the remainder of
dividing x by y.
Example:
>>> remainder(5, 3)
2
Write a function, quotient that takes two numbers, x and y and returns the quotient of
dividing x by y.
Example:
>>> remainder(5, 3)
1
Write a function, equal that takes two numbers and returns True if they are equal, or False otherwise.
Write a function, is_balanced_split that takes two numbers, x and y and returns True
if the remainder and the quotient of dividing x by y are equal. It returns False otherwise.
Example:
>>> is_balanced_split(8, 3)
True
>>> is_balanced_split(6, 5)
True
>>> is_balanced_split(5, 3)
False
Restrictions:
- You cannot use %, // or == inside this function's body
- Body of the function should have only one line
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, add that takes two numbers and returns their sum.
Write a function that takes three numbers and returns their sum.
Restriction: You cannot use + inside this function's body
Hint: You can use add, the function you defined above
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 that takes two numbers, x and y, where y is a single digit number,
and returns True if x ends y, or False otherwise.
Example:
>>> ends_with(16, 6)
True
>>> ends_with(34, 3)
False
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): |
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): |
Complete execution flow of the following program
def calculate(): add() sub() def add(): a = 2 a = a + a a def sub(): add() calculate()
>>>
Complete execution flow of the following program
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) aaa('pse', 0)