锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲激情视频在线观看,亚洲成AV人片在,亚洲精品综合久久http://www.tkk7.com/xylz/category/44738.htmlzh-cnThu, 01 Jul 2010 17:05:37 GMTThu, 01 Jul 2010 17:05:37 GMT60Core Python Programming Exercises P09-22http://www.tkk7.com/xylz/articles/324779.htmlxylzxylzTue, 29 Jun 2010 06:56:00 GMThttp://www.tkk7.com/xylz/articles/324779.htmlhttp://www.tkk7.com/xylz/comments/324779.htmlhttp://www.tkk7.com/xylz/articles/324779.html#Feedback0http://www.tkk7.com/xylz/comments/commentRss/324779.htmlhttp://www.tkk7.com/xylz/services/trackbacks/324779.html 9-22.

ZIP Archive Files. The unzip -l command to dump the contents of ZIP archive is boring. Create a Python script called lszip.py that gives additional information such as: the compressed file size, the compressed percentage of each file (by comparing the original and compressed file sizes), and a full time.ctime() timestamp instead of the unzip output (of just the date and HH:MM). Hint: The date_time attribute of an archived file does not contain enough information to feed to time.mktime()... it is up to you!

#!/usr/bin/env python
#
-*- coding:utf-8 -*-
#
$Id: p0922.py 168 2010-06-29 06:34:01Z xylz $

'''
This is a 'python' study plan for xylz.
Copyright (C)2010 xylz (www.imxylz.info)
'''

import zipfile
import os
import datetime


def list (zip_file,files):
    
print "list %s files from %s" % (len(files) if files!='*' else 'total',zip_file)
    
    f_in 
= zipfile.ZipFile(zip_file, 'r')
    infoes 
= None
    
if '*' == files:
        infoes 
= f_in.infolist()
    
else:
        infoes 
= []
        
for f in files:
            infoes.append(f_in.getinfo(f))
    
print "filename:\t\tuncompress\tcompress\tpercent\tcreatetime"
    
print "-----------------------------------------"
    total_usize,total_csize 
= (0,0)
    
for info in infoes:
        (year, month, day, hour, minute, second) 
= info.date_time
        dtime 
= datetime.datetime(year, month, day, hour, minute, second)
        filename,usize,csize 
= info.filename,info.file_size,info.compress_size 
        
print "%s:\t\t%d\t%d\t%d%%\t%s" % (filename,usize,csize,(csize*100/(usize+0.01)),dtime.ctime())
        total_usize 
+= usize
        total_csize 
+= csize
    f_in.close()

    
print "-----------------------------------------"
    
print "compressed size %d bytes, uncompressed size %d bytes, %d%%" % (total_csize,total_usize,(total_csize*100/total_usize))
            
        

if __name__ == '__main__':
    
import sys
    
if len(sys.argv)<2:
        
print "List files in zip file"
        
print "Usage: %s <zipfile> [files]" % (sys.argv[0],)
        sys.exit(0)
    zip_file 
= sys.argv[1]
    files 
= '*'
    
if len(sys.argv)>2:
        files 
= []
        
for f in sys.argv[2:]:
            files.append(f)
    list(zip_file,files)


xylz 2010-06-29 14:56 鍙戣〃璇勮
]]>
Core Python Programming Exercises P09-21http://www.tkk7.com/xylz/articles/324773.htmlxylzxylzTue, 29 Jun 2010 06:05:00 GMThttp://www.tkk7.com/xylz/articles/324773.htmlhttp://www.tkk7.com/xylz/comments/324773.htmlhttp://www.tkk7.com/xylz/articles/324773.html#Feedback0http://www.tkk7.com/xylz/comments/commentRss/324773.htmlhttp://www.tkk7.com/xylz/services/trackbacks/324773.html 9-21.

ZIP Archive Files. Create a program that can extract files from or add files to, and perhaps creating, a ZIP archive file.

#!/usr/bin/env python
#
-*- coding:utf-8 -*-
#
$Id: p0921.py 167 2010-06-29 06:03:11Z xylz $

'''
This is a 'python' study plan for xylz.
Copyright (C)2010 xylz (www.imxylz.info)
'''

import zipfile
import os

def compress (zip_file,files):
    
print "compress %s files to %s" % (len(files),zip_file)
    f_mode 
= 'a' if os.path.exist(zip_file) else 'w'
    f_out 
= zipfile.ZipFile(zip_file, f_mode) 
    in_size 
= 0
    
for f  in files:
        f_out.write(f)
        in_size 
+= os.path.getsize(f)
    f_out.close()

    out_size 
= os.path.getsize(zip_file)
    
print "source size %d bytes, target size %d bytes, %d%%" % (in_size,out_size,(out_size*100/in_size))
        
def decompress (zip_file,dstdir,files='*'):
    
print "decompress %s  to %s" % (zip_file,dstdir)
    f_in 
= zipfile.ZipFile(zip_file,'r')
    
if files == '*':
        f_in.extractall(dstdir)
    
else:
        
for f in files:
            f_in.extract(f,dstdir)
            
print "extract file",f
    f_in.close()

        
        

if __name__ == '__main__':
    
import sys
    
if len(sys.argv)<4 or ('c'!= sys.argv[1and 'x'!= sys.argv[1and 'a'!= sys.argv[1]):
        
print "Usage: %s c <zipfile> <srcfile>.." % (sys.argv[0],)
        
print "Usage: %s a <zipfile> <srcfile>.." % (sys.argv[0],)
        
print "Usage: %s x <zipfile> <destdir> [srcfile].." % (sys.argv[0],)
        sys.exit(0)
    zip_file 
= sys.argv[2]
    
    
if 'c' == sys.argv[1or 'a' == sys.argv[1]:
        src_files 
= []
        
for f in sys.argv[3:]:
            src_files.append(f)
        compress(zip_file,src_files)
    
    
else:
        dst_dir 
= sys.argv[3]
        files 
= '*'
        
if len(sys.argv)>4:
            files 
= []
            
for f in sys.argv[4:]:
                files.append(f)
        decompress (zip_file,dst_dir,files)
        
        
    


xylz 2010-06-29 14:05 鍙戣〃璇勮
]]>
Core Python Programming Exercises P09-20http://www.tkk7.com/xylz/articles/324759.htmlxylzxylzTue, 29 Jun 2010 03:51:00 GMThttp://www.tkk7.com/xylz/articles/324759.htmlhttp://www.tkk7.com/xylz/comments/324759.htmlhttp://www.tkk7.com/xylz/articles/324759.html#Feedback0http://www.tkk7.com/xylz/comments/commentRss/324759.htmlhttp://www.tkk7.com/xylz/services/trackbacks/324759.html 9-20.

Compressed Files. Write a short piece of code that will compress and decompress gzipped or bzipped files. Confirm your solution works by using the command-line gzip or bzip2 programs or a GUI program like PowerArchiver, StuffIt, and/or WinZip.

 1#!/usr/bin/env python
 2#-*- coding:utf-8 -*-
 3#$Id: p0920.py 166 2010-06-29 03:46:56Z xylz $
 4
 5'''
 6This is a 'python' study plan for xylz.
 7Copyright (C)2010 xylz (www.imxylz.info)
 8'''
 9
10import gzip
11import os
12
13def compress (dst,f):
14    print "compress %s to %s" % (f,dst)
15    f_out = gzip.open(dst,'wb')
16    f_in = open(f,'rb')
17    f_out.writelines(f_in)
18    f_in.close()
19    f_out.close()
20    in_size = os.path.getsize(f)
21    out_size = os.path.getsize(dst)
22    print "source size %d bytes, target size %d bytes, %d%%" % (in_size,out_size,(out_size*100/in_size))
23        
24def decompress (f,dst):
25    print "decompress %s to %s" % (f,dst)
26    f_in = gzip.open(f,'rb')
27    f_out = open(dst,'wb')
28    f_out.writelines(f_in)
29    f_in.close()
30    f_out.close()
31    in_size = os.path.getsize(f)
32    out_size = os.path.getsize(dst)
33    print "source size %d bytes, target size %d bytes, %d%%" % (in_size,out_size,(in_size*100/out_size))
34    
35
36if __name__ == '__main__':
37    import sys
38    if len(sys.argv)<3 or ('c'!= sys.argv[1and 'x'!= sys.argv[1]):
39        print "Usage: %s <c|x> <file> [destfile]" % (sys.argv[0],)
40        sys.exit(0)
41    src_file = sys.argv[2]
42    dest_file = None
43    if len(sys.argv)>3:
44        dest_file = sys.argv[3]
45    
46
47    if 'c' == sys.argv[1]:
48        if not dest_file:
49            dest_file = src_file+".gz"
50        compress(dest_file,src_file)
51    
52    else:
53        if not dest_file:
54            dest_file = src_file[:src_file.index('.gz')] #raise exception if file error
55        decompress(src_file,dest_file)
56        
57        
58    
59
涓嬮潰鏄竴嬈℃墽琛岀殑杈撳嚭緇撴灉錛?br />
C:\Users\xylz\Desktop\core_python>python p0920.py
Usage: p0920.py <c|x> <file> 
[destfile]

C:\Users\xylz\Desktop\core_python>python p0920.py c p0920.py
compress p0920.py to p0920.py.gz
source size 
1587 bytes, target size 614 bytes, 38%

C:\Users\xylz\Desktop\core_python>python p0920.py x p0920.py.gz p0920.py
.2
decompress p0920.py.gz to p0920.py
.2
source size 
614 bytes, target size 1587 bytes, 38%

C:\Users\xylz\Desktop\core_python>md5sum p0920.py p0920.py
.2
3383e1a05fbede400dd016feed8a55bf *p0920.py
3383e1a05fbede400dd016feed8a55bf *p0920.py
.2


xylz 2010-06-29 11:51 鍙戣〃璇勮
]]>
Core Python Programming Exercises P08-04/05/06/07/11/12http://www.tkk7.com/xylz/articles/324709.htmlxylzxylzMon, 28 Jun 2010 12:56:00 GMThttp://www.tkk7.com/xylz/articles/324709.htmlhttp://www.tkk7.com/xylz/comments/324709.htmlhttp://www.tkk7.com/xylz/articles/324709.html#Feedback0http://www.tkk7.com/xylz/comments/commentRss/324709.htmlhttp://www.tkk7.com/xylz/services/trackbacks/324709.html闃呰鍏ㄦ枃

