SlideShare a Scribd company logo
1 of 68
MMeetthhooddoollooggyy ooff 
PPrroobblleemm SSoollvviinngg 
EEffffiicciieennttllyy SSoollvviinngg CCoommppuutteerr PPrrooggrraammmmiinngg PPrroobblleemmss 
SSvveettlliinn NNaakkoovv 
TTeelleerriikk CCoorrppoorraattiioonn 
wwwwww..tteelleerriikk..ccoomm
PPrroobblleemmss SSoollvviinngg 
FFrroomm CChhaaoottiicc ttoo MMeetthhooddoollooggiiccaall AApppprrooaacchh
HHooww ttoo SSoollvvee PPrroobblleemmss?? 
11.. RReeaadd aanndd AAnnaallyyzzee tthhee PPrroobblleemmss 
22.. UUssee aa sshheeeett ooff ppaappeerr aanndd aa ppeenn ffoorr sskkeettcchhiinngg 
33.. TThhiinnkk uupp,, iinnvveenntt aanndd ttrryy iiddeeaass 
44.. BBrreeaakk tthhee pprroobblleemm iinnttoo ssuubbpprroobblleemmss 
55.. CChheecckk uupp yyoouurr iiddeeaass 
66.. CChhoooossee aapppprroopprriiaattee ddaattaa ssttrruuccttuurreess 
77.. TThhiinnkk aabboouutt tthhee eeffffiicciieennccyy 
88.. IImmpplleemmeenntt yyoouurr aallggoorriitthhmm sstteepp--bbyy--sstteepp 
99.. TThhoorroouugghhllyy tteesstt yyoouurr ssoolluuttiioonn 
3
UUnnddeerrssttaannddiinngg tthhee 
RReeqquuiirreemmeennttss
RReeaadd aanndd AAnnaallyyzzee tthhee PPrroobblleemmss 
 CCoonnssiiddeerr yyoouu aarree aatt ttrraaddiittiioonnaall ccoommppuutteerr 
pprrooggrraammmmiinngg eexxaamm oorr ccoonntteesstt 
YYoouu hhaavvee 33--44 pprroobblleemmss ttoo ssoollvvee iinn 44--55 hhoouurrss 
 FFiirrsstt rreeaadd ccaarreeffuullllyy aallll pprroobblleemmss aanndd ttrryy ttoo 
eessttiimmaattee hhooww ccoommpplleexx eeaacchh ooff tthheemm iiss 
RReeaadd tthhee rreeqquuiirreemmeennttss,, ddoonn''tt iinnvveenntt tthheemm!! 
 SSttaarrtt ssoollvviinngg tthhee mmoosstt eeaassyy pprroobblleemm ffiirrsstt 
LLeeaavvee tthhee mmoosstt ccoommpplleexx pprroobblleemm llaasstt 
 AApppprrooaacchh tthhee nneexxtt pprroobblleemm wwhheenn tthhee pprreevviioouuss iiss 
ccoommpplleetteellyy ssoollvveedd aanndd wweellll tteesstteedd 
5
AAnnaallyyzziinngg tthhee PPrroobblleemmss 
 EExxaammppllee:: wwee aarree ggiivveenn 33 pprroobblleemmss:: 
11.. SShhuuffffllee--ccaarrddss 
SShhuuffffllee aa ddeecckk ooff ccaarrddss iinn rraannddoomm oorrddeerr 
11.. SSttuuddeennttss 
RReeaadd aa sseett ooff ssttuuddeennttss aanndd tthheeiirr mmaarrkkss aanndd pprriinntt 
ttoopp 1100 ssttuuddeennttss wwiitthh tthhee bbeesstt rreessuullttss ((bbyy 
aavveerraaggiinngg tthheeiirr mmaarrkkss)) 
11.. SSoorrttiinngg nnuummbbeerrss 
SSoorrtt aa sseett ooff nnuummbbeerrss iinn iinnccrreeaassiinngg oorrddeerr 
6
AAnnaallyyzziinngg tthhee PPrroobblleemmss ((22)) 
RReeaadd ccaarreeffuullllyy tthhee pprroobblleemmss aanndd tthhiinnkk aa bbiitt 
aabboouutt tthheeiirr ppoossssiibbllee ssoolluuttiioonnss 
OOrrddeerr tthhee pprroobblleemmss ffrroomm tthhee eeaassiieesstt ttoo tthhee 
mmoosstt ccoommpplleexx:: 
11.. SSoorrttiinngg nnuummbbeerrss 
TTrriivviiaall –– wwee ccaann uussee tthhee bbuuiilltt--iinn ssoorrttiinngg iinn ..NNEETT 
11.. SShhuuffffllee--ccaarrddss 
NNeeeedd ttoo rraannddoommiizzee tthhee eelleemmeennttss ooff aarrrraayy 
11.. SSttuuddeennttss 
NNeeeeddss ssuummmmiinngg,, ssoorrttiinngg aanndd tteexxtt ffiillee pprroocceessssiinngg 
7
UUssiinngg aa PPaappeerr 
aanndd aa PPeenn 
VViissuuaalliizziinngg aanndd SSkkeettcchhiinngg 
yyoouurr IIddeeaass
UUssee aa SShheeeett ooff PPaappeerr aanndd aa PPeenn 
NNeevveerr ssttaarrtt ssoollvviinngg aa pprroobblleemm wwiitthhoouutt aa sshheeeett 
ooff ppaappeerr aanndd aa ppeenn 
YYoouu nneeeedd ttoo sskkeettcchh yyoouurr iiddeeaass 
PPaappeerr aanndd ppeenn iiss tthhee bbeesstt vviissuuaalliizzaattiioonn ttooooll 
AAlllloowwss yyoouurr bbrraaiinn ttoo tthhiinnkk eeffffiicciieennttllyy 
PPaappeerr wwoorrkkss ffaasstteerr 
tthhaann kkeeyybbooaarrdd // ssccrreeeenn 
OOtthheerr vviissuuaalliizzaattiioonn ttooooll 
ccoouulldd aallssoo wwoorrkk wweellll 
9
PPaappeerr aanndd PPeenn 
 CCoonnssiiddeerr tthhee ""ccaarrddss sshhuuffffllee"" pprroobblleemm 
WWee ccaann sskkeettcchh iitt ttoo ssttaarrtt tthhiinnkkiinngg 
SSoommee iiddeeaass iimmmmeeddiiaatteellyy ccoommee,, ee..gg.. 
SSpplliitt tthhee ddeecckk iinnttoo ttwwoo ppaarrttss aanndd sswwaapp tthheemm aa 
mmuullttiippllee ttiimmeess 
SSwwaapp 22 rraannddoomm ccaarrddss aa rraannddoomm nnuummbbeerr ooff ttiimmeess 
SSwwaapp eeaacchh ccaarrdd wwiitthh aa rraannddoomm ccaarrdd 
10
IInnvveenntt IIddeeaass 
TThhiinnkk--uupp,, IInnvveenntt IIddeeaass aanndd CChheecckk TThheemm
TThhiinnkk uupp,, IInnvveenntt aanndd TTrryy IIddeeaass 
 FFiirrsstt ttaakkee aann eexxaammppllee ooff tthhee pprroobblleemm 
SSkkeettcchh iitt oonn tthhee sshheeeett ooff ppaappeerr 
NNeexxtt ttrryy ttoo iinnvveenntt ssoommee iiddeeaa tthhaatt wwoorrkkss ffoorr 
yyoouurr eexxaammppllee 
 CChheecckk iiff yyoouurr iiddeeaa wwiillll wwoorrkk ffoorr ootthheerr eexxaammpplleess 
TTrryy ttoo ffiinndd aa ccaassee tthhaatt bbrreeaakkss yyoouurr iiddeeaa 
TTrryy cchhaalllleennggiinngg eexxaammpplleess aanndd uunnuussuuaall ccaasseess 
 IIff yyoouu ffiinndd yyoouurr iiddeeaa iinnccoorrrreecctt,, ttrryy ttoo ffiixx iitt oorr 
jjuusstt iinnvveenntt aa nneeww iiddeeaa 
12
IInnvveenntt aanndd TTrryy IIddeeaass –– EExxaammppllee 
 CCoonnssiiddeerr tthhee ""ccaarrddss sshhuuffffllee"" pprroobblleemm 
 IIddeeaa ##11:: rraannddoomm nnuummbbeerr ooff ttiimmeess sspplliitt tthhee 
ddeecckk iinnttoo lleefftt aanndd rriigghhtt ppaarrtt aanndd sswwaapp tthheemm 
HHooww ttoo rreepprreesseenntt tthhee ccaarrddss?? 
HHooww ttoo cchhoossee aa rraannddoomm sspplliitt ppooiinntt?? 
HHooww ttoo ppeerrffoorrmm tthhee eexxcchhaannggee?? 
 IIddeeaa ##22:: sswwaapp eeaacchh ccaarrdd wwiitthh aa rraannddoomm ccaarrdd 
HHooww mmaannyy ttiimmeess ttoo rreeppeeaatt tthhiiss?? 
IIss tthhiiss ffaasstt eennoouugghh?? 
13
IInnvveenntt aanndd TTrryy IIddeeaass –– 
EExxaammppllee ((22)) 
 IIddeeaa ##33:: sswwaapp 22 rraannddoomm ccaarrddss aa rraannddoomm 
nnuummbbeerr ooff ttiimmeess 
HHooww ttoo cchhoooossee ttwwoo rraannddoomm ccaarrddss?? 
HHooww mmaannyy ttiimmeess rreeppeeaatt tthhiiss?? 
 IIddeeaa ##44:: cchhoooossee aa rraannddoomm ccaarrdd aanndd iinnsseerrtt iitt iinn 
ffrroonntt ooff tthhee ddeecckk 
HHooww ttoo cchhoooossee rraannddoomm ccaarrdd?? 
HHooww mmaannyy ttiimmeess rreeppeeaatt tthhiiss?? 
14
DDiivviiddee aanndd CCoonnqquueerr 
DDeeccoommppoossee PPrroobblleemmss iinnttoo MMaannaaggeeaabbllee PPiieecceess
DDeeccoommppoossee tthhee PPrroobblleemm 
iinnttoo SSuubbpprroobblleemmss 
WWoorrkk ddeeccoommppoossiittiioonn iiss nnaattuurraall iinn eennggiinneeeerriinngg 
IItt hhaappppeennss eevveerryy ddaayy iinn tthhee iinndduussttrryy 
PPrroojjeeccttss aarree ddeeccoommppoosseedd iinnttoo ssuubbpprroojjeeccttss 
 CCoommpplleexx pprroobblleemmss ccoouulldd bbee ddeeccoommppoosseedd iinnttoo 
sseevveerraall ssmmaalllleerr ssuubbpprroobblleemmss 
TTeecchhnniiqquuee kknnoowwnn aass ""DDiivviiddee aanndd CCoonnqquueerr"" 
SSmmaallll pprroobblleemmss ccoouulldd eeaassiillyy bbee ssoollvveedd 
SSmmaalllleerr ssuubbpprroobblleemmss ccoouulldd bbee ffuurrtthheerr 
ddeeccoommppoosseedd aass wweellll 
16
DDiivviiddee aanndd CCoonnqquueerr –– EExxaammppllee 
 LLeett''ss ttrryy iiddeeaa ##11:: 
MMuullttiippllee ttiimmee sspplliitt tthhee ddeecckk iinnttoo lleefftt aanndd rriigghhtt 
ppaarrtt aanndd sswwaapp tthheemm 
DDiivviiddee aanndd ccoonnqquueerr 
SSuubbpprroobblleemm ##11 ((ssiinnggllee eexxcchhaannggee)) –– sspplliitt tthhee 
ddeecckk iinnttoo ttwwoo rraannddoomm ppaarrttss aanndd eexxcchhaannggee tthheemm 
SSuubbpprroobblleemm ##22 –– cchhoooossiinngg aa rraannddoomm sspplliitt ppooiinntt 
SSuubbpprroobblleemm ##33 –– ccoommbbiinniinngg ssiinnggllee eexxcchhaannggeess 
HHooww mmaannyy ttiimmeess ttoo ppeerrffoorrmm ""ssiinnggllee eexxcchhaannggee""?? 
17
Subproblem ##11 ((SSiinnggllee EExxcchhaannggee)) 
 SSpplliitt tthhee ddeecckk iinnttoo 22 ppaarrttss aatt rraannddoomm sspplliitt ppooiinntt 
aanndd eexxcchhaannggee tthheessee 22 ppaarrttss 
WWee vviissuuaalliizzee tthhiiss bbyy ppaappeerr aanndd ppeenn:: 
18
Subproblem #2 
(Random Split Point) 
 CChhoooossiinngg aa rraannddoomm sspplliitt ppooiinntt 
NNeeeeddss ttoo uunnddeerrssttaanndd tthhee ccoonncceepptt ooff ppsseeuuddoo-- 
rraannddoomm nnuummbbeerrss aanndd hhooww ttoo uussee iitt 
IInn IInntteerrnneett lloottss ooff eexxaammpplleess aarree aavvaaiillaabbllee,, ssoommee 
ooff tthheemm iinnccoorrrreecctt 
 TThhee ccllaassss SSyysstteemm..RRaannddoomm ccaann ddoo tthhee jjoobb 
 IImmppoorrttaanntt ddeettaaiill iiss tthhaatt tthhee RRaannddoomm ccllaassss 
sshhoouulldd bbee iinnssttaannttiiaatteedd oonnllyy oonnccee 
NNoott aatt eeaacchh rraannddoomm nnuummbbeerr ggeenneerraattiioonn 
19
Subproblem #3 (Combining 
Single Exchanges) 
 Combining a sseeqquueennccee ooff ssiinnggllee eexxcchhaannggeess ttoo 
ssoollvvee tthhee iinniittiiaall pprroobblleemm 
HHooww mmaannyy ttiimmeess ttoo ppeerrffoorrmm ssiinnggllee eexxcchhaannggeess ttoo 
rreelliiaabbllyy rraannddoommiizzee tthhee ddeecckk?? 
NN ttiimmeess ((wwhheerree NN iiss tthhee nnuummbbeerr ooff tthhee ccaarrddss)) sseeeemmss 
eennoouugghh 
WWee hhaavvee aann aallggoorriitthhmm:: 
NN ttiimmeess sspplliitt aatt rraannddoomm ppoossiittiioonn aanndd eexxcchhaannggee 
tthhee lleefftt aanndd rriigghhtt ppaarrttss ooff tthhee ddeecckk 
20
CChheecckk--uupp YYoouurr IIddeeaass 
DDoonn''tt ggoo AAhheeaadd bbeeffoorree CChheecckkiinngg YYoouurr IIddeeaass
CChheecckk--uupp YYoouurr IIddeeaass 
 CChheecckk--uupp yyoouurr iiddeeaass wwiitthh eexxaammpplleess 
IItt iiss bbeetttteerr ttoo ffiinndd aa pprroobblleemm bbeeffoorree tthhee iiddeeaa iiss 
iimmpplleemmeenntteedd 
WWhheenn tthhee ccooddee iiss wwrriitttteenn,, cchhaannggiinngg rraaddiiccaallllyy 
yyoouurr iiddeeaass ccoossttss aa lloott ooff ttiimmee aanndd eeffffoorrtt 
 CCaarreeffuullllyy sseelleecctt eexxaammpplleess ffoorr cchheecckk--uupp 
EExxaammpplleess sshhoouulldd bbee ssiimmppllee eennoouugghh ttoo bbee 
cchheecckkeedd bbyy hhaanndd iinn aa mmiinnuuttee 
EExxaammpplleess sshhoouulldd bbee ccoommpplleexx eennoouugghh ttoo ccoovveerr 
tthhee mmoosstt ggeenneerraall ccaassee,, nnoott jjuusstt aann iissoollaatteedd ccaassee 
22
CChheecckk--uupp YYoouurr IIddeeaass –– EExxaammppllee 
 LLeett''ss cchheecckk tthhee iiddeeaa:: 
AAfftteerr 33 rraannddoomm sspplliittss aanndd sswwaappss wwee oobbttaaiinn tthhee 
ssttaarrtt ppoossiittiioonn  sseeeemmss lliikkee aa bbuugg!! 
23
IInnvveenntt NNeeww IIddeeaa IIff NNeeeeddeedd 
WWhhaatt ttoo ddoo wwhheenn yyoouu ffiinndd yyoouurr iiddeeaa iiss nnoott 
wwoorrkkiinngg iinn aallll ccaasseess?? 
TTrryy ttoo ffiixx yyoouurr iiddeeaa 
SSoommeettiimmee aa ssmmaallll cchhaannggee ccoouulldd ffiixx tthhee pprroobblleemm 
IInnvveenntt nneeww iiddeeaa aanndd ccaarreeffuullllyy cchheecckk iitt 
 IItteerraattee 
IItt iiss uussuuaall tthhaatt yyoouurr ffiirrsstt iiddeeaa iiss nnoott tthhee bbeesstt 
IInnvveenntt iiddeeaass,, cchheecckk tthheemm,, ttrryy vvaarriioouuss ccaasseess,, ffiinndd 
pprroobblleemmss,, ffiixx tthheemm,, iinnvveenntt bbeetttteerr iiddeeaa,, eettcc.. 
24
IInnvveenntt NNeeww IIddeeaass –– EExxaammppllee 
 IInnvveenntt ffeeww nneeww iiddeeaass:: 
NNeeww iiddeeaa ##11 –– mmuullttiippllee ttiimmeess sseelleecctt 22 rraannddoomm 
ccaarrddss aanndd eexxcchhaannggee tthheemm 
NNeeww iiddeeaa ##22 –– mmuullttiippllee ttiimmeess sseelleecctt aa rraannddoomm 
ccaarrdd aanndd eexxcchhaannggee iitt wwiitthh tthhee ffiirrsstt ccaarrdd 
NNeeww iiddeeaa ##33 –– mmuullttiippllee ttiimmeess sseelleecctt aa rraannddoomm 
ccaarrdd aanndd mmoovvee iitt ttoo aann eexxtteerrnnaall lliisstt 
 LLeett''ss cchheecckk tthhee nneeww iiddeeaa ##22 
IIss iitt ccoorrrreecctt?? 
25
CChheecckk--uupp tthhee NNeeww IIddeeaa –– EExxaammppllee 
26 
TThhee rreessuulltt 
sseeeemmss 
ccoorrrreecctt
TThhiinnkk aabboouutt DDaattaa SSttrruuccttuurreess 
SSeelleecctt DDaattaa SSttrruuccttuurreess tthhaatt WWiillll WWoorrkk WWeellll
CChhoooossiinngg AApppprroopprriiaattee 
DDaattaa SSttrruuccttuurreess 
 CChhoooossee aapppprroopprriiaattee ddaattaa ssttrruuccttuurreess bbeeffoorree tthhee 
ssttaarrtt ooff ccooddiinngg 
TThhiinnkk hhooww ttoo rreepprreesseenntt iinnppuutt ddaattaa 
TThhiinnkk hhooww ttoo rreepprreesseenntt iinntteerrmmeeddiiaattee pprrooggrraamm 
ssttaattee 
TThhiinnkk hhooww ttoo rreepprreesseenntt tthhee rreeqquueesstteedd oouuttppuutt 
YYoouu ccoouulldd ffiinndd tthhaatt yyoouurr iiddeeaa ccaannnnoott bbee 
iimmpplleemmeenntteedd eeffffiicciieennttllyy 
OOrr iimmpplleemmeennttaattiioonn wwiillll bbee vveerryy ccoommpplleexx oorr 
iinneeffffiicciieenntt 
28
CChhoooossee AApppprroopprriiaattee DDaattaa 
SSttrruuccttuurreess –– EExxaammppllee 
HHooww ttoo rreepprreesseenntt aa ssiinnggllee ccaarrdd?? 
TThhee bbeesstt iiddeeaa iiss ttoo ccrreeaattee aa ssttrruuccttuurree CCaarrdd 
FFaaccee –– ccoouulldd bbee ssttrriinngg,, iinntt oorr eennuummeerraattiioonn 
SSuuiitt –– eennuummeerraattiioonn 
HHooww ttoo rreepprreesseenntt aa ddeecckk ooff ccaarrddss?? 
AArrrraayy –– CCaarrdd[[]] 
IInnddeexxeedd lliisstt –– LLiisstt<<CCaarrdd>> 
SSeett // DDiiccttiioonnaarryy // QQuueeuuee // SSttaacckk –– nnoott aa ffiitt 
29
EEffffiicciieennccyy aanndd PPeerrffoorrmmaannccee 
IIss YYoouurr AAllggoorriitthhmm FFaasstt EEnnoouugghh??
TThhiinnkk AAbboouutt tthhee EEffffiicciieennccyy 
 TThhiinnkk aabboouutt eeffffiicciieennccyy bbeeffoorree wwrriittiinngg tthhee ffiirrsstt 
lliinnee ooff ccooddee 
EEssttiimmaattee tthhee rruunnnniinngg ttiimmee ((aassyymmppttoottiicc 
ccoommpplleexxiittyy)) 
CChheecckk tthhee rreeqquuiirreemmeennttss 
WWiillll yyoouurr aallggoorriitthhmm bbee ffaasstt eennoouugghh ttoo ccoonnffoorrmm 
wwiitthh tthheemm 
YYoouu ddoonn''tt wwaanntt ttoo iimmpplleemmeenntt yyoouurr aallggoorriitthhmm 
aanndd ffiinndd tthhaatt iitt iiss ssllooww wwhheenn tteessttiinngg 
YYoouu wwiillll lloossee yyoouurr ttiimmee 
31
EEffffiicciieennccyy iiss nnoott AAllwwaayyss RReeqquuiirreedd 
 BBeesstt ssoolluuttiioonn iiss ssoommeettiimmeess jjuusstt nnoott nneeeeddeedd 
RReeaadd ccaarreeffuullllyy yyoouurr pprroobblleemm ssttaatteemmeenntt 
SSoommeettiimmeess uuggllyy ssoolluuttiioonn ccoouulldd wwoorrkk ffoorr yyoouurr 
rreeqquuiirreemmeennttss aanndd iitt wwiillll ccoosstt yyoouu lleessss ttiimmee 
EExxaammppllee:: iiff yyoouu nneeeedd ttoo ssoorrtt nn nnuummbbeerrss,, aannyy 
aallggoorriitthhmm wwiillll wwoorrkk wwhheenn nn ∈∈ [[00....550000]] 
 IImmpplleemmeenntt ccoommpplleexx aallggoorriitthhmmss oonnllyy wwhheenn tthhee 
pprroobblleemm rreeaallllyy nneeeeddss tthheemm 
32
EEffffiicciieennccyy –– EExxaammppllee 
HHooww mmuucchh ccaarrddss wwee hhaavvee?? 
IInn aa ttyyppiiccaall ddeecckk wwee hhaavvee 5522 ccaarrddss 
NNoo mmaatttteerr hhooww ffaasstt tthhee aallggoorriitthhmm iiss –– iitt wwiillll wwoorrkk 
ffaasstt eennoouugghh 
IIff wwee hhaavvee NN ccaarrddss,, wwee ppeerrffoorrmm NN sswwaappss  tthhee 
eexxppeecctteedd rruunnnniinngg ttiimmee iiss OO((NN)) 
OO((NN)) wwiillll wwoorrkk ffaasstt ffoorr mmiilllliioonnss ooff ccaarrddss 
 CCoonncclluussiioonn:: tthhee eeffffiicciieennccyy iiss nnoott aann iissssuuee iinn tthhiiss 
pprroobblleemm 
33
IImmpplleemmeennttaattiioonn 
CCooddiinngg aanndd TTeessttiinngg SStteepp--bbyy--SStteepp
SSttaarrtt CCooddiinngg:: CChheecckk LLiisstt 
NNeevveerr ssttaarrtt ccooddiinngg bbeeffoorree yyoouu ffiinndd ccoorrrreecctt iiddeeaa 
tthhaatt wwiillll mmeeeett tthhee rreeqquuiirreemmeennttss 
WWhhaatt yyoouu wwiillll wwrriittee bbeeffoorree yyoouu iinnvveenntt aa ccoorrrreecctt 
iiddeeaa ttoo ssoollvvee tthhee pprroobblleemm?? 
 CChheecckklliisstt ttoo ffoollllooww bbeeffoorree ssttaarrtt ooff ccooddiinngg:: 
EEnnssuurree yyoouu uunnddeerrssttaanndd wweellll tthhee rreeqquuiirreemmeennttss 
EEnnssuurree yyoouu hhaavvee iinnvveenntteedd aa ggoooodd iiddeeaa 
EEnnssuurree yyoouurr iiddeeaa iiss ccoorrrreecctt 
EEnnssuurree yyoouu kknnooww wwhhaatt ddaattaa ssttrruuccttuurreess ttoo uussee 
EEnnssuurree tthhee ppeerrffoorrmmaannccee wwiillll bbee ssuuffffiicciieenntt 
35
CCooddiinngg CChheecckk LLiisstt –– EExxaammppllee 
 CChheecckklliisstt bbeeffoorree ssttaarrtt ooff ccooddiinngg:: 
EEnnssuurree yyoouu uunnddeerrssttaanndd wweellll tthhee rreeqquuiirreemmeennttss 
YYeess,, sshhuuffffllee ggiivveenn ddeecckk ooff ccaarrddss 
EEnnssuurree yyoouu hhaavvee iinnvveenntteedd aa ccoorrrreecctt iiddeeaa 
YYeess,, tthhee iiddeeaa sseeeemmss ccoorrrreecctt aanndd iiss tteesstteedd 
EEnnssuurree yyoouu kknnooww wwhhaatt ddaattaa ssttrruuccttuurreess ttoo uussee 
CCllaassss CCaarrdd,, eennuummeerraattiioonn SSuuiitt aanndd LLiisstt<<CCaarrdd>> 
EEnnssuurree tthhee ppeerrffoorrmmaannccee wwiillll bbee ssuuffffiicciieenntt 
LLiinneeaarr rruunnnniinngg ttiimmee  ggoooodd ppeerrffoorrmmaannccee 
36
IImmpplleemmeenntt yyoouurr 
AAllggoorriitthhmm SStteepp--bbyy--SStteepp 
 ""SStteepp--bbyy--sstteepp"" aapppprrooaacchh iiss aallwwaayyss bbeetttteerr tthhaann 
""bbuuiilldd aallll,, tthheenn tteesstt"" 
IImmpplleemmeenntt aa ppiieeccee ooff yyoouurr pprrooggrraamm aanndd tteesstt iitt 
TThhaann iimmpplleemmeenntt aannootthheerr ppiieeccee ooff tthhee pprrooggrraamm 
aanndd tteesstt iitt 
FFiinnaallllyy ppuutt ttooggeetthheerr aallll ppiieecceess aanndd tteesstt iitt 
 SSmmaallll iinnccrreemmeennttss ((sstteeppss)) rreevveeaall eerrrroorrss eeaarrllyy 
""BBiigg--bboooomm"" iinntteeggrraattiioonn ttaakkeess mmoorree ttiimmee 
37
SStteepp ##11 –– CCllaassss CCaarrdd 
38 
ccllaassss CCaarrdd 
{{ 
ppuubblliicc ssttrriinngg FFaaccee {{ ggeett;; sseett;; }} 
ppuubblliicc SSuuiitt SSuuiitt {{ ggeett;; sseett;; }} 
ppuubblliicc oovveerrrriiddee ssttrriinngg TTooSSttrriinngg(()) 
{{ 
SSttrriinngg ccaarrdd == ""(("" ++ tthhiiss..FFaaccee ++ "" "" ++ tthhiiss..SSuuiitt ++""))"";; 
rreettuurrnn ccaarrdd;; 
}} 
}} 
eennuumm SSuuiitt 
{{ 
CCLLUUBB,, DDIIAAMMOONNDD,, HHEEAARRTT,, SSPPAADDEE 
}}
 TTeessttiinngg tthhee ccllaassss CCaarrdd ttoo ggeett ffeeeeddbbaacckk aass eeaarrllyy 
aass ppoossssiibbllee:: 
 TThhee rreessuulltt iiss aass eexxppeecctteedd:: 
SStteepp ##11 –– TTeesstt 
39 
ssttaattiicc vvooiidd MMaaiinn(()) 
{{ 
CCaarrdd ccaarrdd == nneeww CCaarrdd(()) {{ FFaaccee==""AA"",, SSuuiitt==SSuuiitt..CCLLUUBB }};; 
CCoonnssoollee..WWrriitteeLLiinnee((ccaarrdd));; 
}} 
((AA CCLLUUBB))
Step #22 –– CCrreeaattee aanndd PPrriinntt aa 
DDeecckk ooff CCaarrddss 
40 
ssttaattiicc vvooiidd MMaaiinn(()) 
{{ 
LLiisstt<<CCaarrdd>> ccaarrddss == nneeww LLiisstt<<CCaarrdd>>(());; 
ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ""77"",, SSuuiitt == SSuuiitt..HHEEAARRTT }}));; 
ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ""AA"",, SSuuiitt == SSuuiitt..SSPPAADDEE }}));; 
ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ""1100"",, SSuuiitt == SSuuiitt..DDIIAAMMOONNDD }}));; 
ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ""22"",, SSuuiitt == SSuuiitt..CCLLUUBB }}));; 
ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ""66"",, SSuuiitt == SSuuiitt..DDIIAAMMOONNDD }}));; 
ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ""JJ"",, SSuuiitt == SSuuiitt..CCLLUUBB }}));; 
PPrriinnttCCaarrddss((ccaarrddss));; 
}} 
ssttaattiicc vvooiidd PPrriinnttCCaarrddss((LLiisstt<<CCaarrdd>> ccaarrddss)) 
{{ 
ffoorreeaacchh ((CCaarrdd ccaarrdd iinn ccaarrddss)) 
CCoonnssoollee..WWrriittee((ccaarrdd));; 
CCoonnssoollee..WWrriitteeLLiinnee(());; 
}}
SStteepp ##22 –– TTeesstt 
 Testing tthhee ddeecckk ooff ccaarrddss sseeeemmss ttoo bbee wwoorrkkiinngg 
ccoorrrreeccttllyy:: 
41 
((77 HHEEAARRTT))((AA SSPPAADDEE))((1100 DDIIAAMMOONNDD))((22 CCLLUUBB))((66 DDIIAAMMOONNDD))((JJ CCLLUUBB))
SStteepp ##3 –– SSiinnggllee EExxcchhaannggee 
42 
ssttaattiicc vvooiidd PPeerrffoorrmmSSiinngglleeEExxcchhaannggee((LLiisstt<<CCaarrdd>> ccaarrddss)) 
{{ 
RRaannddoomm rraanndd == nneeww RRaannddoomm(());; 
iinntt rraannddoommIInnddeexx == rraanndd..NNeexxtt((11,, ccaarrddss..CCoouunntt -- 11));; 
CCaarrdd ffiirrssttCCaarrdd == ccaarrddss[[11]];; 
CCaarrdd rraannddoommCCaarrdd == ccaarrddss[[rraannddoommIInnddeexx]];; 
ccaarrddss[[11]] == rraannddoommCCaarrdd;; 
ccaarrddss[[rraannddoommIInnddeexx]] == ffiirrssttCCaarrdd;; 
}}
 To tteesstt tthhee ssiinnggllee eexxcchhaannggee wwee uussee tthhee 
ffoolllloowwiinngg ccooddee:: 
 TThhee rreessuulltt iiss uunneexxppeecctteedd:: 
SStteepp ##3 –– TTeesstt 
43 
ssttaattiicc vvooiidd MMaaiinn(()) 
{{ 
LLiisstt<<CCaarrdd>> ccaarrddss == nneeww LLiisstt<<CCaarrdd>>(());; 
ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ""22"",, SSuuiitt == SSuuiitt..CCLLUUBB }}));; 
ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ""33"",, SSuuiitt == SSuuiitt..HHEEAARRTT }}));; 
ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ""44"",, SSuuiitt == SSuuiitt..SSPPAADDEE }}));; 
PPeerrffoorrmmSSiinngglleeEExxcchhaannggee((ccaarrddss));; 
PPrriinnttCCaarrddss((ccaarrddss));; 
}} 
((22 CCLLUUBB))((33 HHEEAARRTT))((44 SSPPAADDEE))
SStteepp ##3 –– FFiixx BBuugg aanndd TTeesstt 
 The first eelleemmeenntt ooff lliisstt iiss aatt iinnddeexx 00,, nnoott 11:: 
 TThhee rreessuulltt iiss aaggaaiinn iinnccoorrrreecctt ((3 ttiimmeess tthhee ssaammee)):: 
44 
ssttaattiicc vvooiidd PPeerrffoorrmmSSiinngglleeEExxcchhaannggee((LLiisstt<<CCaarrdd>> ccaarrddss)) 
{{ 
RRaannddoomm rraanndd == nneeww RRaannddoomm(());; 
iinntt rraannddoommIInnddeexx == rraanndd..NNeexxtt((11,, ccaarrddss..CCoouunntt -- 11));; 
CCaarrdd ffiirrssttCCaarrdd == ccaarrddss[[00]];; 
CCaarrdd rraannddoommCCaarrdd == ccaarrddss[[rraannddoommIInnddeexx]];; 
ccaarrddss[[00]] == rraannddoommCCaarrdd;; 
ccaarrddss[[rraannddoommIInnddeexx]] == ffiirrssttCCaarrdd;; 
}} 
((33 HHEEAARRTT))((22 CCLLUUBB))((44 SSPPAADDEE)) 
((33 HHEEAARRTT))((22 CCLLUUBB))((44 SSPPAADDEE)) 
((33 HHEEAARRTT))((22 CCLLUUBB))((44 SSPPAADDEE))
SStteepp ##3 –– FFiixx AAggaaiinn aanndd TTeesstt 
 RRaannddoomm..NNeexxtt(()) hhaass eexxcclluussiivvee eenndd rraannggee:: 
 TThhee rreessuulltt nnooww sseeeemmss ccoorrrreecctt:: 
