2020-04
24

用两天琢磨一道题

By xrspook @ 20:32:30 归类于: 扮IT

前面还在沾沾自喜我写出来的脚本运行效率战胜了参考答案,但这道题目我是看着参考答案都不知道他们在说什么。如果只是一个词,我的确可以列举出它一次减少一个字母可以出现的所有可能,但怎么知道上一层可能和这一层的哪个配套???我花了2天时间去研究、消化答案。一边搞清楚答案为什么这样,另一边考虑有没有其它容易吃透的表达方式。这道题之所以让我非常纠结,根本的原因是我想不透到底我可以用什么手段实现。没有可以实现的逻辑,就不会有可行的编程。

Exercise 4: Here’s another Car Talk Puzzler (http://www.cartalk.com/content/puzzlers): What is the longest English word, that remains a valid English word, as you remove its letters one at a time? Now, letters can be removed from either end, or the middle, but you can’t rearrange any of the letters. Every time you drop a letter, you wind up with another English word. If you do that, you’re eventually going to wind up with one letter and that too is going to be an English word—one that’s found in the dictionary. I want to know what’s the longest word and how many letters does it have? I’m going to give you a little modest example: Sprite. Ok? You start off with sprite, you take a letter off, one from the interior of the word, take the r away, and we’re left with the word spite, then we take the e off the end, we’re left with spit, we take the s off, we’re left with pit, it, and I. Write a program to find all words that can be reduced in this way, and then find the longest one. This exercise is a little more challenging than most, so here are some suggestions: You might want to write a function that takes a word and computes a list of all the words that can be formed by removing one letter. These are the “children” of the word. Recursively, a word is reducible if any of its children are reducible. As a base case, you can consider the empty string reducible. The wordlist I provided, words.txt, doesn’t contain single letter words. So you might want to add “I”, “a”, and the empty string. To improve the performance of your program, you might want to memoize the words that are known to be reducible. Solution: http://thinkpython2.com/code/reducible.py.

最终,我觉得自己总算消化了,顺便画了个思维导图帮助大家理解到底分解到什么程度叫做完成,什么状态叫做分解失败。[”]和[]是两种不同的东西!!!!!!

is_reducible()是最关键的函数,memos用在这里,memos初始设置了known[”] = [”]也很关键,这是个守卫模式,没有守卫is_reducible()根本没法玩。这个脚本里的5个函数,除了一开始的创建字典函数,其余函数都可以单独测试,把一个固定单词放进去脚手架测试,可以帮助理解。cut_letter(),is_reducible()和all_reducible()这三个函数最终返回的都是列表,它们的样式都是类似的。希望我理解过程中的注释能帮助到有需要的人。PS一句:参考答案的打印效果让人很晕,我修改版的打印效果很美丽:)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from time import time
def set_dict(fin):
    d = {}
    for line in fin:
        word = line.strip()
        d[word] = 0
    for word in ['a', 'i', '']:
        d[word] = 0
    return d
def cut_letter(word, d): # 生成子单词,返回列表
    l = []
    for i in range(len(word)):
        new_word = word[:i] + word[i+1:]
        if new_word in d:
            l.append(new_word)
    return l # ['']长度为1,[]长度为0,无子词不能分解时返回[],'a'返回['']
def is_reducible(word, d): # 判断能否生成无限子单词,返回列表
    if word in known: # 守卫模式下,''空字符串被列入初始字典,不列入永远会被递归到[],无果
        return known[word]
    # if word == '': # 不用memos的时候,需要加入这句守卫
    #     return ['']
    l = []
    for new_word in cut_letter(word, d):
        if len(is_reducible(new_word, d)) > 0:
            l.append(new_word)
    known[word] = l
    return l
def all_reducible(d): # 收集所有无限子单词的单词,返回列表
    l = []
    for word in d:
        if len(is_reducible(word, d)) > 0: # 有列表,即有无限子单词
            l.append((len(word), word)) # 列表含有N个元组,元组里有2个元素,1为单词的字母数量,2为单词本尊
    new_l = sorted(l, reverse = True) # 每次减少一个字母,单词的字母越多当然就能降解出越多层了
    return new_l
def word_list(word): # 打印单词及子单词
    if len(word) == 0: # 最后一个进入is_reducible()的是[''],对应l[0]为无,打印结束
        return
    print(word)
    l = is_reducible(word, d) # 因为是被鉴定过词汇表里的词,所以必定有无限子单词
    word_list(l[0]) # 子单词有多个时只选第1个
known = {} # memos实际上只在is_reducible()起作用,除了提高效率,还能用作守卫
known[''] = [''] # 因为is_reducible()返回的是列表,所以即便是空字符串,键值也必须是列表!
fin = open('words.txt')
start = time()
d = set_dict(fin) # 普通的字典,键为单词,键值为0
words = all_reducible(d) # 列表,元组,2元素
for i in range(5):
    word_list(words[i][1]) # 列表里第某个元组的第2个元素
end = time()
print(end - start)
# complecting
# completing
# competing
# compting
# comping
# coping
# oping
# ping
# pig
# pi
# i
# twitchiest
# witchiest
# withiest
# withies
# withes
# wites
# wits
# its
# is
# i
# stranglers
# strangers
# stranger
# strange
# strang
# stang
# tang
# tag
# ta
# a
# staunchest
# stanchest
# stanches
# stances
# stanes
# sanes
# anes
# ane
# ae
# a
# restarting
# restating
# estating
# stating
# sating
# sting
# ting
# tin
# in
# i
# 0.6459996700286865
# 无memos 1.5830001831054688, 有memos 0.6459996700286865
2020-04
24

拉卷纸的熊猫

By xrspook @ 9:19:00 归类于: 烂日记

循环和递归,对路人甲来说那是差不多的玩意,反正就是在那里转圈圈。但是,虽然已经认识递归好些时间了,但我依然非常害怕这个存在。之所以害怕递归,是因为我很难想象到底递归什么时候才是个头,而我又非常明白,达不到递归的头,就不能把东西返回出来,得到我想要的答案。这个玩意就像一个无底洞。于是每次当我遇到递归,我都会在那里瑟瑟发抖,大概要克服这个,我需要非常大量的递归练习,让理解这个东西变成我的条件反射。

我不知道,在实际编程过程之中,到底会不会真的经常用到递归这种恐怖的东西。在Think Python 2这本书里面,很早就已经在说递归。还记得递归这种东西他们是结合小海龟一起折腾人的。现在回想起来,这或许是个正确的选择。因为小海龟是一个画图的东西,会让你用更直观地去理解递归到底在做什么。我还记得他们要我们理解的那个树杈和雪花。树杈那个图还算是一个比较正经的东西,雪花那个图案,简直是让我头皮发毛。每次说起递归,我就会想起小时候家里那个卷纸筒。黄色的卷纸筒上面,有一个卡通熊猫在拉卷纸的图案,而它拉的那个卷纸筒上面也贴着一个熊猫在拉卷纸。每次看到纸筒上的那个图案,我就会盯着看,然后脑子就会不断想象,熊猫卷纸筒,熊猫卷纸筒……想着想着甚至会觉得好恐怖,到底什么时候才是个头!这跟俄罗斯的套娃不一样。我总觉得,俄罗斯套娃无论套子多精细,总会有个头,但是,拉卷纸的熊猫,对我来说简直是个噩梦。我觉得,拉卷纸的熊猫是递归,而套娃是循环。

昨天我花了大半天的时间查单位的某些账本。查出了一箩筐的问题,我不知道为什么之前他们检查居然没发现。绝大多数都是弱智的问题,不弱智的问题则代表了他们做事的时候根本没用脑子去思考。那些莫名其妙的错误几乎到达了一种人人都有永不落空的境地。这也实在是太强大了吧!归根到底,是因为根本没有人去统一他们。每个人都看着那个规则,每个人都有自己的理解,又或者他们没看规则,只是按照上一个人的方法去做,但是他们对上一个人的做法的理解又各不相同。情况就像一个人不断地传话给下一个人,当人数传到一定程度的时候,原本的故事就会变得乱七八糟。我还记得第一次发现这个现象是在翡翠台的综艺节目超级掌门人上。这是我第一次认真地去揪他们的错误。不只是核对核心数据,也看了格式上到底合不合要求。有时我真的不明白他们,这样干活,他们觉得自己对得住自己的工资吗?他们晚上为什么居然能睡得着觉?换个说法,为什么他们这般吊儿郎当却没有东西惩罚他们?即便不是金钱上的惩罚,但起码也要让他们心里不好过,比如批评一下。但或许批评根本无效,就像你妈妈骂你一样,左耳进右耳出,就只是一阵耳边风。既然不能用惩罚,能不能用奖励的机制呢?在这个单位,爬上去的那些人你也说不上到底牛逼在哪里。没有惩罚,也没有奖励,于是也就可以理解,为什么他们会这样。

不是人人都能自律,对不自律的人必须用铁手腕。

2020-04
23

记忆中的万松园市场

By xrspook @ 11:49:41 归类于: 烂日记

