Professional Documents
Culture Documents
Downloadtheimage
Theblacklineistheroad.Attheverystartyou’llnoticeadoor.Thereisalsoapersoninpurple
standingrightbesidethatdoor,whoisthedoorkeeper.Heistheretoexplainyouonhowtoget
totheendofthemap...theplacethatistermedas“Goal”
Yourinitialposition:Youcametothecitythroughthedoorandmetthedoorkeeper.Youhave
abreastpocketonyourshirtbutyouhavenomoney.Youarenowgoingtoknowabouthowto
reachthe“Goal”.
Thispartisimportanttoremember:Thedoorkeeperputs1$inyourbreastpocketandgives
youanemptybox.So,yourcurrentpositionis:
pocket=1,box=0
HereyoumightseeIhavetriedtodrawabreastpocketandabox.
He(thedoorkeeper)explainsthatyouneedtogofrompointA totheG oal.Inyourroadtothe
Goalyouwillfaceseveralthings.AtfirstinpointByou’llfindaBanker...butreallyheisaMad
Banker.
MadBanker:WhenyoumeettheMadBanker...hechecksyourpocketandcalculatesthe
moneyyouhaveinthereandputsitback.Thenfromhisownlockerhebringsoutthesame
amountofmoneyandp utsitinyouremptybox.
Robot:FromthereyougotopointR,w hereyoufindaRobotwhoisprogrammedtodo
somethingweird.Wheneveryoucometohim,h eputs$2inyourbreastpocket.Andthenyou
leavethespot
TimeMachine:Afterthatyouarriveatthisspotw herey ouf inda T
imeM achine.T hefunctiono f
thistimem achineisthatitc hecksy ourp ocketa
ndifthereitfindsm orethan1 0d ollarsitletsy ou
pass.Ifn otitthrowsy oub acktotheA s potw
herey ouh avetob eginy ourjourneya gain.
You’llb ethrownb acktotheA s pote
achtimetheT imeM achinefindsthaty ouh avelessthan$ 10.
Inthed rawingy ouc ans eeIh avew ritten“ while( pocket< 1
0)”.W hilethev alueo fp
ocketislessthan
10
See…e verytimey oum eettheM adB ankera ndtheR oboty ourm oneyincreases.A ndw hen
ultimatelyy ouh avem orethan$ 10iny ourb reastp ocket,theT imem achinew illlety oum ovetothe
“Goal”.A ndy ourjourneyisfinished.
Understandthew holethingthatIh avew rittena lready…b ecausethes toryfinishesh ere.
NowI’lle xplainw hatc omesn ext.T hes toryisfinishedb utw
ew anttom akea p
rogramthatk indo f
describesthisw holes toryina w ay.
Atfirsta nalyzey ourp ocket…s eethea mounto fm oneyinitincreasesb ecausetheT imeM achine
throwsy oub acktotheA s pottos tarta gaina nda gain.T hisincreasesy ourm oneyinthiss eries:
1,3 ,5
,7 ,9
,1
1
After1 1thetimem achinew illlety oug ob ecausen owy ouh
avem orethan$ 10iny ourp ocket.I
hopethism akess ense.Y ous tartedw ith1 $a ndfinishedw ith1 1$iny ourp ocket.
RememberitistheR obotthatincreasesthem oneyiny ourp ocketa nditistheM adB ankerw ho
increasesy ourm oneyintheb ox.
InthetableIh avelaido utthee quationw ithw hichther esultsa refound.A nalyzeit,p lease.M ad
Bankerd oesn ota ffectthep ocket,h eo nlya ffectstheb ox.T heR obotd oesn’ta ffectthetheb ox,h e
onlya ffectsthep ocket
Initials tateo fp
ocketa ndb ox:p ocket= 1
,b
ox=0
( MadB anker) ( Robot)
Equation: Equation:
box= b
ox+
p
ocket
pocket= p
ocket+
2
1st Pocket=1
,B
ox=1 Pocket=3
,B
ox=1
2nd Pocket=3
,B
ox=4
Pocket=5
,B
ox=4
3rd Pocket=5
,B
ox=9
Pocket=7
,B
ox=9
4th Pocket=7
,B
ox=1
6 Pocket=9
,B
ox=1
6
5th Pocket=9
,B
ox=2
5 Pocket=1
1,B
ox=2
5
Ify oua nalyzethefirstr ow,y ou’lls eethatw heny oufirstc rossthem adb ankerthem oneyiny our
pocketisthes ame.Itisthes amea sitw
asw henthed oorkeeperg avey outhem oney.H eg
avey ou
pocket= 1a
ndb ox=0.S o,h ea lsog avey oua ne
mptyb ox.S o,a
fterc rossingthem adb ankerforthe
firsttimethen umberintheb oxincreased.B ecauser ememberw hatthem adb ankerd oes.
box= b
ox+ p
ocket
Andthen wheny ouc rossthew eirdR obotthea mountiny ourp ocketincreases.T hee quationfor
thatisp ocket= p
ocket+ 2
Analyzethetable.Iu sedp ena ndp aperw henIfirstd
iditm
yself.S eethes erieso fp
ocket
Itg oeslikethis:1 ,3
,5
,7
,9
.Iw
on’ta dd1 1h
ereb ecauseitw asn’tincludedintheb ox.
Theb oxf unctionsa ss omethingt hath oldst hes umo ft hen umberincludedint hep ocket.
Id
on’tk nowh owtoe xplainthisinw ritingb utitisfairlye asytos eethattheb oxisw orkinga sthe
sumo fthen umberincludedinthep ocket.
Thes umo fthev alueo fthep ocketinthefirstr owa ndp ocketinthes econdr owis4 w
hichisthe
valueo fb oxinthe2 ndr ow.S imilarlythes umo fthew holes eries1 +3+5+7+9is2 5.W hichisa lso
thelastv alueo fb
oxa ss eeninthetable.Ifitiss tilltood ifficulttou nderstandthend on’tw orryitw ill
bea sc leara sc rystalw heny oud othec odeforthiss eriesy ourself.
So,o urg oalistow ritea c odeforthiss eriesa ndw antthec omputertoc alculatethew holethinga nd
giveu sthea nsb yp rintingitinitso utputb ar
Let’sg etintothec odethen.
Iu
seV isualS tudioC odea sa c odee ditor.C odeE ditorisa s oftwarew herey ouw ritey ourc odea nd
Runit.T herea reo therc odee ditorsthaty ouc anu sea sw ell
Wen eedtou nderstandw hatw ew anttod o.W ew anttog eta na
nswero fthiss eries1 +3+5+7+9
So,w ew illw ritethec odea ndtellthec omputertod othem atha ndthec omputerw illg iveu sa
n
answer
Firstw ea reg onnau setwov ariables.O urv ariablesa reb oxa ndp ocket.
Wed eclaretoo urc omputerthatw eh avetwov ariablesa ndtheinitialv aluesa res uch.F ore xample
herew ed eclare:
box= 0
pocket= 1
Thenc omestheA s pot.W ew ritew hile(pocket<10)thisisa p arto fthec ode.Whatisn ots p arto fthe
codeb utiss tillincludedh asb eenc ommentedo utw itha #
s ign.w ew riteo urc ommentsw ith# s ign.
Whenw ew rites omethingu singthe# s ignthec omputerignoresit.S ow
ec anw ritew hateverw e
want.S o,tom akeite asiertou nderstandIh aveu sedthe# s igntoc ommenta ndm akeits imilarto
ours tory
box
=0
pocket=1
while
(pocket
<1
0
):
#Aspot
b
ox
=b
ox+p
ocket
#MadB
anker
p
ocket=p
ocket+
2
#
Robot
#
timem
achinew
ills
endy
oub
ackt
oA
spot
print
(b
ox
)#
Goal
Thisw
illg
ivey outheo utput2 5a
sw ea
rep
rintingthev ariablen
amed‘box’.S
o,w
hateverv alueb
ox
holdsw illb
ep
rinteda sa no
utput
Thisish
owther ealc odelookslike
box
=0
pocket=1
while
(pocket
<1
0
):
b
ox
=b
ox+
p
ocket
p
ocket
=p
ocket+
2
print
(b
ox
)
Itisn
ota
p
roblemify ouu sethec omments.Iu sedtou
sethemw henIs tartedo ut.H
opethism
akes
sense.
Wheny oua red
onew iththiss eriesy ouc hangethes eriesto1 +2
+3
+4
+5 o
rs omethinglikethat
ands eeify ouc anfindthea nswers.A fterthatm
aybes omethinglike2 +4+6+8.Y ous eew
hatim
ean