45 
ssttaattiicc vvooiidd PPeerrffoorrmmSSiinngglleeEExxcchhaannggee((LLiisstt<<CCaarrdd>> ccaarrddss)) 
{{ 
RRaannddoomm rraanndd == nneeww RRaannddoomm(());; 
iinntt rraannddoommIInnddeexx == rraanndd..NNeexxtt((11,, ccaarrddss..CCoouunntt));; 
CCaarrdd ffiirrssttCCaarrdd == ccaarrddss[[00]];; 
CCaarrdd rraannddoommCCaarrdd == ccaarrddss[[rraannddoommIInnddeexx]];; 
ccaarrddss[[00]] == rraannddoommCCaarrdd;; 
ccaarrddss[[rraannddoommIInnddeexx]] == ffiirrssttCCaarrdd;; 
}} 
((33 HHEEAARRTT))((22 CCLLUUBB))((44 SSPPAADDEE)) 
((44 SSPPAADDEE))((33 HHEEAARRTT))((22 CCLLUUBB)) 
((44 SSPPAADDEE))((33 HHEEAARRTT))((22 CCLLUUBB))
SStteepp ##44 –– SShhuuffffllee tthhee DDeecckk 
 SShhuuffffllee tthhee eennttiirree ddeecckk ooff ccaarrddss:: 
 TThhee rreessuulltt iiss ssuurrpprriissiinnggllyy iinnccoorrrreecctt:: 
46 
ssttaattiicc vvooiidd SShhuufffflleeCCaarrddss((LLiisstt<<CCaarrdd>> ccaarrddss)) 
{{ 
ffoorr ((iinntt ii == 11;; ii <<== ccaarrddss..CCoouunntt;; ii++++)) 
{{ 
PPeerrffoorrmmSSiinngglleeEExxcchhaannggee((ccaarrddss));; 
}} 
}} 
IInniittiiaall ddeecckk:: ((77 HHEEAARRTT))((AA SSPPAADDEE))((1100 DDIIAAMMOONNDD))((22 CCLLUUBB))((66 
DDIIAAMMOONNDD))((JJ CCLLUUBB)) 
AAfftteerr sshhuuffffllee:: ((77 HHEEAARRTT))((AA SSPPAADDEE))((1100 DDIIAAMMOONNDD))((22 CCLLUUBB))((66 
DDIIAAMMOONNDD))((JJ CCLLUUBB))
SStteepp ##44 –– SSttrraannggee BBuugg 
WWhheenn wwee sstteepp tthhrroouugghh tthhee ccooddee wwiitthh tthhee 
ddeebbuuggggeerr,, tthhee rreessuulltt sseeeemmss ccoorrrreecctt:: 
WWiitthhoouutt tthhee ddeebbuuggggeerr tthhee rreessuulltt iiss wwrroonngg!! 
47 
IInniittiiaall ddeecckk:: ((77 HHEEAARRTT))((AA SSPPAADDEE))((1100 DDIIAAMMOONNDD))((22 CCLLUUBB))((66 
DDIIAAMMOONNDD))((JJ CCLLUUBB)) 
AAfftteerr sshhuuffffllee:: ((1100 DDIIAAMMOONNDD))((77 HHEEAARRTT))((AA SSPPAADDEE))((JJ CCLLUUBB))((22 
CCLLUUBB))((66 DDIIAAMMOONNDD))
SStteepp ##44 –– FFiixx AAggaaiinn aanndd TTeesstt 
 RRaannddoomm sshhoouulldd bbee iinnssttaannttiiaatteedd oonnllyy oonnccee:: 
 TThhee rreessuulltt ffiinnaallllyy iiss ccoorrrreecctt wwiitthh aanndd wwiitthhoouutt 
tthhee ddeebbuuggggeerr 
48 
pprriivvaattee ssttaattiicc RRaannddoomm rraanndd == nneeww RRaannddoomm(());; 
ssttaattiicc vvooiidd PPeerrffoorrmmSSiinngglleeEExxcchhaannggee((LLiisstt<<CCaarrdd>> ccaarrddss)) 
{{ 
iinntt rraannddoommIInnddeexx == rraanndd..NNeexxtt((11,, ccaarrddss..CCoouunntt));; 
CCaarrdd ffiirrssttCCaarrdd == ccaarrddss[[00]];; 
CCaarrdd rraannddoommCCaarrdd == ccaarrddss[[rraannddoommIInnddeexx]];; 
ccaarrddss[[00]] == rraannddoommCCaarrdd;; 
ccaarrddss[[rraannddoommIInnddeexx]] == ffiirrssttCCaarrdd;; 
}}
TTeessttiinngg 
TThhoorroouugghhllyy TTeesstt YYoouurr SSoolluuttiioonn
Thoroughly TTeesstt yyoouurr SSoolluuttiioonn 
WWiissee ssooffttwwaarree eennggiinneeeerrss ssaayy tthhaatt:: 
IInnvveennttiinngg aa ggoooodd iiddeeaa aanndd iimmpplleemmeennttiinngg iitt iiss 
hhaallff ooff tthhee ssoolluuttiioonn 
TTeessttiinngg iiss tthhee sseeccoonndd hhaallff ooff tthhee ssoolluuttiioonn 
AAllwwaayyss tteesstt tthhoorroouugghhllyy yyoouurr ssoolluuttiioonn 
IInnvveesstt iinn tteessttiinngg 
OOnnee 110000%% ssoollvveedd pprroobblleemm iiss bbeetttteerr tthhaann 22 oorr 3 
ppaarrttiiaallllyy ssoollvveedd 
TTeessttiinngg eexxiissttiinngg pprroobblleemm ttaakkeess lleessss ttiimmee tthhaann 
ssoollvviinngg aannootthheerr pprroobblleemm ffrroomm ssccrraattcchh 
50
HHooww ttoo TTeesstt?? 
 TTeessttiinngg ccoouulldd nnoott cceerrttiiffyy aabbsseennccee ooff ddeeffeeccttss 
IItt jjuusstt rreedduucceess tthhee ddeeffeeccttss rraattee 
WWeellll tteesstteedd ssoolluuttiioonnss aarree mmoorree lliikkeellyy ttoo bbee 
ccoorrrreecctt 
 SSttaarrtt tteessttiinngg wwiitthh aa ggoooodd rreepprreesseennttaattiivvee ooff tthhee 
ggeenneerraall ccaassee 
NNoott aa ssmmaallll iissoollaatteedd ccaassee 
LLaarrggee aanndd ccoommpplleexx tteesstt,, bbuutt 
SSmmaallll eennoouugghh ttoo bbee eeaassiillyy cchheecckkaabbllee 
51
HHooww ttoo TTeesstt?? ((22)) 
 TTeesstt tthhee bboorrddeerr ccaasseess 
EE..gg.. iiff nn ∈∈ [[00....550000]]  ttrryy nn==00 ,, nn==11,, nn==22,, nn==449999,, 
nn==550000 
 IIff aa bbuugg iiss ffoouunndd,, rreeppeeaatt aallll tteessttss aafftteerr ffiixxiinngg iitt 
ttoo aavvooiidd rreeggrreessssiioonnss 
RRuunn aa llooaadd tteesstt 
HHooww ttoo bbee ssuurree tthhaatt yyoouurr aallggoorriitthhmm iiss ffaasstt 
eennoouugghh ttoo mmeeeett tthhee rreeqquuiirreemmeennttss?? 
UUssee ccooppyy--ppaassttiinngg ttoo ggeenneerraattee llaarrggee tteesstt ddaattaa 
52
RReeaadd tthhee PPrroobblleemm SSttaatteemmeenntt 
RReeaadd ccaarreeffuullllyy tthhee pprroobblleemm ssttaatteemmeenntt 
DDooeess yyoouurr ssoolluuttiioonn pprriinntt eexxaaccttllyy wwhhaatt iiss 
eexxppeecctteedd?? 
DDooeess yyoouurr oouuttppuutt ffoollllooww tthhee rreeqquueesstteedd ffoorrmmaatt?? 
DDiidd yyoouu rreemmoovvee yyoouurr ddeebbuugg pprriinnttoouuttss?? 
 BBee ssuurree ttoo ssoollvvee tthhee rreeqquueesstteedd pprroobblleemm,, nnoott 
tthhee pprroobblleemm yyoouu tthhiinnkk iiss rreeqquueesstteedd!! 
EExxaammppllee:: ""WWrriittee aa pprrooggrraamm ttoo pprriinntt tthhee nnuummbbeerr 
ooff ppeerrmmuuttaattiioonnss oonn nn eelleemmeennttss"" mmeeaannss ttoo pprriinntt 
aa ssiinnggllee nnuummbbeerr,, nnoott aa sseett ooff ppeerrmmuuttaattiioonnss!! 
53
TTeessttiinngg –– EExxaammppllee 
 TTeesstt wwiitthh ffuullll ddeecckk ooff 5522 ccaarrddss 
SSeerriioouuss eerrrroorr ffoouunndd  cchhaannggee tthhee aallggoorriitthhmm 
 CChhaannggee tthhee aallggoorriitthhmm 
EExxcchhaannggee tthhee ffiirrsstt ccaarrdd wwiitthh aa rraannddoomm ccaarrdd  
eexxcchhaannggee ccaarrddss 00,, 11,, ……,, NN--11 wwiitthh aa rraannddoomm ccaarrdd 
TTeesstt wwhheetthheerr tthhee nneeww aallggoorriitthhmm wwoorrkkss 
 TTeesstt wwiitthh 11 ccaarrdd 
 TTeesstt wwiitthh 22 ccaarrddss 
 TTeesstt wwiitthh 00 ccaarrddss 
 LLooaadd tteesstt wwiitthh 5522 000000 ccaarrddss 
54
TTeesstt wwiitthh 5522 CCaarrddss –– EExxaammppllee 
55 
ssttaattiicc vvooiidd TTeessttSShhuuffffllee5522CCaarrddss(()) 
{{ 
LLiisstt<<CCaarrdd>> ccaarrddss == nneeww LLiisstt<<CCaarrdd>>(());; 
ssttrriinngg[[]] aallllFFaacceess == nneeww ssttrriinngg[[]] {{ ""22"",, ""33"",, ""44"",, ""55"",, ""66"",, 
""77"",, ""88"",, ""99"",, ""1100"",, ""JJ"",, ""QQ"",, ""KK"",, ""AA"" }};; 
SSuuiitt[[]] aallllSSuuiittss == nneeww SSuuiitt[[]] {{ SSuuiitt..CCLLUUBB,, SSuuiitt..DDIIAAMMOONNDD,, 
SSuuiitt..HHEEAARRTT,, SSuuiitt..SSPPAADDEE }};; 
ffoorreeaacchh ((ssttrriinngg ffaaccee iinn aallllFFaacceess)) 
ffoorreeaacchh ((SSuuiitt ssuuiitt iinn aallllSSuuiittss)) 
{{ 
CCaarrdd ccaarrdd == nneeww CCaarrdd(()) {{ FFaaccee == ffaaccee,, SSuuiitt == ssuuiitt }};; 
ccaarrddss..AAdddd((ccaarrdd));; 
}} 
SShhuufffflleeCCaarrddss((ccaarrddss));; 
PPrriinnttCCaarrddss((ccaarrddss));; 
}}
TTeesstt wwiitthh 5522 CCaarrddss –– EExxaammppllee ((22)) 
 TThhee rreessuulltt iiss ssuurrpprriissiinngg:: 
 HHaallff ooff tthhee ccaarrddss kkeeeepp tthheeiirr iinniittiiaall ppoossiittiioonnss 
WWee hhaavvee sseerriioouuss pprroobblleemm –– tthhee rraannddoommiizzaattiioonn 
aallggoorriitthhmm iiss nnoott rreelliiaabbllee 
56 
((44 DDIIAAMMOONNDD))((22 DDIIAAMMOONNDD))((66 HHEEAARRTT))((22 SSPPAADDEE))((AA SSPPAADDEE))((77 SSPPAADDEE))((33 
DDIIAAMMOONNDD))((33 SSPPAADDEE))((44 SSPPAADDEE))((44 HHEEAARRTT))((66 CCLLUUBB))((KK HHEEAARRTT))((55 CCLLUUBB))((55 
DDIIAAMMOONNDD))((55 HHEEAARRTT))((AA HHEEAARRTT))((99 CCLLUUBB))((1100 CCLLUUBB))((AA CCLLUUBB))((66 SSPPAADDEE))((77 
CCLLUUBB))((77 DDIIAAMMOONNDD))((33 CCLLUUBB))((99 HHEEAARRTT))((88 CCLLUUBB))((33 HHEEAARRTT))((99 SSPPAADDEE))((44 
CCLLUUBB))((88 HHEEAARRTT))((99 DDIIAAMMOONNDD))((55 SSPPAADDEE))((88 DDIIAAMMOONNDD))((JJ HHEEAARRTT))((1100 
DDIIAAMMOONNDD))((1100 HHEEAARRTT))((1100 SSPPAADDEE))((QQ HHEEAARRTT))((22 CCLLUUBB))((JJ CCLLUUBB))((JJ SSPPAADDEE)) 
((QQ CCLLUUBB))((77 HHEEAARRTT))((22 HHEEAARRTT))((QQ SSPPAADDEE))((KK CCLLUUBB))((JJ DDIIAAMMOONNDD))((66 
DDIIAAMMOONNDD))((KK SSPPAADDEE))((88 SSPPAADDEE))((AA DDIIAAMMOONNDD))((QQ DDIIAAMMOONNDD))((KK DDIIAAMMOONNDD))
FFiixxiinngg tthhee AAllggoorriitthhmm 
 NNeeww iiddeeaa tthhaatt sslliigghhttllyy cchhaannggeess tthhee aallggoorriitthhmm:: 
EExxcchhaannggee tthhee ffiirrsstt ccaarrdd wwiitthh aa rraannddoomm ccaarrdd  
eexxcchhaannggee ccaarrddss 00,, 11,, ……,, NN--11 wwiitthh aa rraannddoomm ccaarrdd 
57 
ssttaattiicc vvooiidd PPeerrffoorrmmSSiinngglleeEExxcchhaannggee((LLiisstt<<CCaarrdd>> ccaarrddss,, iinntt iinnddeexx)) 
{{ 
iinntt rraannddoommIInnddeexx == rraanndd..NNeexxtt((11,, ccaarrddss..CCoouunntt));; 
CCaarrdd ffiirrssttCCaarrdd == ccaarrddss[[iinnddeexx]];; 
ccaarrddss[[iinnddeexx]] == ccaarrddss[[rraannddoommIInnddeexx]];; 
ccaarrddss[[rraannddoommIInnddeexx]] == ffiirrssttCCaarrdd;; 
}} 
ssttaattiicc vvooiidd SShhuufffflleeCCaarrddss((LLiisstt<<CCaarrdd>> ccaarrddss)) 
{{ 
ffoorr ((iinntt ii == 00;; ii << ccaarrddss..CCoouunntt;; ii++++)) 
PPeerrffoorrmmSSiinngglleeEExxcchhaannggee((ccaarrddss,, ii));; 
}}
TTeesstt wwiitthh 5522 CCaarrddss ((AAggaaiinn)) 
 TThhee rreessuulltt nnooww sseeeemmss ccoorrrreecctt:: 
 CCaarrddss aarree ccoommpplleetteellyy rraannddoommiizzeedd 
58 
((99 HHEEAARRTT))((55 CCLLUUBB))((33 CCLLUUBB))((77 SSPPAADDEE))((66 CCLLUUBB))((55 SSPPAADDEE))((66 HHEEAARRTT)) 
((44 CCLLUUBB))((1100 CCLLUUBB))((33 SSPPAADDEE))((KK DDIIAAMMOONNDD))((1100 HHEEAARRTT))((88 CCLLUUBB))((AA 
CCLLUUBB))((JJ DDIIAAMMOONNDD))((KK SSPPAADDEE))((99 SSPPAADDEE))((77 CCLLUUBB))((1100 DDIIAAMMOONNDD))((99 
DDIIAAMMOONNDD))((88 HHEEAARRTT))((66 DDIIAAMMOONNDD))((88 SSPPAADDEE))((55 DDIIAAMMOONNDD))((44 HHEEAARRTT)) 
((1100 SSPPAADDEE))((JJ CCLLUUBB))((QQ SSPPAADDEE))((99 CCLLUUBB))((JJ HHEEAARRTT))((KK CCLLUUBB))((22 HHEEAARRTT)) 
((77 HHEEAARRTT))((AA HHEEAARRTT))((33 DDIIAAMMOONNDD))((KK HHEEAARRTT))((AA SSPPAADDEE))((88 DDIIAAMMOONNDD))((44 
SSPPAADDEE))((33 HHEEAARRTT))((55 HHEEAARRTT))((QQ HHEEAARRTT))((44 DDIIAAMMOONNDD))((22 SSPPAADDEE))((AA 
DDIIAAMMOONNDD))((22 DDIIAAMMOONNDD))((JJ SSPPAADDEE))((77 DDIIAAMMOONNDD))((QQ DDIIAAMMOONNDD))((22 CCLLUUBB)) 
((66 SSPPAADDEE))((QQ CCLLUUBB))
TTeesstt wwiitthh 11 CCaarrdd 
59 
 CCrreeaattee aa mmeetthhoodd ttoo tteesstt wwiitthh 11 ccaarrdd:: 
