• 国际摩尔斯密码


    国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: "a" 对应 ".-", "b" 对应 "-...", "c" 对应 "-.-.", 等等。

    为了方便,所有26个英文字母对应摩尔斯密码表如下:

    [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
    给定一个单词列表,每个单词可以写成每个字母对应摩尔斯密码的组合。例如,"cab" 可以写成 "-.-..--...",(即 "-.-." + "-..." + ".-"字符串的结合)。我们将这样一个连接过程称作单词翻译。

    返回我们可以获得所有词不同单词翻译的数量。

    例如:
    输入: words = ["gin", "zen", "gig", "msg"]
    输出: 2
    解释:
    各单词翻译如下:
    "gin" -> "--...-."
    "zen" -> "--...-."
    "gig" -> "--...--."
    "msg" -> "--...--."

    共有 2 种不同翻译, "--...-." 和 "--...--.".

    注意:

    单词列表words 的长度不会超过 100。
    每个单词 words[i]的长度范围为 [1, 12]。
    每个单词 words[i]只包含小写字母。

    解答:

    def word2moersi(m):
    alpha = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---",
    ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]

    # words 可以这么写morse_dict = {chr(i + 97): morse[i] for i in range(0, len(morse))}
    words = ["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"]
    #建立两者间的映射关系
    newdict = dict(zip(alpha,words))
    temp = set()
    for w in m:
    new_moersi = []
    for i in range(len(w)):
    v = list(newdict.keys())[list(newdict.values()).index(w[i])]
    new_moersi.append(v)
    s = ''.join(new_moersi)
    temp.add(s)
    return temp
    #words = ["ab", "cd", "ef", "msg"]
    words =["gin", "zen", "gig", "msg"]
    t = word2moersi(words)
    print("不同单词翻译的数量为:%d" %(len(t)))
    print("摩尔斯密码为:" + str(t))

    第二种方法:
    第二种方法与第一种基本上一样,思路一样,唯一差别之处就是 第二层循环取值稍有点儿差别,贴出代码,大家自行区别吧。

    ####3 唯一摩尔斯密码词####
    #考察点:1 将字符与对应的字母一一匹配 ,2 字符串的拼接
    #题目解析:输入不同的单词,返回密码的个数
    class Solution:
    def uniqueMorseRepresentations(self, words):

    alpha = ["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"]
    morse = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
    morse_alpha = dict(zip(alpha,morse))
    ###set 表示无重复的集合
    temp = set()
    for i in words:
    temp_morse = []
    for j in i:
    temp_morse.append(morse_alpha[j])
    ##字符串的拼接 方法1
    s = "".join(temp_morse)

    temp.add(s)

    return len(temp)
    s2=Solution()
    words = ["gin", "zen", "gig", "msg"]
    s2.uniqueMorseRepresentations(words)
    ###字符串的拼接 方法2:
    sm=['a','b','c']
    from functools import reduce
    t=reduce(lambda x, y:x+y,sm)
    ###对reduce函数的解释
    #对参数中的序列进行累积
    #用传给 reduce 中的函数 function(有两个参数)先对集合中的第 1、2 个元素进行操作,得到的结果再与第三个数据用 function 函数运算,最后得到一个结果

  • 相关阅读:
    windows下cocos2d-x环境搭建
    QT使用QPainter加水印
    在Visual Studio中使用.lib和.dll的环境搭建
    Android7.0 Phone应用源码分析(四) phone挂断流程分析
    Android7.0 Phone应用源码分析(三) phone拒接流程分析
    Android7.0 拨号盘应用源码分析(一) 界面浅析
    Android7.0 Phone应用源码分析(二) phone来电流程分析
    Android7.0 Phone应用源码分析(一) phone拨号流程分析
    cnblog之初来乍到
    深入浅出Android动态加载jar包技术
  • 原文地址:https://www.cnblogs.com/zbligang/p/10411549.html