Discuss Scratch

wolfdude999_test
Scratcher
12 posts

Binary Search with Chars

I need help with coming up with an algorithm to search through a list of characters. I don't just wan't to cycle through the whole list because as it gets bigger and more parts are added to the program, it will lag. I have considered using a binary search, but I don't know how I would be able to do that with chars due to the fact that Scratch does not have some sort of ASCII interpreter to convert the chars to numbers. Does anyone know what I am talking about and would be able to help?
andre_rifaut
Scratcher
100+ posts

Binary Search with Chars

Just iterate on a string that has all chars. Then when a math is found the index can be the basis of the identifier of the letter.

See function “encode” in http://scratch.mit.edu.ezproxyberklee.flo.org/projects/10087691
DadOfMrLog
Scratcher
1000+ posts

Binary Search with Chars

Note that you can use greater than and less than on character strings. So ‘abc’>'aaa', for example.
The order is standard ASCII (dunno about other characters, like unicode…?)
But it doesn't matter what order scratch actually uses - just that it's consistent, and you use it consistently everywhere…

If you order your list of strings (or just characters in your case?) then you can perform a binary search on that list very easily.
DadOfMrLog
Scratcher
1000+ posts

Binary Search with Chars

Oh, one issue though is that scratch treats upper case and lower case as identical.
If you don't care about that then it's fine, otherwise you have to use some hack to get around it…
wolfdude999_test
Scratcher
12 posts

Binary Search with Chars

DadOfMrLog wrote:

Oh, one issue though is that scratch treats upper case and lower case as identical.
If you don't care about that then it's fine, otherwise you have to use some hack to get around it…
Thanks! That really helps, I didn't realize that the signs would work with chars

Powered by DjangoBB