ssttaattiicc vvooiidd TTeessttSShhuufffflleeOOnneeCCaarrdd(()) 
{{ 
LLiisstt<<CCaarrdd>> ccaarrddss == nneeww LLiisstt<<CCaarrdd>>(());; 
ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ""AA"",, SSuuiitt == SSuuiitt..CCLLUUBB }}));; 
CCaarrddssSShhuuffffllee..SShhuufffflleeCCaarrddss((ccaarrddss));; 
CCaarrddssSShhuuffffllee..PPrriinnttCCaarrddss((ccaarrddss));; 
}} 
WWee ffoouunndd aa bbuugg:: 
UUnnhhaannddlleedd EExxcceeppttiioonn:: SSyysstteemm..AArrgguummeennttOOuuttOOffRRaannggeeEExxcceeppttiioonn:: 
IInnddeexx wwaass oouutt ooff rraannggee.. MMuusstt bbee nnoonn--nneeggaattiivvee aanndd lleessss tthhaann 
tthhee ssiizzee ooff tthhee ccoolllleeccttiioonn.. PPaarraammeetteerr nnaammee:: iinnddeexx 
……
Test wwiitthh 11 CCaarrdd –– BBuugg FFiixxiinngg 
60 
WWee ttaakkee 11 ccaarrdd aarree ssppeecciiaall ccaassee:: 
ssttaattiicc vvooiidd SShhuufffflleeCCaarrddss((LLiisstt<<CCaarrdd>> ccaarrddss)) 
{{ 
iiff ((ccaarrddss..CCoouunntt >> 11)) 
{{ 
ffoorr ((iinntt ii == 00;; ii << ccaarrddss..CCoouunntt;; ii++++)) 
{{ 
PPeerrffoorrmmSSiinngglleeEExxcchhaannggee((ccaarrddss,, ii));; 
}} 
}} 
}} 
 TTeesstt sshhoowwss tthhaatt tthhee pprroobblleemm iiss ffiixxeedd
TTeesstt wwiitthh 22 CCaarrddss 
61 
 CCrreeaattee aa mmeetthhoodd ttoo tteesstt wwiitthh 22 ccaarrddss:: 
ssttaattiicc vvooiidd TTeessttSShhuufffflleeTTwwooCCaarrddss(()) 
{{ 
LLiisstt<<CCaarrdd>> ccaarrddss == nneeww LLiisstt<<CCaarrdd>>(());; 
ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ""AA"",, SSuuiitt == SSuuiitt..CCLLUUBB }}));; 
ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ""33"",, SSuuiitt == SSuuiitt..DDIIAAMMOONNDD }}));; 
CCaarrddssSShhuuffffllee..SShhuufffflleeCCaarrddss((ccaarrddss));; 
CCaarrddssSShhuuffffllee..PPrriinnttCCaarrddss((ccaarrddss));; 
}} 
 BBuugg:: sseeqquueennttiiaall eexxeeccuuttiioonnss ggeett tthhee ssaammee rreessuulltt:: 
((33 DDIIAAMMOONNDD))((AA CCLLUUBB)) 
 TThhee pprroobblleemm:: tthhee ffiirrsstt aanndd tthhee sseeccoonndd ccaarrddss 
aallwwaayyss eexxcchhaannggee eeaacchh ootthheerr eexxaaccttllyy oonnccee
Test with 22 CCaarrddss –– BBuugg FFiixxiinngg 
62 
WWee aallllooww eeaacchh ccaarrdd ttoo bbee eexxcchhaannggeedd wwiitthh aannyy 
ootthheerr rraannddoomm ccaarrdd,, iinncclluuddiinngg iittsseellff 
ssttaattiicc vvooiidd PPeerrffoorrmmSSiinngglleeEExxcchhaannggee((LLiisstt<<CCaarrdd>> ccaarrddss,, iinntt iinnddeexx)) 
{{ 
iinntt rraannddoommIInnddeexx == rraanndd..NNeexxtt((00,, ccaarrddss..CCoouunntt));; 
CCaarrdd ffiirrssttCCaarrdd == ccaarrddss[[iinnddeexx]];; 
CCaarrdd rraannddoommCCaarrdd == ccaarrddss[[rraannddoommIInnddeexx]];; 
ccaarrddss[[iinnddeexx]] == rraannddoommCCaarrdd;; 
ccaarrddss[[rraannddoommIInnddeexx]] == ffiirrssttCCaarrdd;; 
}} 
 TTeesstt sshhoowwss tthhaatt tthhee pprroobblleemm iiss ffiixxeedd
TTeesstt wwiitthh 00 CCaarrddss;; RReeggrreessssiioonn 
TTeessttss 
63 
 TTeessttiinngg wwiitthh 00 ccaarrddss ((eemmppttyy lliisstt)) ggeenneerraatteess aann 
eemmppttyy lliisstt  ccoorrrreecctt rreessuulltt 
 SSeeeemmss lliikkee tthhee ccaarrddss sshhuuffffllee aallggoorriitthhmm wwoorrkkss 
ccoorrrreeccttllyy aafftteerr tthhee llaasstt ffeeww ffiixxeess 
NNeeeeddss aa rreeggrreessssiioonn tteesstt 
TTeesstt aaggaaiinn tthhaatt nneeww cchhaannggeess ddiidd nnoott bbrreeaakk aallll 
pprreevviioouussllyy wwoorrkkiinngg ccaasseess 
 TTeesstt wwiitthh ffuullll ddeecckk ooff 5522 ccaarrddss;; wwiitthh 11 ccaarrdd;; 
wwiitthh 22 ccaarrddss wwiitthh 00 ccaarrddss  eevveerryytthhiinngg wwoorrkkss
LLooaadd TTeesstt –– 5522 000000 CCaarrddss 
 FFiinnaallllyy wwee nneeeedd aa llooaadd tteesstt wwiitthh 5522 000000 ccaarrddss:: 
64 
ssttaattiicc vvooiidd TTeessttSShhuuffffllee5522000000CCaarrddss(()) 
{{ 
LLiisstt<<CCaarrdd>> ccaarrddss == nneeww LLiisstt<<CCaarrdd>>(());; 
ssttrriinngg[[]] aallllFFaacceess == nneeww ssttrriinngg[[]] {{""22"",, ""33"",, ""44"",, ""55"",, 
""66"",, ""77"",, ""88"",, ""99"",, ""1100"",, ""JJ"",, ""QQ"",, ""KK"",, ""AA""}};; 
SSuuiitt[[]] aallllSSuuiittss == nneeww SSuuiitt[[]] {{ SSuuiitt..CCLLUUBB,, SSuuiitt..DDIIAAMMOONNDD,, 
SSuuiitt..HHEEAARRTT,, SSuuiitt..SSPPAADDEE}};; 
ffoorr ((iinntt ii == 00;; ii << 11000000;; ii++++)) 
ffoorreeaacchh ((ssttrriinngg ffaaccee iinn aallllFFaacceess)) 
ffoorreeaacchh ((SSuuiitt ssuuiitt iinn aallllSSuuiittss)) 
ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ffaaccee,, SSuuiitt == ssuuiitt }}));; 
SShhuufffflleeCCaarrddss((ccaarrddss));; 
PPrriinnttCCaarrddss((ccaarrddss));; 
}}
SSuummmmaarryy 
11.. PPrroobblleemmss ssoollvviinngg nneeeeddss mmeetthhooddoollooggyy:: 
 UUnnddeerrssttaannddiinngg aanndd aannaallyyzziinngg pprroobblleemmss 
 UUssiinngg aa sshheeeett ooff ppaappeerr aanndd aa ppeenn ffoorr sskkeettcchhiinngg 
 TThhiinnkkiinngg uupp,, iinnvveennttiinngg aanndd ttrryyiinngg iiddeeaass 
 DDeeccoommppoossiinngg pprroobblleemmss iinnttoo ssuubbpprroobblleemmss 
 SSeelleeccttiinngg aapppprroopprriiaattee ddaattaa ssttrruuccttuurreess 
 TThhiinnkkiinngg aabboouutt tthhee eeffffiicciieennccyy aanndd ppeerrffoorrmmaannccee 
 IImmpplleemmeennttiinngg sstteepp--bbyy--sstteepp 
 TTeessttiinngg tthhee nnoommiinnaall ccaassee,, bboorrddeerr ccaasseess aanndd 
eeffffiicciieennccyy 
65
MMeetthhooddoollooggyy ooff 
PPrroobblleemm SSoollvviinngg 
QQuueessttiioonnss?? 
http://academy.telerik.com
EExxeerrcciisseess 
11.. FFoolllloowwiinngg tthhee pprroobblleemmss 
ssoollvviinngg mmeetthhooddoollooggyy ttrryy ttoo 
aapppprrooaacchh tthhee ffoolllloowwiinngg 
pprroobblleemm:: WWee aarree ggiivveenn NN 
ppooiinnttss ((NN << 110000 000000)) iinn tthhee 
ppllaannee ((rreepprreesseenntteedd aass iinntteeggeerr 
ccoooorrddiinnaatteess xxii aanndd yyii)).. WWrriittee aa 
pprrooggrraamm ttoo ffiinndd aallll ppoossssiibbllee 
hhoorriizzoonnttaall oorr vveerrttiiccaall ssttrraaiigghhtt 
lliinneess tthhaatt sspplliitt tthhee ppllaannee iinnttoo 
ttwwoo ppaarrttss,, eeaacchh hhoollddiinngg eeqquuaall 
nnuummbbeerr ooff ppooiinnttss ((ppooiinnttss aatt 
tthhee lliinnee aarree nnoott ccoouunntteedd)).. 
67
EExxeerrcciisseess ((22)) 
22.. FFoolllloowwiinngg tthhee pprroobblleemmss ssoollvviinngg mmeetthhooddoollooggyy ssoollvvee 
tthhee ffoolllloowwiinngg pprroobblleemm:: wwee aarree ggiivveenn aa sseett SS ooff nn 
iinntteeggeerr nnuummbbeerrss aanndd aa ppoossiittiivvee iinntteeggeerr kk ((kk ≤≤ nn ≤≤ 1100)).. 
AAlltteerrnnaattiinngg sseeqquueennccee ooff nnuummbbeerrss iiss aa sseeqquueennccee tthhaatt 
aalltteerrnnaattiivveellyy cchhaannggeess ffrroomm iinnccrreeaassiinngg ttoo ddeeccrreeaassiinngg 
ooff vviiccee vveerrssaa aafftteerr eeaacchh ooff iittss eelleemmeennttss.. WWrriittee aa 
pprrooggrraamm ttoo ggeenneerraattee aallll aalltteerrnnaattiinngg sseeqquueenncceess ss11,, ss22,, 
……,, sskk ccoonnssiissttiinngg ooff kk ddiissttiinncctt eelleemmeennttss ffrroomm SS.. 
EExxaammppllee:: SS == {{ 22,, 55,, 33,, 44 }}  {{22,, 44,, 33}},, {{22,, 55,, 33}},, {{22,, 55,, 44}},, 
{{33,, 22,, 44}},, {{33,, 22,, 55}},, {{33,, 44,, 22}},, {{33,, 55,, 22}},, {{33,, 55,, 44}},, {{44,, 22,, 33}},, 
{{44,, 22,, 55}},, {{44,, 33,, 55}},, {{55,, 22,, 33}},, {{55,, 22,, 44}},, {{55,, 33,, 44}} 
68

More Related Content

What's hot

Developed and developing countries lecture
Developed and developing countries lectureDeveloped and developing countries lecture
Developed and developing countries lecturecruxxade
 
Presentación curso 2014 - 15
Presentación curso 2014 - 15Presentación curso 2014 - 15
Presentación curso 2014 - 15MercheClemente
 
Proyecto Aparato Circulatorio Ilan Carmona
Proyecto Aparato Circulatorio Ilan CarmonaProyecto Aparato Circulatorio Ilan Carmona
Proyecto Aparato Circulatorio Ilan Carmonaalumnosdeprimaria2014
 
Lt9 -pricing stragsforaspac
Lt9 -pricing stragsforaspacLt9 -pricing stragsforaspac
Lt9 -pricing stragsforaspacsamuel abebaw
 
Lambi Village — This is how you help Haitians
Lambi Village — This is how you help HaitiansLambi Village — This is how you help Haitians
Lambi Village — This is how you help HaitiansChris Johnson
 
Social work practice with individuals
Social work practice with individualsSocial work practice with individuals
Social work practice with individualsCathy Co
 
Parecer Técnico-Científicodo perito independente Leví Inimá de Miranda
 Parecer Técnico-Científicodo perito independente Leví Inimá de Miranda Parecer Técnico-Científicodo perito independente Leví Inimá de Miranda
Parecer Técnico-Científicodo perito independente Leví Inimá de MirandaMarcelo Auler
 
Source control system
Source control systemSource control system
Source control systemDotNetMarche
 
Infective endocarditis case presentation by dr imtiaz
Infective endocarditis case presentation by dr imtiazInfective endocarditis case presentation by dr imtiaz
Infective endocarditis case presentation by dr imtiazWest Medicine Ward
 
FISIKA TANAH
FISIKA TANAHFISIKA TANAH
FISIKA TANAHEDIS BLOG
 

What's hot (20)

Chaste beautiful love 1
Chaste beautiful love   1Chaste beautiful love   1
Chaste beautiful love 1
 
A true woman 5
A true woman   5A true woman   5
A true woman 5
 
Julia Beatriz Rodriguez
Julia Beatriz RodriguezJulia Beatriz Rodriguez
Julia Beatriz Rodriguez
 
Christian courtship 4
Christian courtship   4Christian courtship   4
Christian courtship 4
 
Developed and developing countries lecture
Developed and developing countries lectureDeveloped and developing countries lecture
Developed and developing countries lecture
 
True love 6
True love   6True love   6
True love 6
 
Surete
SureteSurete
Surete
 
Presentación curso 2014 - 15
Presentación curso 2014 - 15Presentación curso 2014 - 15
Presentación curso 2014 - 15
 
Teoriabalance
TeoriabalanceTeoriabalance
Teoriabalance
 
Jihad
JihadJihad
Jihad
 
Proyecto Aparato Circulatorio Ilan Carmona
Proyecto Aparato Circulatorio Ilan CarmonaProyecto Aparato Circulatorio Ilan Carmona
Proyecto Aparato Circulatorio Ilan Carmona
 
Lt9 -pricing stragsforaspac
Lt9 -pricing stragsforaspacLt9 -pricing stragsforaspac
Lt9 -pricing stragsforaspac
 
Lambi Village — This is how you help Haitians
Lambi Village — This is how you help HaitiansLambi Village — This is how you help Haitians
Lambi Village — This is how you help Haitians
 
Social work practice with individuals
Social work practice with individualsSocial work practice with individuals
Social work practice with individuals
 
Parecer Técnico-Científicodo perito independente Leví Inimá de Miranda
 Parecer Técnico-Científicodo perito independente Leví Inimá de Miranda Parecer Técnico-Científicodo perito independente Leví Inimá de Miranda
Parecer Técnico-Científicodo perito independente Leví Inimá de Miranda
 
Demand forecasting
Demand forecasting Demand forecasting
Demand forecasting
 
Source control system
Source control systemSource control system
Source control system
 
Infective endocarditis case presentation by dr imtiaz
Infective endocarditis case presentation by dr imtiazInfective endocarditis case presentation by dr imtiaz
Infective endocarditis case presentation by dr imtiaz
 
FISIKA TANAH
FISIKA TANAHFISIKA TANAH
FISIKA TANAH
 
Chapter 1 revised2
Chapter 1 revised2Chapter 1 revised2
Chapter 1 revised2
 

Similar to 22 Methodology of problem solving

pre-prosthetic surgery
 pre-prosthetic surgery pre-prosthetic surgery
pre-prosthetic surgeryvasanramkumar
 
Political Identity Assignment
Political Identity AssignmentPolitical Identity Assignment
Political Identity Assignmentjkoryan
 
Treatment at Grace Walker Physical Therapy and Pain Center
Treatment at Grace Walker Physical Therapy and Pain CenterTreatment at Grace Walker Physical Therapy and Pain Center
Treatment at Grace Walker Physical Therapy and Pain CenterDoctor Grace Walker
 
Implant parts/dental implant courses by Indian dental academy
Implant parts/dental implant courses by Indian dental academyImplant parts/dental implant courses by Indian dental academy
Implant parts/dental implant courses by Indian dental academyIndian dental academy
 
Liver cirrhosis-hematemsis-peptic Ulcer Lecture
Liver cirrhosis-hematemsis-peptic Ulcer LectureLiver cirrhosis-hematemsis-peptic Ulcer Lecture
Liver cirrhosis-hematemsis-peptic Ulcer LectureNadine El-Hussieny
 
1 pigmentation
1 pigmentation1 pigmentation
1 pigmentationAhmed Amer
 
business plan presentation
business plan presentationbusiness plan presentation
business plan presentationHotRat
 
3r ESO: ESTRUCTURA DE LA MATÈRIA
3r ESO: ESTRUCTURA DE LA MATÈRIA3r ESO: ESTRUCTURA DE LA MATÈRIA
3r ESO: ESTRUCTURA DE LA MATÈRIArosaquima
 
Character n characterisation
Character n characterisationCharacter n characterisation
Character n characterisationEric Mukira
 