xylz 2010-06-28 20:56 鍙戣〃璇勮
]]>
Core Python Programming Exercises P07-10http://www.tkk7.com/xylz/articles/324056.htmlxylzxylzMon, 21 Jun 2010 04:25:00 GMThttp://www.tkk7.com/xylz/articles/324056.htmlhttp://www.tkk7.com/xylz/comments/324056.htmlhttp://www.tkk7.com/xylz/articles/324056.html#Feedback0http://www.tkk7.com/xylz/comments/commentRss/324056.htmlhttp://www.tkk7.com/xylz/services/trackbacks/324056.html7-10. Encryption. Using your solution to the previous problem, and create a "rot13" translator. "rot13" is an old and fairly simplistic encryption routine whereby each letter of the alphabet is rotated 13 characters. Letters in the first half of the alphabet will be rotated to the equivalent letter in the second half and vice versa, retaining case. For example, a goes to n and X goes to K. Obviously, numbers and symbols are immune from translation.

(b) Add an application on top of your solution to prompt the user for strings to encrypt (and decrypt on reapplication of the algorithm), as in the following examples:

    % rot13.py
    Enter string to rot13: This is a short sentence.
    Your string to en/decrypt was: [This is a short
    sentence.].
    The rot13 string is: [Guvf vf n fubeg fragrapr.].
    %
    % rot13.py
    Enter string to rot13: Guvf vf n fubeg fragrapr.
    Your string to en/decrypt was: [Guvf vf n fubeg
    fragrapr.].
    The rot13 string is: [This is a short sentence.].
 

 1#!/usr/bin/env python
 2#-*- coding:utf-8 -*-
 3#$Id: p0710.py 153 2010-06-21 04:19:15Z xylz $
 4
 5'''
 6This is a 'python' study plan for xylz.
 7Copyright (C)2010 xylz (www.imxylz.info)
 8'''
 9
10endic = None
11if not endic:
12    endic = {}
13    import string
14    for cc in (string.lowercase,string.uppercase):
15        for i,c in enumerate(cc):
16            if i<13: endic[c]=cc[i+13]
17            else: endic[c]=cc[i-13]
18
19def encrypt_decrypt(s):
20    ret=[]
21    for c in s:
22        ret.append(endic.get(c,c))
23    return "".join(ret)
24
25if __name__ == '__main__':
26    while True:
27        my_input = raw_input('Enter string to rot13: ')
28        if not my_input: break
29        print "Your string to en/decrypt was: [",encrypt_decrypt(my_input),"]."
30
鐢變簬鏄縐扮殑錛屾墍浠ュ湪14,15琛屼腑鍙渶瑕侀亶鍘嗕竴嬈℃墍鏈夊ぇ鍐欏瓧姣嶅氨鍙互鎷垮埌鎵鏈夊搴斿叧緋諱簡錛屽寘鎷姞瀵嗐佽В瀵嗐傚彟澶栧湪22琛岄噷闈㈢敤鍒頒簡dict鐨刧et鏂規(guī)硶錛岃繖鏍峰湪闈炲瓧姣嶇鍙峰氨鍙互淇濇寔鍘熸牱浜嗐?img src ="http://www.tkk7.com/xylz/aggbug/324056.html" width = "1" height = "1" />

xylz 2010-06-21 12:25 鍙戣〃璇勮
]]>
Core Python Programming Exercises P06-13/14/15/16http://www.tkk7.com/xylz/articles/322771.htmlxylzxylzFri, 04 Jun 2010 09:03:00 GMThttp://www.tkk7.com/xylz/articles/322771.htmlhttp://www.tkk7.com/xylz/comments/322771.htmlhttp://www.tkk7.com/xylz/articles/322771.html#Feedback0http://www.tkk7.com/xylz/comments/commentRss/322771.htmlhttp://www.tkk7.com/xylz/services/trackbacks/322771.html闃呰鍏ㄦ枃

xylz 2010-06-04 17:03 鍙戣〃璇勮
]]>
Core Python Programming Exercises P06-12http://www.tkk7.com/xylz/articles/322006.htmlxylzxylzThu, 27 May 2010 04:13:00 GMThttp://www.tkk7.com/xylz/articles/322006.htmlhttp://www.tkk7.com/xylz/comments/322006.htmlhttp://www.tkk7.com/xylz/articles/322006.html#Feedback0http://www.tkk7.com/xylz/comments/commentRss/322006.htmlhttp://www.tkk7.com/xylz/services/trackbacks/322006.html6-12. Strings.

(1)Create a function called findchr(), with the following declaration:

def findchr(string, char)
findchr() will look for character char in string and return the index of the first occurrence of char, or -1 if that char is not part of string. You cannot use string.*find() or string.*index() functions or methods.

(2)Create another function called rfindchr() that will find the last occurrence of a character in a string. Naturally this works similarly to findchr(), but it starts its search from the end of the input string.

(3)Create a third function called subchr() with the following declaration:

