<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
    主站蜘蛛池模板: 成人精品国产亚洲欧洲| 日韩精品视频免费网址| 一级免费黄色毛片| 亚洲一区精品视频在线| 亚洲无码在线播放| 免费A级毛片无码A| 久久午夜免费视频| 久久综合给合久久国产免费| 国产精品视频全国免费观看| 亚洲AV第一成肉网| 在线观看亚洲AV日韩A∨| 久久精品亚洲精品国产色婷| 亚洲一区AV无码少妇电影☆| 四虎影视精品永久免费网站| 拨牐拨牐x8免费| 久草视频免费在线观看| 日韩免费人妻AV无码专区蜜桃 | 国产日韩AV免费无码一区二区三区 | 亚洲av日韩av不卡在线观看| 亚洲另类少妇17p| 国产精品99久久免费| 无码一区二区三区免费视频| 蜜臀AV免费一区二区三区| 人人玩人人添人人澡免费| 久青草视频97国内免费影视| 国产精品内射视频免费| 国产精品福利片免费看| 一级毛片免费视频网站| 一级毛片在线免费视频| 国产JIZZ中国JIZZ免费看| 免费国产va在线观看| 免费精品久久久久久中文字幕 | 成人永久免费高清| 在线视频免费国产成人| 国产在线观看免费视频播放器| 全免费a级毛片免费看不卡| 成全视频免费高清| 天天拍拍天天爽免费视频| 欧洲精品免费一区二区三区| 在线观看免费毛片| 免费一区二区三区四区五区 |