Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 help with anagram finder
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
JakeTakeLake




PostPosted: Fri Oct 18, 2013 9:22 pm   Post subject: help with anagram finder

What is it you are trying to achieve?
I'm trying to make an anagram finder. i put in a word, then the program finds all the anagrams of that word from a text file with all the words from a dictionary.


What is the problem you are having?
the program finds all the anagrams, but some are wrong. the main problem is the words normally have double letters. i understand the concept, but there must be a small problem that i've overlooked.


Describe what you have tried to solve this problem
i've tried subtracting the letter each time there is a match to the word and at the end testing whether all the letters are removed. i've also tried using an array to "cancel" a letter when it makes a match to a letter in the other word, so that there are no repeated letters.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
ask if u want the webster.txt

Turing:


var line := 0
var fileNo : int
var s : string
var word : string

get word

open : fileNo, "webster.txt", get, seek

loop
    var spot : array 1 .. length (word) of boolean
    var putword : boolean := true
    var spotstaken:int:=0

    for i : 1 .. length (word)
        spot (i) := false
    end for

    exit when eof (fileNo)

    seek : fileNo, line
    get : fileNo, s


    if length (s) = length (word) then
        for i : 1 .. length (word)
            for j : 1 .. length (word)
                if word (i) = s (j) then
                    if spot (j) = false then
                        spot (j) := true
                        spotstaken:=spotstaken + 1
                    end if
                end if
            end for
        end for
    end if

    if spotstaken = length(word) then
        put s
    end if

    line := line + length (s) + 1
end loop


Please specify what version of Turing you are using
4.1.1
Sponsor
Sponsor
Sponsor
sponsor
Dreadnought




PostPosted: Fri Oct 18, 2013 10:45 pm   Post subject: Re: help with anagram finder

So basically it looks like the following scenario would cause a problem.
Turing:
word := "abcde"
s := "aaaaa"


but this one wont
Turing:
word := "abcde"
s := "aaaaz"

Let's examine some of the logic in your code.
So you go through each letter of word and check if it appears in s. What exactly do you want to do when you find the letter in s? What crucial little step are you missing?



Hint:
Spoiler:
How many letters of word do you check before spotstaken = length(word)?
JakeTakeLake




PostPosted: Sat Oct 19, 2013 9:25 am   Post subject: Re: help with anagram finder

[quote="Dreadnought @ Fri Oct 18, 2013"]So basically it looks like the following scenario would cause a problem.
Turing:
word := "abcde"
s := "aaaaa"


but this one wont
Turing:
word := "abcde"
s := "aaaaz"


ok i got it working. this is what helped me. i just made another spot variable. so when it matches a letter it takes up the spots of j AND i. thanks
Dreadnought




PostPosted: Sat Oct 19, 2013 10:24 am   Post subject: Re: help with anagram finder

JakeTakeLake wrote:

ok i got it working. this is what helped me. i just made another spot variable. so when it matches a letter it takes up the spots of j AND i. thanks

That works, another (perhaps more natural) way of doing it would be to exit the innermost for loop when you find a letter (right after spostaken := spotstaken + 1). This would stop the program from matching a letter of word[/d] multiple times.

It's really a matter of preference, but I thought you might be interested in another way of approaching things.
Raknarg




PostPosted: Sat Oct 19, 2013 11:54 am   Post subject: RE:help with anagram finder

A nice simple method too would be to sort both strings and see if they're the same
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: