You are on page 1of 30

Chapter 1 OBJ:main.py Whatworked? Astandardforloopthatquicklycalculatesthefactorialofawholenumber.Pythondoesn'thave numbersizelimitationssoevenlargenumberslike!99999canbecalculatedwithinseconds. Whatdidn'twork? Ican'tputlettersornonwholenumbersintotheinputelseitexitswithanexception.Negative valuesaresimplyignored. CODE(Python) x =i n t ( i n p u t ( Wh a t n u mb e r d oy o uwa n t t of a c t o r i a l i z e ?

) ) i f x <0 : p r i n t ( I n p u t mu s t b e p o s i t i v e ) e x i t ( ) f =x f o r i i nr a n g e ( 1 , x ) : f * =i p r i n t ( f ) SCREENCAP

Chapter 2 Project:BacteriaGrowth Wasaninterestingprojectbecauseitwasharderthanitappeared.Istartedoutwithjustfindingthe nearestpoweroftwoandusingthatforthetime,butIrealizedthebacteriadon'tjustsuddenlydouble atthe23minutemark.SoIhadtolookupgrowthratesandPertorF=Pe^(rt).UsingknownvaluesF, P,andtIcouldsolveforr.KnowingRIcouldsolvefort.Usingthedeathvalue7.3millionasF,I couldeasilyfindtheamountoftimeitwouldtakeforbacteriatoreachthatnumber. AfterabunchofconfusingmodificationsIthinkIgotavalidanswertoaprogramthatcancalculate bacteriagrowths,eveniftheydon'tsimplydouble. Code:(python) #Variables killCap=7.3#inmillionsofbacteria cureTime=2.5#inhours doubleTime=23#inminutes startingBacteria=2 bacteriaAfterOneHour=4 #Work killCap=int(killCap*1000000) growthRate=math.log(bacteriaAfterOneHour/startingBacteria,10)/doubleTime cureTime=cureTime*60 killTime=math.log(killCap/startingBacteria,10)/growthRate print(killTime) lastMinuteDiagnoseTime=killTimecureTime print("Curemustbeadministeredwithin",lastMinuteDiagnoseTime/60,"hoursafterfirstexposure, otherwisedeathwillensue.") Screenshot(nextpage):

Chapter 3 BayesTheorem Whatworked? Iusedobjectorientedprogrammingtocreateatreenodestructure,withfunctionsthatcancalculate probabilitiesfromleavesofthetree.Itsupportsasmanyeventsasneededandcancalulateconditional probabilitiesusingbayestheorem.Itidentifieseventsbystringname.Everythingworksreally elegantlybesidesactuallysettingupthedatastructure.Icouldn'tthinkofabetterwaytosetitupatthe timeandnowI'mtoolazytochangeit. Code


#c a l c u l a t e s t h e p r o b a b i l i t yo f a ne v e n t h a p p e n i n g . d e f c a l c P r o b ( e v e n t ) : e =e v e n t n=e . p r o b a b i l i t y wh i l e e . p a r e n t ! =N o n e a n de . p a r e n t . n a me ! =N o n e : e =e . p a r e n t n=n*e . p r o b a b i l i t y r e t u r nn #a ne v e n t n o d e t r e e t h a t s u p p o r t s m a n y e v e n t s wi t hi n d e p e n d a n t p r o b a b i l i t y v a l u e s . c l a s s e v e n t : n a me =N o n e e v e n t s =N o n e p r o b a b i l i t y =N o n e p a r e n t =N o n e d e f _ _ i n i t _ _ ( s e l f , n a , p) : s e l f . n a me =n a s e l f . p r o b a b i l i t y =p s e l f . e v e n t s =[ ] d e f a d d E v e n t ( s e l f , n a , p) : e =e v e n t ( n a , p) e . s e t P a r e n t ( s e l f ) s e l f . e v e n t s . a p p e n d ( e ) d e f g e t E v e n t ( s e l f , n a ) : f o r i i ns e l f . e v e n t s : i f i . n a me = =n a : r e t u r ni d e f f n d E v e n t s ( s e l f , n a ) : L =[ ] f o r i i ns e l f . e v e n t s : i f i . n a me = =n a : L . a p p e n d ( i ) b r e a k i f l e n ( i . e v e n t s ) > 0 : L =L +i . f n d E v e n t s ( n a ) r e t u r nL d e f s e t P a r e n t ( s e l f , p) : s e l f . p a r e n t =p #c a l c u l a t e s t h e p r o b a b i l i t y o f a ne v e n t h a p p e n i n gwi t ha c o n d i t i o nt h a t mu s t h a p p e n , u s i n gb a y e s t h e r o m. d e f c a l c C o n P r o b ( s e l f , e v e n t n a me , c o n d i t i o n) : #f n dt h e f r s t e v e n t t h a t we a r e c a l c u l a t i n gt h e p r o b a b i l i t y o f .

e =s e l f . f n d E v e n t s ( e v e n t n a me ) [ 0 ] #t h e nr e t r i e v e t h e c o n d i t i o n a l p a r t o f t h e e v e n t #t h e n u me n a t o r p a r t o f b a y e s t h e r o m n=c a l c P r o b ( e . f n d E v e n t s ( c o n d i t i o n) [ 0 ] ) #n o wt h e d e n o mi n a t o r p a r t o f b a y e s t h e r o m L =s e l f . f n d E v e n t s ( c o n d i t i o n) d=0 f o r i i nL : d=d+c a l c P r o b ( i ) #t h e nf n a l l y r e t u r nt h e p r o b a b i l i t y r e t u r nn / d #t h e r e ' s p r o b a b l ya mu c hb e t t e r wa yt os e t u pt h i s d a t a s t r u c t u r e , b u t I c a n ' t i ma g i n e a s s i g n me n t s b e i n g #mu c hmo r e c o mp l i c a t e dt h a nt h i s , a n dI ' mt o ol a z y t oc h a n g e i t . e =e v e n t ( N o n e , N o n e ) e . a d d E v e n t ( " E c o nG r o ws " , 0 . 7) e . a d d E v e n t ( " E c o nS l o ws " , 0 . 3) e . g e t E v e n t ( " E c o nG r o ws " ) . a d d E v e n t ( " S t o c k U p " , 0 . 8) e . g e t E v e n t ( " E c o nG r o ws " ) . a d d E v e n t ( " S t o c k D o wn " , 0 . 2) e . g e t E v e n t ( " E c o nS l o ws " ) . a d d E v e n t ( " S t o c k U p " , 0 . 3) e . g e t E v e n t ( " E c o nS l o ws " ) . a d d E v e n t ( " S t o c k D o wn " , 0 . 7) p r i n t ( " G i v e nt h a t t h e s t o c k we n t u p , t h e p r o b a b i l i t y t h a t t h e e c o n o my g r e wi s " , e . c a l c C o n P r o b ( " E c o nG r o ws " , " S t o c k U p " ) ) e =e v e n t ( N o n e , N o n e ) e . a d d E v e n t ( " F r a n c e " , 2 / 9) e . a d d E v e n t ( " U K " , 3 / 9) e . a d d E v e n t ( " C a n a d a " , 4 / 9) e . g e t E v e n t ( " F r a n c e " ) . a d d E v e n t ( " B o y " , 1 / 2) e . g e t E v e n t ( " F r a n c e " ) . a d d E v e n t ( " G i r l " , 1 / 2) e . g e t E v e n t ( " U K " ) . a d d E v e n t ( " B o y " , 1 / 3) e . g e t E v e n t ( " U K " ) . a d d E v e n t ( " G i r l " , 2 / 3) e . g e t E v e n t ( " C a n a d a " ) . a d d E v e n t ( " B o y " , 1 / 4) e . g e t E v e n t ( " C a n a d a " ) . a d d E v e n t ( " G i r l " , 3 / 4) p r i n t ( " G i v e nt h a t t h e s t u d e n t i s a b o y , t h e p r o b a b i l i t y o f t h a t b o y b e i n gf r e n c hi s " , e . c a l c C o n P r o b ( " F r a n c e " , " B o y " ) ) e =e v e n t ( N o n e , N o n e ) e . a d d E v e n t ( " H a v e d i s e a s e " , 1 / 1 0 0) e . a d d E v e n t ( " N od i s e a s e " , 9 9 / 1 0 0) e . g e t E v e n t ( " H a v e d i s e a s e " ) . a d d E v e n t ( " T e s t p o s i t i v e " , 9 0 / 1 0 0) e . g e t E v e n t ( " H a v e d i s e a s e " ) . a d d E v e n t ( " T e s t n e g a t i v e " , 1 0 / 1 0 0) e . g e t E v e n t ( " N od i s e a s e " ) . a d d E v e n t ( " T e s t p o s i t i v e " , 1 5 / 1 0 0) e . g e t E v e n t ( " N od i s e a s e " ) . a d d E v e n t ( " T e s t n e g a t i v e " , 8 5 / 1 0 0) p r i n t ( " G i v e nt h a t t h e p a t i e n t t e s t e dp o s i t i v e , t h e p r o b a b i l i t yo f t h e p a t i e n t a c t u a l l y b e i n gd i s e a s e di s " , e . c a l c C o n P r o b ( " H a v e d i s e a s e " , " T e s t p o s i t i v e " ) )

S c r e e n s h o t :

Chapter 4 C a e s a r C i p h e r O B J : ma i n . p y Wh a t wo r k e d ? I u s e dt h e b u i l t i nf u n c t i o n e v a l t oa l l o wu s e r i n p u t o f f u n c t i o n s . I t s e a s y a n df a s t , b u t t h e r e wa s n owa yo f e a s i l y g e t t i n gt h e i n v e r s e o f t h e g i v e na l g o r i t h m. S ob a s i c a l l y m y a p p l i c a t i o na s s u me s y o uk n o w wh a t y o u ' r e d o i n ga n dj u s t b l i n d l y a p p l i e s t h e a l g o r i t h mt oa l l t h e c h a r a c t e r s g i v e nt oi t . T od e c r y p t s o me t h i n gy o uj u s t h a v e t og e t t h e i n v e r s e o f wh a t e v e r a l g o r i t h my o uu s e da n di n p u t i t . C o d e : d e f e n c r y p t ( L , a ) : L 2=" " f o r i i nL : i f i = =' ' : L 2=L 2+' ' c o n t i n u e x =o r d ( i ) f o o=r o u n d ( e v a l ( a ) ) L 2=L 2+c h r ( f o o) r e t u r nL 2 p r i n t ( " E n t e r s t r i n gt om o d i f y ( h e l l owo r l d ) : " ) L =i n p u t ( ) p r i n t ( " E n t e r a l g o r i t h mt ou s e ( x +3 ) : " ) a =i n p u t ( ) p r i n t ( e n c r y p t ( L , a ) ) S c r e e n s h o t :

Chapter 5

P r o j e c t : T r i pT h r o u g hG e r ma n y I f o u n dt h i s p r o j e c t i n c r e d i b l y u n e n j o y a b l e d u e t ov a r i o u s r e a s o n s . F i r s t o f a l l s o me o f t h e c i t y n a me s we r e mi s s p e l l e dwh i c hc a u s e dl o t s o f p r o b l e ms wh e nI wa s t r y i n gt og e t t h e i r c o o r d i n a t e s o r wh e nI wa s c r e a t i n gt h e i r n e i g h b o r i n gt r a v e r s a b l e c i t i e s . I p r o b a b l y s p e n t o v e r 6h o u r s j u s t g e t t i n gt h e d a t a b a s e s e t u pe n o u g hs oI c a n a c t u a l l y c r e a t e a na l g o r i t h mb a s e do ni t . I h a dt owo r k f a r t omu c hm a n u a l l y a d d i n g i n f o r ma t i o ni n t ot h e d a t a b a s e . A f t e r a l l t h i s s t r i f e a n dt o i l wh e nI f n a l l y a c t u a l l y g o t t ot h e p r o g r a m mi n gp a r t I d i d n ' t e v e nh a v e t i me t og e t i t wo r k i n gt h e wa y I wa n t e d . G i v i n gme l e s s t h a no p t i ma l r e s u l t s , b u t r e s u l t s n o n t h e l e s s . R e a l l y wi s ht h e p r o j e c t i n v o l v e dl e s s d a t a b a s e c r e a t i o na n dmo r e p r o g r a mmi n g . C o d e : p y t h o n , ma i n . p y i mp o r t s q l i t e 3a s s q l i mp o r t s y s i mp o r t j s o n i mp o r t s t r i n g i mp o r t ma t h c i t i e s =[ ] c l a s s C i t y : d e f _ _ i n i t _ _ ( s e l f , i d , n a me , n e i g h b o r s , l a t i t u d e , l o n g i t u d e , e x t r a f e e s , e x t r a t i me , e x t r a t r a v e l ) : s e l f . i d=i d s e l f . n a me =n a me s e l f . l a t i t u d e =l a t i t u d e i f n e i g h b o r s ! =N o n e : s e l f . n e i g h b o r s =j s o n . l o a d s ( n e i g h b o r s ) e l s e : s e l f . n e i g h b o r s =N o n e s e l f . c o s t s =N o n e s e l f . d i s t a n c e s =N o n e s e l f . t i me s =N o n e s e l f . mo n e y=N o n e s e l f . l o n g i t u d e =l o n g i t u d e s e l f . e x t r a f e e s =e x t r a f e e s

s e l f . e x t r a t i me =e x t r a t i me s e l f . e x t r a t r a v e l =e x t r a t r a v e l

#U s e s h a v e r s i n e f o r mu l a t oa p p r o x i ma t e d i s t a n c e #f r o ml a t i t u d e a n dl o n g i t u d e s d e f d i s t a n c e ( l o n A , l a t A , l o n B , l a t B) : r =6 3 7 1#r a d i u s o f t h e e a r t hi nk m a =ma t h . r a d i a n s ( l a t A) b=ma t h . r a d i a n s ( l a t B) d a =m a t h . r a d i a n s ( l a t B l a t A) d b=m a t h . r a d i a n s ( l o n B l o n A) e =ma t h . s i n ( d a / 2) *m a t h . s i n ( d a / 2) +ma t h . c o s ( a ) *ma t h . c o s ( b) * ma t h . s i n ( d b / 2) *ma t h . s i n ( d b / 2) f =2*m a t h . a t a n 2 ( m a t h . s q r t ( e ) , m a t h . s q r t ( 1 a ) ) r e t u r nr *f #r e t u r n s i nk m


d e f g e t D i s t a n c e ( c i t y A , c i t y B) : r e t u r nd i s t a n c e ( c i t y A . l o n g i t u d e , c i t y A . l a t i t u d e , c i t y B . l o n g i t u d e , c i t y B . l a t i t u d e ) + c i t y B . e x t r a t r a v e l d e f g e t C o s t ( c i t y A , c i t y B) : d i s =d i s t a n c e ( c i t y A . l o n g i t u d e , c i t y A . l a t i t u d e , c i t y B . l o n g i t u d e , c i t y B . l a t i t u d e ) + c i t y B . e x t r a t r a v e l #a s s u me a t a x i i s t h e o n l ya v a i l a b l e f o r mo f t r a v e l #a n dt h a t i t c o s t s 2E u r o s p e r k m mo n e y =d i s *2+c i t y B . e x t r a f e e s #t i me i nh o u r s , a s s u me t a x i ' s mo v e a t a s o l i d1 3 0 k m/ h t i me =d i s / 1 3 0+c i t y B . e x t r a t i me r e t u r nd i s +mo n e y +t i me d e f g e t Mo n e y ( c i t y A , c i t y B) : d i s =d i s t a n c e ( c i t y A . l o n g i t u d e , c i t y A . l a t i t u d e , c i t y B . l o n g i t u d e , c i t y B . l a t i t u d e ) + c i t y B . e x t r a t r a v e l #a s s u me a t a x i i s t h e o n l ya v a i l a b l e f o r mo f t r a v e l #a n dt h a t i t c o s t s 2E u r o s p e r k m mo n e y =d i s *2+c i t y B . e x t r a f e e s #t i me i nh o u r s , a s s u me t a x i ' s mo v e a t a s o l i d1 3 0 k m/ h t i me =d i s / 1 3 0+c i t y B . e x t r a t i me

