Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Profile Log in to check your private messages Log in
 
REXX contest

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> CLIST & REXX
View previous topic :: :: View next topic  
Author Message
sergeyken

Active User


Joined: 29 Apr 2008
Posts: 379
Location: Maryland

PostPosted: Tue Jan 09, 2018 1:55 am    Post subject: REXX contest
Reply with quote

For training purposes in REXX, let's try to create simple but effective REXX code to solve classical task: placing given list of words into rectangular matrix, with at least one common letter of every word with another one.
The recommended interface is like this
Code:
//COUNTRY  EXEC PGM=IKJEFT01,DYNAMNBR=2454     
//*                                           
//SYSEXEC  DD  DISP=SHR,DSN=&REXXLIB           
//*                                           
//SYSTSPRT DD  SYSOUT=*                       
//SYSTSIN  DD  *                               
 %PUZZLEZ X(13) Y(21) FILL(RANDOM)             
//WORDS    DD  *                               
AFGHANISTAN                                   
ALBANIA                                       
ALGERIA                                       
ANDORRA                                       
. . . . . . . .


Placing 15 words into matrix 13 X 21 filled with RANDOM
Successfully attempted at 775 step                     
 B A H R A I N E Q A V B T                             
 A F G H A N I S T A N K H                             
 N Q A N T I G U A S L R X                             
 G F R B K C A S Z J V L D                             
 L T M A U S T R I A S Q M                             
 A Z E R B A I J A N R V T                             
 D C N B B U G Y P I V J D                             
 E F I U A S L W H B O N S                             
 S R A D H T L V K L V F W                             
 H N N A A R G E N T I N A                             
 S O D H M A S E G F L U J                             
 J A O U A L Q H Y M X D P                             
 S V R T S I Q I G U S T D                             
 L J R K F A E F W J J Z J                             
 W O A L G E R I A C K B A                             
 A N L A D L N A T N H L X                             
 D X B F U R V Y W A E G Z   
 P G A C F S A C F Y R M X   
 K A N G O L A S M G B V J   
 H Q I Z O O M V P O Q J U   
 W K A E V Y X Q M N U L J   
READY                       
END                         


Or, for clarity:
Code:
//COUNTRY  EXEC PGM=IKJEFT01,DYNAMNBR=2454     
//*                                           
//SYSEXEC  DD  DISP=SHR,DSN=&REXXLIB           
//*                                           
//SYSTSPRT DD  SYSOUT=*                       
//SYSTSIN  DD  *                               
 %PUZZLEZ X(13) Y(21) FILL(DOTS)             
//WORDS    DD  *                               
AFGHANISTAN                                   
ALBANIA                                       
ALGERIA                                       
ANDORRA                                       
. . . . . . . .


Placing 15 words into matrix 13 X 21 filled with DOTS
Successfully attempted at 775 step                     
B A H R A I N . . . . . .
A F G H A N I S T A N . .
N . A N T I G U A . . . .
G . R B . . . . . . . . .
L . M A U S T R I A . . .
A Z E R B A I J A N . . .
D . N B B U . . . . . . .
E . I U A S . . . . . . .
S . A D H T . . . . . . .
H . N A A R G E N T I N A
. . D . M A . . . . . . .
. . O . A L . . . . . . .
. . R . S I . . . . . . .
. . R . . A . . . . . . .
. . A L G E R I A . . . .
. . L . . . . . . . . . .
. . B . . . . . . . . . .
. . A . . . . . . . . . .
. A N G O L A . . . . . .
. . I . . . . . . . . . .
. . A . . . . . . . . . .
READY                       
END                         


My code (normally aligned, and separated) is 152 lines.
15-50 words are allocated in few moments.
Tried to use it up to 199 input words into matrix 34x50; that took several minutes to complete.
Back to top
View user's profile Send private message

sergeyken

Active User


Joined: 29 Apr 2008
Posts: 379
Location: Maryland

PostPosted: Tue Jan 09, 2018 10:49 pm    Post subject:
Reply with quote

My version - for critics, and suggestions.
Code:
/* REXX */
PUZZLEZ: /* non-recursive version */

Arg . 'FILL(' Filler ')' .
Arg . 'X(' XSize ')' .
Arg . 'Y(' YSize ')' .
If Abbrev( 'RANDOM', Filler, 1 ) Then Filler = 'RANDOM'
Else                                  Filler = 'DOTS'

Address TSO "EXECIO * DISKR WORDS (open finis"

Word# = 0
MaxLen = 0
/* split the whole input into words, prefixed by aligned length field */
Do iL = 1 By 1 While Queued() > 0
   Pull NewLine
   #Words = Words(NewLine)
   Do iW = 1 to #Words
      Word# = Word# + 1
      NewWord = Word( NewLine, iW )
      LenWord = D2Pic( Length(NewWord), '999' )
      WordList.Word# = LenWord NewWord
      MaxLen = Max( MaxLen, LenWord )
   End iW
End /* Queued() */
WordList.0 = Word#

/* sort words descending length, using REXXTOOLS */
Call StemSort 'WordList.', , , 'D'

Do iW = 1 to WordList.0
   Say WordList.iW
