Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/my_project/interviews/top_150_questions_round_20/pow_x_n.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod

class Solution:
def myPow(self, x, n):

if not n:
return 1
if n < 1:
return self.myPow(1/x, -n)
if n % 2:
return x*self.myPow(x,n-1)
else:
return self.myPow(x**2,n//2)
29 changes: 29 additions & 0 deletions tests/test_150_questions_round_20/test_pow_x_n_round_20.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import unittest
from src.my_project.interviews.top_150_questions_round_20\
.pow_x_n import Solution

class PowxnTestCase(unittest.TestCase):

def test_powxn_zero_power(self):
solution = Solution()
output = solution.myPow(x=2, n=0)
target = 1
self.assertEqual(output, target)

def test_powxn_negative_power(self):
solution = Solution()
output = solution.myPow(x=2, n=-1)
target = 0.5
self.assertEqual(output, target)

def test_powxn_odd_power(self):
solution = Solution()
output = solution.myPow(x=2, n=1)
target = 2
self.assertEqual(output, target)

def test_powxn_even_power(self):
solution = Solution()
output = solution.myPow(x=2, n=2)
target = 4
self.assertEqual(output, target)
17 changes: 17 additions & 0 deletions tests/test_150_questions_round_20/test_sqrtx_round_20.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import unittest
from src.my_project.interviews.top_150_questions_round_20\
.sqrtx import Solution

class SqrtxTestCase(unittest.TestCase):

def test_even_sqrtx(self):
solution = Solution()
output = solution.mySqrt(x=4)
target = 2
self.assertEqual(output, target)

def test_odd_sqrtx(self):
solution = Solution()
output = solution.mySqrt(x=8)
target = 2
self.assertEqual(output, target)