-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpower-of-three.py
More file actions
32 lines (25 loc) · 877 Bytes
/
power-of-three.py
File metadata and controls
32 lines (25 loc) · 877 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
# Given an integer, write a function to determine if it is a power of three.
#
# Follow up:
# Could you do it without using any loop / recursion?
# This is a pretty basic solution ; not sure how I would do it without loops
# More reading at https://leetcode.com/discuss/78532/summary-all-solutions-new-method-included-at-15-30pm-jan-8th
import unittest
class Solution(object):
def isPowerOfThree(self, n):
"""
:type n: int
:rtype: bool
"""
if n>1:
while n%3==0:
n = n/3
return n==1
class TestPowerofThree(unittest.TestCase):
my_solution = Solution()
def test_default_pass(self):
self.assertTrue(self.my_solution.isPowerOfThree(27))
def test_default_fail(self):
self.assertFalse(self.my_solution.isPowerOfThree(30))
if __name__ == "__main__":
unittest.main()