File tree Expand file tree Collapse file tree 2 files changed +52
-3
lines changed
src/08-dynamic-programming Expand file tree Collapse file tree 2 files changed +52
-3
lines changed Original file line number Diff line number Diff line change @@ -24,10 +24,35 @@ class HanoiTowers {
2424 }
2525
2626 getMovements ( ) {
27- if ( ! this . t1 . isEmpty ( ) ) {
28- this . move ( this . t1 , this . t2 ) ;
29- this . move ( this . t2 , this . t3 ) ;
27+ const t1 = this . t1 ;
28+ const t2 = this . t2 ;
29+ const t3 = this . t3 ;
30+
31+ while ( ! this . t1 . isEmpty ( ) ) {
32+ this . move ( t1 , t2 ) ;
33+ while ( t2 . peek ( ) > t3 . peek ( ) ) {
34+ this . move ( t3 , t2 ) ;
35+ while ( t2 . peek ( ) > t1 . peek ( ) ) {
36+ this . move ( t1 , t2 ) ;
37+ while ( t2 . peek ( ) > t3 . peek ( ) ) {
38+ this . move ( t3 , t2 ) ;
39+ while ( t2 . peek ( ) > t1 . peek ( ) ) {
40+ this . move ( t1 , t2 ) ;
41+ while ( t2 . peek ( ) > t3 . peek ( ) ) {
42+ this . move ( t3 , t2 ) ;
43+ this . move ( t2 , t1 ) ;
44+ }
45+ this . move ( t2 , t3 ) ;
46+ }
47+ this . move ( t2 , t1 ) ;
48+ }
49+ this . move ( t2 , t3 ) ;
50+ }
51+ this . move ( t2 , t1 ) ;
52+ }
53+ this . move ( t2 , t3 ) ;
3054 }
55+
3156 return this . movements ;
3257 }
3358
Original file line number Diff line number Diff line change @@ -9,4 +9,28 @@ describe('hanoiTower', function() {
99 it ( '1' , function ( ) {
1010 expect ( hanoiTower ( 1 ) ) . to . equal ( 2 ) ;
1111 } ) ;
12+
13+ it ( '2' , function ( ) {
14+ expect ( hanoiTower ( 2 ) ) . to . equal ( 8 ) ;
15+ } ) ;
16+
17+ it ( '3' , function ( ) {
18+ expect ( hanoiTower ( 3 ) ) . to . equal ( 26 ) ;
19+ } ) ;
20+
21+ it ( '4' , function ( ) {
22+ expect ( hanoiTower ( 4 ) ) . to . equal ( 80 ) ;
23+ } ) ;
24+
25+ it ( '5' , function ( ) {
26+ expect ( hanoiTower ( 5 ) ) . to . equal ( 242 ) ;
27+ } ) ;
28+
29+ it ( '6' , function ( ) {
30+ expect ( hanoiTower ( 6 ) ) . to . equal ( 728 ) ;
31+ } ) ;
32+
33+ it ( '7' , function ( ) {
34+ expect ( hanoiTower ( 7 ) ) . to . equal ( 728 ) ;
35+ } ) ;
1236} ) ;
You can’t perform that action at this time.
0 commit comments