2020-04
19

秒杀的感觉真爽!

By xrspook @ 20:06:17 归类于: 扮IT

配合我的二分法搜索,10万单词找出397对回文词,我只需1.7秒。list.index()需要291秒,期间如果不输出单词,你绝对认为自己的电脑卡死了!参考答案用了70秒,而且搜出了885对,其中91对准确来说是91个,那些词自己跟自己回文,自己跟自己根本算不上两个词好吗!余下的397对是因为A词和B词算一对,B词和A词他们又输出了一遍。参考答案的语句很精炼,但特殊情况没有处理好。

赢了参考答案,真爽!!!

练习11:两个词如果互为逆序,就称它们是『翻转配对』。写一个函数来找一下在这个词汇表中所有这样的词对。

Exercise 11: Two words are a “reverse pair” if each is the reverse of the other. Write a program that finds all the reverse pairs in the word list. Solution: http://thinkpython2.com/code/reverse_pair.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import time
def in_bisect(library, first, last, myword): # 二分法搜索,10万数据查询最多只需不到20步
    if first > last: # 这是一句拯救了我的条件
        return -1
    else:
        mid = (first + last)//2
        if myword == library[mid]:
            return mid
        elif library[mid] > myword:
            return in_bisect(library, first, mid-1, myword)
        else:
            return in_bisect(library, mid+1, last, myword)
j = 0
count = 0
library = []
fin = open('words.txt')
for line in fin:
    word = line.strip()
    library.append(word)
library.sort()
start = time.time()
for i in range(len(library)-1): # 二分法搜索 
    j = in_bisect(library, 0, len(library)-1, library[i][::-1])
    if j > -1 and library[i] < library[j]:
        print(library[i], library[j])
        count += 1