在我记忆之中,我从来没有把一本编程类的书完整看完过,以前我都是挑着来看。自己需要用哪些功能,就去找哪些章节去看,而这次学习python,我是下了狠心的。几乎可以这么说,一天到晚我的脑子里就只有那些东西,即便是睡着了也一样。做梦的时候,我依然是想着python。还记得在学习递归的时候,某天晚上我做梦梦见了一个瀑布,但实际上瀑布并不是真的瀑布,那不过是一个正在流着很多水的建筑物外墙。那个瀑布从一个分成两个,再分成多个。不同的水量有不同的效果。我第一个感觉是那应该是一个自然形成的瀑布吧。但当水全部停住的以后我才发现,原来一切都是人造的。瀑布是在一个玻璃屋的外墙上形成的。之所以有这个脑洞,大概是因为我的记忆深处借鉴了东站广场的那个瀑布。还记得初中的时候我们几个同学还专门过去那里看人造瀑布。后来我不记得那里变成怎样了。反正那应该是东方宝泰外面的吧。再到后来,去东站附近通常都只是为了去宜家家私,然后下到东方宝泰里面的吉之岛,其它的东西一点印象都没有了。至于从前的那个瀑布还有没有,我实在想不起来。自从宜家家私再也不在东站的那个卖场以后,感觉我好久都没有去过那个地方了。光是东方宝泰一家,对我和我妈都没有什么吸引力。

城市的变迁,总免不了会发生东西不断消亡,但当从前熟悉的东西消失,会让人觉得无比怀念、依依不舍,但即便这样,它们还是会消失,除了在从前的照片和影像资料里再次重温,无论如何我们都回不到那个时候了。

有时我会很怀念从前到万松园市场。万松园市场在我的脑子里印象非常深刻,那些卖鱼的、卖菜的,还有卖杂货的,我都记忆犹新。我甚至还记得了当年不同品种货物的布局。虽然有些小店在我印象之中已经很模糊了,但是我还非常记得,卖鱼的在哪里,卖烧腊的在哪里。那个卖烧腊对出的通道上,某年过节,有人在那里卖老鼠肉。虽然我根本不记得卖的老鼠肉是什么样的,我甚至没有见过一眼,但是从叫卖声让我知道,有人在路中央卖老鼠肉。万松园市场对我来说是一个神奇的地方,虽然跟沙园市场比起来,那个地方不大。那条叫做万松路的路永远都是熙熙攘攘,我甚至记不起汽车是怎么在那条路上行驶的,因为上面总是挤满人。不是每个市场都会让一整条马路不复存在。那里对我来说就是最早的步行街。从前的万松园市场,或者不是这样的,或许我只记住了它热闹的一面。很久以前,这个市场进行了改造,全部都档口都入室经营。我一直觉得,新的万松园市场我很陌生。不只是我,我的家人也很少再去那里买菜了。之所以这样,其中一个很重要的原因是他们把蔬菜和水果档口放到了二楼,对老人来说,这非常不方便,因为没有电梯。我甚至一点都记不起2楼到底是怎么布局的。过去十几二十年我加起来,估计上去5次都不到。

在南丰商场下车,穿过人挤人的万松园市场,再过一条马路,山货铺的上面就是外婆的家了。

2020-04
22

字典还能这样玩!

By xrspook @ 18:30:55 归类于: 扮IT

一开始,我自己写的脚本能运行,但慢到怀疑人生。吃了个饭,折腾了半个小时后,字母表才处理到b而已,显然这是个失败的操作。我的做法是常规地为词汇表建立字典,然后历遍字典里的每个单词,单词进入函数后跟字典的另一个单词比较,比较方法是把单词(即字符串)打散为字符列表然后排列,如果排列一致,且被比较的单词小于拿去比的单词,它们就是一伙的,贴在被比较的单词列表下。列表长度大于2就返回列表然后打印。这样是可以选出异构词的,但非常非常慢!

看过参考答案之后我跳起来了,他们用了一句”.join(lists),这等于是把列表str重新粘成一个字符串,我那个去!他们把单词用列表打散重排再粘回去,最关键的是,这个唯一的重排字符串他们在建立字典的时候就作为key,所有与之有一样字符的全部被看作小弟被放置这个键的键值里。字典还是字典,但字典的键成了规则字符串,键值则是排列组合过的词汇表。我根本没想到啊,怎么可能想得到呢!!!!!

题目要求倒序打印,然后要求找出能组成最多异构词的8个字母。但实际上参考答案的输出问非所答,比如没有倒序,比如只是把8个字母的异构词摆出来,没确切告诉你最多的是什么。

