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)
Write the above formula (pythagorean theorem) in python, representing it as a function.
Write a function pythagorean_theorem, which takes two numbers, a and b, and returns c.
Write a function, hypotenuse_times_3, that takes a and b as arguments,
it finds c using pythagorean_theorem, and returns the result of c * 3
ⓘ
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, 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 (<, >=, ...).
ⓘ
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
ⓘ
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): |
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.
ⓘ
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 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
ⓘ