Symmetry and group theory
Symmetry and group theorySymmetry and group theory
Symmetry and group theorychemistry50
 
Indian Financial System Notes MBA PPT
Indian Financial System Notes MBA PPTIndian Financial System Notes MBA PPT
Indian Financial System Notes MBA PPTashishseo2013
 
#6 digestive system general maintenance and disorders
#6 digestive system general maintenance and disorders#6 digestive system general maintenance and disorders
#6 digestive system general maintenance and disordersHome Makers
 
Presentacion InfoLac 2014 - generacion de interfaz de usuario para sistemas d...
Presentacion InfoLac 2014 - generacion de interfaz de usuario para sistemas d...Presentacion InfoLac 2014 - generacion de interfaz de usuario para sistemas d...
Presentacion InfoLac 2014 - generacion de interfaz de usuario para sistemas d...Pablo Pazos
 
Prehistoriaenlapennsulaibrica 090220134645-phpapp02
Prehistoriaenlapennsulaibrica 090220134645-phpapp02Prehistoriaenlapennsulaibrica 090220134645-phpapp02
Prehistoriaenlapennsulaibrica 090220134645-phpapp02Sergio Garcia
 

Similar to 22 Methodology of problem solving (20)

Concurrent Engineering
Concurrent EngineeringConcurrent Engineering
Concurrent Engineering
 
pre-prosthetic surgery
 pre-prosthetic surgery pre-prosthetic surgery
pre-prosthetic surgery
 
Political Identity Assignment
Political Identity AssignmentPolitical Identity Assignment
Political Identity Assignment
 
Treatment at Grace Walker Physical Therapy and Pain Center
Treatment at Grace Walker Physical Therapy and Pain CenterTreatment at Grace Walker Physical Therapy and Pain Center
Treatment at Grace Walker Physical Therapy and Pain Center
 
Implant parts/dental implant courses by Indian dental academy
Implant parts/dental implant courses by Indian dental academyImplant parts/dental implant courses by Indian dental academy
Implant parts/dental implant courses by Indian dental academy
 
Liver cirrhosis-hematemsis-peptic Ulcer Lecture
Liver cirrhosis-hematemsis-peptic Ulcer LectureLiver cirrhosis-hematemsis-peptic Ulcer Lecture
Liver cirrhosis-hematemsis-peptic Ulcer Lecture
 
1 pigmentation
1 pigmentation1 pigmentation
1 pigmentation
 
business plan presentation
business plan presentationbusiness plan presentation
business plan presentation
 
Types of communication
Types of communicationTypes of communication
Types of communication
 
Chapter5
Chapter5Chapter5
Chapter5
 
3r ESO: ESTRUCTURA DE LA MATÈRIA
3r ESO: ESTRUCTURA DE LA MATÈRIA3r ESO: ESTRUCTURA DE LA MATÈRIA
3r ESO: ESTRUCTURA DE LA MATÈRIA
 
Life insurance
Life insuranceLife insurance
Life insurance
 
Ch. 1 history
Ch. 1 historyCh. 1 history
Ch. 1 history
 
Character n characterisation
Character n characterisationCharacter n characterisation
Character n characterisation
 
Symmetry and group theory
Symmetry and group theorySymmetry and group theory
Symmetry and group theory
 
Indian Financial System Notes MBA PPT
Indian Financial System Notes MBA PPTIndian Financial System Notes MBA PPT
Indian Financial System Notes MBA PPT
 
Els sentits
Els sentitsEls sentits
Els sentits
 
#6 digestive system general maintenance and disorders
#6 digestive system general maintenance and disorders#6 digestive system general maintenance and disorders
#6 digestive system general maintenance and disorders
 
Presentacion InfoLac 2014 - generacion de interfaz de usuario para sistemas d...
Presentacion InfoLac 2014 - generacion de interfaz de usuario para sistemas d...Presentacion InfoLac 2014 - generacion de interfaz de usuario para sistemas d...
Presentacion InfoLac 2014 - generacion de interfaz de usuario para sistemas d...
 
Prehistoriaenlapennsulaibrica 090220134645-phpapp02
Prehistoriaenlapennsulaibrica 090220134645-phpapp02Prehistoriaenlapennsulaibrica 090220134645-phpapp02
Prehistoriaenlapennsulaibrica 090220134645-phpapp02
 

More from maznabili

21 High-quality programming code construction part-ii
21 High-quality programming code construction part-ii21 High-quality programming code construction part-ii
21 High-quality programming code construction part-iimaznabili
 
21 high-quality programming code construction part-i
21 high-quality programming code construction part-i21 high-quality programming code construction part-i
21 high-quality programming code construction part-imaznabili
 
20 Object-oriented programming principles
20 Object-oriented programming principles20 Object-oriented programming principles
20 Object-oriented programming principlesmaznabili
 
19 Algorithms and complexity
19 Algorithms and complexity19 Algorithms and complexity
19 Algorithms and complexitymaznabili
 
18 Hash tables and sets
18 Hash tables and sets18 Hash tables and sets
18 Hash tables and setsmaznabili
 
17 Trees and graphs
17 Trees and graphs17 Trees and graphs
17 Trees and graphsmaznabili
 
16 Linear data structures
16 Linear data structures16 Linear data structures
16 Linear data structuresmaznabili
 
15 Text files
15 Text files15 Text files
15 Text filesmaznabili
 
14 Defining classes
14 Defining classes14 Defining classes
14 Defining classesmaznabili
 
13 Strings and text processing
13 Strings and text processing13 Strings and text processing
13 Strings and text processingmaznabili
 
12 Exceptions handling
12 Exceptions handling12 Exceptions handling
12 Exceptions handlingmaznabili
 
11 Using classes and objects
11 Using classes and objects11 Using classes and objects
11 Using classes and objectsmaznabili
 
10 Recursion
10 Recursion10 Recursion
10 Recursionmaznabili
 
08 Numeral systems
08 Numeral systems08 Numeral systems
08 Numeral systemsmaznabili
 
05 Conditional statements
05 Conditional statements05 Conditional statements
05 Conditional statementsmaznabili
 
04 Console input output-
04 Console input output-04 Console input output-
04 Console input output-maznabili
 
03 Operators and expressions
03 Operators and expressions03 Operators and expressions
03 Operators and expressionsmaznabili
 

More from maznabili (20)

21 High-quality programming code construction part-ii
21 High-quality programming code construction part-ii21 High-quality programming code construction part-ii
21 High-quality programming code construction part-ii
 
21 high-quality programming code construction part-i
21 high-quality programming code construction part-i21 high-quality programming code construction part-i
21 high-quality programming code construction part-i
 
20 Object-oriented programming principles
20 Object-oriented programming principles20 Object-oriented programming principles
20 Object-oriented programming principles
 
19 Algorithms and complexity
19 Algorithms and complexity19 Algorithms and complexity
19 Algorithms and complexity
 
18 Hash tables and sets
18 Hash tables and sets18 Hash tables and sets
18 Hash tables and sets
 
17 Trees and graphs
17 Trees and graphs17 Trees and graphs
17 Trees and graphs
 
16 Linear data structures
16 Linear data structures16 Linear data structures
16 Linear data structures
 
15 Text files
15 Text files15 Text files
15 Text files
 
14 Defining classes
14 Defining classes14 Defining classes
14 Defining classes
 
13 Strings and text processing
13 Strings and text processing13 Strings and text processing
13 Strings and text processing
 
12 Exceptions handling
12 Exceptions handling12 Exceptions handling
12 Exceptions handling
 
11 Using classes and objects
11 Using classes and objects11 Using classes and objects
11 Using classes and objects
 
10 Recursion
10 Recursion10 Recursion
10 Recursion
 
09 Methods
09 Methods09 Methods
09 Methods
 
08 Numeral systems
08 Numeral systems08 Numeral systems
08 Numeral systems
 
07 Arrays
07 Arrays07 Arrays
07 Arrays
 
06 Loops
06 Loops06 Loops
06 Loops
 
05 Conditional statements
05 Conditional statements05 Conditional statements
05 Conditional statements
 
04 Console input output-
04 Console input output-04 Console input output-
04 Console input output-
 
03 Operators and expressions
03 Operators and expressions03 Operators and expressions
03 Operators and expressions
 

