Skip to content

Commit cce028b

Browse files
Use deques for stacks instead of lists
1 parent 5bd2f46 commit cce028b

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

karatsuba.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
Iterative Karatsuba multiplication algorithm
55
"""
66

7+
from collections import deque
8+
79
number_base_bit_shift = 16
810
number_base = 1 << number_base_bit_shift # 65536
911

@@ -43,13 +45,13 @@ def karatsuba_split_inputs(multiplicand, multiplier):
4345
def karatsuba_multiply_iterative(multiplicand, multiplier):
4446
# The node stack holds the input arguments to the Karatsuba multiplication function along with the branch of each
4547
# node in the tree with respect to its parent
46-
node_stack = [[multiplicand, multiplier, 0]] # Assign root node with left branch 0
48+
node_stack = deque([[multiplicand, multiplier, 0]]) # Assign root node with left branch 0
4749

4850
# These stacks hold the results of lower depth calculations and maintain information about which higher order
4951
# calculations they belong to
50-
branch_path = []
51-
m_stack = []
52-
z_stack = [[], [], []]
52+
branch_path = deque()
53+
m_stack = deque()
54+
z_stack = [deque(), deque(), deque()]
5355
leaf_count = 0
5456

5557
# Perform the depth first tree traversal and calculate the results - since the recursive Karatsuba multiplication

0 commit comments

Comments
 (0)