Skip to content

Commit 8831152

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 342
1 parent bf8fe89 commit 8831152

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@
223223
- [322 Coin Change](https://leetcode.com/problems/coin-change/description/)
224224
- [337 House Robber III](https://leetcode.com/problems/house-robber-iii/description/)
225225
- [338 Counting Bits](https://leetcode.com/problems/counting-bits/description/)
226+
- [342 Power of Four](https://leetcode.com/problems/power-of-four/description/)
226227
- [354 Russian Doll Envelopes](https://leetcode.com/problems/russian-doll-envelopes/description/)
227228
- [373 Find K Pairs with Smallest Sums](https://leetcode.com/problems/find-k-pairs-with-smallest-sums/description/)
228229
- [377 Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/description/)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
"""Base class for all LeetCode Problems."""
3+
4+
def isPowerOfFour(self, n: int) -> bool:
5+
"""
6+
Given an integer n, return true if it is a power of four.
7+
Otherwise, return false.
8+
9+
An integer n is a power of four, if there exists an integer x such that n == 4x.
10+
"""
11+
x = n
12+
count_ones = 0
13+
while x > 0:
14+
count_ones += x & 1
15+
x >>= 1
16+
return (count_ones == 1) & (n.bit_length() % 2 == 1)

tests/test_342_power_of_four.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest
2+
3+
from awesome_python_leetcode._342_power_of_four import Solution
4+
5+
6+
@pytest.mark.parametrize(
7+
argnames=["n", "expected"],
8+
argvalues=[
9+
(16, True),
10+
(5, False),
11+
(1, True),
12+
],
13+
)
14+
def test_func(n: int, expected: bool):
15+
"""Tests the solution of a LeetCode problem."""
16+
is_power_of_four = Solution().isPowerOfFour(n)
17+
assert is_power_of_four is expected

0 commit comments

Comments
 (0)