Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
3
3_homework_yangpeng
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
杨鹏
3_homework_yangpeng
Commits
93ba45cf
Commit
93ba45cf
authored
Jan 12, 2019
by
牛家玺
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
批改
parent
c6043871
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
37 deletions
+38
-37
inter_morse_code.py
+38
-37
No files found.
inter_morse_code.py
View file @
93ba45cf
# 方法一,笨办法,手动组合字典
def
uniqueMorseRepresentations1
(
self
,
words
):
ref
=
{
'a'
:
".-"
,
"b"
:
"-..."
,
"c"
:
"-.-."
,
"d"
:
"-.."
,
"e"
:
"."
,
"f"
:
"..-."
,
"g"
:
"--."
,
"h"
:
"...."
,
"i"
:
".."
,
"j"
:
".---"
,
"k"
:
"-.-"
,
"l"
:
".-.."
,
"m"
:
"--"
,
"n"
:
"-."
,
"o"
:
"---"
,
"p"
:
".--."
,
"q"
:
"--.-"
,
"r"
:
".-."
,
"s"
:
"..."
,
"t"
:
"-"
,
"u"
:
"..-"
,
"v"
:
"...-"
,
"w"
:
".--"
,
"x"
:
"-..-"
,
"y"
:
"-.--"
,
"z"
:
"--.."
}
ref
=
{
'a'
:
".-"
,
"b"
:
"-..."
,
"c"
:
"-.-."
,
"d"
:
"-.."
,
"e"
:
"."
,
"f"
:
"..-."
,
"g"
:
"--."
,
"h"
:
"...."
,
"i"
:
".."
,
"j"
:
".---"
,
"k"
:
"-.-"
,
"l"
:
".-.."
,
"m"
:
"--"
,
"n"
:
"-."
,
"o"
:
"---"
,
"p"
:
".--."
,
"q"
:
"--.-"
,
"r"
:
".-."
,
"s"
:
"..."
,
"t"
:
"-"
,
"u"
:
"..-"
,
"v"
:
"...-"
,
"w"
:
".--"
,
"x"
:
"-..-"
,
"y"
:
"-.--"
,
"z"
:
"--.."
}
res
=
[]
for
word
in
words
:
s
=
''
...
...
@@ -36,21 +34,23 @@ def uniqueMorseRepresentations1(self, words):
res
.
append
(
s
)
print
(
len
(
set
(
res
)))
uniqueMorseRepresentations1
(
self
=
uniqueMorseRepresentations1
,
words
=
[
"gin"
,
"zen"
,
"gig"
,
"msg"
])
uniqueMorseRepresentations1
(
self
=
uniqueMorseRepresentations1
,
words
=
[
"gin"
,
"zen"
,
"gig"
,
"msg"
])
# 利用字典的zip函数拼接字母和摩尔斯密码为字典
def
uniqueMorseRepresentations2
(
self
,
words
):
morsecode
=
[
".-"
,
"-..."
,
"-.-."
,
"-.."
,
"."
,
"..-."
,
"--."
,
"...."
,
".."
,
".---"
,
"-.-"
,
".-.."
,
"--"
,
"-."
,
"---"
,
".--."
,
"--.-"
,
".-."
,
"..."
,
"-"
,
"..-"
,
"...-"
,
".--"
,
"-..-"
,
"-.--"
,
"--.."
]
charlist
=
[
"a"
,
"b"
,
"c"
,
"d"
,
"e"
,
"f"
,
"g"
,
"h"
,
"i"
,
"j"
,
"k"
,
"l"
,
"m"
,
"n"
,
"o"
,
"p"
,
"q"
,
"r"
,
"s"
,
"t"
,
"u"
,
"v"
,
"w"
,
"x"
,
"y"
,
"z"
]
morsemap
=
dict
(
zip
(
charlist
,
morsecode
))
morselist
=
[]
morsecode
=
[
".-"
,
"-..."
,
"-.-."
,
"-.."
,
"."
,
"..-."
,
"--."
,
"...."
,
".."
,
".---"
,
"-.-"
,
".-.."
,
"--"
,
"-."
,
"---"
,
".--."
,
"--.-"
,
".-."
,
"..."
,
"-"
,
"..-"
,
"...-"
,
".--"
,
"-..-"
,
"-.--"
,
"--.."
]
charlist
=
[
"a"
,
"b"
,
"c"
,
"d"
,
"e"
,
"f"
,
"g"
,
"h"
,
"i"
,
"j"
,
"k"
,
"l"
,
"m"
,
"n"
,
"o"
,
"p"
,
"q"
,
"r"
,
"s"
,
"t"
,
"u"
,
"v"
,
"w"
,
"x"
,
"y"
,
"z"
]
morsemap
=
dict
(
zip
(
charlist
,
morsecode
))
morselist
=
[]
for
i
in
words
:
s
=
""
for
j
in
i
:
s
+=
morsemap
.
get
(
j
)
#print(s)
#
print(s)
morselist
.
append
(
s
)
...
...
@@ -58,7 +58,7 @@ def uniqueMorseRepresentations2(self, words):
print
(
len
(
set
(
morselist
)))
uniqueMorseRepresentations2
(
self
=
uniqueMorseRepresentations2
,
words
=
[
"gin"
,
"zen"
,
"gig"
,
"msg"
])
uniqueMorseRepresentations2
(
self
=
uniqueMorseRepresentations2
,
words
=
[
"gin"
,
"zen"
,
"gig"
,
"msg"
])
# 方法三用ord()函数求出a的ASCII值, chr() 是将ASCII值变为字符. ord('a') = 97 , ord("A") = 65
...
...
@@ -69,4 +69,6 @@ def uniqueMorseRepresentations3(self, words):
print
(
lenth
)
uniqueMorseRepresentations3
(
self
=
uniqueMorseRepresentations3
,
words
=
[
"gin"
,
"zen"
,
"gig"
,
"msg"
])
\ No newline at end of file
uniqueMorseRepresentations3
(
self
=
uniqueMorseRepresentations3
,
words
=
[
"gin"
,
"zen"
,
"gig"
,
"msg"
])
# 实现的算法没有问题 ,三种方法可以看出逐步优化,如果是在项目中可以把 morsecode 提取到方法之外保证只初始化一次
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment