File tree Expand file tree Collapse file tree 3 files changed +34
-0
lines changed Expand file tree Collapse file tree 3 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 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/ )
Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments