Author Topic: Permutations Generator  (Read 3687 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Permutations Generator
« on: March 31, 2019, 05:23:40 pm »
Handy Permutation Generator:
Code: QB64: [Select]
  1. _TITLE "Permutations wo recursion" ' B+ translate from SmallBasic to QB64 2019-03-31
  2. SCREEN _NEWIMAGE(600, 600, 32)
  3. 'Permutations
  4. 'translation from: PowerBASIC
  5. 'tsh copy from Liberty link 2017-02-04
  6. '   until (i=0) or (a(i)<a(i+1))
  7. 'before executing i=i-1 in loop body.
  8.  
  9.  
  10. 'let's load result$() with results
  11. REDIM result$(0)
  12. loadPerms 5, result$()
  13. 'display result$
  14. FOR i = 0 TO UBOUND(result$)
  15.     PRINT i + 1, result$(i)
  16.     IF i MOD 30 = 29 THEN
  17.         PRINT "press any to continue..."
  18.         SLEEP
  19.         CLS
  20.     END IF
  21.  
  22. 'load dynamic string array r() with permutations of n digits
  23. SUB loadPerms (n, r() AS STRING)
  24.     DIM a(0 TO n + 1) '+1 needed due to bug in LB that checks loop condition
  25.     FOR i = 0 TO n: a(i) = i: NEXT 'load a() with minimum values
  26.     DO
  27.         b$ = ""
  28.         FOR i = 1 TO n
  29.             b$ = b$ + STR$(a(i))
  30.         NEXT
  31.         REDIM _PRESERVE r(e)
  32.         r(e) = b$
  33.         e = e + 1
  34.  
  35.         i = n
  36.         DO
  37.             i = i - 1
  38.         LOOP UNTIL (i = 0) OR (a(i) < a(i + 1))
  39.         j = i + 1
  40.         k = n
  41.         WHILE j < k
  42.             SWAP a(j), a(k)
  43.             j = j + 1
  44.             k = k - 1
  45.         WEND
  46.         IF i > 0 THEN
  47.             j = i + 1
  48.             WHILE a(j) < a(i)
  49.                 j = j + 1
  50.             WEND
  51.             SWAP a(i), a(j)
  52.         END IF
  53.     LOOP UNTIL i = 0
  54.  
  55.  
« Last Edit: March 31, 2019, 05:26:31 pm by bplus »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Permutations Generator
« Reply #1 on: March 31, 2019, 06:14:59 pm »
We've had a few of these over the years at: https://www.tapatalk.com/groups/qbasic/

One thread from 2010 with a few routines: https://www.tapatalk.com/groups/qbasic/math-expert-needed-for-permutation-problem-t37777.html

I put together something similar to solve Sudoku puzzles, but right now, I'm not sure where (what computer) it's on.

I did a little testing and helped debug this one, from a friend of mine, named Moneo: https://www.tapatalk.com/groups/qbasic/math-expert-needed-for-permutation-problem-t37777-s10.html
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Permutations Generator
« Reply #2 on: March 31, 2019, 07:19:53 pm »
Here is a handier variation that translates a number permutation into a Values string setup in a ValuesData section:
Code: QB64: [Select]
  1. _TITLE "Permutations with value translator" 'B+ add value translator 2019-03-31
  2. 'from "Permutations wo recursion"  translate from SmallBasic to QB64 2019-03-31
  3. 'from "Permutations" translation from: PowerBASIC, tsh copy from Liberty link 2017-02-04
  4.  
  5. SCREEN _NEWIMAGE(800, 600, 32)
  6. _SCREENMOVE 300, 40
  7.  
  8. REDIM results$(0)
  9. loadPermsValues results$()
  10. 'display result$
  11. FOR i = 0 TO UBOUND(results$)
  12.     PRINT i + 1, results$(i)
  13.     IF i MOD 30 = 29 THEN
  14.         PRINT "press any to continue..."
  15.         SLEEP
  16.         CLS
  17.     END IF
  18.  
  19. ValuesData:
  20. DATA "Steve","Pete","Fellippe","Bill","Ashish","Qwerky","Petr","TempodiBasic","Colbalt","END"
  21.  
  22. 'this reads data from ValuesData line and translates Permutations to those values
  23. SUB loadPermsValues (r() AS STRING)
  24.     'load values array one way or another? read data
  25.     REDIM values(0) AS STRING
  26.     RESTORE ValuesData
  27.     DO
  28.         READ r$
  29.         IF r$ = "END" THEN
  30.             done = 1
  31.         ELSE
  32.             n = n + 1
  33.             REDIM _PRESERVE values(n) AS STRING
  34.             values(n) = r$
  35.         END IF
  36.     LOOP UNTIL done
  37.     n = UBOUND(values)
  38.     DIM a(0 TO n + 1) '+1 needed due to bug in LB that checks loop condition: until (i=0) or (a(i)<a(i+1))
  39.     FOR i = 0 TO n: a(i) = i: NEXT 'load a() with minimum values
  40.     DO
  41.         b$ = ""
  42.         FOR i = 1 TO n
  43.             b$ = b$ + values(a(i)) + " "
  44.         NEXT
  45.         REDIM _PRESERVE r(e)
  46.         r(e) = b$
  47.         e = e + 1
  48.  
  49.         i = n
  50.         DO
  51.             i = i - 1
  52.         LOOP UNTIL (i = 0) OR (a(i) < a(i + 1))
  53.         j = i + 1
  54.         k = n
  55.         WHILE j < k
  56.             SWAP a(j), a(k)
  57.             j = j + 1
  58.             k = k - 1
  59.         WEND
  60.         IF i > 0 THEN
  61.             j = i + 1
  62.             WHILE a(j) < a(i)
  63.                 j = j + 1
  64.             WEND
  65.             SWAP a(i), a(j)
  66.         END IF
  67.     LOOP UNTIL i = 0
  68.     EXIT SUB
  69.  

362,880 = 9! of permutations of 9 members of this forum. :)

Perms of members.PNG
* Perms of members.PNG (Filesize: 43.46 KB, Dimensions: 663x560, Views: 264)
« Last Edit: March 31, 2019, 07:34:25 pm by bplus »