Skip to content

Commit 2ad9510

Browse files
committed
primitive multiply (py)
1 parent a86f247 commit 2ad9510

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

elements-of-programming-interviews/problem_mapping.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ problem_mapping = {
8080
"total": 10006
8181
},
8282
"Python: primitive_multiply.py": {
83-
"passed": 0,
83+
"passed": 10006,
8484
"total": 10006
8585
}
8686
},

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
from test_framework import generic_test
22

33

4+
def add(x: int, y: int) -> int:
5+
val = x ^ y
6+
carry = (x & y) << 1
7+
while carry:
8+
x = val
9+
y = carry
10+
val = x ^ y
11+
carry = (x & y) << 1
12+
return val
13+
14+
415
def multiply(x: int, y: int) -> int:
5-
# TODO - you fill in here.
6-
return 0
16+
product = 0
17+
while y:
18+
if (y & 1):
19+
product += x
20+
x <<= 1
21+
y >>= 1
22+
return product
723

824

925
if __name__ == '__main__':

0 commit comments

Comments
 (0)