練習2

1. 隨機輸入一個數字判斷是否爲素數,參考第三課課後題代碼,要求使用dict做緩存。緩存結果,避免重複計算。

2. 給你一首優美的英文詩選段
s = “”“
Have you thought about what you want people to say about you after you’re gone? Can you hear the voice saying, “He was a great man.” Or “She really will be missed.” What else do they say?
One of the strangest phenomena of life is to engage in a work that will last long after death. Isn’t that a lot like investing all your money so that future generations can bare interest on it? Perhaps, yet if you look deep in your own heart, you’ll find something drives you to make this kind of contribution---something drives every human being to find a purpose that lives on after death.
”“”

統計出其中共用到多少個字母,每個字母在字符串中出現的次數是多少,然後按照出現的頻率進行排序輸出。
1.        區分大小寫
2.        不區分大小寫
     


1.

#!/usr/bin/env python

#!coding:utf-8

cache = {}

while True:

    num = raw_input('please input a num: ')

    try:

        num = float(num) 

    except ValueError:

        print 'Error Type'

        continue

    num = int(num)

    rs = cache.get(num)

    if rs:

        print 'hit'

        print rs

        continue

    else:

        print 'miss'

    is_prime = False

    for i in range(2,num):

        print 'count',i

        if num % i == 0:

            is_prime = False

            break

        else:

            is_prime = True

    msg=""

    if is_prime:

        msg = '%d is prime' % num

    else:

        msg = '%d is not prime' % num

    cache[num] = msg

    print msg 

        

2.


#!/usr/bin/env python

#!coding:utf-8

#!author:liang

s = """

Have you thought about what you want people to say about you after you’re gone? Can you hear the voice saying, “He was a great man.” Or “She really will be missed.” What else do they say?

One of the strangest phenomena of life is to engage in a work that will last long after death. Isn’t that a lot like investing all your money so that future generations can bare interest on it? Perhaps, yet if you look deep in your own heart, you’ll find something drives you to make this kind of contribution---something drives every human being to find a purpose that lives on after death.

"""

import re

letters = re.findall(r'\w',s)

print "區分大小寫:"

dx_dict = dict([i,0] for i in letters )

for letter in letters:

    dx_dict[letter] = dx_dict[letter] + 1

dx_sort = sorted(dx_dict.items(),key = lambda x:x[1],reverse = True) 

dx_count = len(dx_dict.keys())

print "優美的英文詩選段共用到【%d】個字母,分別爲:" % dx_count

for i in dx_sort:

    print "字母【%s】,出現次數爲【%d】次 " % (i[0],i[1])

print "============================================================"

print "============================================================"




print "不區分大小寫:"

s_lower = s.lower()

letters_lower = re.findall(r'\w',s_lower)

x_dict = dict([i,0] for i in letters_lower )

for letter_s in letters_lower:

    x_dict[letter_s] = x_dict[letter_s] + 1

x_sort = sorted(x_dict.items(),key = lambda x:x[1],reverse = True)

x_count = len(x_dict.keys())

print "優美的英文詩選段共用到【%d】個字母,分別爲:" % x_count

for j in x_sort:

    print "字母【%s】,出現次數爲【%d】次 " % (j[0],j[1])



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章