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 阅读(16865) 评论(0)  编辑  收藏 所属分类: Python

只有注册用户登录后才能发表评论。


网站导航:
 

©2009-2014 IMXYLZ