一个字典,给你一个word找出所有anagram

所谓anagram, 就是一个词A通过里面字母顺序的变换得到另一个词B,那个B就是A的anagram。

现在给你一个词A,然后给你一个字典,这个字典里含有很多次,在字典里找出所有A 的 anagram.

这道题的关键点是,怎么去判断两个单词是否是anagram。 这里用到的方法是 把A 进行排序,比如如果A 是 test, 排序后变成 estt, 然后我们把所有要进行比较的也通过排序处理,如果排序后的两个单词相等,那么它们的原始的单词就是anagram.

老师教学上字典和元组是这样的“在需要使⽤序列的情况下: • 函数的返回值 • 字典的键值 • 函数的参数 directory[last, first] = number • for last, first in directory: • print(first, last, directory[last,first]) def has_match(t1, t2): • for x, y in zip(t1, t2): • if x == y: • return True • return False • for index, element in enumerate('abc'): • print(index, element)请你基于老师教学的代码完成练习12-2 “Exercise 12-2. More anagrams! 148 | Chapter 12: Tuples 1. Write a program that reads a word list from a file (see "Reading Word Lists" on page 99) and prints all the sets of words that are anagrams. Here is an example of what the output might look like: ['deltas', 'desalt', 'lasted', 'salted', 'slated', 'staled'] ['retainers' ['renerating greatening , 'greatening'] ['resmelts' , 'smelters' , 'termless'] Hint: you might want to build a dictionary that maps from a collection of letters to a list of words that can be spelled with those letters. The question is, how can you represent the collection of letters in a way that can be used as a key? 2. Modify the previous program so that it prints the longest list of anagrams first, followed by the second longest, and so on. 3. In Scrabble, a "bingo" is when you play all seven tiles in your rack, along with a letter on the board, to form an eight-letter word. What collection of eight letters forms the most possible bingos? Hint: there are seven.”请你运用python运行规则 编写练习12-2要求的运行代码 注意代码编写具有可读性和完整性 能够真实运行出符合要求的数据 注意分两版答案 第一版应该有注释 第二版为纯代码 其中题目有说明解题的思路 可以按照题目的思路步骤一步一步完成
06-05
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值