Ad

recursive implementation.

3rd return -> num ^ p = (num ^ 2) ^ (p / 2)

Code
Diff
  • def power(num, p):
        assert p >= 0
        if num == 0 or p == 0:
            return int(num != 0)
        if p % 2:
            return power(num, p - 1) * num
        return power(num * num, p // 2)
    • def power(num1, num2):
    • return num1**num2
    • number_1 = 2
    • number_2 = 5
    • power(number_1, number_2)
    • def power(num, p):
    • assert p >= 0
    • if num == 0 or p == 0:
    • return int(num != 0)
    • if p % 2:
    • return power(num, p - 1) * num
    • return power(num * num, p // 2)
Code
Diff
  • print_String = lambda element: ''.join(reversed(str(element)))
    • print_String=lambda l: str(l)[::-1]
    • print_String = lambda element: ''.join(reversed(str(element)))
Code
Diff
  • def remove(integers, values):
        values = set(values)
        return list(filter(lambda element: element not in values, integers))
    • def remove(integers, values):
    • value_set = set(values)
    • res = []
    • for x in integers:
    • if x not in value_set:
    • res += [x]
    • return res
    • values = set(values)
    • return list(filter(lambda element: element not in values, integers))
Code
Diff
  • def find_first_sub_string(text: str, sub: str):
        index = text.find(sub)
        return index if index != -1 else None
    • def find_first_sub_string(text, sub):
    • try: return text.index(sub)
    • except: pass
    • def find_first_sub_string(text: str, sub: str):
    • index = text.find(sub)
    • return index if index != -1 else None