IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

how to get different combination of words from a string REXX


IBM Mainframe Forums -> Mainframe Interview Questions
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
srajanbose

New User


Joined: 11 Oct 2004
Posts: 69
Location: chennai

PostPosted: Tue Dec 29, 2009 12:38 pm
Reply with quote

Hi,
How to get different combination of words from a string using rexx.

For example if i give the input as 'COW' i need the output as

COW
CWO
OWC
...
Back to top
View user's profile Send private message
expat

Global Moderator


Joined: 14 Mar 2007
Posts: 8797
Location: Welsh Wales

PostPosted: Tue Dec 29, 2009 12:58 pm
Reply with quote

The idea of the interview questions forum is that YOU give your answer or suggestion before other forum members step in with replies.

We await your response.
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Tue Dec 29, 2009 12:59 pm
Reply with quote

looks more like a math issue, so for this time i' ll post the answer and the code
the scripts prints the permutations in Lexicographical order
the TS should do a bit of research on FACTORADICALS

Code:
#!/opt/ooRexx/bin/rexx
numeric digits 27
z    = time("E")
src = "abcdefghijklmnopqrstuvwxyz"
--- src = "CWO"

parse arg n ord
if   n > 26 then ,
   exit
l = length(fact(n))

low = 0
max = fact(n) - 1
if   ord \= "" then ,
   if   ord > max then ,
      exit
   else do
      low = ord - 1
      max = ord - 1
      l   = ""
   end
   
do   m = low to max
   k = FRADICALS(m, n)
   r = 0
   do   i = 1 to k
   --   call charout , f.i"x"k-i"! +"
      r = r + fact(k-i)*f.i
   end
   -- say   
   if   r \= m then do
      say "oh shit!"
      exit
   end

   ans = ""
   tmp = left(src,n)
   do   i = 1 to k
      j = f.i + 1
      ans = ans || substr(tmp,j,1)
      tmp = delstr(tmp,j,1)
   end
   if l = "" then ,
      say m+1 ans
   else ,
      say right(m+1,l) ans
end

say "Ended Elaps("time("E")") "
exit

FRADICALS:procedure expose F.
   parse arg M, N
   do   I =  1 to N
      F.I = 0
   end
   do   J = 2 while ( M \= 0 )
      I = N - J + 1
      F.I = M // J
      M   = M % J
   end
   return N
exit -1

FACT:procedure
   parse arg N
   if   N = 0 then ,
      return 1
   F = 1
   do   I = 1 to N
      F *= I
   end
   return F
exit -1



the script takes two parameters
the first one is mandatory ans the number of <things>
the second one if entered will print the Nth lexical permutation in Lexicographical order

<scriptname> 3 will yeld
Code:
1 abc
2 acb
3 bac
4 bca
5 cab
6 cba

scriptname> 3 2 will yeld
Code:

 2 acb


edited to add the correct NUMERIC DIGITS for large numbers

added...
the Lexicographical order in the permutation jargon is the order relative to the original tokens configuration
for the example posted the lLexicographical order generation

Code:
1 CWO
2 COW
3 WCO
4 WOC
5 OCW
6 OWC
Back to top
View user's profile Send private message
expat

Global Moderator


Joined: 14 Mar 2007
Posts: 8797
Location: Welsh Wales

PostPosted: Tue Dec 29, 2009 1:03 pm
Reply with quote

Enrico, methinks that your code gives cartesian joins on all characters used, so one result would be AAA, but unfortunately there is only one character A in the input.

I know this because I thought along exactly the same lines as you did and almost posted my cartesian logic.
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Tue Dec 29, 2009 1:24 pm
Reply with quote

HI Expat! just corrected an posted the right code icon_biggrin.gif
PS. I had just messed up the script names and had some time ago saved as permutations what really were combinations

PS2.
reread the thread, the TS asked for combinations, so after all the first script was right icon_biggrin.gif

I enjoyed more researching and writing the second one anyway
Back to top
View user's profile Send private message
expat

Global Moderator


Joined: 14 Mar 2007
Posts: 8797
Location: Welsh Wales

PostPosted: Tue Dec 29, 2009 1:58 pm
Reply with quote

Hi Enrico, and wishing you a happy and prosperous new year,

I must admit after a quick reread, it could have been interpretted anyway one wanted as the OP did not bother to give the full list of output options.

So nothing much has improved over the last year then, we still have the same quality of ambiguous / unclear postings.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> Mainframe Interview Questions

 


Similar Topics
Topic Forum Replies
No new posts Compile Several JCL JOB Through one r... CLIST & REXX 4
No new posts Replace each space in cobol string wi... COBOL Programming 3
No new posts PARSE Syntax for not fix length word ... JCL & VSAM 7
No new posts Running REXX through JOB CLIST & REXX 13
No new posts Error to read log with rexx CLIST & REXX 11
Search our Forums:

Back to Top