Exercise 2: More anagrams! Write a program that reads a word list from a file (see Section 9.1) 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’, ‘ternaries’]
[‘generating’, ‘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? Modify the previous program so that it prints the longest list of anagrams first, followed by the second longest, and so on. 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 8 letters forms the most possible bingos? Solution: http://thinkpython2.com/code/anagram_sets.py.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from time import time
def sorted_anagram(d):
    l = []
    for key in d:
        if len(d[key]) > 1:
            l.append((len(d[key]), d[key])) # 这是个由列表创建的元组?
    return sorted(l, reverse = True) # 倒序神马真折腾
def eight_letters(d, num):
    global length # 全局变量都用上了,就为了记录个最大值
    new_l = []
    for key in d:
        if len(key) == num and len(d[key]) > 1:
           new_l.append((len(d[key]), d[key]))
           if len(d[key]) >= length:
               length = len(d[key])
    return sorted(new_l)
def sorted_letters(word):
    list_word = sorted(list(word)) # 先把字符串打散为字符列表,然后排序
    reword =''.join(list_word) # 再把字符列表回粘成字符串
    return reword
def set_dict(fin):
    d = {}
    for line in fin:
        word = line.strip()
        reword = sorted_letters(word) # 打散重排相当关键,必须在建立字典时就做!!!
        if reword not in d:
            d[reword] = [word] # 字典的键已经不是单词,是纯粹的规律字符串
        else:
            d[reword].append(word) # 字典的键值才是词汇表里的单词
    return d
fin = open('words.txt')
length = 0
count = 0
start = time()
d = set_dict(fin)
for item in sorted_anagram(d):
    print(item)
    count += 1
print(count)
for item in eight_letters(d, 8):
    if item[0] == length:
        print(item)
end = time()
print(end - start)
# ......
# (2, ['abacas', 'casaba'])
# (2, ['aba', 'baa'])
# (2, ['aals', 'alas'])
# (2, ['aal', 'ala'])
# (2, ['aahed', 'ahead'])
# (2, ['aah', 'aha'])
# 10157 # 全体异构词
# (7, ['angriest', 'astringe', 'ganister', 'gantries', 'granites', 'ingrates', 'rangiest'])
# 异构词最多的8字母单词(共7个异构词)
# 0.6079998016357422
2020-04
22

字典转元组

By xrspook @ 13:15:37 归类于: 扮IT

不搞复杂的,不用超纲的方法做感觉上很简单的事其实不简单。搞明白这个练习后,列表、字典、元组的相爱相杀我算是有点明白了。感谢那个我觉得过于复杂的参考答案,逼我折腾出了我自己的版本。

开心!居然习题1就用上了zip这个这章书最后才提到的大招。字典的键值对互换变得如此简单,我的脑洞又开大了。

Exercise 1: Write a function called most_frequent that takes a string and prints the letters in decreasing order of frequency. Find text samples from several different languages and see how letter frequency varies between languages. Compare your results with the tables at http://en.wikipedia.org/wiki/Letter_frequencies. Solution: http://thinkpython2.com/code/most_frequent.py.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def most_frequent(sth):
    d = {}
    for letter in sth: # 字符串转为字符映射到频率的字典
        d[letter.lower()] = d.get(letter.lower(), 0) + 1 # 大写的你给我降为小写
    t = tuple(zip(d.values(), d.keys())) # 用zip互换字典的键和键值,生成元组(也可以生成列表,但生成新字典你会哭死)
    return sorted(t, reverse = True) # 以第一元素降序输出
sth = 'This chapter presents one more built-in type, the tuple, and then shows how lists, dictionaries, and tuples work together. I also present a useful feature for variable-length argument lists, the gather and scatter operators.'
t = most_frequent(sth)
for item in t:
    print(item) # 原汁原味输出元组好,因为空格不用''圈着都不知道那里有东西
# (33, ' ')
# (25, 'e')
# (23, 't')
# (16, 's')
# (15, 'r')
# (14, 'a')
# (11, 'o')
# (11, 'n')
# (10, 'i')
# (10, 'h')
# (9, 'l')
# (7, 'u')
# (7, 'p')
# (5, ',')
# (4, 'g')
# (4, 'd')
# (3, 'w')
# (3, 'f')
# (3, 'c')
# (2, 'm')
# (2, 'b')
# (2, '.')
# (2, '-')
# (1, 'y')
# (1, 'v')
# (1, 'k')
© 2004 - 2026 我的天 | Theme by xrspook | Power by WordPress