You are on page 1of 290

Aditya Bhargava

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

ISBN 978-1617292231 © 2016 Manning All


ISBN 978-5-496-02541-6 2017

2017
2017

Manning.

192102, 3,
- 034-2014, 58.1 t .12.000 -

23.12.16. 24,510. 1700.

610033 , 122. (8332) 53-53-80, 62-10-36


e-mail:
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

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)

(Karen Bensdon), (Rob Green),


(Michael Hamrah), (Ozren Harlovic),
(Colin Hastie), (Christopher Haupt),
(Chuck Henderson), (Pawel Kozlowski),
(Amit Lamba), Morin),
(Robert Morrison), (Sankar Ramanathan),
(Sander Rossel), (Doug Sparling)
(Damien White).
Flashkit,

-
(Ben Vinegar),
(Karl Puzon), (Alex Manning),
13

(Esther Chan), (Anish Bhatt), (Michael Glass),


(Nikrad Mahdi), (Charles Lee),
Qared
(Hari Raja), (Murali Gudipati),
(Srinivas Varadan) (Gerry Brady),

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

Facebook

null.

i
22 1.

1, 2, 3, 4 ""

2 \ \ ... " f ,I)())


23

99, 99

50.

(§) )'-.-\ !S2 \ ·+3

...
1-50 75.

...

63
50 75).
24 1.

1100 1 / ---'>/ 25 1-'> [ill ---'> l±J -1I) iri :


t ' 1 '

240

_
-

240

"\--+( ___,. 1--) \1s1< /


[s'I] <- [ 1111 1<-- \z3s I 1... @!J .... ])
( \ 1 / /

4 J
, 1 1 '
1&
25

18 -

log 2n
26 1.

r----- -- ----- -- ------ --- --- --- -- -------- ------,

Python.

- 1
binary_search

low = 0
high = len(list) - 1

LO\.J IOGH

J, J.
[· 1 • 1 . 1. J
\... .J
"('

mid = (low + high) / 2 {low+high) Python


guess = list[mid] mid
27

low

i f guess < item :


low = mid + 1

LO\,J 1-HGI-\
J,
1.
high .

low high
def item): .."".::::::.·····
low = 0/// ... "..
high = len(list)-1 .....""" .

while low <= high : """ "" ··" · ".•


mid = (low + high) .•.
guess = list[mid]
if guess == item : " ." ... "
return mid
if guess > item:
high = mid - 1
else:
low = mid + 1
return None

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),

r------------------------------- - --- -- -------- ,

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' ·""


-

' ,' ' ' "


.-

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

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'\

't. HEUTP.AL MILK HOTEL


5.
STP.OKJ;;S
1-. \.JILCO

),
56 2.

\. 11.ADIOHEAD '· k'UMi\R \. .STP.OKE.S


2.. Kl.SHOP.E KUMAP. 2.. KEY.S
3. 3. NEUTP.AL MILK HOTEL
't. Mllk' 4 't.
5. 5.
6. .STll.OKE.S 6. 'WILCO
'WILC.O

'------v--
Q(Y\)

t\
57

-
log

def find Smallest(a r r):


smallest = arr[0]
smallest_index = 0
for i in range(l, len(arr)):
if arr[i] < smallest:
smallest = arr[i]
smallest_index = i
return smallest_index

def selectionSort(arr):
newArr = []
for i in range(len(arr)):
smallest = indSmallest(arr)
newArr.append(arr.pop(smallest)) ero
return newArr

print selectionSort([S, 6, 2, 10])


58 2.

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

> 3 .•. 2 .•• 1 \

def countdown(i):
print i
countdow n(i-1)

p\!,\Mi i i-'

' http:j
64

> ... 2 •.• 1 ••• 0 ••• -1 •. • -2 •••

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

r----------- ----------------------- - - --- ------,

Python print

greet ( "maggie").

L_
67

name
"maggie";

,/

1 MAGGIE 11

hello, maggie ! , greet2( "maggie" ).

how are you, maggie?,

greet;
greet. greet2 greet
68 3.

greet2 greet

getting ready to say ... ,

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,
-

HY&lllMll. tet"Ur-n f aGt(?(-1) 2.

'/.. 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) , •• " . ' ,,1' ''• 1 l'•'i


,·: ,··f'''. ,\ 1•,
1
;,,,,,, 1,,,,, ••••
., .• 1,,••••••• 1,•,,
'•(''''1.••1·1/'•· ······'''••''''''
1'1•'''' ·'•
11 1,1•'',•i,, ,, ,, '.':' r;., ·,•'1:1 6110
,,1,,,J,,,• '1/ i ,., ,, ·' ,", •,,•,, '••','.
,,,,, 1 1'. 1 ,1 1 . ' ' • J •,, ••
1/ i •); J, 1 ' ' . , , ''t •',, • , '.. 1 ••• , .,
/
' / '., '/ \ \ , \ '',, ·,· ' • ,, •• • 1,,,' ,< •.
'1
1_,•1 " / { ,• , , ' '•'•»•' "' " ,.1,,,,,,,,,.,.,•,'
1

1,, , '.,. \. , ••.• \ ,,'\'•\·,•"',


' 1 ' ' / , • , '

l••J, 1 •' •/ ••• •"' ,,' 1 ,'\·,• ••, ••, , , , , ",,


77

-
-

1.

2.

50
2.5
")
, . , •• , , ' ' • ' i '1'·''1 •1'
.'11: ;" 2.5
•• ' \ ••• , , ,' : ' 1 ' ; • '• ' ::
,, '' • ',' '• '" '/' •'• ,''' 't'
•'••,':•.•, •,,:•\ ', '''•'
.:, '' / ' '', \ , '' \ ; "..
25 50
25 25
78 4.

????

..._______, _..J---..--"
..... ___
&ltO &ltO 1100

640 640,
-

6lt0

ltOO

1680 640,
- 640 400.
79

1680 640 640 400!

640 400,

400 400
'---v--'
400

400 240 .

., ••':'' :..., ,•,}


\ .,'
• •''
• 1
....
' ' " \ ''•
'
2.'tO
•• '

,•,
'.\''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

print sum{[l, 2, 4])

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.

def quick sor t(array):


if len(array) < 2:
return array

\ lts \
87

33
ME.Hbll!ME. 33

[ts l.s]
f

1:1

- -

[ 10, 15] + [ 33] + [] = [ 10, 15, 33],

quicksort([lS, 10]) + [33) + quicksort([])


> [ 10, 15, 33] .,.."
88 4.

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.

1's,A: 2 \ i]) itSo.t([ill])


-1t
[1 (ill]
i

5:

3\2\1\4 '\ yt([])


50

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] .. ..... ...

return quicksort(less) + [pivot] + quicksort(greater)

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

from time import sleep


def print_items2(list):
for item in list:
sleep(l)
print item
94 4.

!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 = dict()

book - book

»> book["apple"] = 0.67 67

» > book [ "mil k"]


= 1. 49 ...:" · 1 49
>>> book["avocado"] = 1.49
»> print book
{'avocado' : 1.49, 'apple': 0 . 67, 'milk': 1.49}
107

>>> print book["avocado"]


1. 49 -<··

book
-

5.1 f(x) 1 -<··· ·· "1"


5.2 f(x) rand () -<··· ········ ·
5.3 f(x) next_empty _slot () -<·
5.4 f (x) len(x) " ..
108 5.

t>At>&
2 34'
;r +\5

>>> phone_book = dict()


109

Python

>» phone_book = {} " .. .. " · ··· ·· ··· · ··· phone_book =dict()

»> phone_book["jenny"] = 8675309


>>> phone_book["emergency"] = 911

»> print phone_book["jenny"] r ....


8675309 ...:· "j°EtlN'f
\\
\ <::\11
c;.eNC.Y
\.

-
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 = {}

>» value = voted.get("tom")

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

Google

384 400 Google


...

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.

(}(1) 0(.1\> Q<")

0(1) O<.n> 0Cn)

