FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithms/string/multiply_strings.py at master · MrCanan/algorithms · GitHub
MrCanan
/
algorithms
Public
forked from
keon/algorithms
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithms
/
string
/
multiply_strings.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
36 lines (30 loc) · 913 Bytes
Breadcrumbs
algorithms
/
string
/
multiply_strings.py
Copy path
File metadata and controls
36 lines (30 loc) · 913 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""
Given two non-negative integers num1 and num2 represented as strings,
return the product of num1 and num2.
Note:
The length of both num1 and num2 is < 110.
Both num1 and num2 contains only digits 0-9.
Both num1 and num2 does not contain any leading zero.
You must not use any built-in BigInteger library or convert
the inputs to integer directly.
"""
def
multiply
(
num1
:
"str"
,
num2
:
"str"
)
->
"str"
:
carry
=
1
interm
=
[]
zero
=
ord
(
'0'
)
i_pos
=
1
for
i
in
reversed
(
num1
):
j_pos
=
1
add
=
0
for
j
in
reversed
(
num2
):
mult
=
(
ord
(
i
)
-
zero
)
*
(
ord
(
j
)
-
zero
)
*
j_pos
*
i_pos
j_pos
*=
10
add
+=
mult
i_pos
*=
10
interm
.
append
(
add
)
return
str
(
sum
(
interm
))
if
__name__
==
"__main__"
:
print
(
multiply
(
"1"
,
"23"
))
print
(
multiply
(
"23"
,
"23"
))
print
(
multiply
(
"100"
,
"23"
))
print
(
multiply
(
"100"
,
"10000"
))
Back
|
FazBrowse Home
|
New Git URL