zoukankan      html  css  js  c++  java
  • 《Python核心编程》第二版第160页第六章练习

    和大家分享自己完成的《Python核心编程》习题答案。
    因为不是来自官方资源,是自己的的练习,可能有误或者并非最好的解决办法。

    【推荐】推荐博文:牛B的Python模块
    http://www.cnblogs.com/chu888chu888/archive/2011/01/09/1931084.html

    【推荐】推荐博文:Python核心编程练习第六章,别人做的练习
    http://blog.csdn.net/killua_hzl/archive/2010/05/31/5637828.aspx

    【推荐】推荐博文:全排列算法之字典序法
    http://blog.sina.com.cn/s/blog_4c471b9601000czy.html

    【推荐】博客:异そ度︶空ツ間,关于Python的学习
    http://blog.csdn.net/killua_hzl/category/681280.aspx

    6-1.
    字符串。string模块中是否有一种字符串方法或者函数可以帮我鉴定下一个字符串是否是另一个大字符串的一部分?
    【答案】
    成员操作符用于判断一个字符或者一个字串(中的字符)是否出现在另一个字符串中。出现则返回True,否者返回False。注意,成员操作符不是用来判断一个字符串是否包含另一个字符串的,这样的功能由find(),index(),rfind()和rindex()函数来完成。
    >>> 'bc' in 'abcd'
    True
    >>> 'n' in 'abcd'
    False

    6-2.
    字符串标识符。修改例6-1的idcheck.py脚本,使之可以检测长度为一的标识符,并且可以识别Python关键字。对后一个要求,你可以使用keyword模块(特别是keyword.kwlist)来辅助。
    【评】
    我怀疑题目的原意是修改脚本后使之可以检测长度大于等于一的标识符。
    【参考】
    idcheck.py脚本
    import string

    alphas = string.letters + '_'
    nums = string.digits

    print 'Welcome to the Identifier Checker v1.0'
    print 'Testees must be at least 2 chars long.'

    myInput = raw_input('Identifier to test?')

    if len(myInput) > 1:
       
        if myInput[0] not in alphas:
            print '''invalid: first symbol must be alphabetic'''
       
        else:
            for otherChar in myInput[1:]:
               
                if otherChar not in alphas + nums:
                    print '''invalid: first symbol must be alphabetic'''
                    break
                else:
                    print 'okay as an identifier'

    【参考】关于keyword.kwlist

    >>> import keyword

    >>> dir(keyword)
    ['__all__', '__builtins__', '__doc__', '__file__', '__name__', 'iskeyword', 'kwlist', 'main']

    >>> keyword.kwlist
    ['and', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'yield']
    >>>
    【答案】
    代码如下:
    import string
    import keyword

    alphas = string.letters + '_'
    nums = string.digits

    print 'Welcome to the Identifier Checker v2.0'
    print 'Testees must be at least 1 chars long.'

    myInput = raw_input('Identifier to test?   ')

    isKeyword = False
    isIdentifier = False

    if len(myInput) >= 1:
       
        if myInput[0] not in alphas:
            print '''invalid: first symbol must be alphabetic'''
            isIdentifier = False
        else:
            for otherChar in myInput[1:]:
               
                if otherChar not in alphas + nums:
                    print '''invalid: remaining symbols must be alphabetic'''
                    isIdentifier = False
                    break
                else:
                    isIdentifier = True               

    if myInput in keyword.kwlist:
         print '"%s" is a keyword of Python' % myInput
         isKeyword = True
    if isIdentifier and (not isKeyword): print 'okay as an identifier'

    6-3.
    排序。
    (a)输入一串数字、并从大到小排列之。
    (b)跟a一样。不过要用字典序从大到小排列。
    【答案】
    (a)代码如下:
    aList = raw_input('Please input numbers, separated by space ... ')
    bList = []
    for i in aList.split(' '):
        bList.append(int(i))
    bList.sort()
    print bList[::-1]
    (b)代码如下:
    aList = raw_input('Please input numbers, separated by space ... ')
    bList = []
    for i in aList.split(' '):
        bList.append(i)
    bList.sort()
    print bList[::-1]
    【评】
    我查了字典序的具体意思,好像和题目的意思并不相符。

    关键词:Pyhon核心编程习题代码 非官方 博客园

  • 相关阅读:
    ubuntu 14.04 firefox install flash-plugin
    ubuntu node.js Binaries方式安装(二进制文件安装)
    ubuntu14.04 截图
    ubuntu 14.04下,thinkpad触摸板关闭方法
    ubuntu Mozilla Firefox install flash plugin 火狐浏览器安装flash插件
    win7+ubuntu 14.04双系统 硬盘安装
    后台启动VirtualBox虚拟机
    excel vlookup函数使用方法
    图片添加文字水印和图片水印
    记录使用Stream转多层map数据结构及遇到的坑
  • 原文地址:https://www.cnblogs.com/balian/p/1949107.html
Copyright © 2011-2022 走看看