r e t u r nmo n e y d e f g e t T i me ( c i t y A , c i t y B) : d i s =d i s t a n c e ( c i t y A . l o n g i t u d e , c i t y A . l a t i t u d e , c i t y B . l o n g i t u d e , c i t y B . l a t i t u d e ) + c i t y B . e x t r a t r a v e l #a s s u me a t a x i i s t h e o n l ya v a i l a b l e f o r mo f t r a v e l #a n dt h a t i t c o s t s 2E u r o s p e r k m mo n e y =d i s *2+c i t y B . e x t r a f e e s #t i me i nh o u r s , a s s u me t a x i ' s mo v e a t a s o l i d1 3 0 k m/ h t i me =d i s / 1 3 0+c i t y B . e x t r a t i me r e t u r nt i me d e f t r a v e r s e ( c i t y , c i t i e s , v i s i t e d , mo n e y , t i me , d i s t ) : #G e t a n e i g h b o r t h a t h a s a l o wc o s t a n dh a s n ' t b e e nv i s i t e d l o we s t =N o n e l o we s t c i t y =N o n e f o r i i nr a n g e ( 0 , l e n ( c i t y . n e i g h b o r s ) ) : i f l o we s t i s N o n e o r ( c i t y . c o s t s [ i ] <l o we s t a n dn o t c i t y . n e i g h b o r s [ i ] i nv i s i t e d) : l o we s t =c i t y . c o s t s [ i ] l o we s t c i t y =c i t y . n e i g h b o r s [ i ] #I f we c o u l d n ' t f n da c i t yt oc o n t i n u e t r a v e r s i n g , we j u s t u s e #t h e l o we s t c o s t t r a v e r s a l we c a nf n dwi t h i nt h e r e ma i n i n gs e t i f l o we s t c i t yi nv i s i t e d : l o we s t =N o n e l o we s t c i t y =N o n e f o r i i nr a n g e ( 0 , l e n ( c i t i e s ) ) : i f l o we s t i s N o n e o r ( c i t y! =c i t i e s [ i ] a n dg e t C o s t ( c i t y , c i t i e s [ i ] ) < =l o we s t a n d n o t c i t i e s [ i ] i nv i s i t e d) o r l o we s t c i t yi nv i s i t e d : l o we s t =g e t C o s t ( c i t y , c i t i e s [ i ] ) l o we s t c i t y =c i t i e s [ i ] i f l o we s t c i t yi nv i s i t e d : r e t u r n[ mo n e y , t i me , d i s t ] mo n e y + =g e t Mo n e y ( c i t y , l o we s t c i t y) t i me + =g e t T i me ( c i t y , l o we s t c i t y) d i s t + =g e t D i s t a n c e ( c i t y , l o we s t c i t y) v i s i t e d . a p p e n d ( c i t y) p r i n t ( c i t y . n a me , " > " , e n d = " " ) i f l e n ( c i t i e s ) = =l e n ( v i s i t e d) :

r e t u r n[ mo n e y , t i me , d i s t ] r e t u r nt r a v e r s e ( l o we s t c i t y , c i t i e s , v i s i t e d , mo n e y , t i me , d i s t )

#Me a n t t or e p l a c e n a me s wi t hr e f e r e n c e s t ot h e a c t u a l c i t i e s #t h i s ma k e s i t e a s i e r t oma n i p u l a t e t h e m d e f s o l v e N e i g h b o r s ( c i t i e s ) : f o r i i nc i t i e s : f o r oi nr a n g e ( 0 , l e n ( i . n e i g h b o r s ) ) : f o u n d Ma t c h=F a l s e f o r pi nc i t i e s : i f p . n a me = =i . n e i g h b o r s [ o ] : i . n e i g h b o r s [ o ] =p f o u n d Ma t c h=T r u e i f n o t f o u n d Ma t c h : p r i n t ( " E r r o r : C i t y n o t f o u n d : " , i . n e i g h b o r s [ o ] ) s y s . e x i t ( 1) #A s s u me s s o l v e N e i g h b o r s wa s r a n d e f g e n e r a t e C o s t s ( c i t i e s ) : #C o s t i s d i s t a n c e +t i me +mo n e y f o r i i nc i t i e s : i . c o s t s =[ ] i . t i me s =[ ] i . mo n e y=[ ] i . d i s t a n c e s =[ ] f o r oi ni . n e i g h b o r s : d i s =d i s t a n c e ( i . l o n g i t u d e , i . l a t i t u d e , o . l o n g i t u d e , o . l a t i t u d e ) +o . e x t r a t r a v e l #a s s u me a t a x i i s t h e o n l ya v a i l a b l e f o r mo f t r a v e l #a n dt h a t i t c o s t s 2E u r o s p e r k m mo n e y =d i s *2+o . e x t r a f e e s #t i me i nh o u r s , a s s u me t a x i ' s mo v e a t a s o l i d1 3 0 k m/ h t i me =d i s / 1 3 0+o . e x t r a t i me i . c o s t s . a p p e n d ( d i s +t i me +m o n e y) i . d i s t a n c e s . a p p e n d ( d i s ) i . t i me s . a p p e n d ( t i me ) i . mo n e y . a p p e n d ( mo n e y)

d b=N o n e t r y : d b=s q l . c o n n e c t ( ' c i t i e s . d b ' ) c u r =d b . c u r s o r ( ) c u r . e x e c u t e ( ' S E L E C T*F R O M` c i t i e s ` ' ) d a t a =c u r . f e t c h o n e ( ) wh i l ed a t a i s n o t N o n e : c i t i e s . a p p e n d ( C i t y ( d a t a [ 0 ] , d a t a [ 1 ] , d a t a [ 2 ] , d a t a [ 3 ] , d a t a [ 4 ] , d a t a [ 5 ] , d a t a [ 6 ] , d a t a [ 7 ] ) ) d a t a =c u r . f e t c h o n e ( ) e x c e p t s q l . E r r o r a s e : p r i n t ( " S Q L E x c e p t i o n : " , e . a r g s [ 0] ) s y s . e x i t ( 1) f n a l l y : d b . c l o s e ( )

#T h i s s e c t i o nwa s u s e dt op o p u l a t e t h e t a b l e f o r n e i g h b o r s #c o mp l e t e l yu s e l e s s n o wt h a t i t s p o p u l a t e d # f o r i i nc i t i e s : # p r i n t ( " Wh oi s " +i . n a me + " \ ' s n e i g h b o r s ? : " ) # n e i g h b o r s t r i n g=i n p u t ( ) # n e i g h b o r s =n e i g h b o r s t r i n g . s p l i t ( " " ) # p r i n t ( " U s i n g" , n e i g h b o r s , " \ nf r o m" , i . n e i g h b o r s ) # i . n e i g h b o r s =n e i g h b o r s # d b=s q l . c o n n e c t ( ' c i t i e s . d b ' ) # d b . e x e c u t e ( ' U P D A T E` c i t i e s `S E Tn e i g h b o r s = ? WH E R Ei d = ? ; ' , [ j s o n . d u mp s ( n e i g h b o r s ) , s t r ( i . i d) ] ) # d b . c o mmi t ( ) #G e n e r a t e d i s t a n c e s f o r a l l t h e c i t yc o n n e c t i o n s s o l v e N e i g h b o r s ( c i t i e s ) g e n e r a t e C o s t s ( c i t i e s ) p r i n t ( " Al i s t o f t h e c i t i e s , t h e i r n e i g h b o r s , a n dt h e i r c o s t s t ov i s i t t h e n e i g h b o r s . . . " ) f o r i i nc i t i e s : p r i n t ( i . n a me ) f o r oi nr a n g e ( 0 , l e n ( i . n e i g h b o r s ) ) : p r i n t ( " \ t " , i . n e i g h b o r s [ o ] . n a me , i . c o s t s [ o ] ) s t a r t i n g c i t y =N o n e

f o r i i nc i t i e s : i f i . n a me = =" F r a n k f u r t " : s t a r t i n g c i t y =i b r e a k p r i n t ( " C a l c u l a t i n gg r e e d y o p t i ma l p a t hs t a r t i n gf r o mF r a n k f u r t . . . " ) c o s t =t r a v e r s e ( s t a r t i n g c i t y , c i t i e s , [ ] , 0 , 0 , 0) p r i n t ( " " ) p r i n t ( " U s i n gt h i s p a t hy o uwo u l du s e " , c o s t [ 0 ] , " E u r o s , s p e n d " , c o s t [ 1 ] , " h o u r s ( t r a v e l i n g , r e s t i n g) , a n dt r a v e l " , c o s t [ 2 ] , " k m. " )

S c r e e n s h o t :

Chapter 6 ModularExponentiation OBJ:main.py Whatworked? Readingandinterpretingthepseudocodefromthebookitwaseasytoimplementfunctionsfor Algorithm1and5inpython.Ieasilycreatedabaseconverterfunctionanduseditwithinmymodular exponentiationfunctionusingtheconceptsIlearnedfromthebook.UsingtheModularExponentiation Icouldeasilysolveproblems2528onpage255. CODE(Python3.3)


i mp o r t ma t h d e f b a s e ( n , b) : a =" " wh i l e n! =0 : a =s t r ( n%b) +a n=ma t h . f o o r ( n/ b) r e t u r na d e f mo d E x p o ( b , n , m) : x =1 p o we r =b%m f o r i i nr e v e r s e d ( b a s e ( n , 2) ) : i f i = =" 1 " : x =( x *p o we r ) %m p o we r =( p o we r *p o we r ) %m #x =b ^ n % m r e t u r nx p r i n t ( " 4 . 2 . 2 5 : ( 7 ^ 6 4 4 ) % 6 4 5= " , mo d E x p o ( 7 , 6 4 4 , 6 4 5) ) p r i n t ( " 4 . 2 . 2 6 : ( 1 1 ^ 6 4 4 ) % 6 4 5= " , mo d E x p o ( 1 1 , 6 4 4 , 6 4 5) ) p r i n t ( " 4 . 2 . 2 7 : ( 3 ^ 2 0 0 3 ) % 9 9= " , mo d E x p o ( 3 , 2 0 0 3 , 9 9) ) p r i n t ( " 4 . 2 . 2 8 : ( 1 2 3 ^ 1 0 0 1 ) % 1 0 1= " , mo d E x p o ( 1 2 3 , 1 0 0 1 , 1 0 1) )

S C R E E N S H O T

Chapter 7 Project: n-queens Since my student ID is odd, I had to find all solutions to a specified board size to the nqueen problem. It proved quite difficult due to error in translation from my brain logic to code. What I mean by that is that even without looking up what backtracking was, I already had the idea of backtracking in my head. I just didn't know how to translate it into clean functioning code at first. The first iteration of my program simply solving ONE solution of the puzzle was a nasty nested while loop with confusing backtracking. It did work but it was really messy and didn't find ALL solutions. From here I looked up how other people coded it, I noticed they did it recursively instead and it made waaay more sense to me how to make it cleaner. From there I had a function that could simply solve the puzzle once, but after much thought, trial, and error (hours of it) I realized the same function couldn't generate all the solutions at once. So I split up the tasks into solve(), and permutate(). The solve() function simply takes any partial or uncompleted puzzles and attempts to complete them. While the permutate() function takes completed puzzles and recursively permutates them by row, and then uses solve() to attempt to solve the new permutation. Since the permutations all happen linearly-- there's no chance for repeating solutions to end up in the final list of solutions. Thus with a simple permutate( solve() ) my program generates all solutions. However the implementation of the program is kinda bad it definitely isn't just a simple permutate( solve() ). In other words the functions are not modular and are completely state dependant, and they require somewhat of a kick-start macroing to work properly. Which is a pretty big deal to me, but I'm running low on time and I really don't want to restructure/recomment everything again. Code: solutions = [] class queen: x=0 y=0 def __init__( self, x, y ): self.x = x self.y = y def hasCollision( self, q ): # can't collide with itself if q == self: return False dx = abs( self.x - q.x ) dy = abs( self.y - q.y ) if dx == dy: # diagnals is the same return True if dx == 0: # row is the same return True if dy == 0: # column is the same return True return False def findCollisions( queens, q ):

hc = False for nq in queens: if q.hasCollision( nq ): hc = True break return hc # takes an existing board of queens and attempts to recursively add more queens under the condition that they cannot be attacked # if it fails to add a queen it returns an empty table, otherwise it will return the completed puzzle def solve( row, size, queens ): # just setting up some variables to use global solutions # if we have a completed solution if row >= size: # add it to the list of solutions solutions.append( queens ) return queens for column in range( 0, size ): # we create a plain old queen in the specified area only to test it against the queens on the board already. q = queen( column, row ) if findCollisions( queens, q ) == False: # if our position is safe, add the queen to the board! queens.append( q ) # if we couldn't find a position for the next row, backtrack to a previous queen. if solve( row+1, size, queens ) == []: queens.pop() else: # since the above solve( row+1, size, queens ) alters the queens table, its actually already a completed puzzle now! # return it since its complete. return queens return [] # start the first iteration of the recursive permutation for us. # this is basically a macro to get the permutations running recursively correctly. def permutateAll( queens ): permutate( 0, len( queens ), queens ) for i in range( 1, len( queens ) ): permutate( i, len( queens ), queens ) # recursively permutates a completed queen set starting by moving the queen at the specified row over by one safe-spot, # then using solve() to complete the new unique puzzle fragment # then recurses onto all the new unique generated rows def permutate( row, size, queens ): # if we're asking to permutate a row that doesn't exist, exit. if row >= size:

return # copy the queen table to the specified row, permutatedQueens = [] for i in range( 0, row ): permutatedQueens.append( queens[ i ] ) q = queens[ row ] # instead of copying everything directly, we mutate the specififed row by attempting to move it along the board permutated = False for column in range( q.x+1, size ): q = queen( column, q.y ) if findCollisions( permutatedQueens, q ) == False: permutatedQueens.append( q ) permutated = True # if we couldn't move it any further along, due to collisions/running out of board space, we exit. if not permutated: return # now with our new permutated partial board, we attempt to solve the rest of the puzzle test = solve( row+1, size, permutatedQueens ) # if we couldn't solve it, we continue to permutate the current row if test == []: permutate( row, size, permutatedQueens ) return # if we succeeded, we not only do we continue to permutate the current row permutate( row, size, permutatedQueens ) # but we also recursively permutate all the new rows that were generated for i in range( row+1, size ): permutate( i, size, test ) return # some silly code to generate a grid of blocks and Qs to represent the board and where the queens are. # might not be an accurate chess board def printTable( queens ): size = len( queens ) toggle = True table = [] for x in range( 0, size ): row = [] for y in range( 0, size ): if toggle: row.append( "" ) else: row.append( " " ) toggle = not toggle table.append( row ) if size % 2 == 0: toggle = not toggle

for i in queens: table[i.y][i.x] = "Q" for x in table: for y in x: print( y, end="" ) print( "" ) # size of the board size = 8 # generates a starting solution of the n-queens problem. startingboard = solve( 0, size, [] ) # recursively permutates the solution to find all other solutions. permutateAll( startingboard ) # print all the found solutions and number them count = 0 for i in solutions: print( "Unique Solution #", count, "--------" ) printTable( i ) count += 1 # print some information print( "Found", len( solutions ), "unique solutions for a " + str( size ) + "x" + str( size ) + " board." ) print( "This application can find all the solutions to different sized boards, just change the size = # variable inside the code" ) Screenshot:

Chapter 9 RecursivevsIterative OBJ:main.py Whatworked? Iusedpython'sbuiltintimingsystemscalledtimeit,whichletmeeasilyandaccuratelytime thedifferentalgorithms.Whatwasinterestingisthatrecursivevsiterativefunctionsissituational. Iterativefibonaccifunctionsarefasterthanrecursiveones,whilerecursivemodexpofunctionsarefar fasterthanmyparticulariterativemodexpo.Itcouldbeduetothearrayallocationshowever. CODE(Python)


#c a l u te u s #c a l u t u #h # u t p h r t l o b a i e y y # f t i t n v t t t t # r t i t g g t . v t l h u d l i t . t t t t h P # r t i t g g t ( v t l h u d l i t ( t t t t h P ) h v t l h u d l i t l o b a i e y y # f t i t : t ( t n t = t l o b a i e y y # f t i t : t w t n t t p h r t # o b a i e y y # f t i t n v t t t t ! t g t . t t t t N t g t ( t t t t r a l t # t # i t l e i m h f t ( * t i t n v t t t t t t t t _ t g t ! t = t N t t t t t t t t ! t g t N t t t t t t t t N t g t _ t t t t l h u d l i t N p h r t b e ) h f t i * t b t n v t t t t e t g t , , t t t t [s # P h t i t ] g t . v t t t t t t t t e t g t ) u l f t i t E t b t n t = t e t t t t t t t t i t g te u s f L a a l f t i t k t b t n t t t t l h u d l i t e p h r ta p > ! c a f t b * t i * tt n v t t t t ! t g t ( t t t t c a [h l t g t b t E t t t t t r a l t # t # i t l h 0 h l ) h p f t b e ) h f t i * t w t n t n v t t t t t t t t # r t # t g g t , ( , v t t t t t t t t t t t t ! t g t f t ! t + t c a [h l t n t E t t t t t t t t t c a [h l t g t f t c a [h l t + t c a [h l t n t E t t t t t C t ! t g t b / i E t t t t l h u d l i t ! p h r t la p > ! c a f t b * t i * tt n v t t t t # r t b t ' g t . t a l tt ' t w t a l t i t ' t . v t t t t t t t t l h u d l i t ( t t t t # r t i t g g t . v t t t t t t t t l h u d l i t ( t t t t h P # r t i t E t w t g g t . v

t t t t t t t t ! t g t la p > ! c a f t b * te u s f L a a l f t i k w t n * tt n t t t t t t t t l h u d l i t f t ! t + t ! t n t E t t t t t h P ) h v t t t t t t t t ! t g t la p > ! c a f t b * te u s f L a a l f t i k w t n * tt n t t t t t t t t l h u d l i t f t f t ! t + t ! t n t E tt + t b t E tt n t E t c l # i u f t , I h y d l ) # 0 h t o b a i e y y # t a r t w ( t u #h v , * t h i p g , t , t n l t g t u #h # u f u #h # u f t z l o b a i e y y # f t w ( t n z * t ) h u d c g , r l at " "e # i " " t #c a l u t l o b a i e y y # , * t i db h l g ( . . t n c l # i u f t l * t ,# y l a ) h y a i p ) , t n c l # i u f t , G u h l e u # a i t o b a i e y y # t a r t w ( t u #h v , * t h i p g , t , t n # t g t u #h # u f u #h # u f t z # o b a i e y y # f t w ( t n z * t ) h u d c g , r l at " "e # i " " t #c a l u t # o b a i e y y # , * t i db h l g ( . . t n c l # i u f t # * t ,# y l a ) h y a i p ) , t n # r t # t ' t l v t t t t c l # i u f t , G u h l e u # 0 h t 0 h l ) # a i t [# i ) ] , t n h P ) h v t t t t c l # i u f t , I h y d l ) # 0 h t 0 h l ) # a i t [# i ) ] , t n c l # i u f t , I h y d l ) # 0 h t a r t f 7 7 7 7 7 / 7 7 7 7 7 n E ( . ( v , * t h i p g , t , t n l t g t u #h # u f u #h # u f t z la p > ! c a f t 7 7 7 7 7 * t 7 7 7 7 7 * t ( . ( t n z * t ) h u d c g , r l at " "e # i " " t #c a l u t la p > ! c a , * t i db h l g ( . . t n c l # i u f t l * t ,# y l a ) h y a i p ) , t n c l # i u f t , G u h l e u # a i t a r t f 7 7 7 7 7 / 7 7 7 7 7 n E ( . ( v , * t h i p g , t , t n # t g t u #h # u f u #h # u f t za p > ! c a f t 7 7 7 7 7 * t 7 7 7 7 7 * t ( . ( t n z * t ) h u d c g , r l at " "e # i " " t #c a l u ta p > ! c a , * t i db h l g ( . . t n c l # i u f t # * t ,# y l a ) h y a i p ) , t n # r t # t ' t l v t t t t c l # i u f t , G u h l e u # 0 h t 0 h l ) # a i t [# i ) ] , t n h P ) h v t t t t c l # i u f t , I h y d l ) # 0 h t 0 h l ) # a i t [# i ) ] , t n

S 3 I > > U 3 8 D

Chapter 10 Project:RSAencryption Whatworked?Mypreviousmodularexponentiationalgorithmworkedperfectlytoencryptand decryptthesentenceTheQueenCan'tRollWhenSandisintheJarinareasonable timeframe. IdidhaveproblemsunderstandingtheextendedEuclideanalgorithmduetowikipedia expectingtoomuchbackgroundknowledge,howeverIdidunderstandwhatmodular multiplicativeinverseisandwasabletoimplementmyownsimplealgorithm. Code:


importmath defbase(n,b): a="" whilen!=0: a=str(n%b)+a n=math.floor(n/b) returna defmodExpo(b,n,m): x=1 power=b%m foriinreversed(base(n,2)): ifi=="1": x=(x*power)%m power=(power*power)%m #x=b^n%m returnx #ShouldbeusingtheExtendedEuclideanalgorithm,buthere'smyowneasiertounderstandone:). definvmod(a,m): foriinrange(1,m): ifi*a%m==1: returni raiseValueError("aandmshouldbeprimeorsomething") #Notevensurewhata"totient"is,butwikipediasaidthisisthefunctionforit. deftotient(p,q): return(p1)*(q1) defRSAEncrypt(p,q,e,message): n=p*q t=totient(p,q) ift%e==0: raiseException("ecan'tbeadivisorofthetotientofpandq,hastobecoprime") d=invmod(e,t) L=[] foriinrange(0,len(message)): L.append(modExpo(ord(message[i]),e,n)) #dandnaretheprivatekey #publickeyisnande returnL,d,n

defRSADecrypt(n,d,message): L="" foriinrange(0,len(message)): L=L+chr(modExpo(message[i],d,n)) returnL #dandnaretheprivatekeytodecrypt emessage,d,n=RSAEncrypt(71,67,17,"TheQueenCan'tRollWhenSandisintheJar") print("Encryptedmessage:",emessage) print("Decryptedmessage:",RSADecrypt(n,d,emessage))

Screenshot:

Sortingalgorithms OBJ:main.py Whatworked? Iusedpython'sbuiltintimingsystemscalledtimeit,whichletmeeasilyandaccuratelytime thedifferentsortingalgorithms.Quicksort,ofcourse,outperformedprettymuchanyotheralgorithm. FortherollyourownItriedtodoashelllikesort,butitwasoutperformedbyanormalbubblesort anyway:(.Understandingthequicksortalgorithmwasn'tveryhardbecauseofvideos,but implementingitwashardbecauseIdon'thaveanykindofproperdebuggingtoolsyet,soIendedup havingtounderstandarecursivefunctionviaprintingtotheconsole.Iendedupgettingitworking anyway. CODE(Python)
i mp o r t r a n d o m i mp o r t t i me i t

d e f i n i t ( ) : #t e nt h o u s a n di n t e g e r s . r e t u r n[ r a n d o m. r a n d i n t ( 0 , 1 0 0 0 0) f o r r i nr a n g e ( 1 0 0 0) ] d e f p r i n t L i s t ( L i s t ) : f o r i i nL i s t : p r i n t ( s t r ( i ) +" , " , e n d = " " ) p r i n t ( " " ) d e f b u b b l e S o r t ( L i s t ) : S o r t e d=F a l s e wh i l e S o r t e d= =F a l s e : S o r t e d=T r u e f o r i i nr a n g e ( 0 , l e n ( L i s t ) 1) : # D e t e c t i f a s wa pi s r e q u i r e d i f L i s t [ i ] > L i s t [ i +1] : # S wa p f o o=L i s t [ i +1] L i s t [ i +1] =L i s t [ i ] L i s t [ i ] =f o o # Ma k e s u r e we k n o wi f we h a dt os wi t c hs o me t h i n g S o r t e d=F a l s e d e f s p l i t ( L i s t , p i v o t I n d e x , r i g h t I n d e x ) : l e f t C u r s o r =p i v o t I n d e x +1 r i g h t C u r s o r =r i g h t I n d e x f n i s h e d=F a l s e s wa p P o i n t =1 wh i l e n o t f n i s h e d : # f o r i i nr a n g e ( p i v o t I n d e x , r i g h t I n d e x +1) : # p r i n t ( i , " : " , L i s t [ i ] , " , " , e n d = " " ) # p r i n t ( " " ) f o u n d L e f t S wa p=F a l s e f o u n d R i g h t S wa p=F a l s e

# F i n da s wa pl o c a t i o nwi t ht h e l e f t c u r s o r wh i l e n o t f o u n d L e f t S wa pa n dl e f t C u r s o r < =r i g h t C u r s o r : i f L i s t [ l e f t C u r s o r ] >L i s t [ p i v o t I n d e x ] : f o u n d L e f t S wa p=T r u e b r e a k i f l e f t C u r s o r > r i g h t I n d e x : b r e a k l e f t C u r s o r + =1 i f n o t f o u n d L e f t S wa pa n dl e f t C u r s o r > =r i g h t I n d e x : # S p e c i a l c a s e wh e r e we p i c k e dt h e h i g h e s t n u mb e r f o r t h e p i v o t p o i n t . # p r i n t ( " S wa p p i n gS p e c i a l " , p i v o t I n d e x , " a n d " , r i g h t I n d e x ) f o o=L i s t [ r i g h t I n d e x ] L i s t [ r i g h t I n d e x ] =L i s t [ p i v o t I n d e x ] L i s t [ p i v o t I n d e x ] =f o o f n i s h e d=T r u e s wa p P o i n t =r i g h t I n d e x b r e a k wh i l e n o t f o u n d R i g h t S wa pa n dr i g h t C u r s o r >l e f t C u r s o r : i f L i s t [ r i g h t C u r s o r ] < =L i s t [ p i v o t I n d e x ] : f o u n d R i g h t S wa p=T r u e b r e a k i f r i g h t C u r s o r <p i v o t I n d e x : b r e a k r i g h t C u r s o r =1 # I f we f o u n dt wov a l i ds wa p s , s wa pa n dc o n t i n u e i f f o u n d R i g h t S wa pa n df o u n d L e f t S wa p : # p r i n t ( " S wa p p i n gN o r ma l " , l e f t C u r s o r , " a n d " , r i g h t C u r s o r ) f o o=L i s t [ r i g h t C u r s o r ] L i s t [ r i g h t C u r s o r ] =L i s t [ l e f t C u r s o r ] L i s t [ l e f t C u r s o r ] =f o o e l s e : # O t h e r wi s e we h a v e r u no u t o f t h i n g s t od o , a n dmu s t p u t t h e p i v o t i nt h e mi d d l e s wa p P o i n t =l e f t C u r s o r 1 # p r i n t ( " S o r t e d , n o ws wa p p i n g " , p i v o t I n d e x , " a n d " , s wa p P o i n t ) f o o=L i s t [ s wa p P o i n t ] L i s t [ s wa p P o i n t ] =L i s t [ p i v o t I n d e x ] L i s t [ p i v o t I n d e x ] =f o o f n i s h e d=T r u e b r e a k # N o w we s h o u l dh a v e a s p l i t l i s t , t oc o mp l e t e l y s o r t i t we mu s t r e c u r s i v e l y s p l i t i t . i f s wa p P o i n t p i v o t I n d e x > 1 : # p r i n t ( " l e f t r e c u r s i o na t " , s wa p P o i n t ) s p l i t ( L i s t , p i v o t I n d e x , s wa p P o i n t 1) i f r i g h t I n d e x s wa p P o i n t > 1 : # p r i n t ( " r i g h t r e c u r s i o na t " , s wa p P o i n t ) s p l i t ( L i s t , s wa p P o i n t +1 , r i g h t I n d e x ) r e t u r n0

d e f q u i c k S o r t ( L i s t ) : # R e c u r s i v e l ys p l i t t h e l i s t u n t i l i t ' s s o r t e d . s p l i t ( L i s t , 0 , l e n ( L i s t ) 1) d e f b o g o S o r t ( L i s t ) : S o r t e d=F a l s e wh i l e n o t S o r t e d : S o r t e d=T r u e f o r i i nr a n g e ( 0 , l e n ( L i s t ) 1) : i f L i s t [ i ] > L i s t [ i +1] : S o r t e d=F a l s e b r e a k i f n o t S o r t e d : #: ) r a n d o m. s h u fe ( L i s t ) d e f r o l l S o r t ( L i s t ) : S o r t e d=F a l s e i =0 j =1 0 0 wh i l e n o t S o r t e do r j >1 : S o r t e d=T r u e i f i > =l e n ( L i s t ) j : i =l e n ( L i s t ) j i f j > 1 : j =1 wh i l e i < l e n ( L i s t ) j : i f L i s t [ i ] > L i s t [ i +j ] : f o o=L i s t [ i +j ] L i s t [ i +j ] =L i s t [ i ] L i s t [ i ] =f o o S o r t e d=F a l s e i + =j p r i n t ( " B u b b l e S o r t T i me : " , e n d = " " ) p r i n t ( t i me i t . t i me i t ( ' b u b b l e S o r t ( i n i t ( ) ) ' , s e t u p = " f r o m_ _ ma i n _ _i mp o r t b u b b l e S o r t ; f r o m_ _ ma i n _ _i mp o r t i n i t " , n u mb e r = 1 0) , " s e c o n d s " ) p r i n t ( " Q u i c k S o r t T i me : " , e n d = " " ) p r i n t ( t i me i t . t i me i t ( ' q u i c k S o r t ( i n i t ( ) ) ' , s e t u p = " f r o m_ _ ma i n _ _i mp o r t q u i c k S o r t ; f r o m_ _ ma i n _ _i mp o r t i n i t " , n u mb e r = 1 0) , " s e c o n d s " ) p r i n t ( " R o l l My O wnS o r t T i me : " , e n d = " " ) p r i n t ( t i me i t . t i me i t ( ' r o l l S o r t ( i n i t ( ) ) ' , s e t u p = " f r o m_ _ m a i n _ _i mp o r t r o l l S o r t ; f r o m_ _ ma i n _ _i mp o r t i n i t " , n u mb e r = 1 0) , " s e c o n d s " )

S C R E E N C A P

You might also like