-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlesson_3_task_4.py
More file actions
47 lines (38 loc) · 927 Bytes
/
lesson_3_task_4.py
File metadata and controls
47 lines (38 loc) · 927 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Простое решение с помощью **
exponentiation = lambda x, y: x ** y
print(exponentiation(2, -5))
# Решение без **
def exponentiation_3(x, y):
if x == 0:
return 0
elif y == 0:
return 1
elif y == 1:
return x
elif y < 0:
positive_number_y = abs(y)
z = x
while positive_number_y > 1:
z *= x
positive_number_y -= 1
return 1 / z
else:
z = x
while y > 1:
z *= x
y -= 1
return z
print(exponentiation_3(2, -5))
# Решение с помощью рекурсии
def exponentiation_2(x, y):
if x == 0:
return 0
elif y == 0:
return 1
elif y == 1:
return x
elif y < 0:
return 1 / (x * exponentiation_2(x, -y - 1))
else:
return x * exponentiation_2(x, y - 1)
print(exponentiation_2(2, -5))