<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 閱讀(16898) 評論(0)  編輯  收藏 所屬分類: Python

    ©2009-2014 IMXYLZ
    主站蜘蛛池模板: 国产成人麻豆亚洲综合无码精品| 国产成人综合久久精品免费| 区久久AAA片69亚洲| 日韩色日韩视频亚洲网站| 影音先锋在线免费观看| 亚洲中文字幕一区精品自拍| 成人无码区免费视频观看| 美女扒开屁股让男人桶爽免费 | 亚洲午夜精品一区二区| 久别的草原电视剧免费观看| 亚洲αv在线精品糸列| 午夜免费福利片观看| 亚洲精品视频在线观看免费| 很黄很黄的网站免费的| 33333在线亚洲| 日本免费高清一本视频| 西西人体大胆免费视频| 亚洲人午夜射精精品日韩| 嫩草在线视频www免费观看 | 成人免费看片又大又黄| 亚洲a∨无码一区二区| 亚洲日韩精品无码专区网站| 美女视频黄a视频全免费网站色窝| 亚洲综合一区二区精品导航 | 日本免费中文字幕在线看| 在线观看亚洲电影| 伊人婷婷综合缴情亚洲五月| 日本一区二区免费看| 亚洲a级片在线观看| 亚洲第一区在线观看| 99久久免费国产精精品| 亚洲福利一区二区三区| 国产国产人免费人成免费视频| 亚洲一区二区三区免费| 亚洲精品熟女国产| 国产色爽免费视频| 天黑黑影院在线观看视频高清免费 | 精品国产sm捆绑最大网免费站| 亚洲AV日韩AV无码污污网站| 国产亚洲精品国产| 最近中文字幕免费mv视频7 |