Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Brain Teaser! Can anyone sort this out?

Status
Not open for further replies.

GlynW

Programmer
Feb 14, 2001
22
0
0
GB
I need to write a fox prog to display every possible combination of stored records in a table possible. The trick is to show every combination without duplicating or missing combinations.

for example:

Cursor BOB
Rec1 A
Rec2 B
Rec3 C
Rec4 D

The result should be 15 combinations (I think!) they can be in any order)
AB counts the same as BA and so does not count as a combination. Also there could be any number of Records (so I expect the code to run slowly)

An example of the results

Combi 1 A
Combi 2 AB
Combi 3 ABC
Combi 4 A C
Combi 5 ABCD
Combi 6 AB D
Combi 7 A CD
Combi 8 A D
Combi 9 B
Combi 10 BC
Combi 11 B D
Combi 12 BCD
Combi 13 C
Combi 14 CD
Combi 15 D

I've tried writting a program using recursion but, it keeps missing out "AB D" DOH!

This is a real brain teaser (or Head F**K), so any help would be appreciated.

Cheers
Glyn
 
Glyn,
This is nothing more than writing out the &quot;binary&quot; representation of the numbers from 1 to 15 (why not zero?). You are just &quot;mapping&quot; the 0's to a <space> and the 1's to a letter based on the column. This seems to work (at least if you just want all of them, and not in a specific &quot;order&quot;):

Code:
CREATE CURSOR BOB (myfield C(1))
INSERT INTO BOB VALUES (&quot;A&quot;)
INSERT INTO BOB VALUES (&quot;B&quot;)
INSERT INTO BOB VALUES (&quot;C&quot;)
INSERT INTO BOB VALUES (&quot;D&quot;)

lnReccount = reccount(&quot;BOB&quot;)
IF lnReccount > 32
	WAIT WINDOW &quot;Too many records to process&quot;
ENDIF
FOR lnii = 1 to 2^lnReccount-1
   ? mytrans(lnii, lnReccount)
ENDFOR


FUNCTION mytrans
PARAMETERS p_nNumber, p_nRecords
PRIVATE l_cReturn, lcString

l_cReturn = &quot;&quot;
** first convert to Binary string
lcString = int2bins(p_nNumber, p_nRecords)
** now translate it **
FOR lnxx = 1 TO p_nRecords
   l_cChar = SUBSTR(lcString, lnxx, 1)
   IF l_cChar = &quot;0&quot;
      l_cReturn = l_cReturn + &quot; &quot;
   ELSE
      GO lnxx
      l_cReturn = l_cReturn + BOB.myfield
   ENDIF
ENDFOR

RETURN l_cReturn


FUNCTION int2bins
PARAMETERS zdecimal, znoutlen
***
*** Assumes a positive integer less than 2^32
*** Returns string of bits (0's or 1's)
***  or 'ERROR: <explanation>' 
***
PRIVATE lval, ret_val

IF PARAMETERS() < 2 OR TYPE('znoutlen') != 'N'
   znoutlen = 0
ENDIF
znoutlen = MAX(0, MIN(32, znoutlen))

lval = min(2^32-1,abs(int(zdecimal)))
DO CASE
CASE lval = zdecimal
   ret_val = ''
CASE lval = int(zdecimal)
   ret_val = &quot;ERROR: Can't Handle Non-Integers&quot;
   lval = 0
CASE lval = (-zdecimal)
   IF zdecimal < 2^znoutlen-1
      ret_val = ''
      lval = MOD(-lval, 2^znoutlen)
   ELSE
      ret_val = &quot;ERROR: Can't Handle Negatives&quot;
      lval = 0
   ENDIF
OTHERWISE
   ret_val = &quot;ERROR: Number greater than 2^32-1 (Too Large)&quot;
   lval = 0
ENDCASE  
DO while lval > 0
   ret_val = iif((lval % 2)=1,'1','0')+ret_val
   lval = int(lval/2)
ENDDO
IF znoutlen > 0 AND LEN(ret_val) < znoutlen
   ret_val = PADL(ret_val, znoutlen, '0')
ENDIF
RETURN ret_val
[code]


Rick
 
Your an absolute star matey...

If you live anywhere near the south west of UK i'd buy you a drink for that.

Thanks

You've been very helpful

Glyn

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top