View previous topic :: :: View next topic

Author 
Message 
srajanbose Warnings : 1 New User
Joined: 11 Oct 2004 Posts: 69 Location: chennai




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 




expat
Global Moderator
Joined: 14 Mar 2007 Posts: 8593 Location: Back in jolly old England




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 


enricosorichetti
Global Moderator
Joined: 14 Mar 2007 Posts: 10176 Location: italy




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"ki"! +"
r = r + fact(ki)*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
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 


expat
Global Moderator
Joined: 14 Mar 2007 Posts: 8593 Location: Back in jolly old England




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 


enricosorichetti
Global Moderator
Joined: 14 Mar 2007 Posts: 10176 Location: italy




HI Expat! just corrected an posted the right code
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
I enjoyed more researching and writing the second one anyway 

Back to top 


expat
Global Moderator
Joined: 14 Mar 2007 Posts: 8593 Location: Back in jolly old England




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 


