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

#1 2011-09-13 04:48:28

dylancurran
New Scratcher
Registered: 2010-10-04
Posts: 3

Palindrome Detector

I'm wondering how it would work in scratch, that if you type in a word, it reads the word and detects whether it's a Palindrome or not. Should work for any length word.

Thanks in advance  big_smile

Offline

 

#2 2011-09-13 06:35:20

PlutoIsHades
Scratcher
Registered: 2010-10-18
Posts: 1000+

Re: Palindrome Detector

I have an idea.  Would it be ok if the person had to type it in backwards or forwards?  Because if so, then this would work:
[blocks]<if><( <{ Backwards }> <=> <{ Forwards }> )>
<broadcast[ Palindrome
<else>
<broadcast[ No Palindrome[/blocks]

Last edited by PlutoIsHades (2011-09-13 06:35:40)


Minecraft, Redwall, and Cyberchase fan, and PROUD.

Offline

 

#3 2011-09-13 06:52:42

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: Palindrome Detector

Hm... well, you can use the length of( ) block to find the length of the word. Then just scan through it letter by letter, adding each letter to a list. Once you get to the center of the word, (making sure to disregard the letter at the very middle if there are an odd number of letters) start checking if each letter is the same as the one stored in the list in reverse order. That should tell you if it's a palindrome.

Just one caveat: make sure to have a script run through and remove all the spaces before running the script I described above. Otherwise, any multi-word palindrones (like race car) won't work.

Hope that helps!


http://www.blocks.scratchr.org/API.php?action=random&amp;return=image&amp;link1=http://i.imgur.com/OZn2RD3.png&amp;link2=http://i.imgur.com/duzaGTB.png&amp;link3=http://i.imgur.com/CrDGvvZ.png&amp;link4=http://i.imgur.com/POEpQyZ.png&amp;link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

#4 2011-09-14 23:44:37

ASD8
New Scratcher
Registered: 2011-06-21
Posts: 15

Re: Palindrome Detector

you'd have to parse it and check if backwards is the same as forwards. by parsing it you can remove the spaces so you can use multi-word palindromes

Offline

 

#5 2011-09-15 14:49:59

Ratty1967UK
Scratcher
Registered: 2007-05-16
Posts: 88

Re: Palindrome Detector

Here's little project with an algorithm to detect palindromes - I'll leave it to you to tweak it for multi-word palindromes  smile

http://scratch.mit.edu/projects/Ratty1967UK/2030437

Offline

 

Board footer