Qbasicnews.com

Full Version: List All Combinations
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Hmm, this has probably been challanged before, but...

Can one make a program where, given letters in a list (a DATA statment) and the number of letters, all different combinations of those letters could be outputted to a file?

So if I put this as my data stament:

Code:
DATA T, H, E

The file (output.txt or something) would print

Code:
THE
TEH
HET
HTE
ETH
EHT

I'm working on one...
could it be set a three or do you have to have it be adjustable.
Quote:could it be set a three or do you have to have it be adjustable.
Dunno. Extra points for adjustable 8)
ok im working on one.
PROGGER,

Because there's more than one way to skin a cat, here's a brute force solution to the problem. Instead of the letters T H E, it uses the numbers 1 2 3. If you really wanted the letter, you could convert the numbers to the letters easily before printing.
Code:
defint a-z
for x=123 to 321
    s$=mid$(str$(x),1)
    if instr(s$,"1")>0 and instr(s$,"2")>0 and instr(s$,"3")>0 then print s$
next x
system
What do you think?
*****
This is my recursive entry:
Code:
'Antoni Gual 2003
DECLARE SUB COMBINE (I%)
DECLARE SUB PRINTIT ()
DEFINT A-Z
CONST NDATA = 5
DATA Q,B,A,S,I,C,N,E,W,S
DIM SHARED CNT&
DIM SHARED A(0 TO NDATA)
DIM SHARED B$, P$
B$ = SPACE$(NDATA)
P$ = B$
FOR I = 1 TO NDATA
  READ c$
  MID$(B$, I, 1) = c$
NEXT
COMBINE 1
PRINT : PRINT CNT&; " PERMUTATIONS"
END

SUB COMBINE (I)
IF I > NDATA THEN EXIT SUB
FOR J = 1 TO NDATA
   FOR K = 1 TO I
     IF A(K - 1) = J THEN GOTO ENDED
   NEXT
   A(I) = J
   COMBINE (I + 1)
   IF I = NDATA THEN PRINTIT
ENDED:
NEXT
END SUB

SUB PRINTIT
FOR KK = 1 TO NDATA
  MID$(P$, KK, 1) = MID$(B$, A(KK), 1)
NEXT
PRINT P$,
CNT& = CNT& + 1
END SUB
moneo:
I took you basic idea a put it into mine and

Code:
CLS
DEFLNG A-Z
INPUT in$
t = TIMER
l = LEN(in$)
FOR i = 1 TO l
l2$ = l2$ + LTRIM$(STR$(i))
NEXT i
FOR i = l TO 1 STEP -1
l3$ = l3$ + LTRIM$(STR$(i))
NEXT i
FOR i = VAL(l2$) TO VAL(l3$)
s$ = LTRIM$(MID$(STR$(i), 1))
FOR z = 1 TO l
  IF INSTR(s$, LTRIM$(STR$(z))) < 1 THEN EXIT FOR
  f$ = ""
  IF z = l THEN
   FOR z2 = 1 TO l
    f$ = f$ + MID$(in$, VAL(MID$(s$, z2, 1)), 1)
   NEXT z2
   PRINT f$
  END IF
NEXT z
NEXT i
PRINT "TIMER: "; INT(TIMER - t)

edit: oops. forggot to chage something *click* done it works now. try it.
Quote:PROGGER,

Because there's more than one way to skin a cat, here's a brute force solution to the problem. Instead of the letters T H E, it uses the numbers 1 2 3. If you really wanted the letter, you could convert the numbers to the letters easily before printing.
Code:
defint a-z
for x=123 to 321
    s$=mid$(str$(x),1)
    if instr(s$,"1")>0 and instr(s$,"2")>0 and instr(s$,"3")>0 then print s$
next x
system
What do you think?
*****
lol, it works

*is testing*
ANTONI,

I tested your program and it works fine. It's hard to verify 120 combinations, so I adjusted your code to only use 3 letters, which were easy to check. Can't figure out your algorithm, but it works 100%. Congratulations!
*****
i fixed mine
Pages: 1 2 3