Discuss Scratch

zacharyn35
Scratcher
12 posts

Words to numbers

How would you make a code so that any words would be changed into numbers and then be able to be changed back again?
-stxllxr
New Scratcher
100+ posts

Words to numbers

You can use lists to do this, have a list with all the numbers you want to convert into words, and vice versa.
say you want the word “hello” to convert into 1, and the word “goodbye” to convert into 2, you can have a list like this:
list:
item 1: hello
item 2: goodbye

then you can use these 2 blocks to convert back and forth:
itemoflistitem#ofinlist
And here is how you can convert them back and forth. This is basically a primitive version of decoding and encoding, I would recommend searching up or researching it.
defineconvertthingtintonumbersetthingtoitem#oftinlistdefinedeconvertnumbernintothingsetthingtoitemnoflist
Using the list shown above, and the blocks above, you can convert a word using items in a list, and convert them back.

Edit:
Using these blocks like this:
convertthinggoodbyeintonumber
would convert goodbye into “2”, since goodbye is item 2 of the list, and deconverting it would use the item number to then again get the original item, “goodbye”.

Last edited by -stxllxr (March 30, 2024 17:58:38)

zacharyn35
Scratcher
12 posts

Words to numbers

-stxllxr wrote:

You can use lists to do this, have a list with all the numbers you want to convert into words, and vice versa.
say you want the word “hello” to convert into 1, and the word “goodbye” to convert into 2, you can have a list like this:
list:
item 1: hello
item 2: goodbye

then you can use these 2 blocks to convert back and forth:
itemoflistitem#ofinlist
And here is how you can convert them back and forth. This is basically a primitive version of decoding and encoding, I would recommend searching up or researching it.
defineconvertthingtintonumbersetthingtoitem#oftinlistdefinedeconvertnumbernintothingsetthingtoitemnoflist
Using the list shown above, and the blocks above, you can convert a word using items in a list, and convert them back.

Edit:
Using these blocks like this:
convertthinggoodbyeintonumber
would convert goodbye into “2”, since goodbye is item 2 of the list, and deconverting it would use the item number to then again get the original item, “goodbye”.
Thanks! Also do you have a code so that it changes it to numbers based on the letter. (Ex. Hi would be 9,10)

Last edited by zacharyn35 (March 30, 2024 18:22:47)

MultiTasker801
Scratcher
100+ posts

Words to numbers

to change a string (text) to a number you'd create a list containing every character (letter, number, symbol, etc) you want to support and loop through the whole string, joining each letter's position in the list with the number (possibly adding leading zeroes to one digit numbers to make converting back easier: 7 -> 07)

converting back would require looping through the whole number and grabbing every pair of numbers and joining that position in the list with the current string
-stxllxr
New Scratcher
100+ posts

Words to numbers

zacharyn35 wrote:

Thanks! Also do you have a code so that it changes it to numbers based on the letter. (Ex. Hi would be 9,10)
Yep, that's a little more complicated version of the code above. Doing so is a similar process, but instead of full words in a list, it's individual letters for each item. To simplify it, you should make a script like this to fill the list, using a variable:
definesetuplistdeletealloflistsetito1repeat9addtolist needed for encoding, I'll explain laterrepeatlengthofabcdefghijklmnopqrstuvwxyz1234567890-_ addletteriofabcdefghijklmnopqrstuvwxyz1234567890-_tolist there is a space here.changeiby1 add more letters as you like.
This will add every letter of the alphabet, and will add numbers and a few other special characters if you wanted them. If you want to add more letters, just add more to both the top and bottom part of the “abcde…” part.

The reason we need 9 empty values at the start is because when you encode letters above j (letter 10 of the alphabet) it would encode into “10” and a would encode into “1” so it wouldn't know the difference between ab (1 2) and n (12). Adding in 9 blank values stops this problem, and allows us to encode far easier.

