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
c6043871
Commit
c6043871
authored
Jan 11, 2019
by
yangpengflag
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update homework
parent
2de403c6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
22 deletions
+29
-22
inter_morse_code.py
+29
-22
No files found.
inter_morse_code.py
View file @
c6043871
def
uniqueMorseRepresentations
(
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
=
[]
for
i
in
words
:
s
=
""
for
j
in
i
:
s
+=
morsemap
.
get
(
j
)
#print(s)
morselist
.
append
(
s
)
print
(
morselist
)
print
(
len
(
set
(
morselist
)))
uniqueMorseRepresentations
(
self
=
uniqueMorseRepresentations
,
words
=
[
"gin"
,
"zen"
,
"gig"
,
"msg"
])
# 方法一,笨办法,手动组合字典
def
uniqueMorseRepresentations1
(
self
,
words
):
def
uniqueMorseRepresentations1
(
self
,
words
):
ref
=
{
'a'
:
".-"
,
ref
=
{
'a'
:
".-"
,
"b"
:
"-..."
,
"b"
:
"-..."
,
...
@@ -49,17 +34,39 @@ def uniqueMorseRepresentations1(self, words):
...
@@ -49,17 +34,39 @@ def uniqueMorseRepresentations1(self, words):
for
i
in
word
:
for
i
in
word
:
s
+=
ref
.
get
(
i
)
s
+=
ref
.
get
(
i
)
res
.
append
(
s
)
res
.
append
(
s
)
#print(len(set(res)))
print
(
len
(
set
(
res
)))
uniqueMorseRepresentations1
(
self
=
uniqueMorseRepresentations1
,
words
=
[
"gin"
,
"zen"
,
"gig"
,
"msg"
])
uniqueMorseRepresentations
(
self
=
uniqueMorseRepresentations1
,
words
=
[
"gin"
,
"zen"
,
"gig"
,
"msg"
])
#
Approach #2 用ord()函数求出a的ASCII值, chr() 是将ASCII值变为字符. ord('a') = 97 , ord("A") = 65
#
利用字典的zip函数拼接字母和摩尔斯密码为字典
def
uniqueMorseRepresentations2
(
self
,
words
):
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
=
[]
for
i
in
words
:
s
=
""
for
j
in
i
:
s
+=
morsemap
.
get
(
j
)
#print(s)
morselist
.
append
(
s
)
print
(
morselist
)
print
(
len
(
set
(
morselist
)))
uniqueMorseRepresentations2
(
self
=
uniqueMorseRepresentations2
,
words
=
[
"gin"
,
"zen"
,
"gig"
,
"msg"
])
# 方法三用ord()函数求出a的ASCII值, chr() 是将ASCII值变为字符. ord('a') = 97 , ord("A") = 65
def
uniqueMorseRepresentations3
(
self
,
words
):
morsecode
=
[
".-"
,
"-..."
,
"-.-."
,
"-.."
,
"."
,
"..-."
,
"--."
,
"...."
,
".."
,
".---"
,
"-.-"
,
".-.."
,
"--"
,
"-."
,
morsecode
=
[
".-"
,
"-..."
,
"-.-."
,
"-.."
,
"."
,
"..-."
,
"--."
,
"...."
,
".."
,
".---"
,
"-.-"
,
".-.."
,
"--"
,
"-."
,
"---"
,
".--."
,
"--.-"
,
".-."
,
"..."
,
"-"
,
"..-"
,
"...-"
,
".--"
,
"-..-"
,
"-.--"
,
"--.."
]
"---"
,
".--."
,
"--.-"
,
".-."
,
"..."
,
"-"
,
"..-"
,
"...-"
,
".--"
,
"-..-"
,
"-.--"
,
"--.."
]
lenth
=
len
(
set
(
""
.
join
(
morsecode
[
ord
(
s
)
-
97
]
for
s
in
word
)
for
word
in
words
))
lenth
=
len
(
set
(
""
.
join
(
morsecode
[
ord
(
s
)
-
97
]
for
s
in
word
)
for
word
in
words
))
#
print(lenth)
print
(
lenth
)
uniqueMorseRepresentations2
(
self
=
uniqueMorseRepresentations2
,
words
=
[
"gin"
,
"zen"
,
"gig"
,
"msg"
])
uniqueMorseRepresentations3
(
self
=
uniqueMorseRepresentations3
,
words
=
[
"gin"
,
"zen"
,
"gig"
,
"msg"
])
\ No newline at end of file
\ No newline at end of file
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