def subchr(string, origchar, newchar)
subchr() is similar to findchr() except that whenever origchar is found, it is replaced by newchar. The modified string is the return value.
 

 1#!/usr/bin/env python
 2#-*- coding:utf-8 -*-
 3#$Id: p0612.py 140 2010-05-27 04:10:06Z xylz $
 4
 5'''
 6This is a 'python' study plan for xylz.
 7Copyright (C)2010 xylz (www.imxylz.info)
 8'''
 9
10
11def findchr(s,ch):
12    """
13    Look for character 'ch' in 's' and return the index of the first occurrence of 'ch', or -f if that 'ch' is not part of 's'
14    """
15    if s is None or len(s)==0: return -1
16    for i,c in enumerate(s):
17        if c == ch: return i
18    return -1
19
20def rfindchr(s,ch):
21    """
22    Look for character 'ch' in 's' and return the index of the last occurrence of 'ch', or -f if that 'ch' is not part of 's'
23    """
24    if s is None or len(s)==0: return -1
25    for i in range(len(s)-1,-1,-1):
26        if s[i] == ch: return i
27    return -1
28
29def subchr(s,oldch,newch):
30    """
31    Look for character 'oldch' in 'newch' and replace each 'oldch' with 'newch' and return the string modified.
32    """
33    if s is None or len(s)==0: return s
34    ret=[]
35    for c in s:
36        ret.append(c if c!=oldch else newch)
37    return ''.join(ret)
38
39
40if __name__ == '__main__':
41    assert 1 == findchr('Good','o')
42    try:
43        assert 0 == findchr('Good','x')
44        raise ValueError, 'Test fail.'
45    except AssertionError as e:
46        print e
47    assert 2 == rfindchr('Good','o')
48    assert 'Gxxd' == subchr('Good','o','x')
49
50
鍦ㄦ綾葷殑嫻嬭瘯紼嬪簭涓紝浣跨敤assert鏂█鏉ユ祴璇曟紜э紝濡傛灉嫻嬭瘯澶辮觸浼?xì)鎶涘囖Z竴涓狝ssertionError鐨勫紓甯搞?br />

xylz 2010-05-27 12:13 鍙戣〃璇勮
]]>
Core Python Programming Exercises P06-11http://www.tkk7.com/xylz/articles/321998.htmlxylzxylzThu, 27 May 2010 03:42:00 GMThttp://www.tkk7.com/xylz/articles/321998.htmlhttp://www.tkk7.com/xylz/comments/321998.htmlhttp://www.tkk7.com/xylz/articles/321998.html#Feedback0http://www.tkk7.com/xylz/comments/commentRss/321998.htmlhttp://www.tkk7.com/xylz/services/trackbacks/321998.html

6-11. Conversion.

Create a program that will convert from an integer to an Internet Protocol (IP) address in the four-octet format of WWW.XXX.YYY.ZZZ.

Update your program to be able to do the vice versa of the above.
 

 1#!/usr/bin/env python
 2#-*- coding:utf-8 -*-
 3#$Id: p0611.py 139 2010-05-21 09:45:30Z xylz $
 4
 5'''
 6This is a 'python' study plan for xylz.
 7Copyright (C)2010 xylz (www.imxylz.info)
 8'''
 9
