BookmarkSubscribeRSS Feed
Stepkwan96
Calcite | Level 5

Hi everyone, i am currently working on an assignment related to Tower of Hanoi. However, i am unsure about part of the codes, please feel free to check it out and provide some guidance for me, Thanks!!

data tofhanoi;

array H[5,3];

do i= 5 to 1 by -1;

H[i,1]=i;

end;

r3=6; r2=6; r1=1;

do until (r3=1); ;

move=0;

do until (move=1);

check=0;

do until (check=1);

 

/********************************* in here i have to input codes to pick a disk from a tower, which F is the tower number, may i ask how to accomplish here? ************************************/

 

if F=1 & r1 ne 6 then do;

check=1;

r=r1;

end;

else if F=2 & r2 ne 6 then do;

check=1; r=r2;

end;

else if F=3 & r3 ne 6 then do;

check=1; r=r3; end;

end;

 

check=0; ....... (remaining other codes)

Thanks for helping!

1 REPLY 1
ChrisNZ
Tourmaline | Level 20

It is difficult to perform recursions from within a data step.

 

Here is a standard solution implemented with macros.

  %macro hanoi(disk, from, to, via); 
    %if &disk.=0 %then %return;
    %hanoi ( %eval(&disk.-1), &from., &via., &to.  );
    %put Move top disk &disk. from tower &from. to tower &to. ; 
    %hanoi ( %eval(&disk.-1), &via. , &to. , &from.) ;
  %mend;
  %hanoi(3,A,B,C); 
Move top disk 1 from tower A to tower B
Move top disk 2 from tower A to tower C
Move top disk 1 from tower B to tower C
Move top disk 3 from tower A to tower B
Move top disk 1 from tower C to tower A
Move top disk 2 from tower C to tower B
Move top disk 1 from tower A to tower B

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 574 views
  • 4 likes
  • 2 in conversation