Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 08c284b

Browse files
python: add problem 43 and unittest
1 parent 99d47c7 commit 08c284b

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Python/sln_1_100/solution_41_50.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
# -*- coding: UTF-8 -*-
22

33
class Solution_41_50:
4+
def multiply(self, num1: str, num2: str) -> str:
5+
"""
6+
43
7+
Given two non-negative integers num1 and num2 represented as strings,
8+
return the product of num1 and num2, also represented as a string.
9+
:param num1:
10+
:param num2:
11+
:return:
12+
"""
13+
mul_ret = 0
14+
for idx1, digit1 in enumerate(num1[::-1]):
15+
val1 = (ord(digit1) - ord('0')) * 10 ** idx1
16+
for idx2, digit2 in enumerate(num2[::-1]):
17+
val2 = (ord(digit2) - ord('0')) * 10 ** idx2
18+
mul_ret += val1 * val2
19+
return str(mul_ret)
20+
421
def permute(self, nums):
522
"""
623
problem 46

Python/sln_1_100/test_solution_41_50.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ class TestSolution_41_50(TestCase):
77
def setUp(self):
88
self.sln = Solution_41_50()
99

10+
def test_multiply(self):
11+
self.assertEqual(self.sln.multiply('3', '2'), '6')
12+
self.assertEqual(self.sln.multiply('123', '456'), '56088')
13+
self.assertEqual(self.sln.multiply('123', '0'), '0')
14+
1015
def test_permute(self):
1116
a = [1, 2, 3]
1217
b = [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]

0 commit comments

Comments
 (0)