-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathQuestion3.py
More file actions
42 lines (34 loc) · 870 Bytes
/
Question3.py
File metadata and controls
42 lines (34 loc) · 870 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
__author__ = 'rohanmathure'
import unittest
import math
def pow(x,n):
ans = float()
if n==0:
ans= 1
elif n<0:
ans = 1.0/ pow(x, abs(n))
elif n >0:
if isinstance(n,int):
ans = powerPositive(x,n)
else:
#fraction
ans= math.exp(n*math.log(x))
return ans
def powerPositive(x,n):
ans = float()
ans = 1
while n >=1:
ans = ans * x
n = n-1
return ans
class TestPower(unittest.TestCase):
def testBaseCase(self):
self.assertEqual(pow(3,3),27)
self.assertEqual(pow(3,0),1)
def testNegative(self):
self.assertEqual(pow(2,-2),0.25)
self.assertEqual(pow(-2,-2),0.25)
self.assertEqual(pow(-2,-1),-0.5)
def testFraction(self):
self.assertEqual(pow(4,0.5),2)
self.assertEqual(pow(16,0.25),2)