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
 
how to get different combination of words from a string REXX

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> Mainframe Interview Questions
View previous topic :: :: View next topic  
Author Message
srajanbose
Warnings : 1

New User


Joined: 11 Oct 2004
Posts: 69
Location: chennai

PostPosted: Tue Dec 29, 2009 12:38 pm    Post subject: how to get different combination of words from a string REXX
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: 8593
Location: Back in jolly old England

PostPosted: Tue Dec 29, 2009 12:58 pm    Post subject:
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

Global Moderator


Joined: 14 Mar 2007
Posts: 10310
Location: italy

PostPosted: Tue Dec 29, 2009 12:59 pm    Post subject: Reply to: how to get different combination of words from a s
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: 8593
Location: Back in jolly old England

PostPosted: Tue Dec 29, 2009 1:03 pm    Post subject:
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

Global Moderator


Joined: 14 Mar 2007
Posts: 10310
Location: italy

PostPosted: Tue Dec 29, 2009 1:24 pm    Post subject: Reply to: how to get different combination of words from a s
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: 8593
Location: Back in jolly old England

PostPosted: Tue Dec 29, 2009 1:58 pm    Post subject:
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    IBMMAINFRAMES.com Support Forums -> Mainframe Interview Questions All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts ejck thru rexx to multiple jobs insid... Susanta All Other Mainframe Topics 5 Tue Sep 19, 2017 1:39 pm
No new posts Search for a string in many PDS KARTHIGADEVI CLIST & REXX 5 Fri Sep 08, 2017 2:32 pm
This topic is locked: you cannot edit posts or make replies. Extract all "IF" Statements... Adarsh Damodaran CLIST & REXX 1 Wed Sep 06, 2017 9:28 am
No new posts Creating ISPF Screens with Scrname vi... Albireo20 TSO/ISPF 9 Tue Sep 05, 2017 4:24 pm
No new posts How to Run Rexx Macro? Kalaivignesh CLIST & REXX 5 Fri Sep 01, 2017 9:08 pm

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