Skip to content

Commit edc34c1

Browse files
committed
EPI: Count bits (py)
1 parent 03e0c47 commit edc34c1

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

elements-of-programming-interviews/problem_mapping.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ problem_mapping = {
1010
"total": 10001
1111
},
1212
"Python: count_bits.py": {
13-
"passed": 0,
13+
"passed": 10001,
1414
"total": 10001
1515
}
1616
},

elements-of-programming-interviews/python/count_bits.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33

44
def count_bits(x: int) -> int:
5-
# TODO - you fill in here.
6-
return 0
5+
bits = 0
6+
while (x):
7+
if (x & 1):
8+
bits += 1
9+
x >>= 1
10+
return bits
711

812

913
if __name__ == '__main__':

0 commit comments

Comments
 (0)