# for i in range(len(library)-1): # list.index()搜索
#     if library[i][::-1] in library:
#         j = library.index(library[i][::-1], 0, len(library)-1)
#         if library[i] < library[j]:
#             print(library[i], library[j])
#             j = 0
#             count += 1
print(count)
end = time.time()
print(end - start)
# abut tuba
# ad da
# ados soda
# agar raga
# agas saga
# agenes senega
# ah ha
# aider redia
# airts stria
# ajar raja
# alif fila
# am ma
# amen nema
# amis sima
# an na
# anger regna
# animal lamina
# animes semina
# anon nona
# ante etna
# are era
# ares sera
# aril lira
# arris sirra
# arum mura
# at ta
# ate eta
# ates seta
# auks skua
# avid diva
# avo ova
# ay ya
# bad dab
# bag gab
# bal lab
# bals slab
# ban nab
# bard drab
# bas sab
# bat tab
# bats stab
# bed deb
# ben neb
# bid dib
# big gib
# bin nib
# bins snib
# bird drib
# bis sib
# bog gob
# bos sob
# bots stob
# bows swob
# brad darb
# brag garb
# bud dub
# bun nub
# buns snub
# bur rub
# burd drub
# burg grub
# bus sub
# but tub
# buts stub
# cam mac
# cap pac
# cares serac
# cod doc
# cram marc
# cud duc
# dag gad
# dah had
# dahs shad
# dam mad
# dap pad
# dart trad
# daw wad
# debut tubed
# decal laced
# dedal laded
# deem meed
# deep peed
# deeps speed
# deer reed
# dees seed
# defer refed
# degami imaged
# deifier reified
# deil lied
# deke eked
# del led
# delf fled
# deliver reviled
# dels sled
# demit timed
# denier reined
# denies seined
# denim mined
# dens sned
# depot toped
# depots stoped
# derat tared
# derats stared
# dessert tressed
# desserts stressed
# devas saved
# devil lived
# dew wed
# dewans snawed
# dexes sexed
# dial laid
# dialer relaid
# diaper repaid
# dig gid
# dim mid
# dinar ranid
# diols sloid
# dirts strid
# do od
# dog god
# dom mod
# don nod
# doom mood
# door rood
# dor rod
# dormin nimrod
# dorp prod
# dos sod
# dot tod
# drail liard
# draw ward
# drawer reward
# draws sward
# dray yard
# dual laud
# ducs scud
# duel leud
# duo oud
# dup pud
# dups spud
# eat tae
# edile elide
# edit tide
# eel lee
# eh he
# elides sedile
# em me
# emes seme
# emir rime
# emit time
# emits stime
# enol lone
# er re
# ergo ogre
# eros sore
# ervil livre
# etas sate
# even neve
# evil live
# eviler relive
# fer ref
# fires serif
# flog golf
# flow wolf
# fool loof
# gal lag
# gals slag
# gam mag
# gan nag
# gar rag
# gas sag
# gat tag
# gats stag
# gel leg
# gelder redleg
# get teg
# gip pig
# girt trig
# gnar rang
# gnat tang
# gnats stang
# gnaws swang
# gnus sung
# got tog
# gul lug
# gulp plug
# guls slug
# gum mug
# gums smug
# guns snug
# gut tug
# habus subah
# hahs shah
# hales selah
# hap pah
# hay yah
# hey yeh
# ho oh
# hoop pooh
# hop poh
# is si
# it ti
# jar raj
# kay yak
# keel leek
# keels sleek
# keep peek
# keets steek
# kips spik
# knaps spank
# knar rank
# knits stink
# lager regal
# lair rial
# lap pal
# lares seral
# larum mural
# las sal
# leer reel
# lees seel
# leets steel
# leper repel
# lever revel
# levins snivel
# liar rail
# lin nil
# lion noil
# lit til
# lobo obol
# loom mool
# loons snool
# loop pool
# loops spool
# loot tool
# looter retool
# loots stool
# lop pol
# lotos sotol
# macs scam
# maes seam
# map pam
# mar ram
# marcs scram
# mart tram
# mat tam
# maws swam
# may yam
# meet teem
# meter retem
# mho ohm
# mils slim
# mir rim
# mis sim
# mon nom
# moor room
# moot toom
# mot tom
# mures serum
# mus sum
# muts stum
# namer reman
# nap pan
# naps span
# neep peen
# net ten
# neves seven
# new wen
# nip pin
# nips spin
# nit tin
# no on
# nolos solon
# nos son
# not ton
# notes seton
# now won
# nu un
# nus sun
# nut tun
# nuts stun
# oat tao
# oohs shoo
# oot too
# os so
# ow wo
# pacer recap
# pals slap
# pans snap
# par rap
# part trap
# parts strap
# pas sap
# pat tap
# paw wap
# paws swap
# pay yap
# peels sleep
# pees seep
# per rep
# pets step
# pins snip
# pis sip
# pit tip
# pols slop
# pools sloop
# poons snoop
# port trop
# ports strop
# pot top
# pots stop
# pow wop
# pows swop
# prat tarp
# pupils slipup
# puris sirup
# pus sup
# put tup
# raps spar
# rat tar
# rats star
# raw war
# ray yar
# rebus suber
# rebut tuber
# recaps spacer
# redes seder
# redips spider
# redraw warder
# redrawer rewarder
# rees seer
# reflet telfer
# reflow wolfer
# reknit tinker
# reknits stinker
# relit tiler
# remeet teemer
# remit timer
# rennet tenner
# repins sniper
# res ser
# rot tor
# sallets stellas
# saps spas
# sat tas
# saw was
# scares seracs
# secret terces
# seeks skees
# selahs shales
# sirs sris
# sit tis
# six xis
# skeets steeks
# skips spiks
# sleeps speels
# sleets steels
# slit tils
# sloops spools
# smart trams
# smuts stums
# snaps spans
# snaw wans
# snaws swans
# snips spins
# snit tins
# snoops spoons
# snoot toons
# snot tons
# snow wons
# sow wos
# spat taps
# spay yaps
# spirt trips
# spirts strips
# spit tips
# sports strops
# spot tops
# spots stops
# sprat tarps
# sprits stirps
# staw wats
# stew wets
# stow wots
# stows swots
# straw warts
# strow worts
# struts sturts
# swat taws
# sway yaws
# swot tows
# tav vat
# taw wat
# tew wet
# tort trot
# tow wot
# trow wort
# way yaw
# tort trot 
# tow wot
# trow wort
# way yaw
# 397, 291.1146504878998 # list.index()搜索
# 397, 1.7120981216430664 # 二分法搜索
# 885, 70.3680248260498 # 参考答案运行结果
2020-04
19

买LED灯泡

By xrspook @ 16:58:01 归类于: 烂日记

