Author Topic: OneKey Binary Selection  (Read 5760 times)

0 Members and 1 Guest are viewing this topic.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
OneKey Binary Selection
« on: October 04, 2021, 07:20:55 pm »
Hi Boys and Gals

just to say
binary search is speeder than bubble sequential choice
so here my OneKey selector using a binary search

it lets me remember the textual game Guess the number!
Code: QB64: [Select]
  1. 'one key input  2021-10-04 TempoDiBasic
  2. ' Saint Francis is patron of this day
  3.  
  4. choice$ = "0123456789"
  5. choosen$ = ""
  6. getInput choice$, choosen$
  7. Print choice$, choosen$
  8. choice$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  9. choosen$ = ""
  10. getInput choice$, choosen$
  11. Print choice$, choosen$
  12. Input "Enter set of one character choice...", choice$
  13. choosen$ = ""
  14. getInput choice$, choosen$
  15. Print choice$, choosen$
  16.  
  17. Sub getInput (cho$, choo$) 'Static
  18.     Dim As Integer leng, tim, f1, s1
  19.     Dim As String FirstHalf, SecondHalf, f, s
  20.     leng = Len(cho$)
  21.     FirstHalf = Left$(cho$, Int(leng / 2))
  22.     SecondHalf = Right$(cho$, leng - Int(leng / 2))
  23.     If leng = 1 Then choo$ = cho$: Exit Sub
  24.     While leng > 1
  25.         Cls
  26.         If f = " " Then
  27.             f = "þ"
  28.             s = " "
  29.             f1 = 29
  30.             s1 = 15
  31.         Else
  32.             f = " "
  33.             s = "þ"
  34.             f1 = 15
  35.             s1 = 29
  36.         End If
  37.         Locate 2
  38.         Color f1
  39.         Print f + FirstHalf + f + "  ";
  40.         Color s1
  41.         Print s + SecondHalf + s
  42.         Locate 3
  43.         Color 15
  44.         Print "press spacebar to choose, wait 2 second to refuse"
  45.         tim = Int(Timer)
  46.         While InKey$ = "" And (Timer - tim <= 2)
  47.             If InKey$ = " " Then
  48.                 If f = " " Then getInput SecondHalf, choo$ Else getInput FirstHalf, choo$
  49.             End If
  50.             If Len(choo$) = 1 Then Exit Sub
  51.         Wend
  52.     Wend

my sub getInput at your service!
One suggestion, you must press and release the spacebar in 1 second to get the best result or you need to increase the time of autoselection if you want more slowly interaction.
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: OneKey Binary Selection
« Reply #1 on: October 04, 2021, 08:17:18 pm »
Hi Boys and Gals

just to say
binary search is speeder than bubble sequential choice
so here my OneKey selector using a binary search

it lets me remember the textual game Guess the number!
Code: QB64: [Select]
  1. 'one key input  2021-10-04 TempoDiBasic
  2. ' Saint Francis is patron of this day
  3.  
  4. choice$ = "0123456789"
  5. choosen$ = ""
  6. getInput choice$, choosen$
  7. Print choice$, choosen$
  8. choice$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  9. choosen$ = ""
  10. getInput choice$, choosen$
  11. Print choice$, choosen$
  12. Input "Enter set of one character choice...", choice$
  13. choosen$ = ""
  14. getInput choice$, choosen$
  15. Print choice$, choosen$
  16.  
  17. Sub getInput (cho$, choo$) 'Static
  18.     Dim As Integer leng, tim, f1, s1
  19.     Dim As String FirstHalf, SecondHalf, f, s
  20.     leng = Len(cho$)
  21.     FirstHalf = Left$(cho$, Int(leng / 2))
  22.     SecondHalf = Right$(cho$, leng - Int(leng / 2))
  23.     If leng = 1 Then choo$ = cho$: Exit Sub
  24.     While leng > 1
  25.         Cls
  26.         If f = " " Then
  27.             f = "þ"
  28.             s = " "
  29.             f1 = 29
  30.             s1 = 15
  31.         Else
  32.             f = " "
  33.             s = "þ"
  34.             f1 = 15
  35.             s1 = 29
  36.         End If
  37.         Locate 2
  38.         Color f1
  39.         Print f + FirstHalf + f + "  ";
  40.         Color s1
  41.         Print s + SecondHalf + s
  42.         Locate 3
  43.         Color 15
  44.         Print "press spacebar to choose, wait 2 second to refuse"
  45.         tim = Int(Timer)
  46.         While InKey$ = "" And (Timer - tim <= 2)
  47.             If InKey$ = " " Then
  48.                 If f = " " Then getInput SecondHalf, choo$ Else getInput FirstHalf, choo$
  49.             End If
  50.             If Len(choo$) = 1 Then Exit Sub
  51.         Wend
  52.     Wend

my sub getInput at your service!
One suggestion, you must press and release the spacebar in 1 second to get the best result or you need to increase the time of autoselection if you want more slowly interaction.


Hi @TempodiBasic

Something really off with that code. Line 28 is
Code: QB64: [Select]
  1. While leng > 1  
  2.  
but nothing inside that loop changes the value of leng.

My output is just a continuous flashing of stuff in upper 3 rows of screen.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: OneKey Binary Selection
« Reply #2 on: October 04, 2021, 11:02:46 pm »
OK I see it follows my Binary Selection routine by going recursive so maybe doesn't matter it doesn't finish looping.

@TempodiBasic this actually worked for you?

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: OneKey Binary Selection
« Reply #3 on: October 05, 2021, 05:08:31 am »
Hi Bplus

at  first thanks for testing an feedback

about
Code: QB64: [Select]
  1. While leng > 1
you have a good eyesight!
It  is a fake (or Dummy) condition to test  because it never change  so the WHILE...WEND is infinite
I learned it from a good coder that use as Nickname in this forum of Bplus and he sometimes loves to use
Code: QB64: [Select]
Thinking over you are right it is more correct to use
Code: QB64: [Select]

About results of output:
Sincerely yesterday  night it was too late to try to answer, so I made another test to my code and it run fine, so that I decided to do a step by step  screenshot... thinking that you were kidding
here are screenshots
 
OneKeyBinay1.JPG

 
OneKeyBinary2JPG.JPG

 
OneKeyBinay5.JPG


But today, robbing some time to my appointments, I have run again the code to choose one screenshot explicative
and SURPRICE  I've got the same messing no sense output!
With just a bit of disappointment I have thinking about issue:
yesterday I noticed that sometime routine missed the input key, and as you see from comment to my code I suggest to be quickly in press and release the spacebar or to increase the time of delay from 2 to 3 or 4 seconds.
Looking at main loop I rem out the inkey$="" condition of inner While, and my memory have said me that in speed concurrency the result of INKEY$ can change between 2 statements. So I save result of Inkey$ and I slow the 2 While loop.
And TADA! All work good with no glith of input.
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: OneKey Binary Selection
« Reply #4 on: October 05, 2021, 05:09:39 am »
Updaate code with no glitch about CPU speed and Inkey$
Code: QB64: [Select]
  1. 'one key input  2021-10-04 TempoDiBasic
  2. ' Saint Francis is patron of this day
  3.  
  4. choice$ = "0123456789"
  5. choosen$ = ""
  6. getInput choice$, choosen$
  7. Print choice$, choosen$
  8. choice$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  9. choosen$ = ""
  10. getInput choice$, choosen$
  11. Print choice$, choosen$
  12. Input "Enter set of one character choice...", choice$
  13. choosen$ = ""
  14. getInput choice$, choosen$
  15. Print choice$, choosen$
  16.  
  17. Sub getInput (cho$, choo$)
  18.     Dim As Integer leng, f1, s1
  19.     Dim As String FirstHalf, SecondHalf, f, s, k
  20.     Dim tim
  21.     leng = Len(cho$)
  22.     FirstHalf = Left$(cho$, Int(leng / 2))
  23.     SecondHalf = Right$(cho$, leng - Int(leng / 2))
  24.     If leng = 1 Then choo$ = cho$: Exit Sub
  25.     While leng > 1
  26.         Cls
  27.         If f = " " Then
  28.             f = "þ"
  29.             s = " "
  30.             f1 = 29
  31.             s1 = 15
  32.         Else
  33.             f = " "
  34.             s = "þ"
  35.             f1 = 15
  36.             s1 = 29
  37.         End If
  38.         Locate 2
  39.         Color f1
  40.         Print f + FirstHalf + f + "  ";
  41.         Color s1
  42.         Print s + SecondHalf + s
  43.         Locate 3
  44.         Color 15
  45.         Print "press spacebar to choose, wait 2 second to refuse"
  46.         tim = Timer
  47.         While (Timer - tim <= 2)
  48.             k = InKey$
  49.             If k = " " Then
  50.                 If f = " " Then getInput SecondHalf, choo$ Else getInput FirstHalf, choo$
  51.             End If
  52.             If Len(choo$) = 1 Then Exit Sub
  53.             _Limit 5
  54.         Wend
  55.         _Limit 5
  56.     Wend
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: OneKey Binary Selection
« Reply #5 on: October 05, 2021, 09:57:55 am »
Ah... that's better!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: OneKey Binary Selection
« Reply #6 on: October 05, 2021, 10:01:01 am »
The blinking is kind of confusing to me but I like how it keeps trying the two choices until press spacebar press.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: OneKey Binary Selection
« Reply #7 on: October 05, 2021, 12:22:58 pm »
Upon further reflection, Steve's method is clearly the winner for One Key access to Whole Key Board or parts of it.

Both yours ( @TempodiBasic ) and mine tend to need confirmation because so easy to screwup and miss the correct path down the Binary Tree. Steve's method has us stopping exactly where we intend to be and THEN waiting for computer to "get it". Ours has the user pausing all the way down the Binary Tree, hurry up and wait, repeat...

Of course my Function mod of his idea is better than his demo ;-)) Still his advice to use _KeyDown for it, was better too! This, my opinion, is based on my experience in applying it to the one app I am trying to get going, Crypt-O-Gram.
Your experience in applying the idea may be different.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: OneKey Binary Selection
« Reply #8 on: October 13, 2021, 05:33:40 am »
Yes I can agree

1. with a binary search or choice we play following a Wait -> Choose(keypress) ---->next level of choice  scheme. The point of vulnerability is keypressing too close the change item event. In this moment you can get a wrong choice! So to avoid this we should add the confirmation method (that used originally by Steve) so it becomes  a Wait -> Choose(keypress) ->waitNoInputToConfirm----->next level of choice 
2. moreover the binary choice is faster than other in a big list of items, not so evidenti in a short list

3. with sequential search or choice we play following a waitingINPUT -> UntilInputActivatedSearch-> waitNoInputToConfirm->choicedone scheme. The point of vulnerability fo this other scheme is a big list of items. So to avoid this we should add the choice by groups of items  using the binary choice (faster) or the multiple groups choice (slower then binary). This last one is like than of buttons of cellular telephone (NOT smartphone) https://www.prezzoforte.it/images/webp/Brondi_AMICOCHIC_56b372db99547b08cb9b7be07343c138_0.webpor telephone with button https://m.media-amazon.com/images/I/81AkSWUyDxL._AC_SX425_.jpg

So my conclusion is that for a poor list like 23 or 40 items the Steve's scheme is still effective. But not for a different list, just think 256 items (printable ASCII like in the tool of Fellippe for QB64 IDE).
In that case (a big list) is better a binary choice with confirm, so a fusion of the two approaches is the best.

Programming isn't difficult, only it's  consuming time and coffee

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: OneKey Binary Selection
« Reply #9 on: October 13, 2021, 07:07:31 am »
For a choice of 256 items, such as an ASCII chart, I'd still go with my method: I'd just run it twice.   First time to choose row, second time to choose column.  If desired, add a third time to toggle between a "Is this what you want" yes/no confirmation if you feel it's necessary.

Still a lot simpler than choosing the proper half the value is in 8 different  times, not counting confirmation checks.

