Hey guys,

I've been writing a program that will take user jumbled input, find it in a given file, and print the unjumbled word. I've written most the code up to this point but I haven't been able to finish it and am stuck at how to fix it. I've included the code below for viewing, any mistakes you see or pointers would be a great help.

Here's what I have to do:

Write a user defined function (to_array) that converts a String object to an array

Write a method (get_permulations) that inputs an array of single characters (like ["j", "o", "e"]). The method returns an array of all permutations of the Jumble string. The permutations in the array will be in string format

Then after that compare the words in the Unix dictionary(dict.txt as seen in below code) to the permutation strings in the array and print any matches

Here's my code, I know there are a lot mistakes, again any help would be appreciated:

def to_array()
s = [" "]
s.each do |a|
end
print a, "\n"
def gets_permutations()
perms = a.permutation(3).to_array.collect do |perm|
   perm.join
end
print perms, "\n"

print "Enter Word to jumble: "
c = STDIN.gets.chomp
a = to_array(c)
print a, "\n"
perms = get_permutations(a)
print perms, "\n"

fin = file.open("dict.txt", "r")
while line = fin.gets
word = line.chomp
     if word = word.downcase
        for perm in perms
            if perm = word
            print word, "\n"
            end
        end
    end
end
fin.close
Mya:) commented: Nice idea! +2

1) your to_array() definition is missing an argument.
2) What do you want to do inside the to_array()?? You could simply use split(//) to create an array from string! (i.e. "abcde".split(//) #=> ["a", "b", "c", "d", "e"]
3) Why permutation function has number 3 in it? Do you want only 3-letter word?
4) You could use to_a() after permutation to create a 2D-liked array, and then join it to create an array of string. Don't forget to downcase them all first (example a.permutation.to_a.each {|w| puts w.join("").downcase}).
5) Line 22, why do you have 'if'? Or you want to compare only words that are all lower case? Shouldn't you convert the word to lower case instead? (i.e. word = word.downcase). You could simply do it in one line when you read in (i.e. word = line.chomp.downcase).
6) You need to understand that '=' is NOT the same as '=='. Also, you must not use '==' to compare strings! You should use eql?() instead (i.e. if perm.eql?(word)).

That's just some suggestions. I didn't dig into the script that much but hope you would fix it first.

Thanks for the suggestions, I will implement your thoughts to see if that fixes some of the issues. The permutations(3) is an error on my behalf, it is supposed to be just a.permutation. I had the if in there because all the words in the dict.txt file were lowercase, but you're right I could just convert the word to lowercase instead.

What do you plan to do about inputs that match multiple words? For example if I input ewst that should match any of west, wets or stew.

Since you have ambiguity in the matching for a scrambled input why have the overhead of iterating all permutations of the input string for every word in the input? Instead, just compare the sorted versions of the input and each word. For example:

def format str
    str.downcase.split(//).sort.join
end

needle = format gets.chomp
File.open('t.t').each do |line|
    puts line if format(line.chomp) == needle
end

Of course, that ignores punctuation, case, and other specifics.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.