FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithms/algorithms/strings/add_binary.py at master · lualgorithm/algorithms · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
lualgorithm
/
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
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithms
/
algorithms
/
strings
/
add_binary.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
26 lines (22 loc) · 472 Bytes
Breadcrumbs
algorithms
/
algorithms
/
strings
/
add_binary.py
Copy path
File metadata and controls
26 lines (22 loc) · 472 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
"""
Given two binary strings,
return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
"""
def
add_binary
(
a
,
b
):
s
=
""
c
,
i
,
j
=
0
,
len
(
a
)
-
1
,
len
(
b
)
-
1
zero
=
ord
(
'0'
)
while
(
i
>=
0
or
j
>=
0
or
c
==
1
):
if
(
i
>=
0
):
c
+=
ord
(
a
[
i
])
-
zero
i
-=
1
if
(
j
>=
0
):
c
+=
ord
(
b
[
j
])
-
zero
j
-=
1
s
=
chr
(
c
%
2
+
zero
)
+
s
c
//=
2
return
s
Back
|
FazBrowse Home
|
New Git URL