<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    xylz,imxylz

    關注后端架構、中間件、分布式和并發編程

       :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      111 隨筆 :: 10 文章 :: 2680 評論 :: 0 Trackbacks

    6-2.

    String Identifiers. Modify the idcheck.py script in Example 6-1 such that it will determine the validity of identifiers of length 1 as well as be able to detect if an identifier is a keyword. For the latter part of the exercise, you may use the keyword module (specifically the keyword.kwlist list) to aid in your cause.

     1#!/usr/bin/env python
     2#-*- coding:utf-8 -*-
     3#$Id: p0602.py 131 2010-04-21 14:20:10Z xylz $
     4
     5'''
     6This is a 'python' study plan for xylz.
     7Copyright (C)2010 xylz (www.imxylz.info)
     8'''
     9
    10import string
    11import keyword
    12
    13'''
    14'''
    15
    16alphas = string.letters + '_'
    17nums = string.digits
    18alphas_nums = alphas+nums
    19kwlist = keyword.kwlist
    20
    21def isPythonId(s):
    22    if not s: return False
    23    if not len(s): return False
    24    if s[0] not in alphas: return False
    25    for c in s: 
    26        if c not in alphas_nums: return False
    27    if s in kwlist: return False
    28    return True
    29
    30if __name__ == '__main__':
    31    '''
    32    String Identifiers. Modify the idcheck.py script in Example 6-1 such that it will determine the validity of identifiers of length 1 as well as be able to detect if an identifier is a keyword. For the latter part of the exercise, you may use the keyword module (specifically the keyword.kwlist list) to aid in your cause.
    33    '''
    34    while True:
    35        myInput = raw_input("Identifier to test? ('exit' for over).\n>>>")
    36        if myInput == 'exit':break
    37        if isPythonId(myInput):
    38            print "'%s' is a valid id" % myInput
    39        else:
    40            print "'%s' is an invalid id" % myInput
    41
    42
    6-3. Sorting.
    Enter a list of numbers and sort the values in largest-to-smallest order.
    Do the same thing, but for strings and in reverse alphabetical (largest-to-smallest lexicographic) order.

     1#!/usr/bin/env python
     2#-*- coding:utf-8 -*-
     3#$Id: p0603.py 132 2010-04-25 10:22:05Z xylz $
     4
     5'''
     6This is a 'python' study plan for xylz.
     7Copyright (C)2010 xylz (www.imxylz.info)
     8'''
     9
    10
    11'''
    12'''
    13
    14
    15if __name__ == '__main__':
    16    '''
    17    '''
    18    nums=[]
    19    while True:
    20        myInput = raw_input("Enter an Integer ('exit' for over).\n>>>")
    21        if myInput == 'exit':break
    22        try:
    23            i=int(myInput)
    24            nums.append(i)
    25        except:
    26            print 'Error Number',
    27    
    28    templist=list(nums) 
    29    nums.sort()
    30    nums.reverse()
    31    print nums
    32    for i in range(0,len(templist),1):
    33        for j in range(i+1,len(templist),1):
    34            if str(templist[i])<str(templist[j]):
    35                templist[i],templist[j]=templist[j],templist[i]
    36    print templist
    37
    38
    6-6.

    Strings. Create the equivalent to string.strip(): Take a string and remove all leading and trailing whitespace. (Use of string.*strip() defeats the purpose of this exercise.)


     1#!/usr/bin/env python
     2#-*- coding:utf-8 -*-
     3#$Id: p0605.py 133 2010-04-25 11:16:34Z xylz $
     4
     5'''
     6This is a 'python' study plan for xylz.
     7Copyright (C)2010 xylz (www.imxylz.info)
     8'''
     9
    10def trim(s):
    11    '''
    12    Take a string and remove all leading and trailing whitespace.
    13    '''
    14    if s is None:return None
    15    if len(s)==0:return ''
    16    ret=[]
    17    for i in range(0,len(s),1):
    18        c=s[i]
    19        if c != ' ' and c != '\t' and c!= '\r' and c!='\n':
    20            s=s[i:]
    21            break
    22        if i==len(s)-1:return ''
    23    for j in range(len(s)-1,-1,-1):
    24        c=s[j]
    25        if c!=' ' and c!='\t' and c!='\r' and c!='\n':
    26            s=s[:j+1]
    27            break
    28        if j==0:return ''
    29    return s
    30
    31
    32if __name__ == '__main__':
    33    '''
    34    Create the equivalent to string.strip(): Take a string and remove all leading and trailing whitespace. (Use of string.*strip() defeats the purpose of this exercise.)
    35    '''
    36    for s in [" a book "," a book","a book",""," \r\n"]:
    37        ns=trim(s)
    38        print "'%s' => '%s', len=%d" % (s,ns,len(ns))
    39


    ©2009-2014 IMXYLZ |求賢若渴
    posted on 2010-04-21 22:25 imxylz 閱讀(16899) 評論(0)  編輯  收藏 所屬分類: Python

    ©2009-2014 IMXYLZ
    主站蜘蛛池模板: 亚洲国产美女福利直播秀一区二区| 亚洲www在线观看| 亚洲成年人免费网站| 亚洲中文字幕乱码AV波多JI| 亚洲成a人片在线网站| 日韩高清免费在线观看| 亚洲精品视频免费 | 亚洲码在线中文在线观看| 成年女人毛片免费播放视频m| 国产99久久久久久免费看| 国产精品亚洲午夜一区二区三区| 亚洲日本韩国在线| 国产2021精品视频免费播放| 日韩在线观看视频免费| 亚洲精品无码久久久久久久| 亚洲国产天堂久久久久久| 国产91色综合久久免费| 国产又黄又爽又大的免费视频| 国产99在线|亚洲| 亚洲成色在线综合网站| 国产大片91精品免费看3| 1a级毛片免费观看| 国产高清对白在线观看免费91| 天天爽亚洲中文字幕| 亚洲AV无码日韩AV无码导航| 国产aa免费视频| 免费AA片少妇人AA片直播| 免费国产99久久久香蕉| 猫咪免费人成网站在线观看入口| 亚洲午夜精品在线| 亚洲日本va中文字幕久久| 四虎AV永久在线精品免费观看| 男女做羞羞的事视频免费观看无遮挡 | 亚洲AV永久无码精品成人| 国产一精品一aⅴ一免费| 免费成人激情视频| 可以免费观看的毛片| 一级成人a做片免费| 亚洲国产精品网站在线播放| 亚洲精品中文字幕乱码影院| 亚洲va久久久噜噜噜久久男同|