无法接受昏暗的电灯,我以为只是我自己的问题,但原来我一家人都不喜欢昏暗的环境。昏暗的客厅我们已经忍受了好久,每次我在那里剪指甲的时候,我都不得不把通常都不开的荧光灯管开,但即便把灯管打开,还是有点不够亮,所以,剪脚趾甲的时候,绝大多数情况下,我是凭感觉,而不是真的看到。要真正的看清,我只能回到自己的房间里开个台灯。

昨天晚上我突发奇想,把自己12瓦的LED灯泡换到了客厅的吊灯上面。把之前那个灯孔上面一组三个的3.5瓦LED拆下来。效果太惊人了,12瓦的灯泡一下子把客厅照得不是一般的光亮。不只是光亮,甚至是光到刺眼。在我的房间,那个12瓦的灯泡是朝上而不是朝下的,而且你也不会抬眼看,所以不觉得刺眼,但是客厅吊灯的灯罩是朝下的,那个12瓦的灯泡又比3.5瓦的突出太多,于是有灯罩等于没灯罩,完全起不到稍微遮挡的作用。12瓦的LED灯放在客厅的吊灯上,像那里有个太阳。可以这么说,那个12瓦的LED灯,比40瓦1.2米的荧光灯管还要亮。这是肯定的,因为5瓦的LED灯泡就相当于40瓦的白炽灯灯泡。同样的换算,12瓦的LED灯泡就意味着接近100瓦的白炽灯灯泡亮度。我无法想象100瓦的白炽灯灯泡是怎样的,我见过那个东西吗?从折算看来,12瓦的LED灯泡估计光通量比40瓦的荧光灯管厉害。我把之前客厅3.5瓦的LED灯泡放到我房间的那个12瓦灯座上。有开灯跟没开灯没区别。3.5瓦的灯泡开了以后,就像只是开了个长明灯一样。因为光线柔和,所以总的来说甚至可能比不上别人家神台上的长明灯。最终我们得出结论,客厅昏暗得我们都无法接受,是灯泡的问题。接下来我们需要决定的是我们要买多少瓦的LED灯泡?要去哪里买?

今天,我跟我妈就跑了一趟宜家家私。那里的LED灯泡质量是有保证的。这么多年来,3.5瓦的LED灯泡虽然变暗了,但还不至于坏掉。这已经很了不起了,因为据说某些LED灯泡没用多长时间就不亮,彻底坏掉了。我已经不记得之前3.5瓦的宜家家私LED灯泡是什么时候买的,我猜买了起码有5年以上。在买那些灯泡之前,我家的客厅用的是荧光灯管。因为吊灯上的灯泡是40瓦的白炽灯,一次最少也要开三个,非常耗电。自从客厅换上了LED灯泡以后,那根1.2米的荧光灯管就几乎没有开过了。今天我打算去买宜家家私买新品4.5瓦的LED灯泡,那个灯泡的光通量是470流明。但天河卖场貌似只摆着5瓦的400流明灯泡。在用他们的网站查询过以后,我发现那个卖场的确有400流明的灯泡。询问工作人员以后,她说有货,但得去仓库里面找一找。她说但其实那个新款跟旧款没什么区别,亮度是一样的,流明不同是算法不一样。新款的瓦数是4.5,470流明,旧款的瓦数是5,400流明。从光亮度来说,二者没差多少。瓦数如果只开一个的话,也没差多少,但如果我一次要开三个,的确是有点差别的。非常时期我们也懒得让她到仓库里找灯泡了。旧款的库存不能在网上购买,新款网上可以在网上购买,实体店也的确有现货,但是却不摆在卖场里,只存在仓库里。我不知道知道这到底什么意思。新旧款的LED灯泡,名字是一样的。货号可能有区别,但我没有仔细研究。最终我们买了5瓦400流明的灯泡。这些灯泡的色温是2700K,相比于我之前的黄一些。黄一些白一些我觉得都无所谓了。灯泡跟光管不一样,黄一些反而不那么刺眼。在卖场里我们也对比过400流明跟600流明的灯泡,600流明的灯泡盯着看几秒都会觉得眼睛痛。

新买的5瓦LED灯泡换上以后,感觉光亮了好多,6个灯一起开的时候也不会莫名其妙地闪了。

2020-04
18

随机应变

By xrspook @ 16:10:30 归类于: 烂日记

我经常会出现这么一个状况,在写某篇东西的时候其实我没什么思路,我只是凭着感觉走。当然开始的时候,我是有点话题的,因为完全没有话题,根本就无法开展。随着那片东西逐步深入,我会越来越有想法,脱口而出的东西也越来越有逻辑和章法,虽然有时可能思维会比较跳跃。这让我想起一句话“女人嘛,胸这种东西,挤挤总会有的。”不只是女人,男人如果努力挤一挤,也可以用。所以刚开始落笔的时候没有思路,但是当你硬逼自己,思路总会来。问题只是你有没有真的很用力地挤。或许你根本就没有挤,你把那放在怨恨上面了,想把那锅甩给别人,甩给逼迫让你挤出思路的人。

甩锅这种事情,我非常的不喜欢,尤其是新冠之后,见识过某些地方某些领导人的处理方式以后,我就更憎恨甩锅这种行为了。憎恨归憎恨,我自己有没有耍过锅呢?肯定有。是我犯的错误,我一定会承认,但是如果不是我的东西,你甩给我,绝大多数情况之下,我会用加倍奉还给始作俑者,但有些时候,可能这个锅我根本甩不回去,因为那个东西已经消失了,别人也已经默认了这是我有问题。接下来的我也就只能在猛烈吐槽之后,开始寻求解决方法。

通常来说,生活中的绝大多数问题按照固定的规律模式去执行就好,但是当你身边有一些经常性、习惯性随心所欲的人,那些明明很规律的东西会变得难以直接用规律去套用。不规律的东西,整理一下还是可以用规律方法去处理的,但问题是,每个小东西都自成一格,每个环节都必须用不同的整理方式。虽然,其实只要有整理方式,就意味着可以写出判断条件。人最终能处理掉的事情就总能写出一个方案让机器去执行。有些时候,我们觉得这做不到,大概是因为我们没有那个决心,把人肉处理的很多东西一条一条写清楚写明白。连把图画抽象成梵高风格都能执行出来,有什么是不可以做到的呢?当科技发展到那个程度,人应该找什么乐子呢?对现在的人来说,必须还得用人去解决的东西,到时估计都可以用AI解决。

单位的很多事情常规操作的时候,我只需默默执行某些已经形成规律的东西,但是更多时候,我的作用是在应对一些突发、甚至是首发的各种状况的时候。那些东西说不准会在什么时候什么地点以什么形式出现。要事先做好准备肯定是不可能的,因为身边的猪队友埋下了太多的地雷。即便其他人再小心翼翼,也难免埋雷的那个人自己踩他自己栽培的地雷上面,而且这种事情屡见不鲜。于是,这就必须有好几个大脑应对这些突发的神经状况。我大概就是那几个大脑之中的其中一个,但是我又是一个脾气很大的人,所以我总会咒骂一番。临时做应对只是迫不得已的补救,更重要的是杜绝这种事情以后再次发生。我更愿意把时间和精力用在不让事情再次发生的规划上。

人过得再小心翼翼也会遇到状况。遇到状况的时候,继续保持冷静沉着,是我们要努力做到的。

2020-04
18

死磕二分法搜索

By xrspook @ 15:00:07 归类于: 扮IT

我是看着题目的中文版做题的

练习10:要检查一个单词是不是在上面这个词汇列表里,你可以使用 in 运算符,但可能会很慢,因为这个 in 运算符要从头到尾来搜索整个词汇表。我们知道这些单词是按照字母表顺序组织的,所以我们可以加速一下,用一种对折搜索(也叫做二元搜索),这个过程就和你在现实中用字典来查单词差不多。你在中间部分开始,看看这个要搜索的词汇是不是在中间位置的前面。如果在前面,就又对前半部分取中间,继续这样来找。当然了,不在前半部分,就去后半部分找了,思路是这样的。不论怎样,每次都会把搜索范围缩减到一半。如果词表包含了113809个单词,最多就是17步就能找到单词,或者能确定单词不在词汇表中。那么问题来了,写一个函数,名为 in_bisect,接收一个整理过的按照字母顺序排列的列表,以及一个目标值,在列表中查找这个值,找到了就返回索引位置,找不到就返回空。

做到死去活来词语在词汇表里有索引正确,没有时却会疯掉的时候我不得不去看答案,看到答案后傻眼了,答案对单词的判断只有True和False,再去找原题,我那个去,题目改了!不要求索引了好吗!

