How to resolve the algorithm Word wheel step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Word wheel step by step in the Haskell programming language

Table of Contents

Problem Statement

A "word wheel" is a type of word game commonly found on the "puzzle" page of newspapers. You are presented with nine letters arranged in a circle or 3×3 grid. The objective is to find as many words as you can using only the letters contained in the wheel or grid. Each word must contain the letter in the centre of the wheel or grid. Usually there will be a minimum word length of 3 or 4 characters. Each letter may only be used as many times as it appears in the wheel or grid.

Write a program to solve the above "word wheel" puzzle. Specifically:

A "word" is defined to be any string contained in the file located at   http://wiki.puzzlers.org/pub/wordlists/unixdict.txt. If you prefer to use a different dictionary,   please state which one you have used. Word wheel puzzles usually state that there is at least one nine-letter word to be found. Using the above dictionary, find the 3x3 grids with at least one nine-letter solution that generate the largest number of words of three or more letters.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Word wheel step by step in the Haskell programming language

The provided code is a Haskell program that implements a function to find words that can be formed from a given grid of letters, known as the Word Wheel problem. It reads a dictionary of words from a file and applies the gridWords function to each line of the dictionary to identify words that meet specific criteria:

  1. Word Length: The word must be at least 3 characters long.

  2. Central Letter: The middle letter of the word must match the middle letter of the grid.

  3. Letters in Wheel: All the letters in the word must be present in the grid after sorting the letters of the grid alphabetically.

The gridWords function takes two parameters:

  • grid: A list of strings representing the rows of the grid.

  • dictionary: A list of strings representing the dictionary of words.

It first calculates the concatenated, lowercase letters of the grid (cs) and sorts them to create the "wheel" (wheel). The middle letter of the grid (mid) is also determined.

The wheelFit function checks if a word can be formed from the grid by comparing the sorted letters of the word to the sorted letters of the grid. It returns True if all letters in the word are present in the grid, and False otherwise.

The main function reads the dictionary from a file named "unixdict.txt." For each line in the dictionary, it applies the gridWords function to the line and the given grid ["NDE", "OKG", "ELW"]. Words that meet the criteria are then printed using putStrLn.

Overall, this code provides a solution to the Word Wheel problem by identifying words that can be formed from a given grid of letters while satisfying certain constraints.

Source code in the haskell programming language

import Data.Char (toLower)
import Data.List (sort)
import System.IO (readFile)

------------------------ WORD WHEEL ----------------------

gridWords :: [String] -> [String] -> [String]
gridWords grid =
  filter
    ( ((&&) . (2 <) . length)
        <*> (((&&) . elem mid) <*> wheelFit wheel)
    )
  where
    cs = toLower <$> concat grid
    wheel = sort cs
    mid = cs !! 4

wheelFit :: String -> String -> Bool
wheelFit wheel = go wheel . sort
  where
    go _ [] = True
    go [] _ = False
    go (w : ws) ccs@(c : cs)
      | w == c = go ws cs
      | otherwise = go ws ccs

--------------------------- TEST -------------------------
main :: IO ()
main =
  readFile "unixdict.txt"
    >>= ( mapM_ putStrLn
            . gridWords ["NDE", "OKG", "ELW"]
            . lines
        )


  

You may also check:How to resolve the algorithm Loops/N plus one half step by step in the Fantom programming language
You may also check:How to resolve the algorithm Nested templated data step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Word wrap step by step in the MiniScript programming language
You may also check:How to resolve the algorithm Yahoo! search interface step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Hailstone sequence step by step in the Déjà Vu programming language