Problem of the Day
A new programming or logic puzzle every Mon-Fri

One Letter Off

For this problem you'll need to import a dictionary. The program will take in a word such as "cat" and spit back out all the words that are within one letter of "cat". You should have words such as "cot", "cut", "can", and "rat". If there are no words within one letter than print out "no words found". Note the goal isn't to add or remove letters, only replace.

Permalink: http://problemotd.com/problem/one-letter-off/

Comments:

  • Anonymous - 9 years, 3 months ago

    c#

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter Word:");
            List<string> similarWords = GetSimilarWords(Console.ReadLine());
            if (similarWords.Count == 0)
                Console.WriteLine("no words found");
            else
                similarWords.ForEach(x => Console.WriteLine(x));
    
            Console.Read();
        }
    
        static List<string> GetSimilarWords(string word)
        {
            List<string> output = new List<string>();
            List<string> dictionary = LoadDictionary();
    
            for( int i = 0; i < word.Length; i++)
            {
                StringBuilder patternBuilder = new StringBuilder();
                for (int j = 0; j < word.Length; j++)
                {
                    if (j==i)
                        patternBuilder.Append("[A-Za-z]");
                    else
                        patternBuilder.Append(word[j]);
                }
    
                Regex reg = new Regex(patternBuilder.ToString());
    
                output.AddRange(dictionary.Where(x => x != word && reg.IsMatch(x)));
            }
            return output;
        }
    }
    

    reply permalink

  • Eddy - 9 years, 3 months ago

    My solution in C.

    reply permalink

  • Anonymous - 9 years ago

    def letters_in_common(word1, word2):
        x = 0
        lic = 0
        while x < len(word1):
            if word1[x] == word2[x]:
                lic = lic + 1
                x = x + 1
            else:
                x = x + 1
    
        return lic
    
    
    
    file = open('WordsEn.txt', 'r')
    
    y = raw_input("Enter a word to find words that are one letter off> ")
    
    one_off = []
    
    for line in file:
        if len(y) == len(line)-1:
            if letters_in_common(y, line) == len(y) - 1:
                one_off.append(line[0:len(y)])
    
    if len(one_off) > 0:
        print one_off
    else:
        print "No Words Found"
    

    Output: Enter a word to find words that are one letter off> hairy ['dairy', 'fairy', 'hairs', 'harry']

    reply permalink

Content curated by @MaxBurstein