Exercise 10: To check whether a word is in the word list, you could use the in operator, but it would be slow because it searches through the words in order. Because the words are in alphabetical order, we can speed things up with a bisection search (also known as binary search), which is similar to what you do when you look a word up in the dictionary (the book, not the data structure). You start in the middle and check to see whether the word you are looking for comes before the word in the middle of the list. If so, you search the first half of the list the same way. Otherwise you search the second half. Either way, you cut the remaining search space in half. If the word list has 113,809 words, it will take about 17 steps to find the word or conclude that it’s not there. Write a function called in_bisect that takes a sorted list and a target value and returns True if the word is in the list and False if it’s not. Or you could read the documentation of the bisect module and use that! Solution: http://thinkpython2.com/code/inlist.py.

又纠结一番后我终于写出了一句“first > last”返回例外情况,终于,世界被拯救了!记录索引和不记录索引很不一样啊,按照参考答案的解法,i即便返回也永远是1,索引无能。纠结是有好处的,让我明白到二分法搜索有多么的高效,简直甩while循环几十条街,但如果真索引的话,估计我会很懒地直接用list.index(),虽然用之前必须用in历遍列表,判断是否存在。

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
import time
def in_bisect(library, first, last, myword): # 二分法搜索,10万数据查询最多只需不到20步
    if first > last: # 这是一句拯救了我的条件
        return None
    else:
        mid = (first + last)//2
        if myword == library[mid]:
            return mid
        elif library[mid] > myword:
            return in_bisect(library, first, mid-1, myword)
        else:
            return in_bisect(library, mid+1, last, myword)
myword = 'zoo' # input('myword is: ')
i = 0
library = []
fin = open('words.txt')
for line in fin:
    word = line.strip()
    library.append(word)
library.sort()
start = time.time()
 
# j = 0
# while i < len(library) - 1: # 我的脑洞第一反应用的循环
#     if myword == library[i]:
#         j = i
#         break
#     i += 1
# if j == 0:
#     print('myword is not in library')
# else:
#     print('index =', j)
 
# if myword in library: # 伟大列表自带的查询索引号,但先得确定单词在那里
#     i = library.index(myword, 0, len(library)-1)
# if i == 0:
#     print('myword is not in library')
# else:
#     print('index =', i)
 
if in_bisect(library, 0, len(library), myword) == None: 
    print('myword is not in library')
else:
    print('index =', in_bisect(library, 0, len(library), myword))
end = time.time()
print(end-start)
# myword is not in library while 0.07   index 0.003  bisect 0.001
# apple 4450               while 0.003  index 0.001  bisect 0.001
# zoo 113707               while 0.07   index 0.005  bisect 0.001
# while,index和bisect没有对比就没有伤害
2020-04
17

51%的概率

By xrspook @ 22:15:06 归类于: 扮IT

思路很简单,但要得出这个概率结论要重复多少次呢?脚本写好以后我先用了10次,100次,1000次,效果都不好,数字太漂浮,于是我都怀疑自己了。搜索之后发现别人用MATLAB写了个大体意思跟我一样的程序,他重复了100万次,好吧,我懂了!本来这种扔随机数进去的测试重复就应该趋向于无穷,但试过你就知道,100万次绝对让你的电脑卡到怀疑死机了。所以我狡猾地控制百分数只输出整数,10万次有点卡(我的破电脑等待结果大概需要4秒),但还可以接受。

Exercise 8:This exercise pertains to the so-called Birthday Paradox, which you can read about at http://en.wikipedia.org/wiki/Birthday_paradox. If there are 23 students in your class, what are the chances that two of you have the same birthday? You can estimate this probability by generating random samples of 23 birthdays and checking for matches. Hint: you can generate random birthdays with the randint function in the random module. You can download my solution from http://thinkpython2.com/code/birthday.py.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import random
def chance(num):
    n = 0
    x = 100000 # 越大越好,但太大了运行慢到怀疑人生…… 1W小卡,10W卡,100W非常卡!
    for i in range(x):
        birth = []
        for i in range(num):
            birth.append(random.randint(1, 365))
        birth.sort()
        for i in range(num - 1):
            if birth[i] == birth[i+1]:
                n += 1
                break
    return n/x
num = 23
print('chance is', '{:.0%}'.format(chance(num)))
# chance is 51%  x = 10W 百分数显示小数将会是个测试噩梦!!!
© 2004 - 2026 我的天 | Theme by xrspook | Power by WordPress