Commit c6043871 by yangpengflag

update homework

parent 2de403c6
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment