Skip to content

Commit 942e52a

Browse files
authored
Merge pull request #1398 from ivan1016017/september18
adding algo
2 parents ad6a748 + cce5cc6 commit 942e52a

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def myPow(self, x, n):
6+
7+
if not n:
8+
return 1
9+
if n < 1:
10+
return self.myPow(1/x, -n)
11+
if n % 2:
12+
return x*self.myPow(x,n-1)
13+
else:
14+
return self.myPow(x**2,n//2)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_19\
3+
.pow_x_n import Solution
4+
5+
class PowxnTestCase(unittest.TestCase):
6+
7+
def test_powxn_zero_power(self):
8+
solution = Solution()
9+
output = solution.myPow(x=2, n=0)
10+
target = 1
11+
self.assertEqual(output, target)
12+
13+
def test_powxn_negative_power(self):
14+
solution = Solution()
15+
output = solution.myPow(x=2, n=-1)
16+
target = 0.5
17+
self.assertEqual(output, target)
18+
19+
def test_powxn_odd_power(self):
20+
solution = Solution()
21+
output = solution.myPow(x=2, n=1)
22+
target = 2
23+
self.assertEqual(output, target)
24+
25+
def test_powxn_even_power(self):
26+
solution = Solution()
27+
output = solution.myPow(x=2, n=2)
28+
target = 4
29+
self.assertEqual(output, target)

0 commit comments

Comments
 (0)