Author Topic: Re: Infinite loop situation?  (Read 952 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Infinite loop situation?
« on: May 01, 2018, 10:08:31 am »
Hi [banned user],

I modified some code I wrote for Secret Santa problem for you to code and decode with:
Code: QB64: [Select]
  1. 'Secret Santa 3.bas for QB64 B+ 2018-05-01
  2. ' based on versions of Secret Santa I did in JB  2016-12-03
  3.  
  4.  
  5. ' the letters and digits want to give each other presents at Christmas
  6. 'if A is Santa to B, then B can't also be Santa to A (or B)
  7.  
  8. 'To keep Code more secret randomize with a secret seed known only to you and intended reciever
  9.  
  10. secret## = 32547698.1000001 '< user and reciever should Enter this number
  11. RANDOMIZE secret##
  12.  
  13. DIM SHARED Reform$, Code$
  14.  
  15. letters$ = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890"
  16. L = LEN(letters$)
  17. DIM LS$(L)
  18. 'laod array
  19. FOR i = 1 TO L
  20.     LS$(i) = MID$(letters$, i, 1)
  21. 'scramble array   Knuth Shuffle method
  22. FOR i = L TO 2 STEP -1
  23.     R = INT(i * RND) + 1
  24.     SWAP LS$(i), LS$(R)
  25. 'now reform the Letters
  26. Reform$ = ""
  27. FOR i = 1 TO L
  28.     Reform$ = Reform$ + LS$(i)
  29.  
  30. ' Here is simple trick to non repetition > pair the letter with the next one up!!!
  31. Code$ = ""
  32. FOR i = 1 TO L - 1
  33.     Code$ = Code$ + MID$(Reform$, i + 1, 1)
  34. 'catch last letter pair to first
  35. Code$ = Code$ + MID$(Reform$, 1, 1)
  36.  
  37. 'now compare alphabet with code
  38. FOR i = 1 TO L
  39.     LOCATE 1, i: PRINT MID$(letters$, i, 1)
  40.     LOCATE 2, i: PRINT encrypt$(MID$(letters$, i, 1))
  41.     'test decode too!
  42.     LOCATE 3, i: PRINT decode$(encrypt$(MID$(letters$, i, 1)))
  43. 'now try some words or phrases
  44.     INPUT "(nothing quits) Enter a word or phrase to code "; codeThis$
  45.     IF codeThis$ = "" THEN END
  46.     coded$ = ""
  47.     FOR i = 1 TO LEN(codeThis$)
  48.         c$ = MID$(codeThis$, i, 1)
  49.         IF INSTR(letters$, c$) = 0 THEN coded$ = coded$ + c$ ELSE coded$ = coded$ + encrypt$(MID$(codeThis$, i, 1))
  50.     NEXT
  51.     decoded$ = ""
  52.     FOR i = 1 TO LEN(coded$)
  53.         c$ = MID$(codeThis$, i, 1)
  54.         IF INSTR(letters$, c$) = 0 THEN decoded$ = decoded$ + c$ ELSE decoded$ = decoded$ + decode$(MID$(coded$, i, 1))
  55.     NEXT
  56.     PRINT "           Coded: "; coded$
  57.     PRINT "Check Decode too: "; decoded$
  58.  
  59. FUNCTION encrypt$ (forLetter$)
  60.     p = INSTR(Reform$, forLetter$)
  61.     encrypt$ = MID$(Code$, p, 1)
  62. FUNCTION decode$ (Letter$)
  63.     p = INSTR(Code$, Letter$)
  64.     decode$ = MID$(Reform$, p, 1)
  65.  


EDIT: spaces and punctuation now preserved in main code but not in the functions.
« Last Edit: May 01, 2018, 10:35:49 am by bplus »

Marked as best answer by on May 31, 2021, 11:13:03 pm

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Infinite loop situation?
« Reply #1 on: May 01, 2018, 10:24:05 am »
Well dang!

Hi [banned user],

Do you want to code spaces too? So you really could code a phrase (code above skips spaces so "Mark missed spaces." becomes "Markmissedspaces", oops! Easy enough to fix of course.


Append:
OK fixed in code above! Still can't use comma with Enter for one variable$.
« Last Edit: May 01, 2018, 10:34:00 am by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Infinite loop situation?
« Reply #2 on: May 01, 2018, 10:49:13 am »
Hmm... trying different seeds for RANDOMIZE, I find I get same coding for 3254768 as 3254769 but not 3254760 (just changing the last digit).

So even if you can use any numeric type for RANDOMIZE seed, it doesn't guarantee a unique code if vary it in that type.
« Last Edit: May 01, 2018, 10:52:27 am by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Infinite loop situation?
« Reply #3 on: May 01, 2018, 08:25:08 pm »
Quote
Such encoding, in my opinion is useless because that would be too simple to decode.

Which is why I scrambled the letters to the variable string I called reform$, before "shifting the letters by x locations".

Take the letters A, B, C... throw them in a hat, pull them out 1, 2, 3, ... and reform$ is a shuffle of the original letters$ string.

reform$ = "MOZSTVA... "

Now M codes to O, O codes to Z, Z codes to S.... you have a code.

To code, find the position in the reform$ string of the letter to code,

say A was in the 15th position of reform$ found by INSTR(reform$, "A")

then it's code is in 15th position of code$ is say "F"  Mid$(code$, 15, 1)

And vice versa to decode:

The letter position in code$ is the decode position in reform$.

There is no constant distance from one letter to it's code and no letter is coded to itself and NO reverse coding like if A codes to S then S codes to A.


See if this is too simple to decode: (see attachhment)
Common quote.PNG
* Common quote.PNG (Filesize: 1.16 KB, Dimensions: 416x21, Views: 103)
« Last Edit: May 01, 2018, 08:32:41 pm by bplus »