• python 核心编程课后练习(chapter 6)


    6-1

     1 #6-1
     2 
     3 #help(string)
     4 import string
     5 
     6 str = "helloworld"
     7 substr = "h1e"
     8 
     9 if string.find(str, substr) != -1:
    10   print"substr=%s is part of %s" % (substr, str)
    11 else:
    12   print"not substring" 
    View Code

    6-2

     1 #6-2
     2 import string
     3 import keyword
     4 
     5 alphas = string.letters + '_'
     6 nums = string.digits
     7 
     8 print "welcome"
     9 print "testees must be at least 2 chars long."
    10 
    11 ips = raw_input("identifier:")
    12 
    13 
    14 if len(ips)== 1:
    15   if ips[0] not in alphas:
    16     print"single letter invalid"
    17   else:
    18     if not keyword.iskeyword(ips):    
    19       print "valid"
    20     else:
    21       print "invalid is keyword"  
    22     #print "valid"  
    23 elif len(ips) > 1:
    24   if ips[0] not in alphas:
    25     print "first symbol invalid"
    26   else:  
    27     for others in ips[1:]:
    28       if others not in alphas + nums:
    29         print "invalid remaining"
    30         break
    31     else:
    32       if not keyword.iskeyword(ips):    
    33         print "valid"
    34       else:
    35         print "invalid, is keyword"  
    36 else:
    37   print"string should be at least 2 chars"
    View Code

    6-3

     1 #6-3   
     2 
     3 import string
     4 
     5 nums = string.digits
     6 
     7 str=raw_input("Enter the nums:")
     8 #a
     9 a = []
    10 for e in str:
    11   a.append(int(e))
    12 a.sort(reverse = True)
    13 print a  
    14 #b
    15 b = sorted(str, reverse = True)        
    16 print b 
    View Code

    6-4

     1 #6-4
     2 
     3 def average(a):
     4   size = len(a)
     5   sum = 0
     6   for e in a:
     7     sum += int(e)
     8   return float(sum)/float(size) 
     9 
    10 print """
    11 (a)add new
    12 (other)exit
    13 """
    14 a = []
    15 
    16 opt = raw_input("your opt:")
    17 while opt == "a":
    18   a.append(raw_input("Enter the score:"))
    19   opt = raw_input("your opt:")
    20 
    21 print "the average of the scores:%f" % average(a) 
    View Code

    6-5

     1 #6-5(a)
     2 
     3 inputstring = raw_input("Enter the string:")
     4 
     5 sz = len(inputstring)
     6 
     7 if sz == 1:
     8   print inputstring
     9 else:
    10   i = 0
    11   while i<sz:
    12     if (i-1)<0:
    13       print inputstring[i], inputstring[i+1]
    14     elif (i+1)>=sz:
    15       print inputstring[i-1], inputstring[i] 
    16     else:
    17       print inputstring[i-1], inputstring[i], inputstring[i+1]
    18     
    19     i = i+1    
    View Code
     1 #6-5(b)
     2 
     3 def str_cmp_helper(str1, str2):
     4   lens = len(str1)
     5   i = 0
     6   while i<lens:
     7     if str1[i]>str2[i]:
     8       return 1
     9     elif str1[i]<str2[i]:
    10       return -1
    11     i = i+1      
    12   return 0
    13 
    14 def str_cmp(str1, str2):
    15   if len(str1)>len(str2):
    16     return 1
    17   elif len(str1)<len(str2):
    18     return -1
    19   else:
    20     return str_cmp_helper(str1, str2)   
    21     
    22 str1 = raw_input("enter the str1:")
    23 str2 = raw_input("enter the str2:")
    24 
    25 result = str_cmp(str1, str2)
    26 if result == 1:
    27   print "str1:%s > str2:%s" % (str1, str2)
    28 elif result == 0:
    29   print "str1:%s is the same to str2:%s" % (str1, str2)   
    30 else:
    31   print "str1:%s < str2:%s" % (str1, str2)
    View Code
     1 #6-5(c)
     2 
     3 def is_palin(str):
     4   lens = len(str)
     5   
     6   if lens == 1:
     7     return True
     8   else:
     9     i = 0
    10     while i<lens/2:
    11       if str[i] <> str[lens -1 - i]:
    12         return False
    13       else:
    14        i= i+1   
    15   return True  
    16   
    17 inputstring = raw_input("enter the string:")
    18 
    19 if is_palin(inputstring):
    20   print "%s is palin"% inputstring
    21 else:
    22   print "%s is not palin"% inputstring
    View Code
     1 #6-5(d)
     2 
     3 def palin(str):
     4   lens = len(str)
     5   if lens%2 != 0:
     6     i = lens -1 -1
     7   else:
     8     i = lens - 1   
     9   
    10   while i>=0:
    11     str = str +str[i]
    12     i= i-1 
    13   
    14   return str    
    15   
    16   
    17 inputstring = raw_input("enter the string:")
    18 
    19 print"the palin of %s is: %s" % (inputstring, palin(inputstring))  
    View Code

    6-6

     1 #6-6
     2 
     3 def trim_end_blacks(str):
     4   lens = len(str)
     5   i = lens -1
     6 #trim the end blanks  
     7   while i >=0 and str[i] == ' ':
     8     str = str[:i]
     9     i = i- 1
    10 #trim the start blanks
    11   while str[0]==' ' and len(str)!=0: 
    12     str = str[1:]
    13     print str  
    14 
    15 inputstr = raw_input("enter the string:")
    16 
    17 print """after trim the blanks in start and end pos: 
    18 %s change to %s
    19 """%(inputstr, trim_end_blacks(inputstr))  
    View Code

    6-8

     1 #6-8
     2 
     3 print """
     4 (zero,one, two, three, four, five, six, seven, eight, nine,),
     5 (ten,eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen,),
     6 (twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety,)
     7 (hundred, thousand, million, billion,...)
     8 """ 
     9 
    10 units = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
    11 teens = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
    12 tens = ["twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
    13 hundreds = ["hundred", "thousand", "million", "billion"]    
    14 
    15 def num_to_eng(num):
    16 #get thousand
    17   need_and = False
    18   eng_str = ""
    19   if(num == 0):
    20     eng_str = "Zero"
    21     return eng_str
    22   num = num%10000
    23   if num/1000 != 0:
    24     need_and = True
    25     eng_str = eng_str + units[num/1000-1]+" thousand "
    26   num = num%1000
    27   if num/100 !=0:
    28     need_and = True
    29     eng_str = eng_str + units[num/100 - 1]+" hundred "
    30   if(need_and):
    31     eng_str = eng_str + "and "
    32   
    33   num = num%100
    34   if(num>19):
    35     eng_str = eng_str + tens[num/10 - 2]+" "
    36     num = num%10
    37     if(num != 0):
    38       eng_str = eng_str + units[num-1]
    39   elif(num>9):
    40     eng_str = eng_str + teens[num -10]
    41   else:
    42     eng_str = eng_str+ units[num-1]    
    43   return eng_str
    44 num = raw_input("enter the number:")
    45 print num_to_eng(int(num)) 
    View Code

    6-9

     1 #6-9
     2 
     3 def min_to_hm(t):
     4   h = t/60
     5   h=h%24
     6   
     7   m = t%60
     8   
     9   return "%d:%d"%(h,m)
    10 
    11 t = raw_input("enter the mins:")
    12 print "the time is:%s" % min_to_hm(int(t))
    View Code

    6-10

    1 #6-10
    2 import string
    3 def ul_covert(str):
    4   return string.swapcase(str)
    5 
    6 str = raw_input("enter the string:") 
    7 
    8 print ul_covert(str)   
    View Code

    6-11

     1 #6-11
     2 def n_to_ip(num):
     3   str = "%d" % num
     4   if len(str)!= 12:
     5     print "invalid ip"
     6     return ""
     7   else:
     8     return str[:3]+":"+str[3:6]+":"+str[6:9]+":"+str[9:12]  
     9 
    10 def ip_to_n(str):
    11   if len(str)!=15:
    12     print "invalid ip str"
    13     return 0
    14   else:
    15     return int(str[0:3]+str[4:7]+str[8:11]+str[12:15])
    16 
    17 
    18 num = int(raw_input("enter the ip num:"))
    19 print"after covert to ip string: %s" % n_to_ip(num)
    20 print"after convert to ip num: %d" % ip_to_n(n_to_ip(num))  
    View Code

    6-12

     1 #6-12
     2 
     3 def findchr(string, char):
     4   if char == "":
     5     return 0
     6   lens = len(string)
     7   if lens == 0:
     8     return -1
     9   i=0
    10   while i<lens:
    11     if string[i] == char:
    12       return i
    13     else:
    14       i=i+1
    15   return -1
    16 
    17 def rfindchr(string, char):
    18   lens = len(string)
    19   if char == "":
    20     return lens
    21   if lens == 0:
    22     return -1
    23   i = lens -1
    24   while i>=0:
    25     if string[i] == char:
    26       return i
    27     else:
    28       i=i-1
    29   
    30   return -1          
    31 
    32 def subchr(string, origchar, newchar):
    33   lens = len(string)
    34   i = 0
    35   while i<lens:
    36     if string[i]==origchar:
    37       string = string[0:i]+newchar+string[i+1:]
    38     i= i+1  
    39   return string    
    40 
    41 
    42 print findchr("","")
    43 print findchr("", "a")
    44 print findchr("abc","a")
    45 print findchr("abc", "d")  
    46 
    47 print rfindchr("","")
    48 print rfindchr("", "a")
    49 print rfindchr("abc","a")
    50 print rfindchr("dabcd", "d")    
    51 
    52 print subchr("test", "t", "a") 
    View Code

    6-13

     1 #6-13
     2 #import string
     3 
     4 def atoc(str):
     5   real = 0
     6   img = 0
     7   
     8   lens = len(str)
     9   
    10   i = lens -1
    11   if str[i] == "j":
    12     if str[i-1]==")":
    13       j = i -2
    14       while j>=0:
    15         if str[j] != "(":
    16           j= j-1
    17         else:
    18           break   
    19       img = float(str[j+1:i-1])    
    20       i = j-1
    21     else:
    22       j=i
    23       while j>0:
    24         if str[j]=="+" or str[j]=="-":
    25           break
    26         else:
    27           j=j-1     
    28       img = float(str[j+1:i])
    29       i=j      
    30   real = float(str[0:i]) 
    31   return complex(real, img)
    32 
    33 str = raw_input("enter the complex:")
    34 print atoc(str) 
    View Code

    6-14

     1 #6-14
     2 import string
     3 import random
     4 
     5 def Rockhambocu(pc_opt,y_opt):
     6   """
     7   pc_opt, the option of pc
     8   y_opt, your option
     9   (r)rock
    10   (s)scissor
    11   (c)cloth
    12   """
    13   str_opt = "rsc"
    14   
    15   rsc_rule =[[0,-1,1],[1,0,-1],[-1,1,0]] 
    16   
    17   pc_i = string.find(str_opt, pc_opt)
    18   y_i = string.find(str_opt, y_opt)
    19   
    20   return rsc_rule[pc_i][y_i]
    21 
    22 def RSC_result(r):
    23   result = ["fail", "draw", "win"]
    24   return result[r+1]
    25 
    26 print """
    27 enter your option:
    28    (r)rock
    29   (s)scissor
    30   (c)cloth 
    31   (other)default r
    32 """
    33 
    34 y_opt = raw_input()
    35 
    36 str_opt ="rsc"
    37 pc_opt = str_opt[random.randint(0,2)]
    38 print "PC option is %s" % pc_opt
    39 
    40 print "you %s"% RSC_result(Rockhambocu(pc_opt, y_opt))  
    View Code

    6-15

      1 #6-15
      2 
      3 def is_leap(y):
      4   if y/400 == 0:
      5     return True
      6   elif y/4 == 0 and y/100 != 0:
      7     return True
      8   return False
      9   
     10 def check_date_valid(y, m, d):
     11   """
     12   -1, invalid date
     13   1, valid date
     14   """
     15   if y<1:
     16     return -1
     17   if m<1 or m>12:
     18     return -1
     19   month_days = [31,28, 31, 30, 31, 30, 31, 30, 31, 30, 31, 30]  
     20   if is_leap(y):
     21     month_days[1] = 29
     22   if d<1 or d>month_days[m-1]:
     23     return -1
     24   
     25   return 1    
     26 
     27 def calcu_days(y, m, d):
     28   sum = d
     29   month_days = [31,28, 31, 30, 31, 30, 31, 30, 31, 30, 31, 30]
     30   year_days = 365
     31   
     32   if is_leap(y):
     33     month_days[1] = 29
     34   
     35   i = 1
     36   while i<m:
     37     sum = sum +  month_days[i-1]
     38     i=i+1
     39   i=1  
     40   while i<y:
     41     if is_leap(i):
     42       sum = sum + year_days + 1
     43     else:
     44       sum = sum + year_days
     45     i = i +1
     46   return sum
     47              
     48   
     49 def days_between_tdate(date1, date2):
     50   y1 = int(date1[6:])
     51   m1 = int(date1[3:5])
     52   d1 = int(date1[:2])
     53   
     54   if check_date_valid(y1, m1, d1) == -1:
     55     return -1
     56   
     57   y2 = int(date2[6:])
     58   m2 = int(date2[3:5])
     59   d2 = int(date2[:2])
     60   
     61   if check_date_valid(y2, m2, d2) == -1:
     62     return -1
     63  
     64   return abs(calcu_days(y1, m1, d1) - calcu_days(y2, m2, d2))
     65  
     66 def all_days_your_life(birthday):
     67   date = raw_input("Enter the date of today:")
     68   return days_between_tdate(birthday, date)
     69 
     70 
     71 def days_of_next_birthday(birthday):
     72   date = raw_input("Enter the date of today:")
     73   
     74   y1 = int(date[6:])
     75   m1 = int(date[3:5])
     76   d1 = int(date[:2])
     77   
     78   if check_date_valid(y1, m1, d1) == -1:
     79     return -1
     80   
     81   y2 = y1
     82   m2 = int(birthday[3:5])
     83   d2 = int(birthday[:2])
     84   
     85   if check_date_valid(y2, m2, d2) == -1:
     86     return -1
     87   
     88   if calcu_days(y2,m2,d2)<calcu_days(y1,m1,d1):
     89     y2 = y2 +1
     90   
     91   return  calcu_days(y2,m2,d2)-calcu_days(y1,m1,d1) 
     92    
     93 
     94 date1 = raw_input("Enter the date1:")
     95 date2 = raw_input("Enter the date2:")
     96 
     97 print "the days between these two date is: %d days"%  days_between_tdate(date1, date2)
     98 
     99 birthday = raw_input("enter the date you have born:")  
    100 print "the days you have been in the world: %s days"% all_days_your_life(birthday)
    101 
    102 print "the next birthday till now: %d days"% days_of_next_birthday(birthday) 
    View Code

    6-16

     1 #6-16
     2 
     3 def add_matrix(M, N):
     4   r1 = len(M)
     5   c1 = len(M[0])
     6   
     7   r2 = len(N)
     8   c2 = len(N[0])
     9   
    10   r = [([0]*r1) for i in range(c1)]
    11 
    12   if r1==r2 and c1==c2:
    13     i = 0
    14     while i<r1:
    15       j = 0
    16       while j<c1:
    17         r[i][j] = M[i][j] + N[i][j]
    18         print M[i][j], N[i][j]
    19         j = j+1
    20       i = i+1
    21     return r  
    22   else:
    23     return -1
    24     
    25     
    26 def mul_matrix(M, N):
    27   r1 = len(M)
    28   c1 = len(M[0])
    29   
    30   r2 = len(N)
    31   c2 = len(N[0])
    32   
    33   if c1 != r2:
    34     return -1
    35   r = [([0]*c2) for i in range(r1)]
    36 
    37   for i in range(r1):
    38     for j in range(c2):
    39       for i1 in range(c1):
    40         r[i][j] = r[i][j] + M[i][i1]*N[i1][j]
    41         i1 = i1 + 1
    42       j = j +1  
    43     i= i+1    
    44   return r  
    45   
    46 M = [[1,1], [2,0]]
    47 N = [[-1,2], [9,0]]
    48 
    49 print add_matrix(M, N)
    50 N=[[0, 2, 3], [1, 1, 2]]
    51 print mul_matrix(M, N)
    View Code

    6-17

     1 #6-17
     2 
     3 def myPop(a):
     4   if len(a[0]) == 0:
     5     return -1
     6   ret = a[0][-1]
     7   
     8   #a[0] = a[0][0: len(a[0])-1]
     9   del a[0][-1]
    10   print a[0]
    11   return ret
    12   
    13 a = [1,2,3]
    14 
    15 print myPop([a])
    16 print a 
    View Code
  • 相关阅读:
    html 中 #include file 的用法
    人人网FED CSS编码前端开发规范
    (转载)css垂直水平居中的整理
    CSS负边距自适应布局三例
    (转载)无缝滚动图片的js和jquery两种写法
    用css3实现鼠标移进去当前亮其他变灰
    应急响应
    扫描工具介绍
    入侵检测
    安全防护与加固
  • 原文地址:https://www.cnblogs.com/huking/p/3260650.html
Copyright © 2020-2023  润新知