0(1) O(n) Q(.") ()<1)

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

./
- .

../ -

,,,,

,,,, -

(BFS, Breadth-First Search).


128

/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: First In, First Out

LIFO: Last In, First Out

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:

from collections import deque


search_queue = deque() -<··· ······ ··· ···· ···· ··· ····
search_queue += graph [ "you"] -<····· ···
142 6.

graph ( "you"]
("alice", "claire"].

while search_queue: ...


person = search_queue. popleft() ....:··· ·
if person_is_seller(person) : ....: ······ ···· ·

print person + " is mango seller ! " ....:··· ·· ·· ········


return True
else:
search_queue += graph[person] ....:···· ·
return False

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 "

." /.( . ...


144 6.
145

'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?

DAG (Directed Acyclic Graph).


160 7.

-
- 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["you"] = ["alice" , "claire"]


172 7.

graph["start"] = {}
graph["start"]["a"] =
graph["start"]["b"] = 2

ll.PYrME

graph("start"]

>>> print graph["start"].keys()

>>> print graph["start"]["a"]


2
>>> print graph["start"]["b"]

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

node = find _ lowest_cost_node(costs) --< """ "" "" ."


while node is not None: while
cost = costs[node]
neighbors = graph[node]
for n in neighbors . keys() : -<·· ·
new_ cos t = cost + neighbors[n]
if costs [ n] > new_cost: ...
co st s [n] = new_co s t · ...
parents [n] = node ·
processed. append (node)
node = find_lowest_cost_ node (c osts)

-
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 /

"

pat-ents (n] hode


1 1
"s'' FIN -
f AR\;NTS

for

}
-t
l'\ev.1-c o!.t "'" c.ost
.J, .:(-tS
2
6 JI.O
=-1
5

7 7

if c.\,)sts(.11) > Y'ew_c.ost:


\ J1 J>Ebll.E JI.0-
\i =f
C O STS
178 7.

5'
2

/1- -
/
f1N
CoST.S

F1N , .
f'ARENi5

r.:;-,
"

(051$
179

c()st c.osts [ V\oJe]


51

ttew-c:o&t -== cos\:. + r1el5\\bC1VS(I\]


v!
• "
..11.0
5
"
1

\f c.osts(V'I] > Y1ew_coi\:.:


"'
(. 1
180 7.

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

AHf/\. 8:30 10:30


10:00 11:00
\0:30 11:30

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

8:00 \0:00 ../


AHf/\. 8:30 \0:30

10:00 11:00 ../


10:30 \1:30

\1:00 ll:OO ..,/


185

ct: 30 1() 10: $<) 11 t1:so 12


1 1 1 1 1
' '

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

states_needed • set( [ "mt", "wa", "or", "id", "nv", "ut",


"az"] )

>>> arr = [1, 2, 2, 3, 3, 3]

»> 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:

covered = states_needed & states_for_station


if len(covered) > len(states_covered) .... ··· ·
best_station = station
states_covered = covered

covered = states_needed & states_for_station


193

"601<.A.ll.O

f.\E

>>> fruits = set(["avocado", "tomato",


>>> = set(["beets", "carrots", "tomato"])
»> fruits 1 ..... "". ····· ···
set(["avocado", "beets", "carrots", "tomato",
»> fruits &
set( [ "tomato"])
»> fruits - ···· ·········· ···
set(["avocado",
>» - frui ts
194 8.

covered = states_needed & states_for_station

covered states_needed,
states_for _station. covered -

best_station:

if len(covered) > len(states_covered):


best_station = station
states_covered = covered
195

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

... r.c .... .•. r.c . ...

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 -

1. MAKC.llMYM &CeLl (i-l][jJ)

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

$'\SOO $\sclO $:2.000

$.2.000
IPHONE
1
219

iPhone .

'f\5o0 •1soo $\.$'110 • \SoO

$\S""oO $1$'00

s-i- $3S'IX>
$\$'00 \S"OO

$i-
1 1

3 iPhone

$3500. iPhone,
3

(
$2000
IPHONE
+

3 $20001 $2000 iPhone + $2000


$4000.

•1soo $\.$'110 •151>0

$\SOO $1$'00

$\5'00 15'°00 ti- $3S'DC>

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

\}- " ......


$Jsoo $\500
1-\ 1-\
221

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

if word_a[ i]== word_b[ j]: -<·


cell[i][j] " cell[i-l][j-1] + 1 · " " ·
else:
cell[i][j] " 0

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
+'

i f word_a[i] == word_b[j]: ··· ···


cell[i][j] = cell[i-l][j-1] + 1
else: .. "" "" "" " " " ".
cell[i][j] = m x(cell[i-l][j], cell[i][j-1])

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 /'

1. z.. E.ro 3. CPE.ll.M COCE.ll.E.K


3
6CE.ro,

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 ,+)

..( . . (<-, -c.i . . (d, -d,) + (


244 10. k

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

Google OCR OCR?

k
1.
2.

OCR

1<"' 1<.PM&AJI .\-

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

>>> arrl = [1, 2, 4, 5]


>>> arr2 = map(lambda 2 * arrl)
[2, 4, 8, 10]
262 11.

!J, j, !J,

arr2 [2, 4, 8, 10) -


arrl

>>> arrl = # URL


>>> arr2 = map(download_page, arrl)

arr2.
1000
100 map
100

reduce
reduce

map

! J, j, J,J.

reduce
Hyperloglog 263

\-1\2/l\4\s]
1S

>>> arrl = [1, 2, 3, 4, 5]


>>> reduce(lambda arrl)
15

1+2+3 +
4+5 = 15!

MapReduce

MapReduce

Hyperloglog
Reddit.

Google.

bit.ly - URL.
264 11.

.... \ ,(}:)V"\

o..A\t..io

Google

adit.io?

adit.io
0(1).
adit.io
Hyperloglog 265

Google

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

''\\ello" 9> f24db ...

SHA -

SHA

SHA

'' \\ell o" 9> 2.cf 24dh ...


..
"t gq ...

r----------- - --------------------------- - -----,

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 SHA-0, SHA-1,


SHA-2 SHA-3. SHA-0 SHA-1
SHA
SHA-2 SHA-3.
bcrypt

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

+ 26), - 26), * 26),


/ 26).
4
-
26

2
2.1

1. n
2.
3.

-
276

2.2

-
-

2.3 Facebook

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 name = maggie;

• 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

5.1 f(x) = 1 .... "1"

5.2 f(x) = rand() ... . """ " .. " .. ···

5.3 f(x) = next_empty_slot() .... ..... .. .... ... ........

5.4 f(x) = len(x) """."."." .".

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

2.. 2.. 2.. n POCHYTbC.JI


3. 3. 3.
4. 4. n PMHJ!Tb .a.Yw 4.

- - -

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

You might also like