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.
Write a function abs, that takes one integer and returns its absolute value.
Write a function add, that takes two integers, a and b, and returns their sum.
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 one integer, n, and:
- It returns 7 if n is fully divisible by 7.
- It returns its absolute value if n is fully divisible by 3 and 4.
- It returns its double if n is not fully divisible by 3 and 4.
- It returns its None otherwise.
Examples:
>>> calculate(14)
7
>>> calculate(-12)
12
>>> calculate(13)
26
>>> calculate(8)
>>>
Restriction: You CANNOT use any arithmetic operator (+, -, ...),
modulus (%), or any relational operator (<, >, !=, ...)
inside this function's body.
Complete execution flow of the following program
def divisible(n, m): if n % m == 0: return 1 return 2 def get_sum(x): total = 0 for i in range(x): if total == 1: print(total) total = 0 total = total + divisible(i, 3) return total res = get_sum(2)
1 >>>
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, is_greater, that takes two numbers, a and b, and returns True if a is greater than b, otherwise False.
Define another function, count_greater_than, that takes three numbers,
n, a, b and c, and returns the count of how many of the three numbers a, b, and c
are greater than n.
Example:
>>> count_greater_than(1, 2, 2, 3)
3
>>> count_greater_than(2, 2, 2, 3)
1
>>> count_greater_than(6, 5, 2, 3)
0
Restriction: You cannot use max, min or any relational operator (<, >=, ...).
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.
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 divisible(n, m):
if n % m == 0:
return 1
return 2
def get_sum(x):
total = 0
for i in range(x):
if total == 3:
print(total)
total = 0
total = total + divisible(i, 3)
return total
res = get_sum(3)
Given the above script:
| What is the value of res: | ||
| What is the printed value: |
Complete execution flow of the following program
def calculate(x, y): res = x * y res = x + res revert(res, x, y) def revert(a, b, c): res = a - b res = res / c print(res) calculate(2, 3)
2.0 >>>
Complete execution flow of the following program
def quotient(x, y): return x // y def remainder(x, y): return x % y def add(x, y): return quotient(x, remainder(x, y))
>>> add(4, 3) 4 >>> add(8, 3) 4 >>>