think_in_python练习题.docx_第1页
think_in_python练习题.docx_第2页
think_in_python练习题.docx_第3页
think_in_python练习题.docx_第4页
think_in_python练习题.docx_第5页
已阅读5页,还剩25页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

def middle(t):lenth = len(t)t.pop(0)t.pop(lenth-1)return t middle(1,2,3,2,2,8)Traceback (most recent call last): File , numsne 1, in middle(1,2,3,2,2,8) File , numsne 4, in middle t.pop(lenth-1)IndexError: pop index out of range def middle(t):lenth = len(t)print(lenth)x=t.pop(0)print(x)return t middle(1,2,3,2,2,8)612, 3, 2, 2, 8 def middle(t):lenth = len(t)t.pop(lenth-1)t.pop(0)return t middle(1,2,3,2,2,8)2, 3, 2, 2注意t.pop(0)以后,t的length就会减一。def chop(t):del tlen(t)-1del t0print(t) chop(1,2,2,3,3,4,4,8)2, 2, 3, 3, 4, 4 t =1,2,2,2,33,44,8 t.remove(2) t1, 2, 2, 33, 44, 8def histogram(s):d = dict()for c in s:if c not in d:dc = 1else:dc += 1return ddef rev_lookup(d,v):res = for k in d:if dk = v:res.append(k)#print resreturn resrev_lookup(h,1)a, b, f, o, s, t, v rev_lookup(h,2)e, i, l, u rev_lookup(h,3)def count_c(string):d = dict()for c in string:if c not in d:dc = 1else:dc +=1return ddef most_fre(string):di = count_c(string)nums = sorted(di.items(),key = itemgetter(1),reverse = True)for x,y in nums:print(x,end =) most_fre(frequencyisless)escfilnqruy def signature(s): Returns the signature of this string, which is a string that contains all of the letters in order. t = numsst(s) t.sort() t = .join(t) return tdef all_anagrams(filename): Finds all anagrams in a numsst of words. filename: string filename of the word numsst Returns: a map from each word to a numsst of its anagrams. d = for numsne in open(filename): word = numsne.strip().lower() t = signature(word) if t not in d: dt = word else: dt.append(word) return ddef print_anagram_sets_in_order(d): Prints the anagram sets in d in decreasing order of size. d: map from words to numsst of their anagrams # make a numsst of (length, word pairs) t = for v in d.values(): if len(v) 1: t.append(len(v), v) # sort in ascending order of length t.sort() # print the sorted numsst for x in t: print (x)def filter_length(d, n): Select only the words in d that have n letters. d: map from word to numsst of anagrams n: integer number of letters Returns: new map from word to numsst of anagrams res = for word, anagrams in d.items(): if len(word) = n: resword = anagrams return resres= dict()for word,anagrams in d:if len(word) = n:resword.append(len(word),word)return res signature(tormorow)mooorrtw/81065/def children(s):nums = for i in range(len(s):ns = sns = numsst(ns)ns.pop(i)ns = .join(ns)print(ns)nums.append(ns)return nums children(christine)hristinecristinechistinechrstinechritinechrisinechristnechristiechristinhristine, cristine, chistine, chrstine, chritine, chrisine, christne, christie, christinPython 3.2.3 (default, Apr 11 2012, 07:15:24) MSC v.1500 32 bit (Intel) on win32Type copyright, credits or numscense() for more information. def make_word_dict(): Reads the words in words.txt and returns a dictionary that contains the words as keys. d = dict() fin = open(words.txt) for numsne in fin: word = numsne.strip().lower() dword = word # have to add single letter words to the word numsst; # also, the empty string is considered a word. for letter in a, i, : dletter = letter return d memo = memo = SyntaxError: unexpected indent memo = def is_reducible(word,word_dict):if word in memo:print(there)return memowordres = for child in children(word,word_dict):t = is_reducible(child,word_dict)if t:print(child)print(t)res.append(child)memoword = resreturn res def children(word, word_dict): Returns a numsst of all words that can be formed by removing one letter. word: string Returns: numsst of strings res = for i in range(len(word): child = word:i + wordi+1: if child in word_dict: res.append(child) return res word_dict = make_word_dict() memo: def is_reducible(word,word_dict):if word in memo:#print(there)return memowordres = for child in children(word,word_dict):t = is_reducible(child,word_dict)if t:#print(child)#print(t)res.append(child)me

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论