Professional Documents
Culture Documents
httpscodernet rumediaГрокаем
httpscodernet rumediaГрокаем
Grokking
Algorithms
An illustrated guide for programmers
and other curious people
• •
•
2017
MCL1UK06Q
32.973-018
004.421
- 2017. - 288 -
ISBN 978-5-496-02541-6
-
- -
12+ 29 201
2017
2017
Manning.
192102, 3,
- 034-2014, 58.1 t .12.000 -
12
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
... ... .. .......... . .... . . . . .... ... ... .. . .... .. ... 15
........................................ 16
................................... 16
....................... 17
.......... . ................ . ........... . ...... . ...... 17
................................................. 17
1. . . . . . . . . . . . . . . . . . . . . . 18
. .. . . ....... . ..... . .. . ... . ... . . .... . ..... . . . . . .. ... . . . 18
.......................... 19
.................................... 19
................................................. 20
....................................... 23
...... . ......... . . . .... ... .. . ... . .. ... .. .... . ..... .. 27
............................................ 28
................................................... 29
... . . .. . . . .. . .. 29
............................. 32
............ 34
..... . .............. . ............ 35
.................................................... 36
......................................... 37
..... .. .... ... ....................................... 39
2. . . . . . . . . . . . . . . . . . . . . . . . . . . 40
............................................. 41
........... . .. .. ....................... 43
..... .. .................. . ......... .. ........ 45
. .... .............. .... . .. . . . ... .... ....... .. ....... 46
................................................ 47
.................................................... 48
. ................. . .................... 49
............... .... . .. ........... . .... . .... .. ... .. .. 50
. ... . ................... ... .. .. ... .. . .. .. .. . ........ 51
............................................. 53
. .. . .... ...... .. . .... ..... ............. . . .. . .. ..... 57
..................................................... 58
3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
...................................................... 60
................................ 63
............... .......... . ... ..... . ....................... 65
................................................. 66
.................................................... 68
....................................... 69
.................................................... 73
....................... .. ........ .... .... . ..... .. .. . . 74
4. .......................... 75
... .... ... . ....... .... ... ....... . .. ... .. ... . 76
.................................................... 85
.............................................. 85
........................................... 92
......................... 93
....................................... 95
.................................................... 99
..................................................... 99
7
5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
......................................... 107
............................ 108
. .. ............ . ............. . .... . ..... 110
............................. 112
............. . ....... .. ........................... 116
........................ .. ...... ..... .... .. ...... .. .. 116
................................................ 119
...................................... 122
......................................... 124
................................................... 125
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
6. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
............... .... ........................ 128
.................... .... ........................ 131
.................... .... ........................ 132
........... .... ........................ 135
.. .................. . .. .... ........................ 136
. ....... . .... . ............ . ........ .. .............. 137
.............................................. 138
........................................... 141
.. . . . ......... . .................... ... ...... 146
................................................... 147
.................................................... 150
7. . . . . . . . . . . . . . . . . . . . . . . . . . . 151
.................................... 152
................. .. .. ... ............. .. .......... 157
.......................................... 160
..................................... 167
................................................... 170
.. .. ..... . ................... ...... ... . ............ 181
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
8. . . . . . . . . . . . . . . . . . . . . . . . . . . 182
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
8
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
.............................................. 196
- . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
. . . . . . . . . . . . . . . . . . . . 202
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
9. . . . . . . . . . . . . . 206
............................................... 206
............................................ 207
................................ 208
....................................... 217
......................... 217
. . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
...................... 220
......... ... 221
..... . ........... 221
................................. 221
............................ 223
..................................... 224
.... 225
.. 226
........ ........................................... 226
. .. . . . . . . .......................... 226
........ .. . . . . . . .......................... 228
........ .. . . . . . . .......................... 229
................. .. . . . . . . .......................... 230
....................... 232
- ............. 233
................................................... 235
.................................................... 235
10. k . . . . . . . . . . . . . . 236
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
9
.................................................. 245
............................................. 248
................................................... 249
................................. 249
OCR ..... . ....... . .. . ........ . ......... . .............. ... .. 250
............. . ...... . ................. 251
................................... 252
.................................................... 252
11. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
............... ........ .... ........................... 254
. ........ .... ........................... 258
... ........ .... ........................... 259
. ........ .... ........................... 260
MapReduce . . . . . . . . . . . . . ........ .... ........................... 261
....................... 261
map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
reduce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Hyperloglog .................................. . .. 263
.............................................. 265
Hyperloglog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
SHA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
........................................... 267
............................................ 268
.............................. 269
................................. 270
...................................... 272
....... . ...... .. ........... . ................ . .......... 273
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
-
«Visual Basic
Git,
Manning·
Manning;
• •••••••••••••••••••••••• " • ••••••••••••• " • " • " • " 1
Manning,
(Marjan (Mike
Stephens) (Bert Bates),
Qennifer
Stout) -
Manning:
(Kevin Sullivan), Piergies),
(Tiffany Taylor), (Leslie Haimes)
-
(Ben Vinegar),
(Karl Puzon), (Alex Manning),
13
CLRS',
' -
-
2),
-
Khan Academy.
15
1:1 1-
1:1 2-
5).
1:1 - -
4).
1:1 4, 8 9.
4)
9).
8).
1:1 5. -
1:1 6 7.
16
6)
7)
k 10.
4 Q» ).
leJWW.manning.com/ books/grokking-
algoritl1ms https.j/p)thub.com/ egonschiele/ grokking_ algorithms)
-
5- 10
17
Python 2.7.
www.
marming;.com/ books/grokking-algorithms https://github.com/egonschiele/
algorithms.
-
...
Etsy,
adit.io.
comp@piter.
www.piter.com
1
./ .
./
.
./
.
./
...
19
4 32 !
GPS
6, 7 8)
9)
k
20 1.
«0».
21
Facebook.
Facebook
null.
i
22 1.
1, 2, 3, 4 ""
99, 99
50.
...
1-50 75.
...
63
50 75).
24 1.
240
_
-
240
4 J
, 1 1 '
1&
25
18 -
log 2n
26 1.
Python.
- 1
binary_search
low = 0
high = len(list) - 1
LO\.J IOGH
J, J.
[· 1 • 1 . 1. J
\... .J
"('
low
LO\,J 1-HGI-\
J,
1.
high .
low high
def item): .."".::::::.·····
low = 0/// ... "..
high = len(list)-1 .....""" .
my_list = [1, 3, 5, 7, 9)
".
print 3) # => 1 ....."" """"
print -1) # => None "'· 1
"None" Python
1.1 128
28 1.
1.2
100
100 4 4
100
7 4
32
100
J. ......
100 1
1t 1t
..J,
1t 3Z.
O(Log n)
O(n)
.,.
&PEMJI &PEMJI
29
NASA.
10
...
100
1
100
100
7 (log 2 100 7),
7
30 1.
n
7
100
1
30 (log 21 30). «32 - -
15
100 100 7
30 15 = 450 10
1 1
11
- - - - . - - - - - - - - - - -- -- - --
\00 7
...
'
-------- -------
W
:1.:1.
:1.4
31
15
1
33
-
log O(log
O<.n>
..7f '\..
«0» -
32 1.
16
' Z\
-5 ",-1- ,_ -
' )\".'
• ,, ..•2-
-- ," r . ""'S -
1
,3'\ '4' ,5 .
1
16
16
16 16
33
-
4
16
' z. 3 4
J, J. .J,
4
34 1.
16 4
1 2-
O(log
0(1),
4.
35
O(log ),
Q * log
- 4).
- 2).
-
16
5
O(log ).
10 O(log 16
4 (log 16 4).
0,4 1024
log 1024 = 10 1
16 16 1024
- 1024
Pt 1
..k::=.(.L
)
lL LL L
()(.,..)
-----------
\6 6.4- 2S.&
2S6
\.1 11 1.2
\.0
36 1.
4,
1:1
1:1
1:1
1:1 O(log
1.3
1.4
1.5
1.6
4.
-
37
5
38 1.
120 13'3
5 120
5 120 6
720 720
7 5040
15
100+
-
39
10.
c:i
1:1 O(log
1:1
1:1
1:1
2
1
41
--
.
)
J
42 2.
1'
feOffeeb -
43
.a.PYrME
'\,
J,
%
44 2.
.3Ul.A
Y;f.E.
.J.
-
3 1
10
Q 10
45
PO&Kk
fi
1.3
123». 123,
847»
46 2.
1 1
Next
f!;IExr)
10
Next 1
1 Next 9
1
2, 2
3".
47
00.
r
______MJ
____
____.)_ \..._..__
,
\ \ \ ()/\-;h
02
04.
1. 20 1.
1 2 3
1
48 2.
20 1»,
20 1».
()Cn) 0(1)
Q(.n) =
0 (.1.) "' 6
2.1
1.
2..
.3.
49
0 (J
0 TPEHllPO&KA
.
.....
t""\
,,, .. .,,,
0
\ \
.'Q
' '
-0
. . , ' , "',,,,,,,,
D
, _
1.3
22
50 2.
---.... &HMJ
0(1) Otn)
Ou>
Q(.t\) 0(1)
51
0(1)
0(1).
2.2
52 2.
2.3 Facebook
Facebook,
Facebook
-
2.4
Facebook.
2.5 Facebook
26
} _ '\.
_ __..\ ""
53
4:Adit
Facebook
1 1
«Adit
4:Zakhir 26,
4:Zakhir
RADIOHEAD 156
k'l.SHOP.E k'UMAP. 141
k'EY.S 35
NEUTML Mllk' HOTE:L 94-
THI; 61
\./ILCO 111
54 2.
llOC-
11 EJl.EH
RADIOHEAD 156 RADIOHEAD 156
KUMi\P. 141
35
NEUTML MILK HOTEL 94
9>S
.S 61
\llLCO 1\1
1. MDIOHHD
&CEro 2.•
P.i\DIOHHD
& HO&t,!;_
llOC-
\
RADIOHEAD 156
l{UMi\P. 141 l{UMAP. 141
3S
MILI{
ii
55
..
!t.ADIOl-\EAD 156
KUMAR 14-1
\./ILCO 111
MILK 94-
61
KfY.S 35
1. RADIOHEAD
2.. KI.SHOP.E KUMAP.
3. KEY.S 1'\
),
56 2.
'------v--
Q(Y\)
t\
57
-
log
def selectionSort(arr):
newArr = []
for i in range(len(arr)):
smallest = indSmallest(arr)
newArr.append(arr.pop(smallest)) ero
return newArr
1:1
1:1
1:1
1:1
Q
3
./ -
.
./
4)
1:1
factorial 5,
60
4 factorial ,
'\_
61
IKE.
6
&
11.0l'Olil\.Y
11
1.
2.
3.
4.
5.
62 3.
!'\ 1'0&
&
l\kUE.TE.
1.
2. 1.
3.
while.
def look_for_key(main_box) :
pile = main_box.make_a_pile_to_look_through()
while pile is not empty:
= pile . grab_a_box()
for item in
if item.is_a_box():
pile.append(item)
elif item.is_a_key():
print "found the key!"
def look_for_key(b
for item in
if item.is_a_box():
look_for_key(item) --<·
elif item.is_a_key():
print "found the key!"
63
Stack Overlow:
u u
def countdown(i):
print i
countdow n(i-1)
p\!,\Mi i i-'
' http:j
64
Ctrl+C.
countdown:
def countdown(i):
print i
if i <= 0: ....
return
else : --<· ·· ······
countdow n(i-1)
i <•' &
1
- '
t
t
65
f.\A
IOJllO
66 3.
def greet(name):
print "hello, " + name + "!"
greet2(name)
print "getting ready to say ... "
def greet2(name):
print "how are you, " + name + "?"
def
print "ok
Python print
greet ( "maggie").
L_
67
name
"maggie";
,/
1 MAGGIE 11
greet;
greet. greet2 greet
68 3.
greet2 greet
I
"
1 MAGGIE 11
ok !
MAGGIE
greet.
greet.
3.1
NA
69
factorial(S)
5! 5! = 5*4*3*2*1.
3*2*1.
def fact(x):
if == 1:
return 1
else:
return * fact(x-1)
fact
6bl306 Fi\CT.
"!..
--- -- - - - - - - - - - - - -- -- 3
if .}( - -1:
-- - - - - - - - - - - - - - - - - - - - - -
eLse=
-- - - - - - ----- - - - - - - -- __ __ ;
l'"Ac:1'
retvrn * factC.X-1) )( 1 2
70 3.
TEKYU1MM
/ z. t- UMM - KOTOPblli.
f"c.,- 6
Ft\C.T. '/..
/ TEKYU1MM
2.
. -- - - - - - -- - -- - - - - - - - - - - -
F"ct'
1 2.
else: )<
l'"Acr 1..,
.)(' -, 3 6
- - -FA C'f' - _
)1 11 '-.
1'
faci.(x-1) FAC.1'
1 2. -
f' .... c."'f
1s
f ACJ'
)( 1 1
FACI
jf
')( ' l
fl\.C."f
'J( 1 '3
__... "------- - - - -- - -- - ---- nEP&blli.
oro,
-
'/.. 2. _ /
.retvr.._ i: f act6t-1) 6
J(\!.3_/ r
2.
71
....,
72 3.
li'PO&E.'PllTb
&
H,_UE.TE.
1t"UE.TE.
tOIOilN- '"' °"·
D
73
J:8
\
D /-
(30)(
.
1
1
'
\
I
, g_I ,'
1
"_,,,
3.2
74 3.
4
./
-
.
./ -
-
76 4.
2).
1&60
-
-
1.
2.
50
2.5
")
, . , •• , , ' ' • ' i '1'·''1 •1'
.'11: ;" 2.5
•• ' \ ••• , , ,' : ' 1 ' ; • '• ' ::
,, '' • ',' '• '" '/' •'• ,''' 't'
•'••,':•.•, •,,:•\ ', '''•'
.:, '' / ' '', \ , '' \ ; "..
25 50
25 25
78 4.
????
..._______, _..J---..--"
..... ___
<O <O 1100
640 640,
-
6lt0
ltOO
1680 640,
- 640 400.
79
640 400,
400 400
'---v--'
400
400 240 .
,•,
'.\''1\••''''
..
\ •• , ' •• 1 ... \ '· 1
' ·, ''.....• 1' \,'
' • •, , •, '•''"
'tOO
80 4.
240 160
160 80.
' / 1 1
._,..,..J
80 80
81
1.
2.
-
[2f 4 [6 /
def sum(arr):
total = 0
for in arr:
total +=
return total
1:
1
82 4.
LJ
[1] ' =
2:
- 12
:::. :12
12. sum
sum
"l
6
83
oJ
OJl.MH
.,
D \ :,12 ,_
l t '
2+ sum(CillJ) 1+ =-12
j, t
4+ sum(IO) 4-+6
J
=6.
_)'
JA6E.POIE.H
84 4.
Haskell ,
surn Haskell:
sum [] = 0 " ""." ."" .. " ." .... ...."" "" .. ..... ..... .
sum (x:xs) = + (sum xs) " ""." """"" .
-
Haskell if:
sum arr = if arr == []
then 0
else (head arr) + (sum (tail arr))
Haskell,
- Haskell.
85
4.1 sum
4.2
4.3
4.4 1?
qsort,
[ ] MACCll&
MACCll&bl
(20 \ MKCll& OJUlllM
86 4.
\ lts \
87
33
ME.Hbll!ME. 33
[ts l.s]
f
1:1
- -
33
15.
1.
2.
3.
[3? \ 10 J 1s l1]
33.
/ l <%> [ J
1!; 1 ]
89
J / 15 l 1 \
J
49 [ J
[]4>
[ili]Q/
1J
4\ <§:> l ]
90 4.
4
4
3.
5:
J 4> [ J
91
92 4.
def quicksort(array):
if len(array) < 2:
return array "
.. ...... .... .... . 1
else:
pivot = array[0] " " ...... .... " ..
less = [i for i in array[l : ] if i <• pivot]
greater " [ i for i in array [ 1 : ] i f i > pi vot] .. ..... ...
print quicksort([10, 5, 2,
C.OPTllPO&KA
C.OPTllPO&KA
PAJMEP
MAc.C.ll&A i.) O(r- • O(rt) Q(n!)
.fl.3 1 19} 4:
1&.6 MllH 2.<J )(
1910 <f't6
10
1
93
10
2.
- -
log
log
log
log
def print_items(list):
for item in list:
print item
!2 f4 / 6 1 !10]
f : 2 +6 1\6
pri"t_ite1»s2: 2 6 i 1\li
print_
i tems
print_i tems
C*h
_j"
10 * print_i tems 1 *
print_i tems2.
1 n
n
95
10
- 1
4
* 4- = ll.HJI
= $2
log
96 4.
[1 l 2 \ 3 / 4 \ 5 \ 6 \ 1 \ 1
[_ \2 / l 4 \ s j t> \ 1] g 1
..J,
PAJ '-+ [ ) [ }4 \s 16 \1- \
s/6}
J.
l1/z\3J<t> [s f 6 \
l
.J
«> L-
1 ....--l
i '\
.J.
4
(
97
-
O(log
{ [ 1 \2 \ 3 /4 15 \6 \ 1 1i 1
,J.
98 4.
t. O(n)
{
/ 1/2 j 4\ s /6 j =t \ 8/
.J.
t . O(n)
{
/1/zl3J<'t> [5 /6
../, -1'
O(n) {
<VC 1
.J.
!i\
(
\ 1 \ 2 \ 3 \ 4- \ 5 \ 6 1 1 \ z]
-- - - - - - - - - - - - -
1
6 O(n)
O(n) [i 1z 1 3 j 0 [f; 1 1 1 )
-------- V\)
O(n)
--------
n.k1-E.
[ J0 [!]
'PE.t#..SI O(n)
O(log
O(log n)»)
* O(log = log
log
99
4.5
4.6
4. 7
4.8
[2 , 3, 7, 8, 10],
2, 3,
7
log
O(log
5
./ -
./
1,
O(log
101
••• cf:
.. 1.qq $
•.. • 1-q '* .. 1.qC\$
QCn)
>1<: >1<:
<.) <.)
s
<.)
""'
<.)
..... .....
.., ..,
O(log -
10
6
Q(h) Q( Loq
j +- L0(,1100 "' 1
1
LOC,11000 • 10
1000 0
"-- LOC,110000 " 14
=Z.
-
102 5.
1)
Q(_Y\)
6 Kl\141"E.
1
1
-
1
2.4")
1
apple
-
103
O(log
1)
).
1-
4
2
t
... 1'\'\. ....
1:1
4.
4.
1:1
1
1
-
104 5.
]
4
-+. 3
«3».
3.
.J.
1 1 l
0 1 2 4
.J-
1 2. 3 4-
105
4.
A&OKUI.O =
\ / ,/
/ 1 1
4, -
106 5.
Q
5
100,
2?
Python
dict:
book - book
book
-
t>At>&
2 34'
;r +\5
Python
-
http/ /adit.io.
adit.io
At>\1". 255.
llS. 2.34\.\33
FAG660*-.<.oM.+ l-13. 2 52.. \
SC:tl.\61),C.Ot\ 23.235.
110 5.
DNS. -
-
111
>» voted = {}
get "tom"
None.
J.
6
. .L \.
1 1-\
E.rOI
\
6
112 5.
voted = {}
def check_voter(name) :
if voted.get(name):
print "kick them out!"
else :
voted[name] = True
print "let them vote!"
>» check_voter("tom")
let them vote!
»> check_voter("mike")
let them vote!
>>> check_voter("mike")
kick them out!
f acebook.com:
1. Face-
book.
113
2.
3.
f
.;
, --
/ '
-
Facebook Facebook
Facebook
Facebook
Facebook,
114 5.
Facebook,
Facebook
/' 1
C.OXPAHE.HHAJI
'\
_,, ir-
::::::::-
1
'
_
1:)
l:J Facebook
Facebook
«0
U
115
Facebook,
Ult.L
Ult.L
&
MJ
cache = {}
def get_page(url) :
if cache.get(url):
return cache [ ur l] ·· · · . " · • · ·· · ·
else:
data = get_data_from_server(url)
cache[url] = data
return data
U RL
116 5.
i:i
i:i
117
33
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 l .'> • 5 6 t & 8 10 11 1l 1.'> 1• 15 16 1& 18 lO l1 ll l.'> l5 l6 lt l& l8 .'>1 .'>l
1
+ j •8• i
J
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 l .'> • 5 6 t & 8 10 11 1l 1.'> 15 16 111 18 lO l1 ll l.'> lS l6 lt l& l8 .'>1 .'>l
l 0.61 /
..J'
( ( o3q ...
J
[ 1 1
:/'- "-.
601<.All.07
118 5.
Qt "'J..
1.4'1
/t
1.4q
D
119
Ot1) Q(n)
0(1) Q(n)
0(1> Q(n)
0(1).
1)
120 5.
()(.>\)
L Q(})
(XEOl-TA
- 1 1 -
121
"1.Elll- "1.Elll-
C.llJl-
(C.PE.a.HMP.
Q
122 5.
&
2
/
5
, 0,4.
.{,
[ \1\
• 1/
5
" 1/ 3 » -
100
100
[1.4- f . ...
123
1.
50
2.
• 31•
I 1 1 1 1 1
hash:
[ 14-/
•
3
/ .
8
124 5.
0,7.
0(1)
SHA
125
1. « 1»
2.
3.
4.
2, = 3, 5, d = 7, = 11
10, «bag»
3+2+17%10 = 22%10 = 2.
10
5.5
- Estl1er, Ben,
Dan.
5.6
5. 7 «Maus», «Fun
«Watcl1men».
126 5.
Python,
Q 0,7,
Q
).
Q
6
10
lS
6
./
- .
../ -
,,,,
,,,, -
/f' 't4
129
130 6.
1.
2.
131
132 6.
1:
2:
2:
1:
Facebook?
133
0
0
0
/ ' .
__;;J Jl.A:
\.
..., ' " .: . '
/' Jl.A:
0
. " _)
.r--
.;;r Jl.A:
134 6.
(2;
/ ' fi.A:
JI.PY.3E.K
.',"" '".
135
1:
2:
1;
2.
- -
r::3
136 6.
2.
{
-
137
& 04 04
6 04 04
FIFO LIFO
6.1
138 6.
6.2 «bat».
->
r , - - .
. '
'
- - -
139
Python:
graph = {}
graph["you"] = ["alice", "claire"]
(you)
graph[ "you"]
-
Python
Python
graph = {}
graph [ "you"] = [ "alice", "claire"]
graph("bob"] = ("anuj", "peggy"]
graph["alice"] = ["peggy"]
graph["claire"] = ["thom", "jonny"]
graph["anuj"] = []
graph["peggy"] = []
graph["thom"] = []
graph("jonny"] = []
140 6.
-
graph["claire"] = [ "thom", "jonny"]
graph["anuj"] []
graph["anuj"] []
graph["claire") = [ "thom", "jonny")
-
141
1.
.,_\ .......
J :t :J :J OJ
2..
3.
S.
&
Python
deque:
graph ( "you"]
("alice", "claire"].
person_is_seller,
def person_is_seller(name):
return name[-1] == 'm'
«m»,
143
TE.KYUlE.E.
.SE1'P.Cl-{_QUEUE
------- -- - - -
J;J ])"]
1
---------- - - - -
------ - - - - --
-- - -
1
- -- ----- ------"J J J
else :
- - - - - - - - - - - - - - -J:J-- J:X
- - ---
( ....
- - - - ------ ----
J J
-
1
------- ---------
J ] .., ::J
-- - - -- -- --------
- - - -
----------- :J "
'jiE.
146 6.
def search(name):
search_queue = deque()
search_queue += graph(name]
searched = [] ....:."
while search_queue:
person = search_queue · popleft()
if not person in searched: ....:" ... ... ..
if person_is_seller(person):
print person +" is mango seller!"
return True
else:
search_queue += graph[person]
searched.append(person) 4 · ··
return False
search("you")
person_is_seller
1) .
O(V+E) (V-
-
147
1.
2.
3.
4.
1.
2.
3.
4.
148 6.
6.3
1. 1. 1.
l. l. L.
3. 3. 3.
4. 4. 4.
6.4
-
149
"
150 6.
6.5
).
FIFO
LIFO
7
./
./
./
152 7.
153
1.
2.
3.
4.
1:
6 ::._' 1 /
6 - 2
cS
- ... 2
2
154 7.
2:
•5
2. TPE.5YE.TCJI
":/- S
6 5
7
155
1:
5
6 2.
'1
2:
5
6
5
2
6
156 7.
- -
6
157
1.
2.
3.
4.
158 7.
CJI
KOHEU
13
159
2.1
8.
6?
-
- Destroyer.
$5». - «0,
- -
$30
161
$15.
j
1
j JlO
KHllrA
KHllrA
-
-
-
1:
$0 -
162 7.
$0?
f1APK
2
6
2
6 ),
.....
J
3
163
2:
S"
z
5
flS
YJMM
- 00
1: - ($5).
2:
s
'11'/l-'l
'
fij:
.'
'1 - '1 1 1 "
..,.., .',,, \",, ,"
,"
;,t:
·1 .:
-
164 7.
5'
9$
LjJ
2S
:-
5'
SZ)
2 $21
2 S"
1
_:-35:..
'
$35.
165
$35,
166 7.
167
$7.
$7.
.,/
1
il
$7.
.t.O
.t.2.
168 7.
- $2!
as
c(L
-
: $ • , , ,
$2
s
>2'
169
mu $0
s
KHMrA
noC.TEP JZi
.... ' /
$35.
35
C.TllHKA
s
KHMrA
35
-
170 7.
5
-2
35
$35.
$33,
-< .. .... ." ... .. ."
171
'-""'
r
,..
8 2
::::r
......
1 - -
:::.t
3 6 ,...
s 2.
- 00 -
_J
(GRAJ>IO (CO.ST.S)
6,
graph = {}
graph["start"] = {}
graph["start"]["a"] =
graph["start"]["b"] = 2
ll.PYrME
graph("start"]
graph["a"] = {}
graph["a"]["fin"] 1
graph["b"] = {}
graph["b"]["a"] = 3
graph["b"]["fin"] 5
graph["fin"] = {}
173
'°'
,.. 1
....
6"_
t. 2.
f\
u.1
1. 1:.
3
" 5 '
-
\... 1
/\ 6
t!. 2
(CO.ST.S) 2
6
infinity = float("inf")
costs:
infinity = float("inf")
costs = {}
costs["a"] 6
costs["b"] 2
costs["fin"] infinity
174 7.
parents = {}
parents["a"] = "start"
parents["b"] = "start"
parents["in"] = None
proces sed = []
E.ro c.oc.EJI.E.K
COC.EJI.E.K
175
-
1 6-
2
,,\) ......, h<>de = fi11JJO\>Jei\._c.ost_V\o.k(<.ostv 4 ()()
STAf.l'
s +
"" 1
..,!...
s
-
176 7.
>
rew _c.ost :: d. +
1'
t. 2. 6 3
.,, S
<,
•5/-.
'
cost5[V1] " \')ew-cost 2
t t
5 F1N 00
177
-, -
"1 /
"
for
}
-t
l'\ev.1-c o!.t "'" c.ost
.J, .:(-tS
2
6 JI.O
=-1
5
7 7
5'
2
/1- -
/
f1N
CoST.S
F1N , .
f'ARENi5
r.:;-,
"
(051$
179
5"
2
F1tJ
' -
S'fl\f:f
f l\'l , '
f'ARENTS
find_lowest_cost_node
def ind_lowest_cost_node(costs):
lowest_cost = loat("inf")
lowest_cost_node = None
for node in costs: ....:· ··· ··· ··· ··· ···· ·
cost = costs[node]
i f cost < lowest_cost and node not in processed: •••
lowest_cost = cost ....:·· ·········· ···· ·· ...
lowest_cost_node = node
return lowest_cost_node
181
7.1
l:J
l:J
1:1
1:J
8
-
183
JI.O
8:00 10:00
10:00 11:00
10:30 11:30
11:00 1i:OO
CJ 1() 11 12
1 1 1 1 1
1.
2.
184 8.
-
10:00),
8:00 10:00
11:00 \l:OO
10:00
8:00 10:00
8:30 10:30
\0:00 11:00
\0:30 11:30
11:00 \l:OO
35
1.
2.
...
186 8.
J:\
S2f
2 (l)
35
-
\ 5
'PIOK.3AKA:
35
[
J
)
30
$3000
$3000.
$3500!
15 ( ::·:=.:·::
\".·:-.:·: • •
Z.O
lf 1
$3500
187
8.1
8.2
50
188 8.
KotJE lt>,IN,ul
K1'wo vJA,
N'l,ct>.
Kfo\IR N'V,V1
Kf'tVE (.A,AZ
•.. . ...
50
1. -
2
2.
50
189
1 MHOJE.CT&O 500
5 10, -
10
6PE.MJI
5
19) 19S2.4c
32 1 3 .6
4')(1
1.
2.
190 8.
1:1
1:1
»> set(arr)
set([l, 2, 3])
1, 2 3
[1,2,2,3,3,)] _. 60 (1,2,1)
191
stations = {}
stations["kone"] set(["id", "nv", "ut"])
stations["ktwo"] set(["wa", "id", "mt"])
stations["kthree"] = set(["or", "nv",
stations["kfour"] set(["nv", "ut"])
stations["kfive"] = set(["ca", "az"])
- -
kone
(id), (nv) (ut).
final_stations set()
192 8.
best_station:
best_station = None
states_covered = set()
for station, states_for_station in stations.items():
states_covered
for
for:
"601<.A.ll.O
f.\E
covered states_needed,
states_for _station. covered -
best_station:
best_station .
best_station
final_stations.add(best_station)
states_needed.
states_needed -= states_covered
states_needed
for
while states_needed:
best_station = None
states_covered = set()
for station, states in stations.items():
covered = states_needed & states
if len(covered) > len(states_covered):
best_station = station
states_covered = covered
states_needed -= states_covered
final_stations.add(best_station)
final_stations:
>>> print final_stations
set(['ktwo', 'kthree', 'kone', 'kfive'])
1, 2, 3
5 2, 3, 4 5.
Q(n!) 0(h'-)
5 '1. 2 ( 2 ..S
32 .1.S. (
102 i rO.JI.A ():t-
196 8.
8.3
8.4
8.5
1 6 500
1.
197
\) 'l
l'S'3
120
llJ 6 llJ
6
FedEx
50 FedEx.
198 8.
&
199
6
6
([) ®
60
roPo.a. - roPo.a. -
200 8.
roPo.a. -
& &
=
::::
&
::::
= 4 6 = 24
201
1 -----------...
----- 2
2 __., 2... * :1.
3 * __,6 MAPOIPYTO&
4 * 24
5 - 5 2."'\- $ 120
720,
5040 7 40 320 8
5! = 120. 10
10! = 3 628 800. 10
3
202 8.
-
203
WF. /
QB /
- -
1.
2.
204 8.
A&TOEiYC 44
i:J
i:J
l:J
l:J
l:J
205
8.6 20
20
8. 7 (
8.8
• • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • t
./ -
./
8.
4
207
J:t
4- 3 1
'
...
.... +
+
-.__ ......-_.-
){
1'
208 9.
3
8 4 - 16
3 4- 5
[ 1 1 1 ]
'8
m 16
MHO.f.ECT6
32
),
- (J.;J
209
1 4
KAU.oro
r 1 2 1 4
-
1 4
1 2 3 4-
210 9.
1
1 -
$1500,
1 2 4
1 2 4
•1soo
-
211
1 2 4- -
$1500 •1500
41soo •1soo
1, 2
4
1 2 4-
$1500 •1500
4\soo •1500
4 $1500.
1 2 4- TEKY\tAJI
•1soo •1soo •isoo •1500
JA .$1500
212 9.
1
1
$1500.
2 4
o\;\S"OO 'flSOO
•1soo $\SOO
$1500.
213
1 2 4
$1soo $"\soo -tlS"oo
1
....
ilsoo
2 3
$1500.
1 2 4
,flS'Ot> $1S'OO 1$'\$0() $1500
1
'
>1- ... ,,,.'
s1soo $1$0()
$1500,
$3000!
1 2 4
ofJS'Ot> $\S'OO .,.-isoo
>1- ...
1
,,,.'
".$00 s1soo $ ISO()
r r
214 9.
4
$3000.
1 2 4
$1S'OI> .$°15"00 $'\S<>O :flSO()
• ...
1 1
.J-
$1soo $1.SOO f \$00 "' l-\06AJI
'°" HTOt060E.
3
1 2
$1500.
1 z 3 4-
$\S:OO $\S'()() t1soo
•1soc.
3 $1500.
$2000.
$2000!
1 2 3 4-
$\S:OO $\SOC> $\S'()() t1soo
$1so0
,. ',,
2000·
' 1-\'
215
4
$3000.
$2000.
$2000
3 1
+ 7. ? ?
1
- )
1
1 z 3 4-
$\SoO t\SOC> $\SOC> $°\500
$11>00
.>/
"' ' f / "'
\ 20()() •
' \ '
1
$1500.
S2000 +
(
216 9.
+ $3500.
1 2 1 4
"IS"oc:> ••soo $1.roo
_.lsoo •sooo
r
$JS"OO $1S00
". .,
""''
1-'
.,.
$3500 -
C6LL(t)
= 2.. C.TOllMOC.Tb TEKYitEro +
{ OC.TA&llErOCJI
LL(!- l)(j -
217
: : 4-
IPHoNE
iPhone.
1 2 4
.... .$\S"OO
r
$\SOO $\SOO $3000
$\Soo $3SOo
r
218 9.
4
$3500.
iPhone.
1 2 4
•isoo .\SOC> .f\SoD
f'IS()b $$000
r
nooD
IPHONE
iPhone
1 $1500, iPhone $2000.
iPhone.
1 2 4
.f\SoD \flS01>
=t'IS'OO f\SOO
f'IS()O $,1000
$\Sl>l>
r
$.2.000
IPHONE
1
219
iPhone .
$\S""oO $1$'00
s-i- $3S'IX>
$\$'00 \S"OO
$i-
1 1
3 iPhone
$3500. iPhone,
3
(
$2000
IPHONE
+
$\SOO $1$'00
1\.
•asoo .f+ooo
1 1 1 1
1'
220 9.
1 1 2
$\$"()() $1500
4
6 jzj QS $3000
9.1
1 $1000.
1 2 3 4
$3000
125 s15
Rf f6 1-\
$3000
0,5
$1000.
3,5
3,5
1, 2, 3 4
3,5
1 1.5 2 2.5' 3 4
-
222 9.
.
f7
1
D..M
PIOKJAK
223
11
1 .D.HJI :f
11
1 .D.HJI 6
rME.PE.JI \ .D.E.H CJ
MYJE.11. 2. .D.HJI
11 .D.HJI
1
-
-
l.z 1 2
MYJE.li.
224 9.
2
=fw -=fw "1-w 1V>
-1• • ,_ : 'j: :
(A){f , • • • v)(I-
,
:
-t
3 4,5
225
1 +1 = 3,5
4,5.
- 1 1,5
1-\0
TPE)I.
226 9.
\ \ 1 1
'
3,5
1 -
11
9.2
6
Q 3 1
Q 1 3;
Q 2 9;
Q 2 5;
Q 1 6
Q
227
dictionary.com.
• Fl.SH
• Vl.SH
228 9.
hish.
fish vista?
hish fish?
hish vista?
hish
his fis.
\ s
1
f '
1
229
- hish fish
3: ish.
1.
2.
· 3.
-
230 9.
\ s
1 '
f
' 2
(3, 3) 2?
(3, 4)
s
F
1 ()
1
s 2
3
231
1.
5
F
5
3
2..
JHA
+1
hish vista:
v S
1 1
s 2
t
232 9.
hish? hish
fish hish vista
fish .
fosh. fish
F 5 F .s
F t
'
F ()
2
R 5 1
Hofosh
F 5
,j, .... =- ;?
FI S
f 1-1
J.. ...
F R
:: 2
233
fish f osh.
F 5
1 1
1
1 2 2
F s F
F F
2 \
2 s
2 2
C.AMAJI C.AMAJI
6 6
• 2. •
234 9.
Z..
6
+'
1:1
235
diff git
diff )?
Microsoft Word?
9.3
clues.
10 v
k
./
k .
./ .
./
./
k
237
•:S:
:21
:z:
IJ
.... r
r
r
1-
uJ r rr
:::]'
•:S:
:zs
"°
:z:
....
· . . ....
•:S:
:21
....::.. r
<.)
r
r
r rr
t:i
::r
•:S:
:21
"°
:z:
.... PAJMEP
· · · ......
238 10. k
-
.
.
,.;:
:25
u
....::.. r
r
r
1-
l.LJ
•
r r r
":::!'
.,.;:
21
...:)
w
....
· · · .... . .
r r
r r r r r
7 /'
k
239
240 10. k
2..
1•
PAJMEP: 2
2
no
..
2.
- .
4-
5
.
241
-Jc2-2J +
= -J
=V
::. 1
242 10. k
1.
Netflix.
(?, ?)
Netflix,
243
3 4 2
3 5
+
D..PAMA 4- 5 1
1 1
4- 5"
. -+ (z,2)
-
:!. -+ 4, t ,+)
1-r 1 +1-ro+I
24.
Netflix, Netflix
Netflix
245
10.1 Netflix
10.2 Netflix
Netflix,
5
246 10. k
«5>.>
2
10 10 k
5
5'
4-
4-
: s-
: 3
4,2.
k
1 5 (1 = 5=
( 1,
(1
247
1, fll(l, 1 , 1) "'
225
1 q>) = +s
' ' (o.ic4- 1 '1) :.
15?> [i( 2 1 1
.::
k
k = 4.
1 i-
6. 2
q
D. '2. '8:.-
€.. 1 1:::-
f. 5
218,75.
248 10. k
k
249
1:1
1:1
Netflix?
Netflix
k
-
-
250 10. k
OCR
OCR «Optical Character
k
1.
2.
OCR
3 ti:... KPM&AJI
OCR
k
251
Facebook,
OCR,
k
252 10. k
k
-
[J k
k
253
- k
11
./ 10
.
./
Facebook,
255
-
256 11.
Maggie.
i
' ' 1
,
Maggie David,
Maggie Manning,
Maggie!
257
O(log -
O(log -
258 11 .
(splay)
r::-r
8
1 -
111
'\
\
Aol-r
hi. l t.16
..._.....
' ....--.,,
j /-11 ,
259
the1-e.
Better
Explained
-
JPG
Shazam -
' Kalid, «An Interact ive Guide to t he Fottrie r Better Exp lained, http:// mng.
260 11.
log
).
-
1000
500
- 1
5
1
50
-
MapReduce 261
MapReduce
MapReduce -
Apache Hadoop.
SQL.
MySQL MySQL
MapReduce
Hadoop!
1
1
100
MapReduce
map reduce.
map
map
!J, j, !J,
arr2.
1000
100 map
100
reduce
reduce
map
! J, j, J,J.
reduce
Hyperloglog 263
\-1\2/l\4\s]
1S
1+2+3 +
4+5 = 15!
MapReduce
MapReduce
Hyperloglog
Reddit.
Google.
bit.ly - URL.
264 11.
.... \ ,(}:)V"\
o..A\t..io
adit.io?
adit.io
0(1).
adit.io
Hyperloglog 265
Google, Reddit
c:J
c:J
Google,
HyperLog Log
HyperLogLog. Google
Amazon
Google
266 11.
HyperLogLog
SHA
5?
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 l 4 5 6 1 11 10 11 1l 1.0 1" 15 16 111 1.0 ll ll. l.O 1. 4 1.5 1. 6 1.1 1.1\ 30 .01 .01.
, ,"
1 j
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 l 3 4 5 6 1 11 10 11 11. 1.0 14 15 16 111 lO 1.1 ll. l3 1.4 1.5 1.6 l1 1.1\ 30 31 .01.
1 0.6=1 f
.:J'
SHA 267
0(1)
SHA (Secure
Hash
SHA -
SHA
SHA
SHA
268 11.
SHA
SHA
ero
po..t:t../
Ero XE.111-KOJL
v
SHA
Gmail
Google
SHA! Google
" ,, 11/ ,.
co..13d." __.,
W
XPAHJlltMMCJI
6
269
-
SHA
...
...
SHA
Gmail,
SHA
270 11.
Simhash.
Simhash
-
Google Simhash
Simhash
Scribd
Scribd
Simhash
Simhash
1, = 2
15, 7 »,
«d,o,g».
271
«9,6,13,13, 16
= 1, =2
9 6 \3
F .S
= 2, = 3
9 6 \?.
L L R L D
272 11.
RSA.
1
5 2 2
11 20 $2,
- $3.
- 1,5
500 300
50
$2, - $1.
$1500.
-
273
-
1
1.1 128
7
1.2
1.3
O(log
1.4
1.5
1.6
4.
-
2 275
26
2
2.1
1. n
2.
3.
-
276
2.2
-
-
2.3 Facebook
Facebook,
Facebook
-
-
277
2.4
Facebook.
Adit Facebook.
2.5 Facebook
26
l - - - --
+ . ""s
". "\-
} «&•
«Adit
Facebook
1 1
278
«Adit
«Zakhir 26,
«Zakhir
-
-
Facebook?
3.1
279
• greet greet2
name = maggie;
• greet
• greet2;
• greet
3.2
4
4.1 sum
def sum(list):
i f list == [] :
return 0
return list[0] + sum(list[l:])
4.2
def count(list):
i f list == [] :
return 0
return 1 + count(list[l:])
280
4.3
def max(list):
if len(list) == 2:
return list[0] if list[0] > list[l] else list[l]
sub_max = max(list[l:])
return list[0) if list[0] > sub_max else sub_max
4.4 1?
4.5
4.6
4. 7
0(1).
4.8
[2, 3, 7, 8, 10],
2, 3,
7
281
1. « 1»
2.
3.
-
4.
2, = 3, 5, d = 7, = 11
10,
«bag» 3 + 2 + 17 % 10 = 22 % 10 = 2.
10
5.5 -
Esther, Ben, Dan.
D
282
5.6
5. 7 «Maus»,
«Watchmen» .
D
6.1
2.
6.2 «bat».
2.
283
6.3
1. n 1. n 1. .a.YW
- - -
6.4
1- 2- 3- 4-
5- 6- 7-
284
6.5
/: {jJ
- - -
7
7 .1
285
- 8; - 60; -
8
8.1
8.2 7
8.3
8.4
8.5
286
8.6 20
20
8.7
8.8
9
9.1
1 $1000.
iPhone
$4500.
9.2
6
• 3 1
• 1 3
• 2 9
• 2 5
• 1 6
287
9.3
clues.
L 5
L 1
u 2
3
10
10.1 Netflix
3, - 3,5.
3,5.
10.2 Netflix
Nett1ix ,
288
3, 4 5
(3 + 4 + 5 / 3 = 4
3 + 4 + 5 + 5 + 5 / 5 = 4,4
10.3 Nettlix
N
sqrt(N)
-
-
-
• 18121703-73-74
books@piter.com
WWW.PITER.COM
@ vk.com/piter_pu
@ instagram.com/p
SCAN IT!
11111 11111 1111 11111111111
1083672150
OZON.ru