How to resolve the algorithm ABC problem step by step in the Elm programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm ABC problem step by step in the Elm programming language

Table of Contents

Problem Statement

You are given a collection of ABC blocks   (maybe like the ones you had when you were a kid).
There are twenty blocks with two letters on each block. A complete alphabet is guaranteed amongst all sides of the blocks. The sample collection of blocks:

Write a function that takes a string (word) and determines whether the word can be spelled with the given collection of blocks.

The rules are simple:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm ABC problem step by step in the Elm programming language

Source code in the elm programming language

import Html exposing (div, p, text)


type alias Block = (Char, Char)


writtenWithBlock : Char -> Block -> Bool
writtenWithBlock letter (firstLetter, secondLetter) =
  letter == firstLetter || letter == secondLetter


canMakeWord : List Block -> String -> Bool
canMakeWord blocks word =
  let
    checkWord w examinedBlocks blocksToExamine =
      case (String.uncons w, blocksToExamine) of
        (Nothing, _) -> True
        (Just _, []) -> False
        (Just (firstLetter, restOfWord), firstBlock::restOfBlocks) ->
           if writtenWithBlock firstLetter firstBlock
           then checkWord restOfWord [] (examinedBlocks ++ restOfBlocks)
           else checkWord w (firstBlock::examinedBlocks) restOfBlocks
  in
  checkWord (String.toUpper word) [] blocks
  
  
exampleBlocks =
  [ ('B', 'O')
  , ('X', 'K')
  , ('D', 'Q')
  , ('C', 'P')
  , ('N', 'A')
  , ('G', 'T')
  , ('R', 'E')
  , ('T', 'G')
  , ('Q', 'D')
  , ('F', 'S')
  , ('J', 'W')
  , ('H', 'U')
  , ('V', 'I')
  , ('A', 'N')
  , ('O', 'B')
  , ('E', 'R')
  , ('F', 'S')
  , ('L', 'Y')
  , ('P', 'C')
  , ('Z', 'M')
  ]
  

exampleWords =
  ["", "A", "bark", "BoOK", "TrEAT", "COmMoN", "Squad", "conFUsE"]


main = 
  let resultStr (word, canBeWritten) = "\"" ++ word ++ "\"" ++ ": " ++ if canBeWritten then "True" else "False" in
  List.map (\ word -> (word, canMakeWord exampleBlocks word) |> resultStr) exampleWords
  |> List.map (\result -> p [] [ text result ])
  |> div []


  

You may also check:How to resolve the algorithm Find the intersection of two lines step by step in the F# programming language
You may also check:How to resolve the algorithm Random Latin squares step by step in the Java programming language
You may also check:How to resolve the algorithm Abstract type step by step in the ReScript programming language
You may also check:How to resolve the algorithm Minimum multiple of m where digital sum equals m step by step in the RPL programming language
You may also check:How to resolve the algorithm Hello world/Web server step by step in the AntLang programming language