This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-03-02 12:13:49

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

character to number - speed is everything!

What is the fastest, most optimised way to determine where a letter falls into the alphabet (e.g. a = 1, z = 26)?

I know of a few ways to do it but I'm wondering if anyone has thought much about this and has a very fast solution?

the fastest method I can think of is to have a list with each item containing a letter, then running this script:

set [count v] to [0]
set [searchChar v] to [x]
repeat until <(item (count) of [letters v]) = (searchChar)>
change [count v] by (1)
end
I wanted to add a < > or <(count) > [26])> to that, but scratchblocks was having none of it.

EDIT: I suppose one way to optimise this would be to store characters, not alphabetically, but in order of most used...

EDIT2: I actually just added time blocks to this:

set [count v] to [0]
reset timer
set [searchChar v] to [z]
repeat until <(item (count) of [letters v]) = (searchChar)>
change [count v] by (1)
end
set [time v] to (timer)
and in turbo-mode, this got me a time of 0.0 seconds for z...

The technique above seems to be able to check a list of 188 characters in 0.1 seconds in Scratch Turbo-mode, so that's fast enough for me. Asking for this topic to be closed.

Last edited by sparks (2012-03-02 13:28:00)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#2 2012-03-02 13:39:47

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: character to number - speed is everything!

I suspect a bisection search would be quicker than a sequential search...but if you're happy with what you have, I'll just close this as you requested  smile


http://i39.tinypic.com/2nav6o7.gif

Offline

 

Board footer