10def convertIp2Str(ip):
11    return '.'.join( ( str((ip>>i) &0xFFfor i in (24,16,8,0)) )
12
13def convertStr2Ip(s):
14    r=0
15    for i,v in enumerate(s.split('.')):
16        r |= ( int(v) << (24-i*8))
17    return r
18
19
20if __name__ == '__main__':
21    '''
22    Convert ip from Integer number to string and do it versa.
23    '''
24    sip = '192.168.1.1'
25    ip = convertStr2Ip(sip)
26    sip2 = convertIp2Str(ip)
27    print sip,ip,sip2
寰堟樉鐒惰繖閲屾病鏈夊IP鏈夋晥鎬ц繘琛屾牎楠岋紝榪欓噷鍋囪IP鍦板潃閮芥槸鏈夋晥鐨勩?br /> 鍦?1琛岋紝棣栧厛鏋勯犱竴涓?涓暟鐨勮凱浠e櫒錛屽浜庤凱浠e櫒閲岄潰鐨勬瘡涓欏癸紝灝唅p鏁存暟寰鍙崇Щ涓涓瓧鑺傦紝鐒跺悗涓?xFF錛岃繖鏍峰氨寰楀埌浜嗘瘡涓欏圭殑鍊箋傜劧鍚庡悓string.join(s)灝嗕竴涓凱浠e櫒鎴栬呭垪琛ㄨ繛鎺ヨ搗鏉ワ紝鏋勬垚涓涓?xxx.xxx.xxx.xxx"鏍煎紡鐨勫瓧絎︿覆銆?img src ="http://www.tkk7.com/xylz/aggbug/321998.html" width = "1" height = "1" />

xylz 2010-05-27 11:42 鍙戣〃璇勮
]]>
Core Python Programming Exercises P06-10http://www.tkk7.com/xylz/articles/321558.htmlxylzxylzFri, 21 May 2010 09:14:00 GMThttp://www.tkk7.com/xylz/articles/321558.htmlhttp://www.tkk7.com/xylz/comments/321558.htmlhttp://www.tkk7.com/xylz/articles/321558.html#Feedback0http://www.tkk7.com/xylz/comments/commentRss/321558.htmlhttp://www.tkk7.com/xylz/services/trackbacks/321558.html6-10.

Strings. Create a function that will return another string similar to the input string, but with its case inverted. For example, input of "Mr. Ed" will result in "mR. eD" as the output string.

 

 1#!/usr/bin/env python
 2#-*- coding:utf-8 -*-
 3#$Id: p0610.py 138 2010-05-21 09:10:35Z xylz $
 4
 5'''
 6This is a 'python' study plan for xylz.
 7Copyright (C)2010 xylz (www.imxylz.info)
 8'''
 9
10import string
11
12_letters = string.ascii_letters
13_map = dict(zip(_letters,_letters[26:52]+_letters[0:26]))
14
15def caseInverted(s):
16    if s is None or len(s) ==0: return s
17    r=[]
18    for c in s:
19        r.append(_map.get(c,c))
20    return ''.join(r)
21
22if __name__ == '__main__':
23    '''
24    Create a function that will return another string similar to the input string, but with its case inverted. For example, input of "Mr. Ed" will result in "mR. eD" as the output string.
25    '''
26    print caseInverted('Mr.Liu')
27
絎?2琛岄鍏堜粠string妯″潡閲岄潰鍔犺澆鎵鏈夊瓧姣嶇殑瀛楃涓詫紝榪欎釜闇瑕佸鍏tring妯″潡銆?br /> 鏈閲嶈鐨勬槸絎?3琛岋紝閫氳繃涓や釜瀛楃涓詫紙a-Z瀵瑰簲A-Z+a-z錛夋潵鏋勯犱竴涓猟ic錛岃繖閲岀敤鍒頒簡zip鍐呯疆鍑芥暟錛屽悓鏃墮氳繃dict鍖呰涓嬶紝榪欐牱灝辨垚浜嗕竴涓猟ict銆?br /> 鑰屽湪19琛岄噷闈㈤渶瑕佹敞鎰忕殑鏄紝瀵逛簬閭d簺涓嶅啀dict閲岄潰鐨勫瓧絎﹂渶瑕佸師鏍瘋繑鍥烇紝鎵浠ヨ繖閲屼嬌鐢ㄤ簡get錛屽鏋滅洿鎺ヤ嬌鐢ㄤ笅琛ㄦ搷浣淸]錛屼細(xì)瑙﹀彂涓涓紓甯搞?br /> 浣跨敤dict鐨勫彟涓涓ソ澶勫氨鏄熷害鍙兘浼?xì)蹇偣锛寴q欎釜娌℃湁嫻嬭瘯錛屾悶涓嶅ソ鐩存帴閬嶅巻瀛楃涓叉壘鍒板搴斿叧緋誨彲鑳芥洿蹇?br />

