FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
PythonAlgorithms/strings/encode_decode.py at master · taoran92/PythonAlgorithms · GitHub
taoran92
/
PythonAlgorithms
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
PythonAlgorithms
/
strings
/
encode_decode.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
37 lines (33 loc) · 856 Bytes
Breadcrumbs
PythonAlgorithms
/
strings
/
encode_decode.py
Copy path
File metadata and controls
37 lines (33 loc) · 856 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
37
""" Design an algorithm to encode a list of strings to a string.
The encoded mystring is then sent over the network and is decoded
back to the original list of strings.
"""
# Implement the encode and decode methods.
def
encode
(
strs
):
"""Encodes a list of strings to a single string.
:type strs: List[str]
:rtype: str
"""
res
=
''
for
string
in
strs
.
split
():
res
+=
str
(
len
(
string
))
+
":"
+
string
return
res
def
decode
(
s
):
"""Decodes a single string to a list of strings.
:type s: str
:rtype: List[str]
"""
strs
=
[]
i
=
0
while
i
<
len
(
s
):
index
=
s
.
find
(
":"
,
i
)
size
=
int
(
s
[
i
:
index
])
strs
.
append
(
s
[
index
+
1
:
index
+
1
+
size
])
i
=
index
+
1
+
size
return
strs
strs
=
"keon is awesome"
print
(
strs
)
enc
=
encode
(
strs
)
print
(
enc
)
dec
=
decode
(
enc
)
print
(
dec
)
Back
|
FazBrowse Home
|
New Git URL