已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年下半年湖南省沅陵县事业单位招考易考易错模拟试题(共500题)试卷后附参考答案
- 2026中国农业银行福建省分行校园招聘528人备考题库附答案详解(完整版)
- 四川省百师联盟2026届高三上学期一轮复习12月质量检测政治试卷(含答案)
- 2025中国邮政储蓄银行芜湖市分行社会招聘备考题库含答案详解(满分必刷)
- 2026嘉兴银行校园招聘备考题库有答案详解
- 中国农业发展银行2026年度校园招聘备考题库及答案详解(典优)
- 2026年三亚城市职业学院单招职业技能考试题库附答案
- 2025年11月广东广州市天河区天润幼儿园招聘编外聘用制专任教师1人(公共基础知识)综合能力测试题附答案解析
- 2023年包头市特岗教师招聘真题题库附答案解析
- 2025年安徽师范大学附属肥西外国语学校(初中部)教师招聘1人备考题库附答案解析
- 职业卫生练习题与答案
- 2025-2026学年上学期上海小学数学四年级期末典型卷2
- 汽车买卖协议范本及签署指导
- 国家开放大学电大本科《社区护理学》期末标准题库及答案
- 湖北省十堰市丹江口市2025-2026学年六年级上学期英语月考教学质量监测(含答案无听力原文无听力音频)
- 统编版2025-2026学年语文五年级上册21古诗词三首《长相思》课件
- 2025年湖北专升本计算机基础考试真题及答案
- 2026中国人民银行所属企业网联清算公司社会招聘历年真题汇编带答案解析
- 2025江苏盐城海氧深冷科技有限公司招聘工作人员2人笔试考试参考试题及答案解析
- GB/T 3183-2025砌筑水泥
- 2025地球小博士知识竞赛试题附答案
评论
0/150
提交评论