xylz 2010-05-21 17:14 鍙戣〃璇勮
]]>
Core Python Programming Exercises Chapter 6http://www.tkk7.com/xylz/articles/319035.htmlxylzxylzWed, 21 Apr 2010 14:25:00 GMThttp://www.tkk7.com/xylz/articles/319035.htmlhttp://www.tkk7.com/xylz/comments/319035.htmlhttp://www.tkk7.com/xylz/articles/319035.html#Feedback0http://www.tkk7.com/xylz/comments/commentRss/319035.htmlhttp://www.tkk7.com/xylz/services/trackbacks/319035.html
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


xylz 2010-04-21 22:25 鍙戣〃璇勮
]]>
Core Python Programming Exercises Chapter 5http://www.tkk7.com/xylz/articles/318818.htmlxylzxylzTue, 20 Apr 2010 02:31:00 GMThttp://www.tkk7.com/xylz/articles/318818.htmlhttp://www.tkk7.com/xylz/comments/318818.htmlhttp://www.tkk7.com/xylz/articles/318818.html#Feedback0http://www.tkk7.com/xylz/comments/commentRss/318818.htmlhttp://www.tkk7.com/xylz/services/trackbacks/318818.html闃呰鍏ㄦ枃

xylz 2010-04-20 10:31 鍙戣〃璇勮
]]>
Core Python Programming Exercises Chapter 2http://www.tkk7.com/xylz/articles/318782.htmlxylzxylzMon, 19 Apr 2010 14:46:00 GMThttp://www.tkk7.com/xylz/articles/318782.htmlhttp://www.tkk7.com/xylz/comments/318782.htmlhttp://www.tkk7.com/xylz/articles/318782.html#Feedback0http://www.tkk7.com/xylz/comments/commentRss/318782.htmlhttp://www.tkk7.com/xylz/services/trackbacks/318782.html闃呰鍏ㄦ枃

xylz 2010-04-19 22:46 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 免费国产va视频永久在线观看| 亚洲国产精品乱码一区二区| 日本不卡视频免费| 女人被男人躁的女爽免费视频| 日韩在线看片免费人成视频播放| 国产成人涩涩涩视频在线观看免费 | 亚洲人成自拍网站在线观看| 亚洲精品日韩一区二区小说| 立即播放免费毛片一级| 免费视频成人手机在线观看网址| 亚欧免费无码aⅴ在线观看| jjizz全部免费看片| 国产无遮挡吃胸膜奶免费看视频 | 亚洲AV伊人久久青青草原| 日本亚洲国产一区二区三区| 亚洲色偷偷av男人的天堂| 亚洲成a∨人片在无码2023| 一级做a爰性色毛片免费| 污污网站18禁在线永久免费观看| 免费视频专区一国产盗摄| 亚洲色欲久久久久综合网| 亚洲精品白色在线发布| 噜噜噜亚洲色成人网站| 99re6在线视频精品免费下载| 国产一区二区三区免费看| 亚洲日本一区二区三区| 免费夜色污私人影院网站电影 | 成人免费福利视频| 亚洲精品美女久久久久99| 亚洲国产成人久久精品软件| 99久久国产精品免费一区二区| 午夜神器成在线人成在线人免费 | 国产yw855.c免费视频| 亚洲黄色免费网站| aa级女人大片喷水视频免费| 国产精品另类激情久久久免费 | 99在线观看免费视频| 在线观看亚洲精品国产| 黄色毛片免费网站| 好爽…又高潮了免费毛片| 亚洲性一级理论片在线观看|