22 Methodology of problem solving

  • 1. MMeetthhooddoollooggyy ooff PPrroobblleemm SSoollvviinngg EEffffiicciieennttllyy SSoollvviinngg CCoommppuutteerr PPrrooggrraammmmiinngg PPrroobblleemmss SSvveettlliinn NNaakkoovv TTeelleerriikk CCoorrppoorraattiioonn wwwwww..tteelleerriikk..ccoomm
  • 2. PPrroobblleemmss SSoollvviinngg FFrroomm CChhaaoottiicc ttoo MMeetthhooddoollooggiiccaall AApppprrooaacchh
  • 3. HHooww ttoo SSoollvvee PPrroobblleemmss?? 11.. RReeaadd aanndd AAnnaallyyzzee tthhee PPrroobblleemmss 22.. UUssee aa sshheeeett ooff ppaappeerr aanndd aa ppeenn ffoorr sskkeettcchhiinngg 33.. TThhiinnkk uupp,, iinnvveenntt aanndd ttrryy iiddeeaass 44.. BBrreeaakk tthhee pprroobblleemm iinnttoo ssuubbpprroobblleemmss 55.. CChheecckk uupp yyoouurr iiddeeaass 66.. CChhoooossee aapppprroopprriiaattee ddaattaa ssttrruuccttuurreess 77.. TThhiinnkk aabboouutt tthhee eeffffiicciieennccyy 88.. IImmpplleemmeenntt yyoouurr aallggoorriitthhmm sstteepp--bbyy--sstteepp 99.. TThhoorroouugghhllyy tteesstt yyoouurr ssoolluuttiioonn 3
  • 5. RReeaadd aanndd AAnnaallyyzzee tthhee PPrroobblleemmss  CCoonnssiiddeerr yyoouu aarree aatt ttrraaddiittiioonnaall ccoommppuutteerr pprrooggrraammmmiinngg eexxaamm oorr ccoonntteesstt YYoouu hhaavvee 33--44 pprroobblleemmss ttoo ssoollvvee iinn 44--55 hhoouurrss  FFiirrsstt rreeaadd ccaarreeffuullllyy aallll pprroobblleemmss aanndd ttrryy ttoo eessttiimmaattee hhooww ccoommpplleexx eeaacchh ooff tthheemm iiss RReeaadd tthhee rreeqquuiirreemmeennttss,, ddoonn''tt iinnvveenntt tthheemm!!  SSttaarrtt ssoollvviinngg tthhee mmoosstt eeaassyy pprroobblleemm ffiirrsstt LLeeaavvee tthhee mmoosstt ccoommpplleexx pprroobblleemm llaasstt  AApppprrooaacchh tthhee nneexxtt pprroobblleemm wwhheenn tthhee pprreevviioouuss iiss ccoommpplleetteellyy ssoollvveedd aanndd wweellll tteesstteedd 5
  • 6. AAnnaallyyzziinngg tthhee PPrroobblleemmss  EExxaammppllee:: wwee aarree ggiivveenn 33 pprroobblleemmss:: 11.. SShhuuffffllee--ccaarrddss SShhuuffffllee aa ddeecckk ooff ccaarrddss iinn rraannddoomm oorrddeerr 11.. SSttuuddeennttss RReeaadd aa sseett ooff ssttuuddeennttss aanndd tthheeiirr mmaarrkkss aanndd pprriinntt ttoopp 1100 ssttuuddeennttss wwiitthh tthhee bbeesstt rreessuullttss ((bbyy aavveerraaggiinngg tthheeiirr mmaarrkkss)) 11.. SSoorrttiinngg nnuummbbeerrss SSoorrtt aa sseett ooff nnuummbbeerrss iinn iinnccrreeaassiinngg oorrddeerr 6
  • 7. AAnnaallyyzziinngg tthhee PPrroobblleemmss ((22)) RReeaadd ccaarreeffuullllyy tthhee pprroobblleemmss aanndd tthhiinnkk aa bbiitt aabboouutt tthheeiirr ppoossssiibbllee ssoolluuttiioonnss OOrrddeerr tthhee pprroobblleemmss ffrroomm tthhee eeaassiieesstt ttoo tthhee mmoosstt ccoommpplleexx:: 11.. SSoorrttiinngg nnuummbbeerrss TTrriivviiaall –– wwee ccaann uussee tthhee bbuuiilltt--iinn ssoorrttiinngg iinn ..NNEETT 11.. SShhuuffffllee--ccaarrddss NNeeeedd ttoo rraannddoommiizzee tthhee eelleemmeennttss ooff aarrrraayy 11.. SSttuuddeennttss NNeeeeddss ssuummmmiinngg,, ssoorrttiinngg aanndd tteexxtt ffiillee pprroocceessssiinngg 7
  • 8. UUssiinngg aa PPaappeerr aanndd aa PPeenn VViissuuaalliizziinngg aanndd SSkkeettcchhiinngg yyoouurr IIddeeaass
  • 9. UUssee aa SShheeeett ooff PPaappeerr aanndd aa PPeenn NNeevveerr ssttaarrtt ssoollvviinngg aa pprroobblleemm wwiitthhoouutt aa sshheeeett ooff ppaappeerr aanndd aa ppeenn YYoouu nneeeedd ttoo sskkeettcchh yyoouurr iiddeeaass PPaappeerr aanndd ppeenn iiss tthhee bbeesstt vviissuuaalliizzaattiioonn ttooooll AAlllloowwss yyoouurr bbrraaiinn ttoo tthhiinnkk eeffffiicciieennttllyy PPaappeerr wwoorrkkss ffaasstteerr tthhaann kkeeyybbooaarrdd // ssccrreeeenn OOtthheerr vviissuuaalliizzaattiioonn ttooooll ccoouulldd aallssoo wwoorrkk wweellll 9
  • 10. PPaappeerr aanndd PPeenn  CCoonnssiiddeerr tthhee ""ccaarrddss sshhuuffffllee"" pprroobblleemm WWee ccaann sskkeettcchh iitt ttoo ssttaarrtt tthhiinnkkiinngg SSoommee iiddeeaass iimmmmeeddiiaatteellyy ccoommee,, ee..gg.. SSpplliitt tthhee ddeecckk iinnttoo ttwwoo ppaarrttss aanndd sswwaapp tthheemm aa mmuullttiippllee ttiimmeess SSwwaapp 22 rraannddoomm ccaarrddss aa rraannddoomm nnuummbbeerr ooff ttiimmeess SSwwaapp eeaacchh ccaarrdd wwiitthh aa rraannddoomm ccaarrdd 10
  • 11. IInnvveenntt IIddeeaass TThhiinnkk--uupp,, IInnvveenntt IIddeeaass aanndd CChheecckk TThheemm
  • 12. TThhiinnkk uupp,, IInnvveenntt aanndd TTrryy IIddeeaass  FFiirrsstt ttaakkee aann eexxaammppllee ooff tthhee pprroobblleemm SSkkeettcchh iitt oonn tthhee sshheeeett ooff ppaappeerr NNeexxtt ttrryy ttoo iinnvveenntt ssoommee iiddeeaa tthhaatt wwoorrkkss ffoorr yyoouurr eexxaammppllee  CChheecckk iiff yyoouurr iiddeeaa wwiillll wwoorrkk ffoorr ootthheerr eexxaammpplleess TTrryy ttoo ffiinndd aa ccaassee tthhaatt bbrreeaakkss yyoouurr iiddeeaa TTrryy cchhaalllleennggiinngg eexxaammpplleess aanndd uunnuussuuaall ccaasseess  IIff yyoouu ffiinndd yyoouurr iiddeeaa iinnccoorrrreecctt,, ttrryy ttoo ffiixx iitt oorr jjuusstt iinnvveenntt aa nneeww iiddeeaa 12
  • 13. IInnvveenntt aanndd TTrryy IIddeeaass –– EExxaammppllee  CCoonnssiiddeerr tthhee ""ccaarrddss sshhuuffffllee"" pprroobblleemm  IIddeeaa ##11:: rraannddoomm nnuummbbeerr ooff ttiimmeess sspplliitt tthhee ddeecckk iinnttoo lleefftt aanndd rriigghhtt ppaarrtt aanndd sswwaapp tthheemm HHooww ttoo rreepprreesseenntt tthhee ccaarrddss?? HHooww ttoo cchhoossee aa rraannddoomm sspplliitt ppooiinntt?? HHooww ttoo ppeerrffoorrmm tthhee eexxcchhaannggee??  IIddeeaa ##22:: sswwaapp eeaacchh ccaarrdd wwiitthh aa rraannddoomm ccaarrdd HHooww mmaannyy ttiimmeess ttoo rreeppeeaatt tthhiiss?? IIss tthhiiss ffaasstt eennoouugghh?? 13
  • 14. IInnvveenntt aanndd TTrryy IIddeeaass –– EExxaammppllee ((22))  IIddeeaa ##33:: sswwaapp 22 rraannddoomm ccaarrddss aa rraannddoomm nnuummbbeerr ooff ttiimmeess HHooww ttoo cchhoooossee ttwwoo rraannddoomm ccaarrddss?? HHooww mmaannyy ttiimmeess rreeppeeaatt tthhiiss??  IIddeeaa ##44:: cchhoooossee aa rraannddoomm ccaarrdd aanndd iinnsseerrtt iitt iinn ffrroonntt ooff tthhee ddeecckk HHooww ttoo cchhoooossee rraannddoomm ccaarrdd?? HHooww mmaannyy ttiimmeess rreeppeeaatt tthhiiss?? 14
  • 15. DDiivviiddee aanndd CCoonnqquueerr DDeeccoommppoossee PPrroobblleemmss iinnttoo MMaannaaggeeaabbllee PPiieecceess
  • 16. DDeeccoommppoossee tthhee PPrroobblleemm iinnttoo SSuubbpprroobblleemmss WWoorrkk ddeeccoommppoossiittiioonn iiss nnaattuurraall iinn eennggiinneeeerriinngg IItt hhaappppeennss eevveerryy ddaayy iinn tthhee iinndduussttrryy PPrroojjeeccttss aarree ddeeccoommppoosseedd iinnttoo ssuubbpprroojjeeccttss  CCoommpplleexx pprroobblleemmss ccoouulldd bbee ddeeccoommppoosseedd iinnttoo sseevveerraall ssmmaalllleerr ssuubbpprroobblleemmss TTeecchhnniiqquuee kknnoowwnn aass ""DDiivviiddee aanndd CCoonnqquueerr"" SSmmaallll pprroobblleemmss ccoouulldd eeaassiillyy bbee ssoollvveedd SSmmaalllleerr ssuubbpprroobblleemmss ccoouulldd bbee ffuurrtthheerr ddeeccoommppoosseedd aass wweellll 16
  • 17. DDiivviiddee aanndd CCoonnqquueerr –– EExxaammppllee  LLeett''ss ttrryy iiddeeaa ##11:: MMuullttiippllee ttiimmee sspplliitt tthhee ddeecckk iinnttoo lleefftt aanndd rriigghhtt ppaarrtt aanndd sswwaapp tthheemm DDiivviiddee aanndd ccoonnqquueerr SSuubbpprroobblleemm ##11 ((ssiinnggllee eexxcchhaannggee)) –– sspplliitt tthhee ddeecckk iinnttoo ttwwoo rraannddoomm ppaarrttss aanndd eexxcchhaannggee tthheemm SSuubbpprroobblleemm ##22 –– cchhoooossiinngg aa rraannddoomm sspplliitt ppooiinntt SSuubbpprroobblleemm ##33 –– ccoommbbiinniinngg ssiinnggllee eexxcchhaannggeess HHooww mmaannyy ttiimmeess ttoo ppeerrffoorrmm ""ssiinnggllee eexxcchhaannggee""?? 17
  • 18. Subproblem ##11 ((SSiinnggllee EExxcchhaannggee))  SSpplliitt tthhee ddeecckk iinnttoo 22 ppaarrttss aatt rraannddoomm sspplliitt ppooiinntt aanndd eexxcchhaannggee tthheessee 22 ppaarrttss WWee vviissuuaalliizzee tthhiiss bbyy ppaappeerr aanndd ppeenn:: 18
  • 19. Subproblem #2 (Random Split Point)  CChhoooossiinngg aa rraannddoomm sspplliitt ppooiinntt NNeeeeddss ttoo uunnddeerrssttaanndd tthhee ccoonncceepptt ooff ppsseeuuddoo-- rraannddoomm nnuummbbeerrss aanndd hhooww ttoo uussee iitt IInn IInntteerrnneett lloottss ooff eexxaammpplleess aarree aavvaaiillaabbllee,, ssoommee ooff tthheemm iinnccoorrrreecctt  TThhee ccllaassss SSyysstteemm..RRaannddoomm ccaann ddoo tthhee jjoobb  IImmppoorrttaanntt ddeettaaiill iiss tthhaatt tthhee RRaannddoomm ccllaassss sshhoouulldd bbee iinnssttaannttiiaatteedd oonnllyy oonnccee NNoott aatt eeaacchh rraannddoomm nnuummbbeerr ggeenneerraattiioonn 19
  • 20. Subproblem #3 (Combining Single Exchanges)  Combining a sseeqquueennccee ooff ssiinnggllee eexxcchhaannggeess ttoo ssoollvvee tthhee iinniittiiaall pprroobblleemm HHooww mmaannyy ttiimmeess ttoo ppeerrffoorrmm ssiinnggllee eexxcchhaannggeess ttoo rreelliiaabbllyy rraannddoommiizzee tthhee ddeecckk?? NN ttiimmeess ((wwhheerree NN iiss tthhee nnuummbbeerr ooff tthhee ccaarrddss)) sseeeemmss eennoouugghh WWee hhaavvee aann aallggoorriitthhmm:: NN ttiimmeess sspplliitt aatt rraannddoomm ppoossiittiioonn aanndd eexxcchhaannggee tthhee lleefftt aanndd rriigghhtt ppaarrttss ooff tthhee ddeecckk 20
  • 21. CChheecckk--uupp YYoouurr IIddeeaass DDoonn''tt ggoo AAhheeaadd bbeeffoorree CChheecckkiinngg YYoouurr IIddeeaass
  • 22. CChheecckk--uupp YYoouurr IIddeeaass  CChheecckk--uupp yyoouurr iiddeeaass wwiitthh eexxaammpplleess IItt iiss bbeetttteerr ttoo ffiinndd aa pprroobblleemm bbeeffoorree tthhee iiddeeaa iiss iimmpplleemmeenntteedd WWhheenn tthhee ccooddee iiss wwrriitttteenn,, cchhaannggiinngg rraaddiiccaallllyy yyoouurr iiddeeaass ccoossttss aa lloott ooff ttiimmee aanndd eeffffoorrtt  CCaarreeffuullllyy sseelleecctt eexxaammpplleess ffoorr cchheecckk--uupp EExxaammpplleess sshhoouulldd bbee ssiimmppllee eennoouugghh ttoo bbee cchheecckkeedd bbyy hhaanndd iinn aa mmiinnuuttee EExxaammpplleess sshhoouulldd bbee ccoommpplleexx eennoouugghh ttoo ccoovveerr tthhee mmoosstt ggeenneerraall ccaassee,, nnoott jjuusstt aann iissoollaatteedd ccaassee 22
  • 23. CChheecckk--uupp YYoouurr IIddeeaass –– EExxaammppllee  LLeett''ss cchheecckk tthhee iiddeeaa:: AAfftteerr 33 rraannddoomm sspplliittss aanndd sswwaappss wwee oobbttaaiinn tthhee ssttaarrtt ppoossiittiioonn  sseeeemmss lliikkee aa bbuugg!! 23
  • 24. IInnvveenntt NNeeww IIddeeaa IIff NNeeeeddeedd WWhhaatt ttoo ddoo wwhheenn yyoouu ffiinndd yyoouurr iiddeeaa iiss nnoott wwoorrkkiinngg iinn aallll ccaasseess?? TTrryy ttoo ffiixx yyoouurr iiddeeaa SSoommeettiimmee aa ssmmaallll cchhaannggee ccoouulldd ffiixx tthhee pprroobblleemm IInnvveenntt nneeww iiddeeaa aanndd ccaarreeffuullllyy cchheecckk iitt  IItteerraattee IItt iiss uussuuaall tthhaatt yyoouurr ffiirrsstt iiddeeaa iiss nnoott tthhee bbeesstt IInnvveenntt iiddeeaass,, cchheecckk tthheemm,, ttrryy vvaarriioouuss ccaasseess,, ffiinndd pprroobblleemmss,, ffiixx tthheemm,, iinnvveenntt bbeetttteerr iiddeeaa,, eettcc.. 24
  • 25. IInnvveenntt NNeeww IIddeeaass –– EExxaammppllee  IInnvveenntt ffeeww nneeww iiddeeaass:: NNeeww iiddeeaa ##11 –– mmuullttiippllee ttiimmeess sseelleecctt 22 rraannddoomm ccaarrddss aanndd eexxcchhaannggee tthheemm NNeeww iiddeeaa ##22 –– mmuullttiippllee ttiimmeess sseelleecctt aa rraannddoomm ccaarrdd aanndd eexxcchhaannggee iitt wwiitthh tthhee ffiirrsstt ccaarrdd NNeeww iiddeeaa ##33 –– mmuullttiippllee ttiimmeess sseelleecctt aa rraannddoomm ccaarrdd aanndd mmoovvee iitt ttoo aann eexxtteerrnnaall lliisstt  LLeett''ss cchheecckk tthhee nneeww iiddeeaa ##22 IIss iitt ccoorrrreecctt?? 25
  • 26. CChheecckk--uupp tthhee NNeeww IIddeeaa –– EExxaammppllee 26 TThhee rreessuulltt sseeeemmss ccoorrrreecctt
  • 27. TThhiinnkk aabboouutt DDaattaa SSttrruuccttuurreess SSeelleecctt DDaattaa SSttrruuccttuurreess tthhaatt WWiillll WWoorrkk WWeellll
  • 28. CChhoooossiinngg AApppprroopprriiaattee DDaattaa SSttrruuccttuurreess  CChhoooossee aapppprroopprriiaattee ddaattaa ssttrruuccttuurreess bbeeffoorree tthhee ssttaarrtt ooff ccooddiinngg TThhiinnkk hhooww ttoo rreepprreesseenntt iinnppuutt ddaattaa TThhiinnkk hhooww ttoo rreepprreesseenntt iinntteerrmmeeddiiaattee pprrooggrraamm ssttaattee TThhiinnkk hhooww ttoo rreepprreesseenntt tthhee rreeqquueesstteedd oouuttppuutt YYoouu ccoouulldd ffiinndd tthhaatt yyoouurr iiddeeaa ccaannnnoott bbee iimmpplleemmeenntteedd eeffffiicciieennttllyy OOrr iimmpplleemmeennttaattiioonn wwiillll bbee vveerryy ccoommpplleexx oorr iinneeffffiicciieenntt 28
  • 29. CChhoooossee AApppprroopprriiaattee DDaattaa SSttrruuccttuurreess –– EExxaammppllee HHooww ttoo rreepprreesseenntt aa ssiinnggllee ccaarrdd?? TThhee bbeesstt iiddeeaa iiss ttoo ccrreeaattee aa ssttrruuccttuurree CCaarrdd FFaaccee –– ccoouulldd bbee ssttrriinngg,, iinntt oorr eennuummeerraattiioonn SSuuiitt –– eennuummeerraattiioonn HHooww ttoo rreepprreesseenntt aa ddeecckk ooff ccaarrddss?? AArrrraayy –– CCaarrdd[[]] IInnddeexxeedd lliisstt –– LLiisstt<<CCaarrdd>> SSeett // DDiiccttiioonnaarryy // QQuueeuuee // SSttaacckk –– nnoott aa ffiitt 29
  • 30. EEffffiicciieennccyy aanndd PPeerrffoorrmmaannccee IIss YYoouurr AAllggoorriitthhmm FFaasstt EEnnoouugghh??
  • 31. TThhiinnkk AAbboouutt tthhee EEffffiicciieennccyy  TThhiinnkk aabboouutt eeffffiicciieennccyy bbeeffoorree wwrriittiinngg tthhee ffiirrsstt lliinnee ooff ccooddee EEssttiimmaattee tthhee rruunnnniinngg ttiimmee ((aassyymmppttoottiicc ccoommpplleexxiittyy)) CChheecckk tthhee rreeqquuiirreemmeennttss WWiillll yyoouurr aallggoorriitthhmm bbee ffaasstt eennoouugghh ttoo ccoonnffoorrmm wwiitthh tthheemm YYoouu ddoonn''tt wwaanntt ttoo iimmpplleemmeenntt yyoouurr aallggoorriitthhmm aanndd ffiinndd tthhaatt iitt iiss ssllooww wwhheenn tteessttiinngg YYoouu wwiillll lloossee yyoouurr ttiimmee 31
  • 32. EEffffiicciieennccyy iiss nnoott AAllwwaayyss RReeqquuiirreedd  BBeesstt ssoolluuttiioonn iiss ssoommeettiimmeess jjuusstt nnoott nneeeeddeedd RReeaadd ccaarreeffuullllyy yyoouurr pprroobblleemm ssttaatteemmeenntt SSoommeettiimmeess uuggllyy ssoolluuttiioonn ccoouulldd wwoorrkk ffoorr yyoouurr rreeqquuiirreemmeennttss aanndd iitt wwiillll ccoosstt yyoouu lleessss ttiimmee EExxaammppllee:: iiff yyoouu nneeeedd ttoo ssoorrtt nn nnuummbbeerrss,, aannyy aallggoorriitthhmm wwiillll wwoorrkk wwhheenn nn ∈∈ [[00....550000]]  IImmpplleemmeenntt ccoommpplleexx aallggoorriitthhmmss oonnllyy wwhheenn tthhee pprroobblleemm rreeaallllyy nneeeeddss tthheemm 32
  • 33. EEffffiicciieennccyy –– EExxaammppllee HHooww mmuucchh ccaarrddss wwee hhaavvee?? IInn aa ttyyppiiccaall ddeecckk wwee hhaavvee 5522 ccaarrddss NNoo mmaatttteerr hhooww ffaasstt tthhee aallggoorriitthhmm iiss –– iitt wwiillll wwoorrkk ffaasstt eennoouugghh IIff wwee hhaavvee NN ccaarrddss,, wwee ppeerrffoorrmm NN sswwaappss  tthhee eexxppeecctteedd rruunnnniinngg ttiimmee iiss OO((NN)) OO((NN)) wwiillll wwoorrkk ffaasstt ffoorr mmiilllliioonnss ooff ccaarrddss  CCoonncclluussiioonn:: tthhee eeffffiicciieennccyy iiss nnoott aann iissssuuee iinn tthhiiss pprroobblleemm 33
  • 34. IImmpplleemmeennttaattiioonn CCooddiinngg aanndd TTeessttiinngg SStteepp--bbyy--SStteepp
  • 35. SSttaarrtt CCooddiinngg:: CChheecckk LLiisstt NNeevveerr ssttaarrtt ccooddiinngg bbeeffoorree yyoouu ffiinndd ccoorrrreecctt iiddeeaa tthhaatt wwiillll mmeeeett tthhee rreeqquuiirreemmeennttss WWhhaatt yyoouu wwiillll wwrriittee bbeeffoorree yyoouu iinnvveenntt aa ccoorrrreecctt iiddeeaa ttoo ssoollvvee tthhee pprroobblleemm??  CChheecckklliisstt ttoo ffoollllooww bbeeffoorree ssttaarrtt ooff ccooddiinngg:: EEnnssuurree yyoouu uunnddeerrssttaanndd wweellll tthhee rreeqquuiirreemmeennttss EEnnssuurree yyoouu hhaavvee iinnvveenntteedd aa ggoooodd iiddeeaa EEnnssuurree yyoouurr iiddeeaa iiss ccoorrrreecctt EEnnssuurree yyoouu kknnooww wwhhaatt ddaattaa ssttrruuccttuurreess ttoo uussee EEnnssuurree tthhee ppeerrffoorrmmaannccee wwiillll bbee ssuuffffiicciieenntt 35
  • 36. CCooddiinngg CChheecckk LLiisstt –– EExxaammppllee  CChheecckklliisstt bbeeffoorree ssttaarrtt ooff ccooddiinngg:: EEnnssuurree yyoouu uunnddeerrssttaanndd wweellll tthhee rreeqquuiirreemmeennttss YYeess,, sshhuuffffllee ggiivveenn ddeecckk ooff ccaarrddss EEnnssuurree yyoouu hhaavvee iinnvveenntteedd aa ccoorrrreecctt iiddeeaa YYeess,, tthhee iiddeeaa sseeeemmss ccoorrrreecctt aanndd iiss tteesstteedd EEnnssuurree yyoouu kknnooww wwhhaatt ddaattaa ssttrruuccttuurreess ttoo uussee CCllaassss CCaarrdd,, eennuummeerraattiioonn SSuuiitt aanndd LLiisstt<<CCaarrdd>> EEnnssuurree tthhee ppeerrffoorrmmaannccee wwiillll bbee ssuuffffiicciieenntt LLiinneeaarr rruunnnniinngg ttiimmee  ggoooodd ppeerrffoorrmmaannccee 36
  • 37. IImmpplleemmeenntt yyoouurr AAllggoorriitthhmm SStteepp--bbyy--SStteepp  ""SStteepp--bbyy--sstteepp"" aapppprrooaacchh iiss aallwwaayyss bbeetttteerr tthhaann ""bbuuiilldd aallll,, tthheenn tteesstt"" IImmpplleemmeenntt aa ppiieeccee ooff yyoouurr pprrooggrraamm aanndd tteesstt iitt TThhaann iimmpplleemmeenntt aannootthheerr ppiieeccee ooff tthhee pprrooggrraamm aanndd tteesstt iitt FFiinnaallllyy ppuutt ttooggeetthheerr aallll ppiieecceess aanndd tteesstt iitt  SSmmaallll iinnccrreemmeennttss ((sstteeppss)) rreevveeaall eerrrroorrss eeaarrllyy ""BBiigg--bboooomm"" iinntteeggrraattiioonn ttaakkeess mmoorree ttiimmee 37
  • 38. SStteepp ##11 –– CCllaassss CCaarrdd 38 ccllaassss CCaarrdd {{ ppuubblliicc ssttrriinngg FFaaccee {{ ggeett;; sseett;; }} ppuubblliicc SSuuiitt SSuuiitt {{ ggeett;; sseett;; }} ppuubblliicc oovveerrrriiddee ssttrriinngg TTooSSttrriinngg(()) {{ SSttrriinngg ccaarrdd == ""(("" ++ tthhiiss..FFaaccee ++ "" "" ++ tthhiiss..SSuuiitt ++""))"";; rreettuurrnn ccaarrdd;; }} }} eennuumm SSuuiitt {{ CCLLUUBB,, DDIIAAMMOONNDD,, HHEEAARRTT,, SSPPAADDEE }}
  • 39.  TTeessttiinngg tthhee ccllaassss CCaarrdd ttoo ggeett ffeeeeddbbaacckk aass eeaarrllyy aass ppoossssiibbllee::  TThhee rreessuulltt iiss aass eexxppeecctteedd:: SStteepp ##11 –– TTeesstt 39 ssttaattiicc vvooiidd MMaaiinn(()) {{ CCaarrdd ccaarrdd == nneeww CCaarrdd(()) {{ FFaaccee==""AA"",, SSuuiitt==SSuuiitt..CCLLUUBB }};; CCoonnssoollee..WWrriitteeLLiinnee((ccaarrdd));; }} ((AA CCLLUUBB))
  • 40. Step #22 –– CCrreeaattee aanndd PPrriinntt aa DDeecckk ooff CCaarrddss 40 ssttaattiicc vvooiidd MMaaiinn(()) {{ LLiisstt<<CCaarrdd>> ccaarrddss == nneeww LLiisstt<<CCaarrdd>>(());; ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ""77"",, SSuuiitt == SSuuiitt..HHEEAARRTT }}));; ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ""AA"",, SSuuiitt == SSuuiitt..SSPPAADDEE }}));; ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ""1100"",, SSuuiitt == SSuuiitt..DDIIAAMMOONNDD }}));; ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ""22"",, SSuuiitt == SSuuiitt..CCLLUUBB }}));; ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ""66"",, SSuuiitt == SSuuiitt..DDIIAAMMOONNDD }}));; ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ""JJ"",, SSuuiitt == SSuuiitt..CCLLUUBB }}));; PPrriinnttCCaarrddss((ccaarrddss));; }} ssttaattiicc vvooiidd PPrriinnttCCaarrddss((LLiisstt<<CCaarrdd>> ccaarrddss)) {{ ffoorreeaacchh ((CCaarrdd ccaarrdd iinn ccaarrddss)) CCoonnssoollee..WWrriittee((ccaarrdd));; CCoonnssoollee..WWrriitteeLLiinnee(());; }}
  • 41. SStteepp ##22 –– TTeesstt  Testing tthhee ddeecckk ooff ccaarrddss sseeeemmss ttoo bbee wwoorrkkiinngg ccoorrrreeccttllyy:: 41 ((77 HHEEAARRTT))((AA SSPPAADDEE))((1100 DDIIAAMMOONNDD))((22 CCLLUUBB))((66 DDIIAAMMOONNDD))((JJ CCLLUUBB))
  • 42. SStteepp ##3 –– SSiinnggllee EExxcchhaannggee 42 ssttaattiicc vvooiidd PPeerrffoorrmmSSiinngglleeEExxcchhaannggee((LLiisstt<<CCaarrdd>> ccaarrddss)) {{ RRaannddoomm rraanndd == nneeww RRaannddoomm(());; iinntt rraannddoommIInnddeexx == rraanndd..NNeexxtt((11,, ccaarrddss..CCoouunntt -- 11));; CCaarrdd ffiirrssttCCaarrdd == ccaarrddss[[11]];; CCaarrdd rraannddoommCCaarrdd == ccaarrddss[[rraannddoommIInnddeexx]];; ccaarrddss[[11]] == rraannddoommCCaarrdd;; ccaarrddss[[rraannddoommIInnddeexx]] == ffiirrssttCCaarrdd;; }}
  • 43.  To tteesstt tthhee ssiinnggllee eexxcchhaannggee wwee uussee tthhee ffoolllloowwiinngg ccooddee::  TThhee rreessuulltt iiss uunneexxppeecctteedd:: SStteepp ##3 –– TTeesstt 43 ssttaattiicc vvooiidd MMaaiinn(()) {{ LLiisstt<<CCaarrdd>> ccaarrddss == nneeww LLiisstt<<CCaarrdd>>(());; ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ""22"",, SSuuiitt == SSuuiitt..CCLLUUBB }}));; ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ""33"",, SSuuiitt == SSuuiitt..HHEEAARRTT }}));; ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ""44"",, SSuuiitt == SSuuiitt..SSPPAADDEE }}));; PPeerrffoorrmmSSiinngglleeEExxcchhaannggee((ccaarrddss));; PPrriinnttCCaarrddss((ccaarrddss));; }} ((22 CCLLUUBB))((33 HHEEAARRTT))((44 SSPPAADDEE))
  • 44. SStteepp ##3 –– FFiixx BBuugg aanndd TTeesstt  The first eelleemmeenntt ooff lliisstt iiss aatt iinnddeexx 00,, nnoott 11::  TThhee rreessuulltt iiss aaggaaiinn iinnccoorrrreecctt ((3 ttiimmeess tthhee ssaammee)):: 44 ssttaattiicc vvooiidd PPeerrffoorrmmSSiinngglleeEExxcchhaannggee((LLiisstt<<CCaarrdd>> ccaarrddss)) {{ RRaannddoomm rraanndd == nneeww RRaannddoomm(());; iinntt rraannddoommIInnddeexx == rraanndd..NNeexxtt((11,, ccaarrddss..CCoouunntt -- 11));; CCaarrdd ffiirrssttCCaarrdd == ccaarrddss[[00]];; CCaarrdd rraannddoommCCaarrdd == ccaarrddss[[rraannddoommIInnddeexx]];; ccaarrddss[[00]] == rraannddoommCCaarrdd;; ccaarrddss[[rraannddoommIInnddeexx]] == ffiirrssttCCaarrdd;; }} ((33 HHEEAARRTT))((22 CCLLUUBB))((44 SSPPAADDEE)) ((33 HHEEAARRTT))((22 CCLLUUBB))((44 SSPPAADDEE)) ((33 HHEEAARRTT))((22 CCLLUUBB))((44 SSPPAADDEE))
  • 45. SStteepp ##3 –– FFiixx AAggaaiinn aanndd TTeesstt  RRaannddoomm..NNeexxtt(()) hhaass eexxcclluussiivvee eenndd rraannggee::  TThhee rreessuulltt nnooww sseeeemmss ccoorrrreecctt:: 45 ssttaattiicc vvooiidd PPeerrffoorrmmSSiinngglleeEExxcchhaannggee((LLiisstt<<CCaarrdd>> ccaarrddss)) {{ RRaannddoomm rraanndd == nneeww RRaannddoomm(());; iinntt rraannddoommIInnddeexx == rraanndd..NNeexxtt((11,, ccaarrddss..CCoouunntt));; CCaarrdd ffiirrssttCCaarrdd == ccaarrddss[[00]];; CCaarrdd rraannddoommCCaarrdd == ccaarrddss[[rraannddoommIInnddeexx]];; ccaarrddss[[00]] == rraannddoommCCaarrdd;; ccaarrddss[[rraannddoommIInnddeexx]] == ffiirrssttCCaarrdd;; }} ((33 HHEEAARRTT))((22 CCLLUUBB))((44 SSPPAADDEE)) ((44 SSPPAADDEE))((33 HHEEAARRTT))((22 CCLLUUBB)) ((44 SSPPAADDEE))((33 HHEEAARRTT))((22 CCLLUUBB))
  • 46. SStteepp ##44 –– SShhuuffffllee tthhee DDeecckk  SShhuuffffllee tthhee eennttiirree ddeecckk ooff ccaarrddss::  TThhee rreessuulltt iiss ssuurrpprriissiinnggllyy iinnccoorrrreecctt:: 46 ssttaattiicc vvooiidd SShhuufffflleeCCaarrddss((LLiisstt<<CCaarrdd>> ccaarrddss)) {{ ffoorr ((iinntt ii == 11;; ii <<== ccaarrddss..CCoouunntt;; ii++++)) {{ PPeerrffoorrmmSSiinngglleeEExxcchhaannggee((ccaarrddss));; }} }} IInniittiiaall ddeecckk:: ((77 HHEEAARRTT))((AA SSPPAADDEE))((1100 DDIIAAMMOONNDD))((22 CCLLUUBB))((66 DDIIAAMMOONNDD))((JJ CCLLUUBB)) AAfftteerr sshhuuffffllee:: ((77 HHEEAARRTT))((AA SSPPAADDEE))((1100 DDIIAAMMOONNDD))((22 CCLLUUBB))((66 DDIIAAMMOONNDD))((JJ CCLLUUBB))
  • 47. SStteepp ##44 –– SSttrraannggee BBuugg WWhheenn wwee sstteepp tthhrroouugghh tthhee ccooddee wwiitthh tthhee ddeebbuuggggeerr,, tthhee rreessuulltt sseeeemmss ccoorrrreecctt:: WWiitthhoouutt tthhee ddeebbuuggggeerr tthhee rreessuulltt iiss wwrroonngg!! 47 IInniittiiaall ddeecckk:: ((77 HHEEAARRTT))((AA SSPPAADDEE))((1100 DDIIAAMMOONNDD))((22 CCLLUUBB))((66 DDIIAAMMOONNDD))((JJ CCLLUUBB)) AAfftteerr sshhuuffffllee:: ((1100 DDIIAAMMOONNDD))((77 HHEEAARRTT))((AA SSPPAADDEE))((JJ CCLLUUBB))((22 CCLLUUBB))((66 DDIIAAMMOONNDD))
  • 48. SStteepp ##44 –– FFiixx AAggaaiinn aanndd TTeesstt  RRaannddoomm sshhoouulldd bbee iinnssttaannttiiaatteedd oonnllyy oonnccee::  TThhee rreessuulltt ffiinnaallllyy iiss ccoorrrreecctt wwiitthh aanndd wwiitthhoouutt tthhee ddeebbuuggggeerr 48 pprriivvaattee ssttaattiicc RRaannddoomm rraanndd == nneeww RRaannddoomm(());; ssttaattiicc vvooiidd PPeerrffoorrmmSSiinngglleeEExxcchhaannggee((LLiisstt<<CCaarrdd>> ccaarrddss)) {{ iinntt rraannddoommIInnddeexx == rraanndd..NNeexxtt((11,, ccaarrddss..CCoouunntt));; CCaarrdd ffiirrssttCCaarrdd == ccaarrddss[[00]];; CCaarrdd rraannddoommCCaarrdd == ccaarrddss[[rraannddoommIInnddeexx]];; ccaarrddss[[00]] == rraannddoommCCaarrdd;; ccaarrddss[[rraannddoommIInnddeexx]] == ffiirrssttCCaarrdd;; }}
  • 49. TTeessttiinngg TThhoorroouugghhllyy TTeesstt YYoouurr SSoolluuttiioonn
  • 50. Thoroughly TTeesstt yyoouurr SSoolluuttiioonn WWiissee ssooffttwwaarree eennggiinneeeerrss ssaayy tthhaatt:: IInnvveennttiinngg aa ggoooodd iiddeeaa aanndd iimmpplleemmeennttiinngg iitt iiss hhaallff ooff tthhee ssoolluuttiioonn TTeessttiinngg iiss tthhee sseeccoonndd hhaallff ooff tthhee ssoolluuttiioonn AAllwwaayyss tteesstt tthhoorroouugghhllyy yyoouurr ssoolluuttiioonn IInnvveesstt iinn tteessttiinngg OOnnee 110000%% ssoollvveedd pprroobblleemm iiss bbeetttteerr tthhaann 22 oorr 3 ppaarrttiiaallllyy ssoollvveedd TTeessttiinngg eexxiissttiinngg pprroobblleemm ttaakkeess lleessss ttiimmee tthhaann ssoollvviinngg aannootthheerr pprroobblleemm ffrroomm ssccrraattcchh 50
  • 51. HHooww ttoo TTeesstt??  TTeessttiinngg ccoouulldd nnoott cceerrttiiffyy aabbsseennccee ooff ddeeffeeccttss IItt jjuusstt rreedduucceess tthhee ddeeffeeccttss rraattee WWeellll tteesstteedd ssoolluuttiioonnss aarree mmoorree lliikkeellyy ttoo bbee ccoorrrreecctt  SSttaarrtt tteessttiinngg wwiitthh aa ggoooodd rreepprreesseennttaattiivvee ooff tthhee ggeenneerraall ccaassee NNoott aa ssmmaallll iissoollaatteedd ccaassee LLaarrggee aanndd ccoommpplleexx tteesstt,, bbuutt SSmmaallll eennoouugghh ttoo bbee eeaassiillyy cchheecckkaabbllee 51
  • 52. HHooww ttoo TTeesstt?? ((22))  TTeesstt tthhee bboorrddeerr ccaasseess EE..gg.. iiff nn ∈∈ [[00....550000]]  ttrryy nn==00 ,, nn==11,, nn==22,, nn==449999,, nn==550000  IIff aa bbuugg iiss ffoouunndd,, rreeppeeaatt aallll tteessttss aafftteerr ffiixxiinngg iitt ttoo aavvooiidd rreeggrreessssiioonnss RRuunn aa llooaadd tteesstt HHooww ttoo bbee ssuurree tthhaatt yyoouurr aallggoorriitthhmm iiss ffaasstt eennoouugghh ttoo mmeeeett tthhee rreeqquuiirreemmeennttss?? UUssee ccooppyy--ppaassttiinngg ttoo ggeenneerraattee llaarrggee tteesstt ddaattaa 52
  • 53. RReeaadd tthhee PPrroobblleemm SSttaatteemmeenntt RReeaadd ccaarreeffuullllyy tthhee pprroobblleemm ssttaatteemmeenntt DDooeess yyoouurr ssoolluuttiioonn pprriinntt eexxaaccttllyy wwhhaatt iiss eexxppeecctteedd?? DDooeess yyoouurr oouuttppuutt ffoollllooww tthhee rreeqquueesstteedd ffoorrmmaatt?? DDiidd yyoouu rreemmoovvee yyoouurr ddeebbuugg pprriinnttoouuttss??  BBee ssuurree ttoo ssoollvvee tthhee rreeqquueesstteedd pprroobblleemm,, nnoott tthhee pprroobblleemm yyoouu tthhiinnkk iiss rreeqquueesstteedd!! EExxaammppllee:: ""WWrriittee aa pprrooggrraamm ttoo pprriinntt tthhee nnuummbbeerr ooff ppeerrmmuuttaattiioonnss oonn nn eelleemmeennttss"" mmeeaannss ttoo pprriinntt aa ssiinnggllee nnuummbbeerr,, nnoott aa sseett ooff ppeerrmmuuttaattiioonnss!! 53
  • 54. TTeessttiinngg –– EExxaammppllee  TTeesstt wwiitthh ffuullll ddeecckk ooff 5522 ccaarrddss SSeerriioouuss eerrrroorr ffoouunndd  cchhaannggee tthhee aallggoorriitthhmm  CChhaannggee tthhee aallggoorriitthhmm EExxcchhaannggee tthhee ffiirrsstt ccaarrdd wwiitthh aa rraannddoomm ccaarrdd  eexxcchhaannggee ccaarrddss 00,, 11,, ……,, NN--11 wwiitthh aa rraannddoomm ccaarrdd TTeesstt wwhheetthheerr tthhee nneeww aallggoorriitthhmm wwoorrkkss  TTeesstt wwiitthh 11 ccaarrdd  TTeesstt wwiitthh 22 ccaarrddss  TTeesstt wwiitthh 00 ccaarrddss  LLooaadd tteesstt wwiitthh 5522 000000 ccaarrddss 54
  • 55. TTeesstt wwiitthh 5522 CCaarrddss –– EExxaammppllee 55 ssttaattiicc vvooiidd TTeessttSShhuuffffllee5522CCaarrddss(()) {{ LLiisstt<<CCaarrdd>> ccaarrddss == nneeww LLiisstt<<CCaarrdd>>(());; ssttrriinngg[[]] aallllFFaacceess == nneeww ssttrriinngg[[]] {{ ""22"",, ""33"",, ""44"",, ""55"",, ""66"",, ""77"",, ""88"",, ""99"",, ""1100"",, ""JJ"",, ""QQ"",, ""KK"",, ""AA"" }};; SSuuiitt[[]] aallllSSuuiittss == nneeww SSuuiitt[[]] {{ SSuuiitt..CCLLUUBB,, SSuuiitt..DDIIAAMMOONNDD,, SSuuiitt..HHEEAARRTT,, SSuuiitt..SSPPAADDEE }};; ffoorreeaacchh ((ssttrriinngg ffaaccee iinn aallllFFaacceess)) ffoorreeaacchh ((SSuuiitt ssuuiitt iinn aallllSSuuiittss)) {{ CCaarrdd ccaarrdd == nneeww CCaarrdd(()) {{ FFaaccee == ffaaccee,, SSuuiitt == ssuuiitt }};; ccaarrddss..AAdddd((ccaarrdd));; }} SShhuufffflleeCCaarrddss((ccaarrddss));; PPrriinnttCCaarrddss((ccaarrddss));; }}
  • 56. TTeesstt wwiitthh 5522 CCaarrddss –– EExxaammppllee ((22))  TThhee rreessuulltt iiss ssuurrpprriissiinngg::  HHaallff ooff tthhee ccaarrddss kkeeeepp tthheeiirr iinniittiiaall ppoossiittiioonnss WWee hhaavvee sseerriioouuss pprroobblleemm –– tthhee rraannddoommiizzaattiioonn aallggoorriitthhmm iiss nnoott rreelliiaabbllee 56 ((44 DDIIAAMMOONNDD))((22 DDIIAAMMOONNDD))((66 HHEEAARRTT))((22 SSPPAADDEE))((AA SSPPAADDEE))((77 SSPPAADDEE))((33 DDIIAAMMOONNDD))((33 SSPPAADDEE))((44 SSPPAADDEE))((44 HHEEAARRTT))((66 CCLLUUBB))((KK HHEEAARRTT))((55 CCLLUUBB))((55 DDIIAAMMOONNDD))((55 HHEEAARRTT))((AA HHEEAARRTT))((99 CCLLUUBB))((1100 CCLLUUBB))((AA CCLLUUBB))((66 SSPPAADDEE))((77 CCLLUUBB))((77 DDIIAAMMOONNDD))((33 CCLLUUBB))((99 HHEEAARRTT))((88 CCLLUUBB))((33 HHEEAARRTT))((99 SSPPAADDEE))((44 CCLLUUBB))((88 HHEEAARRTT))((99 DDIIAAMMOONNDD))((55 SSPPAADDEE))((88 DDIIAAMMOONNDD))((JJ HHEEAARRTT))((1100 DDIIAAMMOONNDD))((1100 HHEEAARRTT))((1100 SSPPAADDEE))((QQ HHEEAARRTT))((22 CCLLUUBB))((JJ CCLLUUBB))((JJ SSPPAADDEE)) ((QQ CCLLUUBB))((77 HHEEAARRTT))((22 HHEEAARRTT))((QQ SSPPAADDEE))((KK CCLLUUBB))((JJ DDIIAAMMOONNDD))((66 DDIIAAMMOONNDD))((KK SSPPAADDEE))((88 SSPPAADDEE))((AA DDIIAAMMOONNDD))((QQ DDIIAAMMOONNDD))((KK DDIIAAMMOONNDD))
  • 57. FFiixxiinngg tthhee AAllggoorriitthhmm  NNeeww iiddeeaa tthhaatt sslliigghhttllyy cchhaannggeess tthhee aallggoorriitthhmm:: EExxcchhaannggee tthhee ffiirrsstt ccaarrdd wwiitthh aa rraannddoomm ccaarrdd  eexxcchhaannggee ccaarrddss 00,, 11,, ……,, NN--11 wwiitthh aa rraannddoomm ccaarrdd 57 ssttaattiicc vvooiidd PPeerrffoorrmmSSiinngglleeEExxcchhaannggee((LLiisstt<<CCaarrdd>> ccaarrddss,, iinntt iinnddeexx)) {{ iinntt rraannddoommIInnddeexx == rraanndd..NNeexxtt((11,, ccaarrddss..CCoouunntt));; CCaarrdd ffiirrssttCCaarrdd == ccaarrddss[[iinnddeexx]];; ccaarrddss[[iinnddeexx]] == ccaarrddss[[rraannddoommIInnddeexx]];; ccaarrddss[[rraannddoommIInnddeexx]] == ffiirrssttCCaarrdd;; }} ssttaattiicc vvooiidd SShhuufffflleeCCaarrddss((LLiisstt<<CCaarrdd>> ccaarrddss)) {{ ffoorr ((iinntt ii == 00;; ii << ccaarrddss..CCoouunntt;; ii++++)) PPeerrffoorrmmSSiinngglleeEExxcchhaannggee((ccaarrddss,, ii));; }}
  • 58. TTeesstt wwiitthh 5522 CCaarrddss ((AAggaaiinn))  TThhee rreessuulltt nnooww sseeeemmss ccoorrrreecctt::  CCaarrddss aarree ccoommpplleetteellyy rraannddoommiizzeedd 58 ((99 HHEEAARRTT))((55 CCLLUUBB))((33 CCLLUUBB))((77 SSPPAADDEE))((66 CCLLUUBB))((55 SSPPAADDEE))((66 HHEEAARRTT)) ((44 CCLLUUBB))((1100 CCLLUUBB))((33 SSPPAADDEE))((KK DDIIAAMMOONNDD))((1100 HHEEAARRTT))((88 CCLLUUBB))((AA CCLLUUBB))((JJ DDIIAAMMOONNDD))((KK SSPPAADDEE))((99 SSPPAADDEE))((77 CCLLUUBB))((1100 DDIIAAMMOONNDD))((99 DDIIAAMMOONNDD))((88 HHEEAARRTT))((66 DDIIAAMMOONNDD))((88 SSPPAADDEE))((55 DDIIAAMMOONNDD))((44 HHEEAARRTT)) ((1100 SSPPAADDEE))((JJ CCLLUUBB))((QQ SSPPAADDEE))((99 CCLLUUBB))((JJ HHEEAARRTT))((KK CCLLUUBB))((22 HHEEAARRTT)) ((77 HHEEAARRTT))((AA HHEEAARRTT))((33 DDIIAAMMOONNDD))((KK HHEEAARRTT))((AA SSPPAADDEE))((88 DDIIAAMMOONNDD))((44 SSPPAADDEE))((33 HHEEAARRTT))((55 HHEEAARRTT))((QQ HHEEAARRTT))((44 DDIIAAMMOONNDD))((22 SSPPAADDEE))((AA DDIIAAMMOONNDD))((22 DDIIAAMMOONNDD))((JJ SSPPAADDEE))((77 DDIIAAMMOONNDD))((QQ DDIIAAMMOONNDD))((22 CCLLUUBB)) ((66 SSPPAADDEE))((QQ CCLLUUBB))
  • 59. TTeesstt wwiitthh 11 CCaarrdd 59  CCrreeaattee aa mmeetthhoodd ttoo tteesstt wwiitthh 11 ccaarrdd:: ssttaattiicc vvooiidd TTeessttSShhuufffflleeOOnneeCCaarrdd(()) {{ LLiisstt<<CCaarrdd>> ccaarrddss == nneeww LLiisstt<<CCaarrdd>>(());; ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ""AA"",, SSuuiitt == SSuuiitt..CCLLUUBB }}));; CCaarrddssSShhuuffffllee..SShhuufffflleeCCaarrddss((ccaarrddss));; CCaarrddssSShhuuffffllee..PPrriinnttCCaarrddss((ccaarrddss));; }} WWee ffoouunndd aa bbuugg:: UUnnhhaannddlleedd EExxcceeppttiioonn:: SSyysstteemm..AArrgguummeennttOOuuttOOffRRaannggeeEExxcceeppttiioonn:: IInnddeexx wwaass oouutt ooff rraannggee.. MMuusstt bbee nnoonn--nneeggaattiivvee aanndd lleessss tthhaann tthhee ssiizzee ooff tthhee ccoolllleeccttiioonn.. PPaarraammeetteerr nnaammee:: iinnddeexx ……
  • 60. Test wwiitthh 11 CCaarrdd –– BBuugg FFiixxiinngg 60 WWee ttaakkee 11 ccaarrdd aarree ssppeecciiaall ccaassee:: ssttaattiicc vvooiidd SShhuufffflleeCCaarrddss((LLiisstt<<CCaarrdd>> ccaarrddss)) {{ iiff ((ccaarrddss..CCoouunntt >> 11)) {{ ffoorr ((iinntt ii == 00;; ii << ccaarrddss..CCoouunntt;; ii++++)) {{ PPeerrffoorrmmSSiinngglleeEExxcchhaannggee((ccaarrddss,, ii));; }} }} }}  TTeesstt sshhoowwss tthhaatt tthhee pprroobblleemm iiss ffiixxeedd
  • 61. TTeesstt wwiitthh 22 CCaarrddss 61  CCrreeaattee aa mmeetthhoodd ttoo tteesstt wwiitthh 22 ccaarrddss:: ssttaattiicc vvooiidd TTeessttSShhuufffflleeTTwwooCCaarrddss(()) {{ LLiisstt<<CCaarrdd>> ccaarrddss == nneeww LLiisstt<<CCaarrdd>>(());; ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ""AA"",, SSuuiitt == SSuuiitt..CCLLUUBB }}));; ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ""33"",, SSuuiitt == SSuuiitt..DDIIAAMMOONNDD }}));; CCaarrddssSShhuuffffllee..SShhuufffflleeCCaarrddss((ccaarrddss));; CCaarrddssSShhuuffffllee..PPrriinnttCCaarrddss((ccaarrddss));; }}  BBuugg:: sseeqquueennttiiaall eexxeeccuuttiioonnss ggeett tthhee ssaammee rreessuulltt:: ((33 DDIIAAMMOONNDD))((AA CCLLUUBB))  TThhee pprroobblleemm:: tthhee ffiirrsstt aanndd tthhee sseeccoonndd ccaarrddss aallwwaayyss eexxcchhaannggee eeaacchh ootthheerr eexxaaccttllyy oonnccee
  • 62. Test with 22 CCaarrddss –– BBuugg FFiixxiinngg 62 WWee aallllooww eeaacchh ccaarrdd ttoo bbee eexxcchhaannggeedd wwiitthh aannyy ootthheerr rraannddoomm ccaarrdd,, iinncclluuddiinngg iittsseellff ssttaattiicc vvooiidd PPeerrffoorrmmSSiinngglleeEExxcchhaannggee((LLiisstt<<CCaarrdd>> ccaarrddss,, iinntt iinnddeexx)) {{ iinntt rraannddoommIInnddeexx == rraanndd..NNeexxtt((00,, ccaarrddss..CCoouunntt));; CCaarrdd ffiirrssttCCaarrdd == ccaarrddss[[iinnddeexx]];; CCaarrdd rraannddoommCCaarrdd == ccaarrddss[[rraannddoommIInnddeexx]];; ccaarrddss[[iinnddeexx]] == rraannddoommCCaarrdd;; ccaarrddss[[rraannddoommIInnddeexx]] == ffiirrssttCCaarrdd;; }}  TTeesstt sshhoowwss tthhaatt tthhee pprroobblleemm iiss ffiixxeedd
  • 63. TTeesstt wwiitthh 00 CCaarrddss;; RReeggrreessssiioonn TTeessttss 63  TTeessttiinngg wwiitthh 00 ccaarrddss ((eemmppttyy lliisstt)) ggeenneerraatteess aann eemmppttyy lliisstt  ccoorrrreecctt rreessuulltt  SSeeeemmss lliikkee tthhee ccaarrddss sshhuuffffllee aallggoorriitthhmm wwoorrkkss ccoorrrreeccttllyy aafftteerr tthhee llaasstt ffeeww ffiixxeess NNeeeeddss aa rreeggrreessssiioonn tteesstt TTeesstt aaggaaiinn tthhaatt nneeww cchhaannggeess ddiidd nnoott bbrreeaakk aallll pprreevviioouussllyy wwoorrkkiinngg ccaasseess  TTeesstt wwiitthh ffuullll ddeecckk ooff 5522 ccaarrddss;; wwiitthh 11 ccaarrdd;; wwiitthh 22 ccaarrddss wwiitthh 00 ccaarrddss  eevveerryytthhiinngg wwoorrkkss
  • 64. LLooaadd TTeesstt –– 5522 000000 CCaarrddss  FFiinnaallllyy wwee nneeeedd aa llooaadd tteesstt wwiitthh 5522 000000 ccaarrddss:: 64 ssttaattiicc vvooiidd TTeessttSShhuuffffllee5522000000CCaarrddss(()) {{ LLiisstt<<CCaarrdd>> ccaarrddss == nneeww LLiisstt<<CCaarrdd>>(());; ssttrriinngg[[]] aallllFFaacceess == nneeww ssttrriinngg[[]] {{""22"",, ""33"",, ""44"",, ""55"",, ""66"",, ""77"",, ""88"",, ""99"",, ""1100"",, ""JJ"",, ""QQ"",, ""KK"",, ""AA""}};; SSuuiitt[[]] aallllSSuuiittss == nneeww SSuuiitt[[]] {{ SSuuiitt..CCLLUUBB,, SSuuiitt..DDIIAAMMOONNDD,, SSuuiitt..HHEEAARRTT,, SSuuiitt..SSPPAADDEE}};; ffoorr ((iinntt ii == 00;; ii << 11000000;; ii++++)) ffoorreeaacchh ((ssttrriinngg ffaaccee iinn aallllFFaacceess)) ffoorreeaacchh ((SSuuiitt ssuuiitt iinn aallllSSuuiittss)) ccaarrddss..AAdddd((nneeww CCaarrdd(()) {{ FFaaccee == ffaaccee,, SSuuiitt == ssuuiitt }}));; SShhuufffflleeCCaarrddss((ccaarrddss));; PPrriinnttCCaarrddss((ccaarrddss));; }}
  • 65. SSuummmmaarryy 11.. PPrroobblleemmss ssoollvviinngg nneeeeddss mmeetthhooddoollooggyy::  UUnnddeerrssttaannddiinngg aanndd aannaallyyzziinngg pprroobblleemmss  UUssiinngg aa sshheeeett ooff ppaappeerr aanndd aa ppeenn ffoorr sskkeettcchhiinngg  TThhiinnkkiinngg uupp,, iinnvveennttiinngg aanndd ttrryyiinngg iiddeeaass  DDeeccoommppoossiinngg pprroobblleemmss iinnttoo ssuubbpprroobblleemmss  SSeelleeccttiinngg aapppprroopprriiaattee ddaattaa ssttrruuccttuurreess  TThhiinnkkiinngg aabboouutt tthhee eeffffiicciieennccyy aanndd ppeerrffoorrmmaannccee  IImmpplleemmeennttiinngg sstteepp--bbyy--sstteepp  TTeessttiinngg tthhee nnoommiinnaall ccaassee,, bboorrddeerr ccaasseess aanndd eeffffiicciieennccyy 65
  • 66. MMeetthhooddoollooggyy ooff PPrroobblleemm SSoollvviinngg QQuueessttiioonnss?? http://academy.telerik.com
  • 67. EExxeerrcciisseess 11.. FFoolllloowwiinngg tthhee pprroobblleemmss ssoollvviinngg mmeetthhooddoollooggyy ttrryy ttoo aapppprrooaacchh tthhee ffoolllloowwiinngg pprroobblleemm:: WWee aarree ggiivveenn NN ppooiinnttss ((NN << 110000 000000)) iinn tthhee ppllaannee ((rreepprreesseenntteedd aass iinntteeggeerr ccoooorrddiinnaatteess xxii aanndd yyii)).. WWrriittee aa pprrooggrraamm ttoo ffiinndd aallll ppoossssiibbllee hhoorriizzoonnttaall oorr vveerrttiiccaall ssttrraaiigghhtt lliinneess tthhaatt sspplliitt tthhee ppllaannee iinnttoo ttwwoo ppaarrttss,, eeaacchh hhoollddiinngg eeqquuaall nnuummbbeerr ooff ppooiinnttss ((ppooiinnttss aatt tthhee lliinnee aarree nnoott ccoouunntteedd)).. 67
  • 68. EExxeerrcciisseess ((22)) 22.. FFoolllloowwiinngg tthhee pprroobblleemmss ssoollvviinngg mmeetthhooddoollooggyy ssoollvvee tthhee ffoolllloowwiinngg pprroobblleemm:: wwee aarree ggiivveenn aa sseett SS ooff nn iinntteeggeerr nnuummbbeerrss aanndd aa ppoossiittiivvee iinntteeggeerr kk ((kk ≤≤ nn ≤≤ 1100)).. AAlltteerrnnaattiinngg sseeqquueennccee ooff nnuummbbeerrss iiss aa sseeqquueennccee tthhaatt aalltteerrnnaattiivveellyy cchhaannggeess ffrroomm iinnccrreeaassiinngg ttoo ddeeccrreeaassiinngg ooff vviiccee vveerrssaa aafftteerr eeaacchh ooff iittss eelleemmeennttss.. WWrriittee aa pprrooggrraamm ttoo ggeenneerraattee aallll aalltteerrnnaattiinngg sseeqquueenncceess ss11,, ss22,, ……,, sskk ccoonnssiissttiinngg ooff kk ddiissttiinncctt eelleemmeennttss ffrroomm SS.. EExxaammppllee:: SS == {{ 22,, 55,, 33,, 44 }}  {{22,, 44,, 33}},, {{22,, 55,, 33}},, {{22,, 55,, 44}},, {{33,, 22,, 44}},, {{33,, 22,, 55}},, {{33,, 44,, 22}},, {{33,, 55,, 22}},, {{33,, 55,, 44}},, {{44,, 22,, 33}},, {{44,, 22,, 55}},, {{44,, 33,, 55}},, {{55,, 22,, 33}},, {{55,, 22,, 44}},, {{55,, 33,, 44}} 68