End iW

If XSize = '' | \Datatype( XSize, 'W' ) Then XSize = 0
XSize = Max( XSize, MaxLen )

If YSize = '' | \Datatype( YSize, 'W' ) Then YSize = XSize
YSize = Max( YSize, MaxLen )

Say "Placing" WordList.0 "words into matrix" XSize "X" YSize ,
    "filled with" Filler

If WordsPlaced?() Then
   Call DisplayMatrix Filler
Else
   Say "Unable to place all" WordList.0 "words in" XSize "X" YSize

Exit 0
/**********************************************************************/
WordsPlaced?: procedure expose XSize YSize WordList.

/* initial words positioning */
Do iW = 1 To WordList.0
   Parse Var WordList.iW lW W .
   If (iW // 2 > 0) Then
      WordList.iW = lW W 1 0 (XSize - lW + 1) YSize
   Else
      WordList.iW = lW W 1 0 XSize (YSize - lW + 1)
End

iW = 1 /* indexWord is running back and forth between 1 and list size */
Do stepW = 1 By 1 While iW > 0 & iW <= WordList.0

   /* re-calculate next position for the current word */
   Parse Var WordList.iW lW W xPos yPos xMax yMax  /* get last values */
   If yPos < yMax Then Do       /* shift vertically */
      WordList.iW = lW W xPos (yPos + 1) xMax yMax /* save updates */
   End
   Else If xPos < xMax Then Do  /* shift horizontally */
      WordList.iW = lW W (xPos + 1) 1 xMax yMax    /* save updates */
   End
   Else Do                      /* get back to previous word */
      WordList.iW = lW W 1 0 xMax yMax   /* reset, and save updates */
      iW = iW - 1               /* previous word index */
      Iterate stepW             /* back to shifting previous word */
   End
   Parse Var WordList.iW lW W xPos yPos xMax yMax  /* re-new values */

   If WordFits?(iW) Then Do /* this word succeeded? */
      If iW >= WordList.0 Then Do  /* is it the last word? */
         Say "Successfully attempted at" stepW "step"
         Return 1                  /* success */
      End
      iW = iW + 1           /* shift to the next word */
   End
End stepW
Say "Failed to match after" stepW "steps"

Return 0                   /* return no-success */
/**********************************************************************/
WordFits?: procedure expose WordList.
Arg nW

Matrix. = ''
CrossCount = (nW = 1)          /* first word intercross not required */
Call PlacePreviousWords(nW - 1)

Parse Var WordList.nW lW W X Y .
Do iL = 1 to lW
   NewLetter = Substr( W, iL, 1 )
   If Matrix.X.Y = '' Then
      Matrix.X.Y = NewLetter       /* empty space occupied */
   Else If Matrix.X.Y = NewLetter Then
      CrossCount = CrossCount + 1  /* good cross-letter */
   Else
      Return 0                     /* letters conflict, break test */

   If (nW // 2 > 0) Then X = X + 1 /* go horizontally */
   Else                  Y = Y + 1 /* go vertically */
End iL

Return (CrossCount > 0)  /* only accept crossed words (after word 1) */
/**********************************************************************/
PlacePreviousWords: procedure expose Matrix. WordList.
Arg nW

Do iW = 1 to nW
   Parse Var WordList.iW lW W X Y . /* handle current word */
   Do iL = 1 to lW
      NewLetter = Substr( W, iL, 1 )
      Matrix.X.Y = NewLetter
      If (iW // 2 > 0) Then X = X + 1
      Else                  Y = Y + 1
   End iL
End iW

Return
/**********************************************************************/
DisplayMatrix: procedure expose XSize YSize WordList.
Arg FillMode

If FillMode = 'RANDOM' Then
Do i = 1 To XSize
   Do j = 1 To YSize
      Rand = Random( 1, 26 )
      Matrix.i.j = Substr( 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', Rand, 1 )
   End j
End i
Else
   Matrix. = '.'

Call PlacePreviousWords WordList.0

Do y = 1 to YSize
   OneLine = ''
   Do x = 1 to XSize
      OneLine = OneLine Matrix.x.y
   End x
   Say OneLine
End y

Return 0
/**********************************************************************/
Back to top
View user's profile Send private message
vasanthz

Global Moderator


Joined: 28 Aug 2007
Posts: 1620
Location: Oregon

PostPosted: Wed Jan 10, 2018 3:35 am    Post subject:
Reply with quote

Wow.. cool.!
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> CLIST & REXX All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Will this code extract 1 byte from fi... pkmurali CLIST & REXX 8 Sat Nov 10, 2018 11:13 pm
No new posts Error reading file in REXX module ashek15 CLIST & REXX 3 Wed Oct 24, 2018 4:50 am
No new posts Encoding using rexx pkmurali CLIST & REXX 2 Sat Sep 08, 2018 1:05 am
No new posts Can we call QMF proc from REXX ? batu544 CLIST & REXX 6 Fri Aug 03, 2018 11:03 am
No new posts REXX CGI - Hello World vasanthz All Other Mainframe Topics 4 Fri Jul 13, 2018 3:53 am

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us