To encode like this, it just involves repeating over every character of the thing you want to encode, and convert it into numbers. The block shown below is how you do so in code, with one extra variable to hold the now converted thing.
defineencodethingtsetito1repeatlengthoftsetencoded stringtojoinencodedstringitem#ofletterioftinlistchangei by1setencoded stringtojoinencodedstring|
The “|” characters is our delimiter, allowing the code when to stop reading and knowing that there is a new value.

Decoding is almost as simple, but we need a few block to do so. Some of the core blocks needed are getting a letter, and getting a value, as shown below, requiring a few new variables.
letvaldefinegetlettersetlettoletteriofencodedstringchangeiby1definegetvaluegetlettersetvalto emptyrepeat2setvaltojoinvallet
These 2 blocks allow us to read a single letter of the encoded string, and a single letter of the decoded string. The process to decode an entire string is just to read every encoded letter until you reach the delimiter, or the end point, with another variable. (I know, alot of variables)
definereadstringsetdecoded stringto emptyrepeatuntillet=|getvaluesetdecoded stringtojoindecodedstringitemvaloflist
And that's it for encoding and decoding! You can use the blocks like this:
setencoded stringtowhenever you reset the string, set it to empty.encodethinghelloencodethinghow are youencodethingbyesetito1 when you want to start at the beginning.readstringsaydecodedstringfor2secsreadstringsaydecodedstringfor2secsreadstringsaydecodedstringfor2secs
This will say “hello” “how are you” “bye”. Hope this helps!

Last edited by -stxllxr (March 30, 2024 18:56:44)

zacharyn35
Scratcher
12 posts

Words to numbers

-stxllxr wrote:

zacharyn35 wrote:

Thanks! Also do you have a code so that it changes it to numbers based on the letter. (Ex. Hi would be 9,10)
Yep, that's a little more complicated version of the code above. Doing so is a similar process, but instead of full words in a list, it's individual letters for each item. To simplify it, you should make a script like this to fill the list, using a variable:
definesetuplistdeletealloflistsetito1repeat9addtolist needed for encoding, I'll explain laterrepeatlengthofabcdefghijklmnopqrstuvwxyz1234567890-_ addletteriofabcdefghijklmnopqrstuvwxyz1234567890-_tolist there is a space here.changeiby1 add more letters as you like.
This will add every letter of the alphabet, and will add numbers and a few other special characters if you wanted them. If you want to add more letters, just add more to both the top and bottom part of the “abcde…” part.

The reason we need 9 empty values at the start is because when you encode letters above j (letter 10 of the alphabet) it would encode into “10” and a would encode into “1” so it wouldn't know the difference between ab (1 2) and n (12). Adding in 9 blank values stops this problem, and allows us to encode far easier.

To encode like this, it just involves repeating over every character of the thing you want to encode, and convert it into numbers. The block shown below is how you do so in code, with one extra variable to hold the now converted thing.
defineencodethingtsetito1repeatlengthoftsetencoded stringtojoinencodedstringitem#ofletterioftinlistchangei by1setencoded stringtojoinencodedstring|
The “|” characters is our delimiter, allowing the code when to stop reading and knowing that there is a new value.

Decoding is almost as simple, but we need a few block to do so. Some of the core blocks needed are getting a letter, and getting a value, as shown below, requiring a few new variables.
letvaldefinegetlettersetlettoletteriofencodedstringchangeiby1definegetvaluegetlettersetvalto emptyrepeat2setvaltojoinvallet
These 2 blocks allow us to read a single letter of the encoded string, and a single letter of the decoded string. The process to decode an entire string is just to read every encoded letter until you reach the delimiter, or the end point, with another variable. (I know, alot of variables)
definereadstringsetdecoded stringto emptyrepeatuntillet=|getvaluesetdecoded stringtojoindecodedstringitemvaloflist
And that's it for encoding and decoding! You can use the blocks like this:
setencoded stringtowhenever you reset the string, set it to empty.encodethinghelloencodethinghow are youencodethingbyesetito1 when you want to start at the beginning.readstringsaydecodedstringfor2secsreadstringsaydecodedstringfor2secsreadstringsaydecodedstringfor2secs
This will say “hello” “how are you” “bye”. Hope this helps!
Thanks!

Powered by DjangoBB