How to resolve the algorithm I before E except after C step by step in the Elixir programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm I before E except after C step by step in the Elixir programming language

Table of Contents

Problem Statement

The phrase     "I before E, except after C"     is a widely known mnemonic which is supposed to help when spelling English words.

Using the word list from   http://wiki.puzzlers.org/pub/wordlists/unixdict.txt, check if the two sub-clauses of the phrase are plausible individually:

If both sub-phrases are plausible then the original phrase can be said to be plausible. Something is plausible if the number of words having the feature is more than two times the number of words having the opposite feature (where feature is 'ie' or 'ei' preceded or not by 'c' as appropriate).

As a stretch goal use the entries from the table of Word Frequencies in Written and Spoken English: based on the British National Corpus, (selecting those rows with three space or tab separated words only), to see if the phrase is plausible when word frequencies are taken into account.

Show your output here as well as your program.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm I before E except after C step by step in the Elixir programming language

Source code in the elixir programming language

defmodule RC do
  def task(path) do
    plausibility_ratio = 2
    rules = [ {"I before E when not preceded by C:", "ie", "ei"},
              {"E before I when preceded by C:", "cei", "cie"} ]
    regex = ~r/ie|ei|cie|cei/
    counter = File.read!(path) |> countup(regex)
    Enum.all?(rules, fn {str, x, y} ->
      nx = counter[x]
      ny = counter[y]
      ratio = nx / ny
      plausibility = if ratio > plausibility_ratio, do: "Plausible", else: "Implausible"
      IO.puts str
      IO.puts "  #{x}: #{nx}; #{y}: #{ny}; Ratio: #{Float.round(ratio,3)}: #{plausibility}"
      ratio > plausibility_ratio
    end)
  end
  
  def countup(binary, regex) do
    String.split(binary)
    |> Enum.reduce(Map.new, fn word,acc ->
         if match = Regex.run(regex, word),
             do: Dict.update(acc, hd(match), 1, &(&1+1)), else: acc
       end)
  end
end

path = hd(System.argv)
IO.inspect RC.task(path)


  

You may also check:How to resolve the algorithm War card game step by step in the 11l programming language
You may also check:How to resolve the algorithm Null object step by step in the Raven programming language
You may also check:How to resolve the algorithm Loops/Nested step by step in the SPL programming language
You may also check:How to resolve the algorithm Palindromic gapful numbers step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the МК-61/52 programming language