File tree Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ class MultiplyStrings :
2
+ @staticmethod
3
+ def multiply (num1 : str , num2 : str ) -> str :
4
+ # Special case
5
+ if num1 == '0' or num2 == '0' :
6
+ return '0'
7
+ # Lengths of two strings
8
+ m , n = len (num1 ), len (num2 )
9
+ # List to store the products
10
+ products = [0 ] * (m + n - 1 )
11
+ # Process the strings
12
+ for i in range (m ):
13
+ for j in range (n ):
14
+ products [i + j ] += (ord (num1 [i ]) - ord ('0' )) * (ord (num2 [j ]) - ord ('0' ))
15
+ # Process the products array further
16
+ for i in range (len (products ) - 1 , 0 , - 1 ):
17
+ products [i - 1 ] += products [i ] // 10
18
+ products [i ] %= 10
19
+ # String for the final result
20
+ output = ''
21
+ for i in products :
22
+ output += str (i )
23
+ return output
Original file line number Diff line number Diff line change
1
+ import unittest
2
+
3
+ from problems .math .multiply_strings import MultiplyStrings
4
+
5
+
6
+ class TestMultiplyStrings (unittest .TestCase ):
7
+
8
+ def setUp (self ):
9
+ self .multiply_strings = MultiplyStrings ()
10
+
11
+ def test_multiply_with_zero (self ):
12
+ self .assertEqual (self .multiply_strings .multiply ("0" , "1234" ), "0" )
13
+ self .assertEqual (self .multiply_strings .multiply ("5678" , "0" ), "0" )
14
+
15
+ def test_multiply_single_digit_numbers (self ):
16
+ self .assertEqual (self .multiply_strings .multiply ("3" , "3" ), "9" )
17
+ self .assertEqual (self .multiply_strings .multiply ("5" , "9" ), "45" )
18
+
19
+ def test_multiply_multi_digit_numbers (self ):
20
+ self .assertEqual (self .multiply_strings .multiply ("123" , "456" ), "56088" )
21
+ self .assertEqual (self .multiply_strings .multiply ("999" , "999" ), "998001" )
22
+
23
+
24
+ if __name__ == '__main__' :
25
+ unittest .main ()
You can’t perform that action at this time.
0 commit comments