Now, if I had a big list of 100,000 items to choose from, I'd....  still stick to my method.  I'd just choose 6 numbers from 0 to 9 and make it a decimal selection. 

Honestly, I don't see many places at all where having an user do a binary choice works very good.  Binary searches are excellent for computers, but not for people.  We think linearly, and in base-10, no binarily or in base-2.

For people interaction, I personally feel that simpler is better than more efficient.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: OneKey Binary Selection
« Reply #10 on: October 13, 2021, 09:24:33 am »
Yeah the main problem with Binary Search with human interacting is the wait at every branch for that dang slow poke human to get his decision in so we can move onto the next branch, and this whole process is so easily messed up when the human has a call of nature or phone and can't devote 100% attention to current branch of tree; then you have to start all over again, sheesh.

With more choices there are more branches = more total waiting to get a selection.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: OneKey Binary Selection
« Reply #11 on: October 23, 2021, 08:17:23 pm »
Hi
here oneKeyBinarySearch with the scheme of Steve Wait to confirm, press key to select (just the opposite of that I have coded here above)
Code: QB64: [Select]
  1. 'one key input  2021-10-15 TempoDiBasic
  2. ' MODification of structure of program
  3. ' following the scheme posted by Steve and discussed with Bplus
  4. Dim choice$, choosen$
  5. choice$ = "0123456789"
  6. choosen$ = ""
  7. getInput choice$, choosen$
  8. Print choice$, choosen$
  9. choice$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  10. choosen$ = ""
  11. getInput choice$, choosen$
  12. Print choice$, choosen$
  13. Input "Enter set of one character choice...", choice$
  14. choosen$ = ""
  15. getInput choice$, choosen$
  16. Print choice$, choosen$
  17.  
  18. Sub getInput (cho$, choo$)
  19.     Dim As Integer leng, f1, s1
  20.     Dim As String FirstHalf, SecondHalf, f, s, k
  21.     Dim tim
  22.     leng = Len(cho$)
  23.     FirstHalf = Left$(cho$, Int(leng / 2))
  24.     SecondHalf = Right$(cho$, leng - Int(leng / 2))
  25.     If leng = 1 Then choo$ = cho$: Exit Sub
  26.     While leng > 1
  27.         Cls
  28.         ShowItem f, s, f1, s1, FirstHalf, SecondHalf
  29.         k = InKey$
  30.         If k = "" Then
  31.             ' if user don't press a key, it starts countdown to confirm choice
  32.             tim = Timer
  33.             While (Timer - tim <= 4)
  34.                 k = InKey$
  35.                 If k = " " Then Exit While 'we stop countdown if user presses key
  36.             Wend
  37.             tim = 0
  38.  
  39.             ' if user  doesn't press a key and time waited is more the 4 second and choice is confirmed
  40.             If (k = "") Then
  41.                 If Len(choo$) = 1 Then Exit Sub
  42.                 If f = " " Then getInput SecondHalf, choo$ Else getInput FirstHalf, choo$
  43.                 Exit Sub
  44.             End If
  45.         End If
  46.     Wend
  47.  
  48. Sub ShowItem (f As String, s As String, f1 As Integer, s1 As Integer, Firsthalf As String, SecondHalf As String)
  49.     If f = " " Then
  50.         f = "þ"
  51.         s = " "
  52.         f1 = 29
  53.         s1 = 15
  54.     Else
  55.         f = " "
  56.         s = "þ"
  57.         f1 = 15
  58.         s1 = 29
  59.     End If
  60.     Locate 2
  61.     Color f1
  62.     Print f + Firsthalf + f + "  ";
  63.     Color s1
  64.     Print s + SecondHalf + s
  65.  
  66.     Locate 3
  67.     Color 15
  68.     Print "press spacebar to choose, wait more than 4 second to select"
surely faster than the first version but user spend more time to choose.

@LOL I have not been able to think how to show 10000 monoitem made with random characters,
in screen 0 we can reach 80x50 = 400 items in the screen.... in sequential search
think now about the binary search... I should show 2 set of 5000 characters

Strange how the perspective changes changing your seeing.

 
Programming isn't difficult, only it's  consuming time and coffee