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
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
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 that takes one number, and returns True if it ends with 4, or False otherwise.
Example:
>>> ends_with(14)
True
>>> ends_with(13)
False
Define a function, are_equal, which takes two values, and returns True if they are equal, False otherwise.
Define a function, get_count, which takes one word and one character, s and char,
and returns how many times char appears in s,
till it becomes char. Example:
>>> get_count('abaa', 'a')
3
>>> get_count('abaa', 'b')
1
>>>
Restrictions:
- You cannot use == or !=
- You cannot use method count of strings
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 (<, >=, ...).
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('traukni')
print(res)
Given the above script:
| What is the value of res: | ||
| What is the printed value: |
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(3, 3)
ani(5, 5)
Given the above script, what are the results of the following calls:
| ani(3, 3): | ||
| ani(5, 5): |
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 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 >>>