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

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

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

    xylz,imxylz

    關(guān)注后端架構(gòu)、中間件、分布式和并發(fā)編程

       :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
      111 隨筆 :: 10 文章 :: 2680 評(píng)論 :: 0 Trackbacks
    8-4.

    Prime Numbers. We presented some code in this chapter to determine a number's largest factor or if it is prime. Turn this code into a Boolean function called isprime() such that the input is a single value, and the result returned is true if the number is prime and False otherwise.

     1#!/usr/bin/env python
     2#-*- coding:utf-8 -*-
     3#$Id: p0804.py 164 2010-06-28 12:49:39Z xylz $
     4
     5'''
     6This is a 'python' study plan for xylz.
     7Copyright (C)2010 xylz (www.imxylz.info)
     8'''
     9
    10def isprime(num):
    11    if num <= 1return False
    12    cnt = num / 2
    13    while cnt > 1:
    14        if num % cnt == 0:
    15            return False
    16        cnt -= 1
    17    return True
    18
    19if __name__ == '__main__':
    20    
    21    assert True == isprime(7)
    22    assert False == isprime(8)
    23    assert True == isprime(19)
    24    assert False == 0
    25
    8-5.

    Factors. Write a function called getfactors() that takes a single integer as an argument and returns a list of all its factors, including 1 and itself.

     1#!/usr/bin/env python
     2#-*- coding:utf-8 -*-
     3#$Id: p0805.py 156 2010-06-21 07:24:12Z xylz $
     4
     5'''
     6This is a 'python' study plan for xylz.
     7Copyright (C)2010 xylz (www.imxylz.info)
     8'''
     9
    10def getfactors (num):
    11    if num==0: return []
    12    if num==1return [1]
    13    cnt = num /2
    14    ret=[]
    15    for i in range(1,cnt+1,1) :
    16        if num % i ==0: ret.append(i)
    17    ret.append(num)
    18    return ret
    19        
    20 
    21
    22if __name__ == '__main__':
    23    assert [1,2,5,10== getfactors(10)
    24    assert [1,5== getfactors(5)
    25    assert [1== getfactors(1)
    26    assert [] == getfactors(0)
    27
    8-6.

    Prime Factorization. Take your solutions for isprime() and getfactors() in the previous problems and create a function that takes an integer as input and returns a list of its prime factors. This process, known as prime factorization, should output a list of factors such that if multiplied together, they will result in the original number. Note that there could be repeats in the list. So if you gave an input of 20, the output would be [2, 2, 5].

     1#!/usr/bin/env python
     2#-*- coding:utf-8 -*-
     3#$Id: p0806.py 157 2010-06-21 07:39:05Z xylz $
     4
     5'''
     6This is a 'python' study plan for xylz.
     7Copyright (C)2010 xylz (www.imxylz.info)
     8'''
     9
    10def get_prime_factors (num):
    11    if num<1return []
    12    cnt = 2
    13    ret=[]
    14    max_factor = num /2
    15    while cnt <= max_factor:
    16        if num % cnt ==0:
    17            ret.append(cnt)
    18            num /= cnt
    19        else:
    20            cnt += 1
    21    return ret    
    22     
    23
    24if __name__ == '__main__':
    25    assert [2,5== get_prime_factors(10)
    26    assert [] == get_prime_factors(5)
    27    assert [2,2,5== get_prime_factors(20)
    28    assert [2,3,7== get_prime_factors(42)
    29    assert [2,7== get_prime_factors(14)
    30    assert [3,3,3== get_prime_factors(27)
    31
    8-7.

    Perfect Numbers. A perfect number is one whose factors (except itself) sum to itself. For example, the factors of 6 are 1, 2, 3, and 6. Since 1 + 2 + 3 is 6, it (6) is considered a perfect number. Write a function called isperfect() which takes a single integer input and outputs 1 if the number is perfect and 0 otherwise.

     1#!/usr/bin/env python
     2#-*- coding:utf-8 -*-
     3#$Id: p0807.py 159 2010-06-21 08:29:21Z xylz $
     4
     5'''
     6This is a 'python' study plan for xylz.
     7Copyright (C)2010 xylz (www.imxylz.info)
     8'''
     9
    10def isperfectnumber (num):
    11    if num<=2return False
    12    cnt = num / 2
    13    ret = 0 
    14    for i in range(1,cnt+1):
    15        if num % i == 0: ret += i
    16    return ret == num
    17    
    18
    19if __name__ == '__main__':
    20    assert True == isperfectnumber(6)
    21    for i in range(10000):
    22        if isperfectnumber(i):
    23            print i
    24        
    25        
    26
    8-11.

    Text Processing. Write a program to ask the user to input a list of names, in the format "Last Name, First Name," i.e., last name, comma, first name. Write a function that manages the input so that when/if the user types the names in the wrong order, i.e., "First Name Last Name," the error is corrected, and the user is notified. This function should also keep track of the number of input mistakes. When the user is done, sort the list, and display the sorted names in "Last Name, First Name" order.

     1#!/usr/bin/env python
     2#-*- coding:utf-8 -*-
     3#$Id: p0811.py 163 2010-06-21 15:53:21Z xylz $
     4
     5'''
     6This is a 'python' study plan for xylz.
     7Copyright (C)2010 xylz (www.imxylz.info)
     8'''
     9from string import printable
    10
    11def sort_name(cnt):
    12    err_cnt = 0
    13    i=0
    14    flnames = []
    15    while i<cnt:
    16        fixit = False
    17        try:
    18            s=raw_input('Please enter name %s: ' % i)
    19            if ',' in s:
    20                names = s.split(',')
    21            else:
    22                names = s.split()
    23                if len(names) ==2
    24                    fixit = True
    25            if len(names)!=2raise ValueError
    26            flnames.append(list(s.strip() for s in names))
    27            i += 1
    28            if fixit:
    29                raise ValueError
    30        except:
    31            err_cnt +=1
    32            print "Wrong formatshould be Last, First."
    33            print "You have done this %s time(s) already." % err_cnt,
    34            if fixit:
    35                print " Fix it "
    36            else:
    37                print
    38
    39    flnames.sort(cmp=lambda x,y:cmp(x[1],y[1]))
    40    print "The sorted list (by last name) is: "
    41    for fn in flnames:
    42        print "\t%s, %s" % (fn[0],fn[1]) 
    43  
    44if __name__ == '__main__':
    45    cnt = int(raw_input('Enter total number of names: '))
    46    print 
    47    sort_name(cnt) 
    48
    8-12.

    (Integer) Bit Operators. Write a program that takes begin and end values and prints out a decimal, binary, octal, hexadecimal chart like the one shown below. If any of the characters are printable ASCII characters, then print those, too. If none is, you may omit the ASCII column header.

     1#!/usr/bin/env python
     2#-*- coding:utf-8 -*-
     3#$Id: p0812.py 164 2010-06-28 12:49:39Z xylz $
     4
     5'''
     6This is a 'python' study plan for xylz.
     7Copyright (C)2010 xylz (www.imxylz.info)
     8'''
     9from string import printable
    10
    11def bit_operators(start,end):
    12    has_print = False
    13    ret = []
    14    for i in range(start,end+1):
    15        pc = ''
    16        if i <256 and chr(i) in printable:
    17            has_print = True
    18            pc = chr(i)
    19        ret.append((str(i),bin(i)[2:],oct(i)[1:],hex(i)[2:],pc))
    20    return (has_print,ret)
    21        
    22  
    23if __name__ == '__main__':
    24    start = int(raw_input('Enter begin value: '))
    25    end = int(raw_input('Enter end value: '))
    26    if start > end: 
    27        import sys
    28        sys.exit(0)
    29
    30    has_print,ret = bit_operators(start,end)
    31    width = ( len(str(end)), len(bin(end))-2, len(oct(end))-1, len(hex(end))-21 )
    32    title = ( 'DEC''BIN''OCT''HEX''ASCII')
    33    width = tuple( max(x,len(y)) for x,y in zip(width,title) )
    34    full_title = None
    35    if has_print:
    36        print " ".join(list(s.rjust(w) for s,w in zip(title,width)))
    37        print "-".join(list(('-'*len(s)).rjust(w,'-'for s,w in zip(title,width)))
    38    else:
    39        print " ".join(list(s.rjust(w) for s,w in zip(title[:-1],width[:-1])))
    40        print "-".join(list(('-'*len(s)).rjust(w,'-'for s,w in zip(title[:-1],width[:-1])))
    41    
    42    for item in ret:
    43        print " ".join(list(s.rjust(w) for s,w in zip(item,width)))
    44            
    45            
    46            
    47            
    48        
    49        
    50        
    51    
    52    
    53        
    54        
    55


    ©2009-2014 IMXYLZ |求賢若渴
    posted on 2010-06-28 20:56 imxylz 閱讀(16887) 評(píng)論(0)  編輯  收藏 所屬分類: Python

    ©2009-2014 IMXYLZ
    主站蜘蛛池模板: 亚洲国产成人AV网站| 看Aⅴ免费毛片手机播放| 国产午夜亚洲精品不卡免下载| 成年免费a级毛片免费看无码| 免费h片在线观看网址最新| 波多野结衣免费视频观看 | 久久亚洲国产成人影院网站| 亚洲福利一区二区| av片在线观看永久免费| 国产免费不卡v片在线观看| 亚洲熟伦熟女新五十路熟妇| 亚洲国产精品成人精品小说| 一二三四在线观看免费中文在线观看 | 亚洲日本VA午夜在线电影| 三级黄色免费观看| 毛色毛片免费观看| 亚洲国产另类久久久精品小说| 亚洲va久久久久| 日本免费久久久久久久网站| 国产极品粉嫩泬免费观看 | 中文字幕乱码亚洲精品一区| 青青操免费在线观看| 日本免费人成黄页网观看视频| 亚洲∧v久久久无码精品| 看全免费的一级毛片| 日本一区二区三区免费高清| 亚洲精品无码不卡在线播放HE| 亚洲AV无码一区二区一二区| 88xx成人永久免费观看| 国产亚洲精品国看不卡| 亚洲色精品三区二区一区| 99热在线免费观看| 自拍偷自拍亚洲精品第1页| 亚洲乱妇老熟女爽到高潮的片| 久久精品一本到99热免费| 国产亚洲老熟女视频| 亚洲精品无码成人片久久不卡 | 免费黄色一级毛片| 亚洲欧洲高清有无| 一区二区免费视频| 国产亚洲精品影视在线产品 |