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

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

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

    xylz,imxylz

    關注后端架構、中間件、分布式和并發編程

       :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      111 隨筆 :: 10 文章 :: 2680 評論 :: 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) 評論(0)  編輯  收藏 所屬分類: Python

    ©2009-2014 IMXYLZ
    主站蜘蛛池模板: 国产乱妇高清无乱码免费| 亚洲伊人久久大香线蕉AV| 两性色午夜视频免费网| 亚洲欧洲中文日韩久久AV乱码| 日本亚洲中午字幕乱码 | 国产成人亚洲合集青青草原精品| 一级毛片在线免费看| 亚洲欧洲第一a在线观看| 2019中文字幕免费电影在线播放| 久久亚洲中文字幕精品有坂深雪| 无码av免费一区二区三区| 精品日韩亚洲AV无码| 国产卡二卡三卡四卡免费网址 | 亚洲Aⅴ无码一区二区二三区软件| 国产成人综合亚洲绿色| 久久久亚洲精品蜜桃臀| 国内精品免费在线观看| 亚洲av成人一区二区三区| 免费看国产一级特黄aa大片| 一区二区免费电影| 亚洲国产日韩一区高清在线 | 亚洲国产精品一区二区三区在线观看| 免费高清在线影片一区| 色婷婷综合缴情综免费观看| 久久亚洲国产午夜精品理论片| 99久久免费看国产精品| 亚洲欧美熟妇综合久久久久| 日本高清免费不卡视频| 韩国免费A级毛片久久| 亚洲最新永久在线观看| 美女被免费视频网站a国产| jzzjzz免费观看大片免费| 中文字幕亚洲免费无线观看日本 | 亚洲av最新在线观看网址| 国产亚洲色婷婷久久99精品91| 99精品视频免费在线观看| 亚洲AV成人无码网站| 亚洲爆乳无码一区二区三区| 大地资源免费更新在线播放| 国产精品偷伦视频免费观看了| 亚洲精品自在线拍|