You are on page 1of 290

Aditya Bhargava

Grokking
Algorithms
An illustrated guide for programmers
and other curious people

1-6r 11 16r
. --
111
2017

.

.
.
~ .
. MCL1UK06Q
. , 1'm
.

32.973-018
004.421
.
94 .
. - .: , 2017. - 288 . : . - (
).

ISBN 978-5-496-02541-6
- ,
- , . , ,
, ,
? ,
, - . r - .

12+ ( 29 201 . 436- .)

ISBN 978-1617292231 . 2016 Manning Puications . All ights eserved .


ISBN 978-5-496-02541-6 , 2017
,
, 2017
, 2017

Manning. .

.

, , ,
. , ,

, .

, 192102, -, . (. ), 3, , . 7 .

- 034-2014, 58.1 t .12.000 -


, .

23.12.16. 70100/16. . . . . 24,510. 1700.


-06096-16.

. -
.

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


http ://www.gipp. kirov. u ; e-mail: oder@gipp.kirov.ru

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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
N- .............................................. 196
- . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
, N-? . . . . . . . . . . . . . . . . . . . . 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), - Qean-Fra:ois 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 Friedan), ( Manickavasaga),
(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 ).
: 1110 ,
.


.
- - .
,
.

.
, .
( leJWW.manning.com/ books/grokking-
algoritl1ms https.j/p)thub.com/ egonschiele/ grokking_ algorithms)
. , .

, .
-
, 5- 10 .
. - , ,
.


,
. ,
.
17

, , r .
, :

-;

, ;

, ;

// ,
.

Python 2.7.
,
. ,
.

www.
marming;.com/ books/grokking-algorithms https://github.com/egonschiele/
gokki;_algorithms.

, , , -
... !


Etsy, -
.
adit.io.


, , comp@piter.
( ~1>, ).

- www.piter.com
.
1

./ .

./ (
) .

./ ,
(-) .

./ ,
().


. ,
, .
, ,
... .
.
19

1 ,
.
4 32 !
GPS (
6, 7 8) .

(. 9)
.

.
~- .
,

: ,

!
, .
:

? - ?
.


, , ,
. :

,
,
.

, k
.
20 1.

! ,
N- , ,
.

,
.

, . .
.


,
( !).
. ,
,

:
.

, ,
0.
.
21

,
Facebook.
Facebook ,
.

. ,
~karlrnageddon~.
Facebook
,
.

.
: .

- ;
( ,
). , , ,
, .
null.

i
~r
22 1.

, .
: 1100.

, .
: ,
.

, : 1, 2, 3, 4 ""
.

t~l 2 \ ~ \ ... " f ,I)())



23

(,
). .
99, , , 99 !

, . 50.

() a~~"'O"'J.. ~-')'-.-\~~:\~-~_1~j51 !S2 \5\ +3



~I

... !
, 1-50 . : 75.

... !

' . 63 (
50 75).
24 1.

.
! ,
.

1100 3 ~ 1S / ---'>/ 25 1-'> [ill ---'> ---'> lJ -1I) ~ iri :


t ' 1 '

,
7 ,
!

, 240 . ,
?

~ : _

~ : -

240 ,
.
,
.

~t+ "\--+( J3~~ [~] ___,. )~ 1--) \1s1< / ~r.sJ~l~s~]

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

~-)~~1 4 J~~ -.;:-


, 1 1 '
1&
25

, 18 - !

log 2n , .
26 1.

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


, .
, ,
. ,
?

L---------------------------------------- -- - -- ~

, Python.
. ,
, : .
,
, .
: ,
- 1 . .
binary_search .
, .
, .
:

low = 0
high = len(list) - 1

LO\.J IOGH

J, J.
[ 1 1 . 1. J
\... .J
"('
,
'&..

{low+high) , Python
mid = (low + high) / 2 -:
mid
guess = list[mid]
27

, low
:

i f guess < item :


low = mid + 1


.

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

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

while low <= high : - """ "" " n ".


mid = (low + high) - .. n
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) < !

". : n a
print nary_search(my_list, 3) # => 1 ....."" """" . cooe-
print nary_search(my_list, -1) # => None "' 1
"None" Python "".
, n

1.1 128 ,
.
?
28 1.

1.2 , .
?

, ,
.
, .

.
? ,
. 100 ,
100 . 4 4
. ,
. .

. 100
, 7 . 4
32 . , ?
.
.

11. I
I

100 '.&
J. ......
100 1

1t 1t
'& '&

~ ..J,
1t 3Z.

O(Log n)
O(n)
.,. ~ r ...

&PEMJI &PEMJI


- 29

-
0-:1>
.
?
,

,
.
,
\
-:1>,

NASA. ,
, .

,
.
. .
, . 10 ,
; ,
. ,
... ,
.

100 .
, 1 () .
100 ,
100 . ,
7 (log 2 100 7),
7 . .

? ? ,
.
30 1.

n. OllC.K ll OllC.K
.00 7

100

1 ,
30 (log 21 30). 32 ! - . -
15 ,
100 100 , 7 . ,
30 15 = 450 , ? 10
~. . ?

, . .
1 1 ,
11 ! ,
.

~ ~

\00 6 .00 7
- - - - . - - - - - - - - - - -- -- - --
\ 6 W :1.4
...

'
-------- -------
6
:1.:1. 3~

,
.
. ,
. ,
- 31

15 , .
1 ,
33 . ,
, - ,
. -
->->.

- ,
. ,
.
,
.
- ().
, ?
- ->->
,

. ,
-
.

.
log . -? O(log ) .
->-> :

O<.n>
-, ..7f '\.. l(Qf\ .&
.

,
. -,
0 ( -
).

.
.
32 1.

,
. ,
16 .

\~1
'
Z\ ~4-
,_ -
-5 ",-1-
' ~ )\".'

~.
-,-r.~ ,, .2-
-- ," r . ""'S -
,3'\ '4' ,5 . ~


?

1
16 , .
: ~-~ .
.
16 .
?

\ ~
_\

16 , 16 .
?
- 33

2
. .

. ,
!

, .

-
! . 4
16 !

' ~.. z. ~. 3 CO~E.HMJI 4 CO~E.HMJI


J, J. .J,

4
34 1.


, 16 4 .
?
, .

: 1 () , 2-
O(log ).

,
. , (),

. ,
((,
. , -
.
()? , 0(1),
?

() .
;
. ((,- .
,
. ().
- ,
().

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



.
4.

~------------------- -- ------------- - ------ - -- - ~


- 35

-
-,
, :

O(log ), . : .
(), . : .

Q ( * log ). : (
- 4).
( 2 ). : (
- . 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 "" ~ OtL~) LL L
lL <>-!> ;~
1
PJIMO- ..k::=.(.L
& ~..,
) ()(.,..)
-----------
~ ME.JIE.HHO
\6 ~~.6 6.4- 2S.& t:.31 t
2S6 .'6 25.6 3.4 1.~ %,&'/.\d'
\.1 11 1.2 516'""
\.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.

5 ,
. :
.

120 13'3

,
. 5 120 ,
5 120 . 6
720 ( 720 ) .
7 5040 !

~ .

15


! (-) . , (!)
( u). -
.
, 100+ ,
- .
39

! ,
, ? .
.
, ,
.
;
10.
: ,
!
.

c:i .

1:1 O(log ) (),


, , .

1:1 .

1:1 .

1:1 -~.
2

~
- ,

.
1
. ,
! ,
.

, ,
.

~ .

. ?
.

.

, .
,
,
.
41


,
. .

--
~ ~

.
, .

Jl #. .30&
..& .\411, & & .
#.\
)
J
42 2.

1'

, !

, .
,
.

~: fe~ffeeb
1

feOffeeb - .
43

, ,
,

. ,
:
. ,
.
, ,

-
. ,
.
.

-
?
,
.
(
).

.n. ~
.f\ .a.PYrME
'\,
J,
1
-
OEJI.
6~

~
~
% ~
~
44 2.

, .
- !

J.. J..
.3Ul.A ..3.,
. Y;f.E. J.
.J.

~
.-
..

~
8

~/~

,
, , .
, .
,
, .

, ,
! . ,
.
,
.
- :
3 , 1 ."
. 10
, . ,
:

Q ,
. ,
.

Q 10 ,
.

, , .
.
45

kMJITb
. ..r .

J
.

~~
~
-
PO&Kk

~~
~
k;_ fi
~
.
, .


&k

1.3

. ,
: 123. 123,
: 847 . .
:

.
46 2.

.
: ,
. ,
, .
. ,
1 . 1 ,
. !
: ,
. ,
.

,
?

.
,

Next

. , ~OJI.EKCKMK f!;IExr)


. 10 ( )
Next , 1 (
).
1 , Next 9
. ,
,
.

. ,
.
, ,

. 1
2, 2
3". , .
, -
47

: ,

. .
-, .

. ,
. ,
, 00.
?

r
______ ____
~..6 MJ JITM ..6
____.)_ \..._..__
,
~,

\ \ \ ()/\-;h JI ..
'1 02

: 04.
,
.
,

i- -
, ,
- ,

i-.

, ,
1. , 20 1.

1 2 3

1 .
.
48 2.

,
.
.
.

. ,
20 1,
20 1.
, .

& / n.

& ()Cn) 0(1)

Q(.n) = ~ &
0 (.1.) "' f\. 6 .

: ()?
, . ?
?
!

2.1 , .

1. ..
2.. ~
.3. Jl
49

.
, .

. -
?

, ,
. ,
.

0 OE.JI. (J OE.JI.

0 TPEHllPO&KA
.
.....
t""\
\
,,, .. .,,,
\
'.'&~
~

0 AEllllE .'Q \('f 4~~ , _


' ' . .
, ' ,
- 0 ~ -
"',,,,,,,,
D
-.,,~,-..,.,"
#-. _._,....,:J
"'1' ""
'
AEllTllE

,' ' ' "


.-

: ?
.


&~
1.3
Ali.

22
50 2.

---.... ~ &HMJ

~
- -
..
&

~~
~
.~

,
! ,
.

, ?
,
.
.

.
, .
.

..&I ..

0(1) Otn)
& 0~) Ou>
'j.Jl.AEHE Q(.t\) 0(1)
51

, 0(1)
, .

0(1).
: ? ,
.
- , .
: .
, .

.
10- ,
9 10- . ,
;
, .
,
.
( ).

2.2 ,
. .
,
. :
,
.
52 2.


: ? (:
/, -
. ?)

2.3 . , Facebook
. -
Facebook, .
,
. Facebook ,
.
, Facebook
. -

. ,
: ?

2.4
Facebook.,
.
? ,
.
?

2.5 Facebook ,
.
: .
26 .
. ,
, .
,
, . .

} _ '\. 6111ii r 6
_ __..\_:-r_ "" ~ 1>6w.t 8

~
.&
53

, 4:Adit ~
Facebook .
1 , 1
Adit ~ . ,
4:Zakhir ~. 26,
, Z~,
, 4:Zakhir ~ .


.
? 4:0-~
, : .


, ,
: .
, ,

-~.
,

:ll &:-
-- POllJ&EJl.EHllA

RADIOHEAD 156
k'l.SHOP.E k'UMAP. 141
k' k'EY.S 35
NEUTML Mllk' HOTE:L 94-
i;Ck' ~~
THI; .S.Ok'(;.s 61
\./ILCO 111
54 2.

,
.
?

-
.
.

& llOC-
1CllCOK~
J 11 EJl.EH POJllEJl.EHK

RADIOHEAD 156 RADIOHEAD 156


