zoukankan      html  css  js  c++  java
  • python 核心编程 第七章习题

    7-9. 翻译
    (a) 编写一个字符翻译程序(功能类似于Unix 中的tr 命令)。我们将这个函数叫做tr(),它有
    三个字符串做参数: 源字符串、目的字符串、基本字符串,语法定义如下:
    def tr(srcstr, dststr, string)
    srcstr 的内容是你打算“翻译”的字符集合,dsrstr 是翻译后得到的字符集合,而string 是
    你打算进行翻译操作的字符串。举例来说,如果srcstr == 'abc', dststr == 'mno', string ==
    'abcdef', 那么tr()的输出将是'mnodef'. 注意这里len(srcstr) == len(dststr).
    在这个练习里,你可以使用内建函数chr() 和 ord(), 但它们并不一定是解决这个问题所必不
    可少的函数。

    def test79(strlist,srcstr = 'abc',dststr = 'mno'):    
        sl = [] 
    d = dict(zip(srcstr,dststr))
    l = len(srcstr)
    while not (strlist.find(srcstr) == -1):
    #find place then replace it
    index = strlist.index(srcstr)
    sl = list(strlist)
    for i in range(l):
    sl[index + i] = d[strlist[index + i]]
    strlist = ''.join(sl)
    print strlist

    if __name__ == "__main__":
        test79("abccncabcdfdfjsnc")

    #output: mnocncmnodfdfjsnc
  • 相关阅读:
    一文搞懂字符集
    机器视觉之eVision
    PID调节
    激光切割质量主要影响因素
    155. 最小栈
    111.二叉树最小深度
    110. 平衡二叉树
    108.将有序数组转换为二叉搜索树
    107. 二叉树的层次遍历 II
    104. 二叉树的最大深度
  • 原文地址:https://www.cnblogs.com/muyiblog/p/6970780.html
Copyright © 2011-2022 走看看