Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Compeuuon rogrammlng and

roblem Solvlng uslng


(of course) yLhon
uhruv 8aldawa
[dhruvbaldawa
AbouL Me
8.L. CompuLer Sclence from unlverslLy of
Mumbal
CSoCer 2011 and 2012
nauonal Cyber Clymplad Cold medalllsL 2007
CurrenLly worklng aL LnLhoughL, lnc.
[dhruvbaldawa
Why ls solvlng dlmculL ?
Lhe daLaseL ls Loo large Lo be lLeraLe even
once
Lhe complexlLy ls huge or very bad
or 8C1P !!
[dhruvbaldawa
WhaL Lhls Lalk covers
Memory ManagemenL
Common problems
LlsL forms v/s lLeraLor forms
lLerLools
1lme ManagemenL
Chooslng Lhe correcL daLa sLrucLures
Approach
rollng ups
[dhruvbaldawa
Memory ManagemenL
!"# %& %' %()*+',-' .
MosL of Lhe common compeuuons requlre
you sLore and compuLe large amounLs of daLa.
8unnlng ouL of memory llmlL ls preuy
common.
1here ls always" a way Lo lmprove memory
consumpuon of your program.
[dhruvbaldawa
Common roblems
MemoryLrror[1]
when you run ouL of memory, buL Lhe slLuauon
can sull be rescued (by deleung cerLaln ob[ecLs)
CverowLrror[1]
when Lhe resulL of an arlLhmeuc operauon ls Loo
large Lo be represenLed
[dhruvbaldawa
LlsL forms v/s lLeraLor forms
LlsL forms
beuer Lo sLore and re-use resulLs of compuLauons
use lf you wanL Lo perform llsL operauons, where
you need Lo sLore enure llsLs
consumes much more memory
only prefer when a real llsL cannoL be avolded
all Lhe elemenLs are generaLed/lnluallzed aL once
[dhruvbaldawa
LlsL forms v/s lLeraLor forms
lLeraLor forms
lazy and on-demand generauon of values
very low memory consumpuon
very useful when you only need Lo work wlLh Lhe
value aL hand
/*-&0
cannoL sLep backwards
cannoL sklp or [ump forwards
Lhey are scalable and memory-frlendly and used
where real llsLs are noL requlred
[dhruvbaldawa
LlsL forms v/s lLeraLor forms
range v/s xrange[2]
llsL comprehenslons v/s generaLor expresslons
funcuons v/s generaLors
[dhruvbaldawa
lmporL lLerLools
lLerLools module provldes a seL of fasL,
memory emclenL lLeraLors
provldes fasL lmplemenLauons for common
[obs llke producL, permuLauons, comblnauons
lLerLools documenLauon
[dhruvbaldawa
lmporL lLerLools
[dhruvbaldawa
lmporL lLerLools
[dhruvbaldawa
1lme Cpumlzauons
chooslng Lhe rlghL daLa sLrucLure
chooslng Lhe rlghL approach
[dhruvbaldawa
chooslng Lhe rlghL daLa sLrucLure
!"#$ &'()'#**+', -(''. #/(01 12+ 3($+4
5(($ &'()'#**+', -(''. #/(01 $#1#
,1'0310'+, #6$ 12+7' '+8#9(6,27&,4:

--Llnus 1orvalds
[dhruvbaldawa
uaLa SLrucLures ln yLhon
don'L re-lnvenL Lhe wheel. use Luple, llsL, dlcL,
seLs, as Lhey are coded ln C and hence are lAS1
for membershlp LesLs use dlcL/seL [C(1)] lnsLead
of llsLs/Luple [C(n)]
use collecuons
Cueue operauons llke pop(), lnserL() are beuer ln
collecuons.deque [C(1)] Lhan llsLs [C(n)]
use blsecL, heapq for sorLed llsLs
[dhruvbaldawa
lmporL collecuons[4]
deque
CounLer
CrderedulcL
defaulLdlcL
[dhruvbaldawa
Memolzauon[3]
cachlng resulLs from prevlous procedure calls,
and uslng lL dlrecLly
lf a funcuon call reLurns Lhe same value when Lhe
same seL of argumenLs are passed, Lhen lL can be
memolzed !

def memoized_function(value):
if value in cache:
return cache[value]
else:
cache[value] = compute(value)
return cache[value]
[dhruvbaldawa
CollaLz Con[ecLure[6]
f(n) = n/2, lf n ls even,
= 3n+1, lf n ls odd

lor example:
f(4) = 4 -> 2 -> 1
f(3) = 3 -> 10 -> 3 -> 16 -> 8 -> 4 -> 2 -> 1
[dhruvbaldawa
Mulupllcauon problem
Muluply Lwo numbers assumlng Lhere ls no
mulupllcauon operaLor avallable
[dhruvbaldawa
rollng
Cn unlx machlnes, you can slmply:
ume pyLhon my_scrlpL.py
pyLhon -m crole my_scrlpL.py
Cn lyLhon, you can use:
umelL my_expenslve_funcuon()
[dhruvbaldawa
8eferences
1. hup://docs.pyLhon.org/llbrary/excepuons.hLml
2. hup://[usundalley.blogspoL.ln/2011/09/pyLhon-range-vs-xrange.hLml
3. hup://en.wlklpedla.org/wlkl/LlsL_comprehenslon
4. hup://docs.pyLhon.org/llbrary/collecuons.hLml
3. hup://en.wlklpedla.org/wlkl/Memolzauon
6. hup://en.wlklpedla.org/wlkl/CollaLz_con[ecLure
7. hup://wlkl.pyLhon.org/moln/yLhonSpeed/erformance1lps
8. hup://wlkl.pyLhon.org/moln/yLhonSpeed
9. hup://www.udaclLy.com/wlkl/CS21320unlL20120Code?
course=cs213
[dhruvbaldawa

You might also like