Kl.SHOP.~ KUMi\P. 141
~ L K~Y.S 35 ~
NEUTML MILK HOTEL 94
9>S
~ .S.01{ ~.S 61
\llLCO 1\1
1. MDIOHHD
&CEro 2. .D.O~&JIEM
&OCPOllJ&E...EHll;__
P.i\DIOHHD
& HO&t,!;_

& llOC-
r ~
\ POICJllEJl.EHMK POJllEJl.EHK

RADIOHEAD 156
l{[.SHOP.~ l{UMi\P. 141 l{\.SHOP.~
l{UMAP. 141
~ I{ l{~Y.S 3S
N~UTP.i\L MILI{ HOT~L .\-

~I{ ii
55

, .

.. ~ :.I. &-
POMJ&EJlE.HA

!t.ADIOl-\EAD 156
Kl.Sl-\01\~ KUMAR 14-1
\./ILCO 111
N~UTRAL MILK 1-\0T~L 94-
~&
TI-\~ .STROK~.S 61
TI-\~ KfY.S 35


, . ,
() ,
. , :
.

1. RADIOHEAD
2.. KI.SHOP.E KUMAP.
3. L KEY.S 1'\
W108
't. HEUTP.AL MILK HOTEL
5.

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


, .
, (). , ,
( ), :
56 2.

\. 11.ADIOHEAD ' Kl.SHOR~ k'UMi\R \. .STP.OKE.S


2.. Kl.SHOP.E KUMAP. 2.. . KEY.S
3. TI-\~ li\Ck' k'~.S 3. NEUTP.AL MILK HOTEL
't. N~UTP.i\L Mllk' HOT~L 4 't. .
5. . 5. TI-\~ .STP.Ok'~.S
6. .STll.OKE.S 6. 'WILCO
. 'WILC.O

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

'------~~---v--.;...-----~---/
t\

( ), ( 2 ).
57

. ,
:

D ;

D ( ).

, .
- ,
( log ). !


,
:
.
:

def find Smallest(a r r):


smallest = arr[0] wr
smallest_index = 0 """"". wr
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)) n ero
return newArr

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


58 2.

1:1 .

1:1 ,
.

Q .

Q ,
.

1:1 .

1:1 .

Q ( ,
. .).
3

./ , -
, . r
.

./
. (
4)
.

,
- .
, .
, , ,
- . .
, :

1:1 .
, .

.
,
: <1, factorial 5,
60 .

4 factorial , ". . .
, .

.
.
, ,
.

,
.

, , , .

'\_ II

61

, .
- . ?
, .

.~
IKE.
6

n~ & .
OC.IOTUI

J. 11.0l'Olil\.Y
11 OTl\.l'\Tb

1. .

2. .

3. ,
.

4. , !

5. .
62 3.

!'\ 1'0& .1'1


K~IJl.I~
1'Jl..
& 1'05.

. &I . &I l\kUE.TE.


~U.. "~~. n.
"&"'1- JkKOH .1\\

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)

. :
!

&l.3& c.ouiDO"'"

p\!,\Mi i .. i - '

' http:j/stac koverlow.co/a/72694 / 139117


64 .

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

, Ctrl+C.
, ,
.
: .
.
" . .

countdown:

def countdown(i):
print i
if i <= 0: ....
return
else : --<
countdow n(i-1)

, .
:

.. i <' & POTll&llOM .&~


l'50 J& c.ouTDO\.IM .n.J\ 1
- '
J.
t
t PEKYl'C.M&HIK
AJO&f:)\K .
.
65

.
; ,
.

, .
.

, ,
-<~ ? , ,

. .
() ,
. ,
. , :
() ( . )

~ J
(6 (6
.6U &&..u .
6 ) JI)

, :

~W


...

f.\A . .:
3..
!( .-..
.3 & E.CJI
.3 . IOJllO
, YJ>rEl'bl

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 . ,
, . .
L------- ---- - ---------------------------------~

, greet ( "maggie").
.

L_ __..~
67

. name
"maggie"; .

,/

: 1 MAGGIE 11

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

TE.KYIJ1I\
6J6

.
. how are you, maggie?,
. ,
.

greet; ,
greet. greet2 greet
. -
: ,
68 3.

.
.
greet2 , greet
, .
getting ready to say ... , .

I
"

.i
!;~ 1 MAGGIE 11

.
ok ! .

: MAGGIE

greet. ,
greet. ,
, .

3.1 , :

<;~

NA
69

,
.

! ,
, . factorial(S)
5! : 5!5*4*3*2*1.
=
factorial() 3*2*1.
:

def fact(x):
if == 1:
return 1
else:
return * fact(x-1)

fact().
, .
, , fact
.

.. . 6IJ0606

fac.t(.) nEP6I~ 6bl306 Fi\CT.


~.. "!.. 6 3
--- -- - - - - - - - - - - - -- --
if .}( - -1:
-- - - - - - - - - - - - - - - - - - - - - -
eLse= ~
-- ~ __ __
-- - - - - - ----- - - - - - - ;

l'"Ac:1'
.611\
retvrn * factC.X-1)
)( 1 2
6IJ061 (G"f

~ \3
70 3.

F:- EPXHMll. && -


.li.: TEKYU1MM
/ z. t- UMM - , KOTOPblli.
. IOPOli. &J&
f"c.,- 6 ..\11.
Ft\C.T. ~ '/..
/ &. TEKYU1MM
& 2.
. -- - - - - - -- - -- - - - - - - - - - - - &J&
F"ct' ~ .&
)< 1 2.
else: l'"Acr . 1..,

.)(' -, 3 !<:" 6 6\6

- - -FA C'f' - _

)1 11 '-. .
1' r &J&
retvn -* faci.(x-1) FAC.1'
....-- 6I6
1 2. HE&OJMO~HO -
f' .... c."'f

1s
f ACJ'
)( 1 1
FACI
jf = 1:
')( ' l
fl\.C."f

'J( 1 '3
__... "------- - - - -- - -- - ---- nEP&blli. , -
\ll. J&
oro, ~ J ..; J
li. 6J6 - ,
.. &J& &J& & &
.. . .36.' &
J&I.\

~ MJ6PA\llAE 1

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

~ '/.. & 2. _/

.retvr.._ i: f act6t-1) ~
~ ~ .36\Jl 6

J(\!.3_/ r
6\6
& 2.
71

, .
, , .

.
. , :

~
6.
6

,
.

...IU1 ,
6 ~ 6I
I
....,

'---~------~~---~~---
I

.
72 3.

li'PO&E.'PllTb
,_"11.I~
ntEJIJAE.
& '!>.

EC.ll &I EC.ll &I H,_UE.TE.


1t"UE.TE.
tOIOilN- '"' " .
.3,_ .\

, ,
? :

l-6

~ l-
D


D
73

,
J:8 ~ &

-----:,;~ \
D /-
(30)(
.
1
1
'
\

){ I
.> 1, "_,,,
g_I ,'

~ !
,
. ,
- .

, :
.
,
, ,
. :

.
, .
.

3.2 , ,
. ,
.
?
74 3.

,
.


: .

:
.

,
.
4

./ .
, , ,
.

,
. ,

. -
, .

./ -
,
.
.

.
.
~ >,>,
.

.
, ,
, - ~ ~
. -
76 4.

. , .
: ,
" "?~


~: .
( 2).
.

,
~,
.
.
,
, , ,
.
-
,
~.

, , .

1&60

:.~;1 ',~ ~''''')"' 1) , " . ' ,,1' '' 1 l''i


11\1, :,,".'''(1 ~.'1;.', 1 ,: 1,,
;,,,,,, ~111,, 1,,,,, ., . ,f'''. 1,,,
1
,\ 1,

'(''''1.
J~J. 11/' '''''''''
1'1'''' .,.,,1~''" '
11 1,1'',i,, ,, ,, '.':' 1 .:~.' 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,, , '.,. \. , . \ ,,'\'\,"',
lJ, 1 ~"' ' / "' ,,' 1 ,'\, , , , , , , ",,


. , ,
.
77

-
-
.
.
6\ ..

JI.6\

?
~!
.

~ :

1. .
.

2. ,
.

~
. ,
?

. -
.

50
2.5

----~--- ")
'1'''1 1'
, . , , , ' ' ' i
~,..::.,,: .'11: ;" ~,'' 2.5
~ ' \ , , ,' : ' 1 ' ; ' ' ~: ::
,, '' ~ ',' ' '" '/' ~ ' ,''' 't'
',':., ~. ,,:\ ', ''''
.:, '' ' '', , ''
/ \ \ ; ".. ,~,

, 25 , 50 .
25 25 ,
.
78 4.

. -
~ .
.
? ,
.

..
..6
.ll...
~
.
????
~

..._______, .........-- .- _..J---..--"


..... ___
&ltO &ltO 1100

640 640,
. - . -
, .
?

6lt0

ltOO

6 U., #..
~

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

.:J! ~
fR}\OM
~
\
J6I \

, : 160 80.
, !

' / 1 1

~J J~
~ ._,..,..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])

r 1: . ,
? ,
, .
1 , .
82 4.

.30~
LJ = '&

.~ ' = . '
[1]

, .

r 2:
. ? :

- 12

:::. 2+1 :12


12. sum
. ,
!

sum :

"l ~

n '& '-
, 6'- '6 '6r
6
r n
83

J
oJ

, .

1-\ OJl.MH .3
6.30606 . , .
.36mu .. r, TllX
.. J!\.. .3k6E.POIE.HHX
J6\ \ 6.30606 YHIllll
.,
\s"'ll4\6 D \ :,12 ,_
l t '
2+ sum(CillJ) 1+ 1~ =-12
j, t
4+ sum(IO) 4-+6 =1
J
.306 \

Su(@) =6.
_)'
~
1.6 6.306 ,

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
v.~ (20 \ ~ MKCll& OJUlllM
86 4.

, ,
. -
:

def quick sor t(array):


if len(array) < 2:
return array

.
.

~ ~66~ .6~ .
&
&r, )(

\ lts (1 \
: .
, ,
. :
, .

, , .
, .

, , ,
.
87

, , /. 33
ME.Hbll!ME. 33 ( ~&)

[ts \1-\ ~ l.s]


f
I
..

. :

1:1 , ;

, .

-
. uiu ,
.

,
~ - -
.
[ 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.

/ 1!; 1 ] l <%> [ J
. ,
:
.
89

J 1 / 15 l 1 \
J
49 [ J
~@
, .
,
. ? ,
.

[]~ ll2)s\4]
[]4> Lls\4]

[ili]Q/ ~
[\2\ 1J ~
r)2j1j 4 \ <:> l ]
90 4.

4 . ,
, 4 ,
! ,

.

, , 3.
.

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


-1t
[1 }2]~ (ill]
i

,
. ,
5:

~So"i: ~ 3\2\1\4 '\ yt([])


50

~
[1\2\\4 J 4> [ J
~
91

, .
, .
. .
92 4.

def quicksort(array):
if len(array) < 2:
return array "
.. ...... .... .... . 6a1o1iii CJ1yai: 1
""
else:
pivot = array[0] " " ...... .... " .. J1
less = [i for i in array[l : ] if i < pivot] ..
,

greater " [ i for i in array [ 1 : ] i f i > pi vot] .. .. ..... ... ,


nw

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

print quicksort([10, 5, 2, ])

,
.
,
~- .

SllHAPHA SC.TPAJI
PllMEP C.OPTllPO&KA ~U -
OllC.K OllC.K C.OPTllPO&KA
ArOPllTMA: & MllllOJl~EPE

PAJMEP
MAc.C.ll&A
l=~lLLLL
O(l~ i.) 0~) O(r- ~h) O(rt) Q(n!)

1~ .fl.3 . 1 . 3. 19}
4: Z..
19) (1. 1~ . 6~- . 1&.6 MllH 2.<J )( 1\~.
10~~ 1. 1910 . <f't6 2":#.~ 1.Z:fy,!o~sE'

. r , nJ1r 10


1 . ,
- 93

, . ,
10
.

.
, 2.
( 2 ), .

- -
( log ). !
.
( 2 ).

! ,
( 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.

r
1 . ,
:

!2 f4 / 6 1~ !10]
~

f .,..;~te.l'ls : 2 +6 < 1\6


pri"t_ite1s2: <.3> 2 <.3> 4<.3> 6 <.3> i <.3> 1\li

,
(). , ? , print_
i tems ,
. , ,
-~, print_i tems
. -~ (, ()),
:

C*h
6~ _j"
~

-
. . ,
10 WUL * print_i tems 1 *
print_i tems2.

,
-~, .
. ,
.

1 .* Lo~ n
nl'OCTO~ l'Hbl~
- 95

: ~! 10
, - 1 . !~
, 4
. :

f\POCTO~ 1 * 4- 1l. = 4~3 ll.HJI

\ = $2 .1l.\

, .
.

.
- .
, , ,
( log ),
.
,
.

:
?


. , ,
.
, ,
.
96 4.

[1 l 2 \ 3 / 4 \ 5 \ 6 \ 1 \ ~1

[_ J \ 2 / l 4 \ s j t> \ 1] g 1
.& -.?'
..J,
r KAUI~ PAJ
&IllPAETCJI -
'-+ [ )< [ }4 \s 16 \1- \ ~
6I~
O\4AJI 6I
&l.30&0&:

: .
,
. ,
. ,
.

l POJMEPtT
l1/2l\4\ s/6} ~Jgf
J.
l1/z\3J<t> [s f 6 \=1}~) \
&.30&0&:
~ 4
~
.J
> L-
1 ....--l
.J.
i '\

( J<$>
- 97

! ,
.
, .

,
- . ().
O(log ).

.
, .
,

().
. ()
!

)(
.~-'!)(
{ [ 1 \2 \ 3 /4 15 \6 \ 1 1i 1
,J.

,
( ) .
98 4.

~ /\..6,
. t. O(n) .
{
/ 1/2 j \ 4\ s /6 j =t \ 8/
.6
.J.
~ /\..6, {
. t . O(n) .
.6
/1/zl3J<'t> [5 /6 \1\~
../, -1'
~ O(n)
/\..6
{
<V <VC 1
.J.
!i\
( J<V
, ().

~J..1\..
06 .6~

/\.~60
\ 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~ ) .\'.:
--------

J
O(Lo V\)
PE.MJI O(n)
0\IJ
--------
n.k1-E.
'&. .~ [ J0 [!]
'PE.t#..SI O(n)

O(log ) (
O(log n)) .
(),
() * O(log ) = ( log ). .

() ,
()* () = ( 2 ) .

: .
,
( log ).
,
.
99

- ?

4.5 .

4.6 .

4. 7 .

4.8 . ,
[2 , 3, 7, 8, 10],
2, 3,
7 . .


,
, ,

,
.
( log )!

- .
.


, O(log ) ()
.
5 -

./ - -
. -
;
.

./ -:
, -.
,

,
. ,
.
,
.
1,
. ,
? ().
, _1::=.~~
,
O(log ).
- 101

i 2.4$ ~q cf:
.. 1.qq $ 2.4$
I .. 1-q '* .. 1.qC\$
.6I .6I
.. ..

Ollo'3~ QCn)

>1<: >1<:
<.) <.)
s

<.)
""'

<.)

~ ~
..... .....
..,
~

..,
~

1.. 1..

, () O(log ) -
! , 10
. ,
.


.3.
6
Q(h) Q( Loq )
. 6
10 1. j . +- L0(, 100 "' 1
1

100 1 . 1. ~ . 6
LOC,11000 10
1000 0 . 2
"-- LOC,110000 " 14 .
=Z.

, .
- ,
. ,
. ,
.
: , .
102 5. -

( 1)
, . ,
.

. .
rr

!/\14 4..60 1
"f\..1\6
Q(_Y\) D(Lo~--)
6 Kl\141"E.

1~
1 r&.
10~
1.
1 r&.
1 QI
- 1 r&.
1 . 6

, ! ?

. :
. ( ,
.) ,

(., 2.4") ~,1.+<1) Q, 0.1~)

1
apple , ,
<<>> . - w.t . ..
- 103

:
. ,
.
, O(log ). ,
( 1) ( ,
1> ). -.

-
- , 1
:

~. ~ ~ 1-
/)&. ~~) 4
_.~) 2
t '1-W-~
... 1'\'\. ....

, -
1>. ,
. -
:

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 1

- , ,
! , :

- .
, ,
. ,
,

, .

- .
4, - .
,
.
106 5. -

Q -
. , 5 ,
- 100,
.

: ! -
, , -.
- ,
.
, - .
-.

, -
, .
: , , ,
. - !
2?
. -
,
.

, -
.
. Python - ; .
- dict:

~~
>>> book = dict()

flYCJI
.-

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

, -
.Q
- .

, -
,

.
,
,
-!

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>& ~ 5~\ ,,~ . \2.


1..&~ MA~N\NC,,.. ~ 4-<+ 2 34' 4-~~
;r AN~ ~.,N ~ +\5 s' ~'\

, .
.
:

,
;

, .

-! -
, :

, ;

Q .

, -, .
-:

>>> phone_book = dict()


109

, Python
: :

> phone_book = {} " .. .. " , phone_book =dict()

> phone_book["jenny"] = 8675309


>>> phone_book["emergency"] = 911

! ,
Ueny). :
~
> print phone_book["jenny"] r .... ~---
8675309 ...: "jEtlN'f
~:~539 \\
, \ i;~R
c;.eNC.Y
<::\11
.
\. ~
1-.-
? -
.. r

.

-
. , , - -
,http/ /adit.io.
adit.io I- .

At>\1". \ ~ \?. 255. 24~.ss

- I- :

c-t0oc;.u:.c.o~ ':f~. llS. 2.34\.\33


FAG660*-.<.oM.+ l-13. 2 52.. \ 2.~.'
SC:tl.\61),C.Ot\ ~ 23.235. 4':1-.1~5
110 5. -

I-?
! DNS. - -
.

,
. ,
11. r
. , &
? ,
,

, , -
!
. ,
.
111

, - ,
, .
: !

> voted = {}

- , ,
:

> value = voted.get("tom")

get , "tom"
. None.
, !

.3

r
J.
,

6-
6
. .L \.
..: 1 1-\ :

&r
r&-
E.rOI
>

~
\
6
.3-
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. -.

-
6

f
.;

~-t--

,

/ '

, Facebook
, . ,
, .
, - . :
Facebook ? , Facebook
,
. Facebook ,
. - Facebook
, ?

, ,
: ?,
?, ?
Google . .
, :
? ,
384 400 . Google
... .
: ,
.

Facebook, , ,
. , afacebook.com,
, .
114 5. -

Facebook, .
. Facebook
: ,
~.
. ,
, .

g~$
XO.Jl
.
6. ~
/' 1

~
C.OXPAHE.HHAJI
6.-.

'\ ..
6.


_,, . ~- ir-
::::::::-

1
'
_

.-.

.
:

1:) - , ,
.
, .
;

l:J Facebook .

- .
. !

Facebook .
0 ~, ~ . ,
U RL-apeca .
115

tQ.(.e'oo'f:_.U.'ftl./o.bt:J.Jt ~ D. . ~ F.

f o.c.~oo~.<:.ot'I\ ~ D. JI.OMAlllHE.~ .I

Facebook, ,
.

~. Ult.L
. F.

Ult.L
..6
& I?

\-\:
D..: .& &I
&.I.. .
I MJ ' 50

cache = {}
def get_page(url) :
if cache.get(url):
return cache [ ur l] <{" . " w
else:
data = get_data_from_server(url)
cache[url] = data <{ " "."....... w
return data

, U RL
. , , .
URL-apec ,
116 5. -

(
).

i:i ;

i:i ;

/
.

, -.
, ,
-.
- !
, , .
-.

, . ,
.

I
&..
JI
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

- :
.

l(
l(-

~ &
u
l(- l(IO-

" & &
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' .

.
.

( .1 ( o3q ...
J ~

! .
.

[ .-r 1 .:~ 1
? :/'- "-.
601<.All.07
118 5. -

! ! ?
: .
: ,
. - ,
, ! -
, - .
.
: ,
.

--~
.- Qt
.
"'J..
-+---7t60... 1.4'1

/t
.
6

,
, .
, - .
, .
,
. , -
. ,
,
.

. 61 ~ 1.4q

( . .
. MCOJYIOTCJI
D
119

! - , .
!
- . ,
.
.

- . -,
, .
- ;

, -
.
-!

- . -
. -?
!

. ,
. , -
.

... '1..Y.ll.lllK

1

Ot1) Q(n)

6~ 0(1) Q(n)

..-

0(1> Q(n)

.ll..& .
)ll-

- 0(1).
( 1) .
120 5. -

. , ;
-.
, , .

()(.>\)
f\ &.
( . .)

- :

Q(lo~n)
r. &.
( .)

- .

L Q(})
n. &.
(XEOl-TA )

? ,
- - 1 1 -
.
:
121

. . -
.

- ()
( ), . -
.

"1.Elll- "1.Elll-
AliMI liI C.llJl-
(C.PE.a.HMP. ()(J. .- JAHHIE
.R) .R) C.MllI .

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


6
0(1) O<.n> 0Cn) (1)
Y.Il.AE.-
.
0(1) O(n) Q(.") ()<1)

-. -
( ) .
, . ,
! -
,
-.
. :

Q ;

Q - .

. ,
-,
. ,
-.
-, ,
.
-.
122 5. -

k'.& &
& .W-.
-
\4.. .&
.
..&

- ,

. , -
2
/
5
, 0,4.

~AHJITE. .
.{, ~

[ \1\ \\
<!>.
JAOHEHMJI 1/
5

, ?

[ \1\
'11.
JAOHE.HMJI

" 1/ 3 - .
- .

, - 100
100 .
.

. .
.&
~

[1.4- f .(),' ~ . . ...


~
123

- 1. -
50 ?
2.
, !
1 ,
.

-
, . ,
.

[41f1f
~
'q).
J. 31

- .
.
.

I 1 1 1 1 1

-
hash:

[ 14-/ (1/l
'q). .3. 3
1~

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

Q . -
.

Q - ,
.

Q -
.

Q 0,7,
- .

Q - (,
- ).

Q - .

6 JI l~
10 JI

lS JI
6

./
- .

../ - ,

?

,,,, ,
.

,,,, -
, .

. , .
, .
(BFS, Breadth-First Search).

.
! , :

,
;
128 .

(
, ,
-> - );

, , .
-
.

, -
- .
. :

"6 /f' 't4


129

?
, .

; .
?

, ,
. ?
130 6.

! . ,
- ,
.

, ,
( ). ,
.
. :
, ( )
. .
.

- ,
:

1. .

2. .

, .
.
? 131

?
.
,

. ,

:

, . .
.

J.
J.
~
132 6.

! .
. .
. ,
, .
.

.
, .

1 :
. ,
.
:

1: ?

2: ?

,
- . 2:
?
1: ?

, . ,
. ,
Facebook?
.
133

.
0
.
0
0
, .
/ ' .

__;;J Jl.A: J&.



~ .
? \. .
..., ' " .: . '

/' Jl.A: J&.

.
? ~ .

.
0
"

_)
.r--
. .;;r Jl.A: J&.
~ ?
~ .: .3 J.
. .
134 6.

, .
.

(2;

, - ,
.

/ ' fi.A: .&.

~ ~ .... ~ ~.:
? &.. JI.PY.3E.K
\
.',"" '".

. JI.&
.

,
. :
. , .
, ,
. . ,
- .
.
135


,
:

1: ? (
?)

2: ? (
?)

, 1;
2. ?
, - , -
.

r::3

,
. .
,

, ,
.
! .
, .
: , ? :
136 6.

, - .
, .

:
.


( ).

{

1 - '
, ,
&
.
~
2. - nr , .
&
{
.n... :
,

. ,
,
. , ,
, ?
, - .
, . ,
.
, .

,
.
,

.
,
;.

. -
:

.
:

.
137

n.&~ & 04 ... .364 . .3 04 ..


. 0bl6- . J&.~.
JIE.C.JI 6 04 ... .3 04 ..

, ,
, .
! ,
, .

FIFO: First In, First Out


( , ).
LIFO: Last In, First Out ( , ).

1)!)1~1;;1
FIFO LIFO
(& &, (.... &,
.& &.) .& &.)

, , ,
!

,
.
k'.

6.1
.
138 6.

6.2 bat.

1-\


.
.
.
- > ? ,
,
: -!

: -
.
.


/:-..
r ,

- - .
.

'
'
- - -
139

Python:

graph = {}
graph["you"] = ["alice", "", "claire"]

: ~ (you) .
, graph[ "you"]
.

- ,
Python . 1,
?

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

. : , . -
, -~
.

, , .
, .
-
. , ,
. ,
. ,
.

. r .6. r
141


, .

1. .J". E.PE.JI.
1~~-_._.,_\-0-.......
,-k'--....,, . _ PO&E.PJIE.-
J ~ :t :J :J OJ MJI. .1\

2.. J&. J ....


..JI. .&.

3. &., Jl&JIE.C.JI
.& . . &

S. L!\

. . E.PE.JI. ,
& &.~ . .
.6U6

. 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. .!I.
.SE1'P.Cl-{_QUEUE


------- -- - - -
[l 1\
J;J ])"]
---------- - - - -

------ - - - - --

-- - -
[ J 1
- -- "J J J
----- ------
else : [J> \\
- - - - - - - - - - - - - - -J:J-- J:X
- - ---
( ]
.... J J
\~

- - - - - ------ ----
1 \ lnE.rr\
------- J ]
---------
.., ::J

~ ~.--&-~,.-!n;-;__,!
-- - - -- -- --------
/~~\n;r~\
- - - -
~: \!.\
:J "
----------- ~]Jn5~j~~
." /.( . . ...

. , :

( ).
144 6.

: . ,
:
.
.

, 6~
....

.D..~A..D..b\l

, ,
. , , .
, ,

, . ,
:

L1 .

. ,
.
145

. ,
.

. 1,
.

, ,
.
.

'jiE.
1'0&.1'.I:
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

7 . , ,
? :

1. ( ,
).

2. ( ,
).

3. , .

4. .

1: .
, : .
, ?

6 ::._' 1 /

6 , - 2 . -
, .

EPE)(OJZ.A
- J J
, , ( cS
.) - ... 2
2 . ()
154 7.

2: , ,
.

J PE.MJI
5I ..-


52.
5JI ..
J , ..
TPE.5YE.TCJI
":/- &.. S

, !
6 .

, ,
5 !

, .
:

( 6 5 ).
(
7 ).
155

111r3:!

r 1: ,
. ,
.

J .

5
6 2.
'1

r 2: .

6 !
(
) . :

, 2 .

, 5 .
, 6 .

J. .
5
2

6
156 7.

- -
. , .

,
,


.
. ,
(),
.

J&.. 1-\.&J&..
( 6 )
157

: :

1. ( ,
).

2. , ,
, .

3. , .

4. ( !).

.
.

,
, .

.
.

J6I HE6J6ElllEHHIK
158 7.


.
. :

!!\ n.
\4!JI
.@,
~ &E.PHYb
CJI .@

, ,
, . ,
, .

~ KOHEU

? ,
:

14!
&.:

13
159

, .
, .

\4
6.:

2.1

, ,
8. ,
.

,
6?

~ ---8
~&li\ ~.&li\

,
. !

.
,
DAG (Directed Acyclic Graph).
160 7.


,
! .
.

,
-
. - Destroyer.

$5. - 0, ,
, - . -
.

. ,
.
:

~~- r

- , .
. ,
$30
161

$15. ,
? !
, .
,
.

.3 ..

5

~

<)

j
JlOXOJl
()) JlO .36
r
j
1

, .
. (
.)

.
.

.3 . Jl.

KHllrA

.. KHllrA


-

-

-

, .
.

1: .
$0 - .
? ,
. ,
162 7.

$0? ,
. : , .
, ,
.
: , .

f1APK

, 2 .
, 6 . ,
2 ?
, , ,
2 . ,
? , .



J.
6 ), ~
!~~ .....



J

3

:
.
!

.
.
163

2: , ,
().

1~~~ r..~ ... J.


r S"
l{HrA .
r : ~9~:.~ l!W-'!IS.~
:np~1p': ~'.ss~-
- <)

- .
,

. , , -,
; .


. J.

- r 5

' YJMM
..
J "
z r


flS
!J
s

- 00

1: - ($5).

2: .


. J.

l{HrA s
r
l{HrA
~~.:.t~.~ '11'/l-'l
'
fij:
.'
..,.., '1 - '1 1 1 "

.',,, \",, ,"


,"
:n: ;,t: ~s.:
1 .: - >()

t'~..t
164 7.

, ! ,
, .
,
.

- -.
.


.3

r 5'
r 9$
LjJ
2S
:-'4-.~ :-

, -
. , .
, - .


.3

r 5'
r SZ)
2 $21
r
2 S"
_:-35:..
1 ~ '

, ,
. ,
$35.
165

, , .
, $35,
? .

.. J.

r .

.!'
166 7.

, . ,
.
, .

~
r ~

!(

n.

, , :

~@
n


,
. , ,
:
- .
.
!
167

,

$7. ,
$7.
?

( . .
.,/ ., .
1 OME.HJIET .&
r il
.

, , !
, $7.
.

.
.
.
,
,
.t.O
.t.2.
168 7.

, - $2!
, , .
.

. as
c(L !>
O\JlAJI . ~~~ -
O\J1AJI .. : $ 5 .6 , , ,

$2 , ,
?

? ,
. .
,
. . ,
, .
.

s
>2'
~

..


. .
, , -
169

mu , $0 ( , !)
, .


n s
KHMrA
noC.TEP JZi
.... ' /

-s:
~ .
.

, $35.
, .

35


-
C.TllHKA
s
KHMrA

6 35
.

~ , .
- , -
170 7.

.
! ,
. .

n-
~ 5
-2
~~
35
r.

, $35. ,
, $33,
. , ,
, .
,
. , ,
, .
, ,
,
- .
, .

, .
, .
-< .. .... ." ... .. ."

171

- .

'-""'
r
,.. ~
I
8 2


::::r
......
:
:>
1 - -
:::.t
3 6 ,... ~

s 2. ~

- 00 -
~ _J

J>
(GRAJ>IO (CO.ST.S) (J>AR~t.S)

-
. . 6,
- :

graph = {}

-:

graph["you"] = ["alice" , "", "claire"]

,
. , , .
172 7.

?
-?

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

-
.~ ll.PYrME
-I

, 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:. I

3 i-~
" 5
~.-
'

-
\... 1

~ -
.
/\ 6
t!. 2 ,
. () .
,

(CO.ST.S) 2 . ,
6 (, ,
). ,
. , .
Pyton? , ~:

infinity = float("inf")

costs:

infinity = float("inf")
costs = {}
costs["a"] 6
costs["b"] 2
costs["fin"] infinity
174 7.

{\
r

-
.
(.~.S)

- :

parents = {}
parents["a"] = "start"
parents["b"] = "start"
parents["in"] = None

,
, :

proces sed = []

. .

n OCIOTCJI
. J

JJIb J.,
~

6
E.ro c.oc.EJI.E.K


- COC.EJI.E.K
6,
6 .

J.

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) -:"

Pytho!
,

-
.. 1 6-
,,\) ......, h<>de = fi11JJO\>Jei\._c.ost_V\o.k(<.ostv 4 2
~
1/<. ()()
i~'t,.I\ "~--

STAf.l'


~
s +
"" 1
~ ..,!...
s
~ tj -
~
176 7.

, ,
. ,
>
> ( > ).

rew _c.ost :: <> d. + riei~\,bors-(."]


1' ~
&, JI
. t. 2. 6 . : 3

:.-< ;z
<,
~ .,,

S

! .

~ 5/-.
'
cost5[V1] " \')ew-cost 2
t t
"" 5 F1N 00

coss
177

, .

._ -, -
"1

"

pat-ents (n] ~ hode s~"1"


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

. for
.

,
?

"ei hovs(nJ
5
}
-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:


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

5'
2
~: /
f1N /1- -
CoST.S

s!t"

F1N :/-
, .
f'ARENi5

, .
.

fYoc.esseJ. f~'tid(n~) r.:;-,


" ~f .: ~

(051$
179

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


51
hei~~'oYs "'~'f~'n(Y\oJ~
t
~
: .

7 .
, ?

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


v! ~
.. .. "
..11.0 .f
E.PE.'1-0..IIJ "
.: 5 .3.: 1

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


"'
. .
. )(..11.
~
..
..11.

(. os-s ..3 :
.3/\: 1
180 7.

! .

5"
2

F1tJ ~6-:.
' -
Cos:s

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

~ , ,
(N-
).

~
(
).

~ ,

N- .

~ -
.
183


, ,
.
.

EJL . JI.O

.. 8:00 10:00

r. 8:~0 10:~0

- 10:00 11:00
- 10:30 11:30
11:00 1i:OO

,
.

CJ <i:6 1() 11 12
1 1 1 1 1

r J

. ,
?

, ?
. :

1. , . ,
.

2. ,
. ,
. .
184 8.

- !
. ( 10:00),
.

. 8:00 10:00

AHf/\. 8:30 10:30


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

MYJIKA 11:00 \l:OO

, 10:00
.

. 8:00 10:00

r. 8:30 10:30
- \0:00 11:00

- \0:30 11:30

J 11:00 \l:OO

- ,
. , ,
.

. 8:00 \0:00 ../


AHf/\. 8:30 \0:30
- 10:00 11:00 ../
- 10:30 \1:30

J \1:00 ll:OO ..,/


185

, .

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


1 1 1 1 1
& '
J '

, .
, , .
: ! :
.
, .
: -
, - .
, ,
!

, .
! .


, .
, ,

.
: 35 .


,
.
?

1. , .

2. ,
... .
186 8.

! , .

~

J:\
~6

$3
~l
fl) 8 $15 S2f
2 (l) 8
158

35 .
- , .
.


\ 5 & .3IJI

'PIOK.3AKA:
35 8
[
J
)
30 8:
r

06\4 :
$3000

$3000. -!
, $3500!

15 8: ( ~-: :::=.::: ~ ~
r \".:-.::

&: f ~~~
Z.O
l 1 to~ .~
6\4 :
$3500
187

, . ,
. ,
. , ,
. ~ .

: -
. ,
.
, ,
.

8.1
. .
,
. ,
?
. ?

8.2 ,
.
( )
.
( ) ?
. ?

,
.


,
50 . ,
.
,
. .
188 8.

- .n..
. 6

KotJE lt>,IN,ul

K1'wo vJA, \,~1

k'H~I:. ., N'l,ct>.

Kfo\IR N'V,V1

Kf'tVE (.A,AZ

.. / . . ...

,
.

, 50
? , ? ,
. :

1. -
.. . 2
.

2. ,
50 .
189

J.& 1 J.&
MHOJE.CT&O 500

... . . ... ... . . ."

,
.
0(2 ) . , 5 10, - .
,
. , 10
.

,
. ! ?

k'.& ~ E.OXOJI.llMOE.
u~ 6PE.MJI

5 .2
19) 19S2.4c
32 1 3 .6 ro.a.~
1~ 4')(1~ ro.a.~

!
, , :

1. , ,
.
, , .

2. , , .
190 8.

.
,
. :

1:1 ;

1:1 .

,
, ,
. (2) ,
- .

, .

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,)] _. .3U 60 ~ (1,2,1)


.60
..60
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 ,
, 1 . 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

, .

n..

06014
193

I, I
I, 1<.I
6U
6U
060\4
~ 060\4

"601<.A.ll.O
6

n..

...

I, I 6U
, f.\E 0601J1

,
( - ).

1
, .

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


>>> vegetaes = set(["beets", "carrots", "tomato"])
> fruits 1 vegetaes ~..... "".
set(["avocado", "beets", "carrots", "tomato", ""])
> fruits & vegetales ~
set( [ "tomato"])
> fruits - vegetales ~-
set(["avocado", ""])
> vegetaes - frui ts ~- , n n?
194 8.

D , ;

-
, .

covered = states_needed & states_for_station

covered , states_needed,
states_for _station. , covered - ,
, u!
, ,
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'-)
.& ..II.
r r

5 '1. 2 ( 2 ..S
1 12 . 4 1~
32 .1.S. ~ r 12..4 (
1 4 10 i
2
rO.JI.A 1. ():t-
196 8.

,
.

8.3 .

8.4 .

8.5 .

N-

~.60 1 ~.60 6 ~.60 500

... r.c ..... .. r.c . . ...

, 1.
.
N- 197

,
. ,
.

\)
'l ~.
l'S'3
120

. , .
.

llJ 6 - llJ -
6

:
, ? ,
. -
.

- . ,
FedEx
. 50 FedEx.
198 8.

,
. ?
,

, .

.
, .

= .

.
?

, .


& .:

- -
N- 199

: ,
.

1-.
6 : I
. 6 .: .

~~
. @)~
~

-
- -

1-. 6
:

([) . .

~
-
'-

, = .

- . ,
.

60 :

6 roPo.a. - .: 6 roPo.a. - :

."~~<h
~
.:~;<.

. .
200 8.

I~ roPo.a. - -:

~
~

, . !
, ,
!
, ! .
, .
. ,
. , ,
.
.

I
& : & -:

&0.3MOil'HX =
&
&0.3MOil'HX ::::
&


& :

&.3.I ::::
&

,
= 4 6 = 24 .
N- 201

? , ,
.

.
..&

1 1 -----------...
----- ~ ..!l U " 2
2 __., 2... I. .. *:1. MAPlllPY ~
~
~ ~ 3 I. OPO.ll.A * ';I::: __,6 MAPOIPYTO&
4 ~ , I. OPO.ll.A * ~ 24
5 - 5 \)1. ...0&* 2."'\- MAPDIPYA $ 120 MAPDIPYO&

? 720,
? , . 5040 7 , 40 320 8 .
(,
?) , 5! = 120. , 10 .
? 10! = 3 628 800. 10
3 w~ .
, !
~

- :
/
. N-.
202 8.

N-:
.
- . ,
.

, N-?


.
: ,

, ,

. .
,
.
N - 203

r I

WF. / r

. l. QB / '1- r

, ,
. , - , -
!

1. ,
.

2. , (
).

N - . ,
, N-.

. ,
N - , .
N- . ,
204 8.

. ,
.

A&TOEiYC R 44

, ,
, N-.
, , ,
, N-. :

Q ,
;

i:J ~ ~ N-
;

i:J ,
?
N-;

l:J (,
, )
, N-;

l:J (,
) ,
N-;

l:J
?
N-.
205

8.6 20 .
, 20 .
N-?

8. 7 (
,
). N-?

8.8 ,
.
,
. N-?

,
.

N- .

N- ,
.

,
.

./ -
, ,
.

./ ,
,


8.
,

4 .
207

~
r

$3
J:t
1-\
l'

$2
$1S91
4-
3 1

, .

,
?

:
.


1-\

'


...
.... + ~

+
r- 1-10
1-\
-.__ ......-_.- ~-

' 1-\ .SI


){ 1-\ ~.SI -13~~~ 1-\ ..SI
1'
.SI

208 9.

, . 3
8 , 4 - 16 . .
!
0(2), , .

3 .11.: 4- .11.: 5 .1I.6:

[ 1 1 1 ]
'8
60J~
.f.6
m 16
60.3MO.f.HIX
MHO.f.ECT6

32
60JMO.f.HIX
~6

-
. 8 , .
, .

: ! ,
.
.

~
(
~ ),
.
~
(J.;J
~
-
;

, 4
- .
.
209

.
! .


. .

.8Jll
H.3ME.l'I .3
1 .. 4

~
...
.

..
KAU.oro
r r
\
1 2 1 4

l'..
~

, -
1 4 . ,
.

.
. ,
. , .
, .


. . .

1 2 3 4-
l'

~
210 9.

; ,
. :
? :
.

1 .
1 - , ! ,
$1500, .

1 2 4
1s

L~r+--+---1~-l

1-\5


, .

.
2 . , !

1 2 4
1s 1soo

~

f.\

. ,
,
- . , .
211

1 2 4- -
$1500 1500
41soo 1soo

1-\5

, .
1, 2 . .,
4 ? , ?
,
. , .
, .

, :

1 2 4-
$1500 1500
4\soo 1500
_

,
.
. , ,
4 $1500.

1 2 4- ~AlllA TEKY\tAJI
1soo 1soo isoo 1500 ,

.Jl..
:
JA .$1500


212 9.

, .
.

, . ,
,
.
, . ,
, / .
( 1 ).
, 1 ,
$1500.

2 4
$\SOO o\;\S"OO 'flSOO
1soo

1 . ? ,
! ,
1-r $1500.
213

1 2 4
~15"00 $1soo $"\soo -tlS"oo

1
....
ilsoo
r

.
2 3 .
$1500.

1 2 4
,flS'Ot> $1S'OO 1$'\$0() $1500

'
>1- ...
1
,,,.'
~$00 s1soo $1$0()

, .

4 ? ,
- !
$1500, ,
$3000! .

1 2 4
ofJS'Ot> $\S'OO ~1$00 .,.-isoo
1
>1- ... ,,,.'
$ ISO() :<:~:
".$00 s1soo
r r '''
r

1-\
214 9.

! 4 ,
$3000.
, .

1 2 4
$1S'OI> .$15"00 $'\S<>O :flSO()
'- CAPAJI
1
... 1


.J-
$1soo $1.SOO f \$00

"'
:~~:.
''"
~ l-\06AJI

1-\ '" HTOt060E. .W..

! 3 ,
1 2 .
$1500.

1 z 3 4-
$\S:OO $\S'()() t1soo

'f~O()()
t
1soc. :t;

1-\ $\'

3 $1500.
, $2000. ,
$2000!

1 2 3 4-
$\S:OO $\SOC> $\S'()() t1soo


<t~o06

$1so0

,. ',,
f{ 2000

' 1-\'
215

4 - .
. $3000.
, $2000.

$ $2000
f

-, . !
3 , 1 ! -
.

$
r
($2.

+ 7. ? ?
60.


-
1
)
1 ?
!

1 z 3 4-
$\SoO t\SOC> $\SOC> $\500

$30~
~\S<>O ~\S $11>00
.._._f\\\t.~ .>/

t1<t-\l-.C.'A'A.._0c.1
."\"0 ~'1\\"\"" "' ' f
20()()
/ "'

.n/\~ \ ~IS'OO

' \ '

1
> $1500. ,
:

$ S2000 + ~\5oOJ
f ( r
216 9.

,
? , !
,
, .
+ $3500.
:

1 2 1 4
~ "IS"oc:>

~IStlO

soo
$1.roo

r _.lsoo sooo
r

$JS"OO $1S00
".~~S"oo:
.,
""''
1-'
.,.
6\

, : ,
, $3500 - .

, ,
. ,
.

,
:

.
1. PEJlUl.YltMA MAKC.llMYM (ll &CeLl (i-l][jJ)
. liE
~ ~
C6LL(t) (
= 2.. C.TOllMOC.Tb TEKYitEro ' +
{ .. OC.TA&llErOCJI ..J

. LL(!- l)(j - J!.~

,
, . , ?
: 217

,
.

: : 4-

:
, - ?
.

IPHoNE
4'20~95
? 1
, ,
!
iPhone.

? . ,

.
:

1 2 4
$\S">

....s ~\SOt> .$\S"OO

.:tl~O() $\SOO $\SOO $3000


f .

.$\!> $\Soo ~.;l.000 $3SOo




r

218 9.

, 4
$3500. , .
iPhone.

1 2 4
.f\SoD ~ISot>
isoo .\SOC>

f'IS()b $$000
$\S~ ~\So1:1
MArHllTOOH
r

$'\S ~\5J nooD 35

IPHONE

~
&I~ &

, !
, .

. iPhone
1 . $1500, iPhone $2000.
, iPhone.

1 2 4
.f\SoD \flS01>
=t'IS'OO f\SOO


f'IS()O $,1000
$\Sl>l> ~\SOI
MArHllTOOH
r
$:2.000 3$'
$'\SOO $\sclO

$.2.000
IPHONE
1
: 219

iPhone .

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

$1$'00 )
~\$00 $\S""oO

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

~

$i- ~350Q
1 1

3 , iPhone ,
, .

.
$3500. iPhone,
3 .


(
$2000
IPHONE
+
+

3 $20001 $2000 iPhone + $2000


: $4000. !
.

<$\50 1soo $\.$'110 151>0



$\SOO $\" $1$'00



ti- $3S'DC>
$\5'00 ~ 15'00
~
1\.

$s- '*'3,!;<) asoo .f+ooo


1 1 1 1

1'
& &.
220 9.

: ? ?

JI

..JI
1 1
~ISoO
2
$\$"()() $1500 ~\Soo
4

6 . jzj QS ~ $3000

, .

: . .
!

9.1 , : -.
1 $1000. ?

? , :
, , . ?
, .

1 2 3 4
$3000
r ~ 125 s15
,
~:i.C>oo ~

1-\ Rf f6 1-\
$3000

\}- " ......
$Jsoo $\500
$::1,.><>> ~3Soo
1-\ 1-\
221

. .

,
?

! ,
.

?
, , 0,5
$1000. ,
. . 3,5 .
3,5 ?
!
1, 2, 3 4 .
3,5 .
- ,
.

.5 1 1.5 2 2.5' 3 .5 4

~...

, .
. ,
, .
~ -
222 9.

.
?

: . ,
, , .
.

!
- ,
. ,

. .

, .

.
~
f7
1

D..M .
.$&/ .$~/ .$2./

, . -
, !
, .

PIOKJAK

, ,
. .
223

, . , ,
, . ,
.

D... . .. PE.MJI .

...... & 11
1 .D.HJI :f
. 11
1 .D.HJI 6
1-\MOHMbHAJI rME.PE.JI \ .D.E.H CJ
.ll. MYJE.11. 2. .D.HJI
~
6. 6 11
1 .D.HJI ~

, ,
,
.
?

! -
. - ,
.
, .

l.z 1~2 2
..

JI ..JI
l-----'~~1--~---i
!i. MYJE.li.
1--~-1--~1--~+---t

&. n&
224 9.

? .
? :

2
.. =fw -=fw "1-w 1V>
. ,_ :, 'j:
-1 (A){f :':
(,.)(
v)(I-

1-\ ..

.
,
6. f6 : ~;
L_~--1!.-:.~_..J'-.-:c.>V~S~.__,;-=-_,
-t
6.:
... 60,
1-\JI rAE.PE.JI,
6. 6

,
.

..6

/\6

-11.

,
. .
3 , 4,5 .
, .
. ,
225

. ,
1 + 1 = 3,5 ,
4,5.
~,
~> - 1 1,5 .
?

. - ,

. ,
, .
,
.

,
n?

,
.
~> - . ,
~ ~.

1-\0 OJI.
TPE)I. OJI.PIOKJAKO&"
PIOKJAK", .
. ~
COJI.EP~A &
. OJl.PIOKJAK"
226 9.

,
?

\ \ 1 1 . ,
' .

: 3,5
1 - ,
. ,
/\~.
11 ,
!
&. ~ .5 ,S ', .

9.2 , .
6 ,
. ; ,
:

Q , 3 , 1;

Q , 1 , 3;
Q , 2 , 9;
Q , 2 , 5;
Q , 1 , 6



.
?

Q
-
227

.

.

Q ,
,
.


. .
:

Q
;

Q
.
;

Q ,
, .
.

. ,
dictionary.com.
, .
,

, .

fish~.
4'hish~. ,
.

., #. "!.S":

Fl.SH

Vl.SH

( , .
, . )
228 9.

, hish. :
fish vista?

?
.


. ,
. hish fish?
hish vista? .
,
, . ,
: ,
.

? ,
. hish fish,
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) ?

,
. ,
.

1 ()
1

s 2

3
231

1. &I
&..I,
J..
5
&


5
~ 3

2.. &.Q.,
. &
JHA .I JI .~
& & +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
frt?

F 5 F .s
F t
'
F ()


R 5 1

: ! Hofosh fish :

F 5
,j, 1, .... =- ;?
FI S

f ~ 1-1
J.. ... :: 2
F R
233

,
:
, .
?

fish f osh.

F 5
1 1
1
; 1 2 2

?

, .
, .

F s F ~

F F
2 \

2 s
2 2

C.AMAJI JlMHHAJI OIAJI C.AMAJI JlMHHAJI O\4AJI


OJl. . . 6 OJl. . . 6
. 2. . . ~
234 9.

Z.. . &I .&..I,


. 6 J.
.~ 6' .&
( 6 .. . .~
+'
..~ oitE~
OJLC.T'POKO~)

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

. . ....

~\~\.

, .
, , , . ,
?

~ rAJZ.O
:S:
:21
:

\
<.)

....::..
:

r
r
r
r rr
t:i



::r
:S:
:21
"

~
:z:
....
:

PAJMEP
......
238 10. k

'.ll ? -
. .

.,.;:
:25

:
u
....::..
:

r
r
r r
1-
l.LJ
.
r r

~

":::!'
.,.;:
21
...:)
w
~
:

....
:

.... . .

, . ,
, , . :
k '.ll!
.

r r
r r r r r
7 /'
~:
_../


1. .. z..
6.JI. E.ro 3. CPE.ll.M COCE.ll.E.K .6
6 JI 3 ~ .... .., . r.6,
, . 6CE.ro,
JIME.CJI

k , !
-,
k . .
239


, Netix ,
.
!

,
. ,
. ,
.
240 10. k

, -, , . ,
, ,
!

,
. -
, .

& ~

2.. ~. .
1 .U
n

. ,
. ,
?


. - , .
, .
, .

PAJMEP:
~& :
2
2
no
~~
..

2.
- .

4-
5
.
241

.
.

P~JME.P

, .
.
.

, :

-Jc2-2J + 2-1/

= -J +

=V
::. 1
242 10. k

1.
.

, :
.

, Netflix.
- . ,
- ,
.

~ (?, ?)

,
.

.
Netflix,
243

: .
!



~
.n.~
~

KOME...MJI 3 4 2
5
50& + 3
D..PAMA 4- 5 1
~ 1 1 ~

. 4- 5"

.
, (
). ,

? .


-
. -+ (z,2)

:!. -+ (,+, 4, t ,+)

,
.
.

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


244 10. k

:
, .
: ?
, .

~ 1-r 1 +1-ro+I

+
2

.
? ,
.

?
24. ,
, .

! :
- , ,
. , .

Netflix, Netflix
: , .
, ~. :
, Netflix ,
.
245

10.1 Netflix
.
. , , ,
. 5 ,
, ~
~ . ,
.
?

10.2 , Netflix ~. ,

Netflix, ,
. ,
?

, :
, .
5 , .
246 10. k

, >.>. 5>.>
: 2
, 10 10 . - k
>.>, 5 !

,
. , -, , ?

~ 5'
~.- 4-
~J.
4-
: s-
: 3

, 4,2.
. k
: :

= ;

= ( ).

. ,
.
, .
:

1 5 ( 1 = , 5=
);

? ( 1,
,
);

? (1 = , = ).

,
.
247

I}(S, 1, ): <!J fll(l, 1 , 1) "'


225

6

~(1 1 q>) = +s
' ' (o.ic4- 1 ~ '1) :. l()

(9(4, ,) ~ 15?> [i( 2 ~ 1 ) .::


s~
1

.
? k
k = 4.
.

(4,1,J:?

. , , D .

. 1 i-
6. 2 ~

. q
D. '2. '8:.-

.. 1 1:::-
f. 5

, 218,75.
, !
248 10. k

.
?
,
, .
,
4~ !

,
- 4 ~, 4
-2~ 4 -3~ .
.

k ,
.
:
249

1:1 , ,
;

1:1 , (,
,
).

, ?
, "~ ,
~,
. Netflix?
:
,
? ,
.
.

, ,
. ,
.

10. Netflix .

. - ? ?


, k
-
! -
.
:
.
.
250 10. k

OCR
OCR Optical Character Recognition~ .
. ,
,
. Google OCR . OCR?
:

, ?
k :
1. .

2. ,
.

,
. OCR ,
.

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

1;:-
3 ..._

ti:... KPM&AJI !'



.

OCR ,
. ,
(, k ) .
251


. Facebook,
.
!

OCR,
, .
:
, .
-,
.

n-

- ,
t .
.

J.

6r

60

~r r 6 6

( . POJIUZ.E.HI .

,
! ? ,

. , z
.
, .
, k
.
252 10. k

,
: .
, ? ,
- , !

, :
.
? , , ,
. ? , ,
, . ?

. - ,
.

~\

~\
~\

, ,
k !
- ,
.

[J k
. k .
253

= .
= (, ).

(
, ) ,
.

- k
.
11 ?

1oti rnae

./ 10 ,
. ,
.
./ ,
,


.
Facebook,

, ,
.
,


.
255

: ,
, ,
.
,
!
.

,
- .
256 11. ?

, Maggie. .

i
~
' ' 1
,

Maggie David, .

Maggie Manning, .

Maggie!
.
257

O(log ), - ().
O(log ) -
, .
.

&
&

n. D(to~\\) .~\'\)

.6 0(~) ou~~)
.~ <!') (~ \)

: -,
. : ~
i- . ,
; .
, , .

, ?
, .
,
(, , - ).
258 11 . ?

? - ,
,
.

,
:

-;

- ;

(splay) .

,
. , - .

r::-r
~
8
(;\

- .
1 -
111 .,~

\ -n\E~E ,
- ,
,

. ,
hi.
.
, l
Aol-r

t.16

&

..._.....
'\
' ....--.,,

j /-11 , ,
259

, .
. , the1-e.
, . , ?
: -,
, . ,
,
. ,
.


- :
, .
Better
Explained ( -,
): , ,
'.
.

,
. , ,
, , - ,
.
.
: .
, ,
. ,
!

- .
JPG .

Shazam - ,
.
. !

' Kalid, An Interact ive Guide to t he Fottrie r Tran s fo n , Better Exp lained, http:// mng.
/874.
260 11. ?


. - .
, ,

. .
.
, ,
!

.
( log ). ,
(),
!
, ( ).

.
, , ,
. : .
,
, ,
. .

- ,
1000 .
? 500 ,

? .

- , 1 ,
5 .
, 1 ,
.
, 50 ,
! ,
?


, , - ,
!
MapReduce 261

MapReduce

: zr. ,
,
, ?
, .
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. ?

/1\2\l4ls)
!J, j, !J,
[2\4\\~\~\

arr2 [2, 4, , 8, 10) -


arrl ! .
,
. :

>>> arrl = # URL


>>> arr2 = map(download_page, arrl)

URL-apeco, u
arr2. .
1000 ! ,
100 map .
100 ,
!

reduce
reduce . ,
~ . ,
map .

/1\2\/4\s)
! J, j, J,J.
[2\4\\~\~\
reduce .
Hyperloglog 263

\-1\2/l\4\s]
~\J//
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.
.
URL-apeco, .
, URL-apec .

.
.
264 11. ?

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

o..A\t..io

, ,
. .
, , Google ,
.

, adit.io? .

adit.io , , .
- 0(1).
, , adit.io
. !
Hyperloglog 265

. Google
-. URL-apeca,
Google, . Reddit it.ly
. ,
!


, . ,
,
. ,
, URL-apec . -
. ,
:

c:J - . : ~
, ;

c:J - . -
, , .

, . -
URL-apeca, Google,
. ,
( ). ,
it.ly : ~ , ,
.

HyperLog Log
,
HyperLogLog. , Google
L , . 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 1.0 ll ll. l.O 1. 4 1.5 1. 6 1.1 1.1\ 1. 30 .01 .01.

, , -.

,
~~~ &
,"
- -

6& &&
,1
1 j ./'l\ & ~

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 1 lO 1.1 ll. l3 1.4 1.5 1.6 l1 1.1\ l 30 31 .01.

1 0.6=1 f

~ .:J'
SHA 267

.
, ,
- , 0(1) ,
.

-
. , - ,
.

- SHA (Secure
Hash Algorith) . - .

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

, , .
SHA - - ; - ,
. - -
, SHA
.

SHA - .

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


"~o..l<3oYiih"" " ~ 1 ..
"r.s.s wOYJ "t ~ 5 ~'4 gq ...

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


- SHA . .

~-------------------------- - -- - ----- - - - --- - ---~


268 11. ?

SHA , .
. ,
4- ,
.
; - SHA
.

ero l\

[2 po..t:t../
Ero XE.111-KOJL
v
. .W-,
Oll.MHAKO&E. \1

SHA
. , ,
Gmail !
? . Google , -
SHA! , Google
-, .

" ,, 11/ ,.
..12,;> ~ co..13d." __.,
'1..111-. .06-
'1..W-. W ,
JI C.PA&HM&AE.CJI
&l\1
. .111-.,
XPAHJlltMMCJI
6 J.
- 269

- - !
SHA .
: - ...

... -:

, - SHA
Gmail, !
, .

SHA : SHA-0, SHA-1,


SHA-2 SHA-3. SHA-0 SHA-1
. SHA
, SHA-2 SHA-3. ~
~ bcrypt (
).

SHA :
01-u. , ,
-:

,
, !
270 11. ?

,
, .

: -
. Simhash.
Simhash -,
. - ,
, - !

Google Simhash
.

Simhash
( ).

Scribd ,
. Scribd ,
, !
Simhash ,
,
.

Simhash .

-
- ,
.
, ,
?

: = 1, = 2 . .
~4, 15, 7 ,
d,o,g. ,
.
, ,
. 1 ,
, . ,
.
!
- 271

,
. ,
9,6,13,13, 16 24 ,1 6,19,13,5~ . ,
= 1, = 2 . .

9 6 \3 \ '
.J.~'1-~ ~
F .S

. = 2, = 3 . .

9 6 \~ \?. '
'1.~'1-.i ~
L L R L D

! .

, .

- :

. ,
;

- :
. .
,
, . .
,
.
. ,
,
!
272 11. ?

-
RSA. ,
- :
.


. -
, .


. ,
: . 1
5 . 2 2
. 11 20 . $2,
- $3.
?

,
.

: ,
. ,
- (, . r
. . ), - 1,5 .
500 - 300
. 50 . ,
- $2, - $1.
$1500.
(-+ )?

, : ~
. ? ,
,
. -
, .
273

-.
, .
,
!

, , .
, - - ,
, . ,
.

1
1.1 128 ,
.
?

: 7
1.2 , .
?

: 8

1.3 , .

: O(log )

1.4 , . (
: !)

: ().

1.5 .

: ().

1.6 ,
. ( ! ,
4.
- , !)
2 275

: (). , - :
26 , ,
(/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 .
. ,
, ~.
,
~, . .

"AZiz3 ADI"I :"D"\


l - - - --
+ . ""s C6JIJAHHIK &.
J&~
56
". "\- . .306..
.---..~ } & &

~
&

, Adit ~
Facebook .
1 , 1
278

Adit . ,
Zakhir . 26,
, Z~,
, 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 . .

: ( 2 ).
281

5
?

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

: .

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

: .

nr
5.3 f(x) = next_empty_slot() .... ..... .. .... ... ........
n w-n

: .

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

: .

, -,
.

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

: - D .

5. 7 . : Maus, Fu
, Watchmen .
: - , D .

6
,
.

6.1 .

: 2.
6.2 bat.

: 2.
283

6.3 .

,
.

. . .
1. n POC.HYbC.JI 1. n POC.HYbC.JI 1. J! .a.YW
2.. n ..W 2.. n J 2.. n POCHYTbC.JI
3. nDJA&HKHb 3. nI 3. n.
4. n. 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 .
N-?

: .

8.7 (
,
.) N-?

: .

8.8 ,
.
,
. N-?

: .

9
9.1 , : -.
1 $1000. ?

: . -, iPhone
$4500.

9.2 , .
6 ,
. ; ,
:

, 3 , 1
, 1 , 3
, 2 , 9
, 2 , 5
, 1 , 6
?

: , .
287

9.3
u clues.
:

L 5

L ) 1

u 2

10
10.1 Netflix
.
. , , ,
. 5 ,
,
. ,
.
?

: :

. , ,
3, - 3,5.
,
3,5. .

10.2 , Netflix . ,

Nett1ix , ,
. ,
?
288

: k
. , : ,
(.) -~
3, 4 5 .
(3 + 4 + 5 / 3 = 4 ),
: 3 + 4 + 5 + 5 + 5 / 5 = 4,4 .

10.3 Nettlix .

. - ? ?

: . ,
,
. : N
sqrt(N) .

11
1l\111 111

,
- ,
.

, ,

,

,
?

,
- .

~nnTEP
:
. : 18121703-73-74
books@piter.com
WWW.PITER.COM
@ vk.com/piter_pu

@ instagram.com/p
SCAN IT!
~=~~:;~~ faceook.com/id 11111 11111 1111 11111111111
1083672150
OZON.ru