DEV Community

Daily Challenge #86 - Wouldn't, not Would.

dev.to staff on October 09, 2019

This challenge is for all those times your mouth said something your brain didn't tell it to. Write a function that will find a misspoken word in ...
Collapse
 
aminnairi profile image
Amin

Elm

Maybe not the best algorithm, show me yours!

import List exposing (map, map2, filter, foldl, member)
import String exposing (words, replace, toUpper)
import Tuple exposing (first, pair, second)

lowerPositives =
    [ "are"
    , "can"
    , "could"
    , "did"
    , "do"
    , "had"
    , "has"
    , "have"
    , "is"
    , "might"
    , "must"
    , "should"
    , "was"
    , "were"
    , "would"
    ]

lowerNegatives =
    [ "aren't"
    , "can't"
    , "couldn't"
    , "didn't"
    , "don't"
    , "hadn't"
    , "hasn't"
    , "haven't"
    , "isn't"
    , "might not"
    , "mustn't"
    , "shouldn't"
    , "wasn't"
    , "weren't"
    , "wouldn't"
    ]

upperPositives =
    map toUpper lowerPositives

upperNegatives =
    map toUpper lowerNegatives

corrections =
    map2 pair lowerPositives lowerNegatives
        |> append (map2 pair lowerNegatives lowerPositives)
        |> append (map2 pair upperPositives upperNegatives)
        |> append (map2 pair upperNegatives upperPositives)

correct (invalid, valid) speech =
    speech -- "I do like pizza"
        |> replace invalid valid -- "I don't like pizza"

neededCorrections speech correction =
    speech -- "I do like pizza."
        |> words -- ["I", "do", "like", "pizza"]
        |> member (first correction) -- [("do", "don't")]

correctSpeech speech =
    corrections -- [("are", "aren't"), ("can", "can't"), ...)]
        |> filter (neededCorrections speech) -- [("do", "don't")]
        |> foldl correct speech -- "I don't like pizza." (that's not true, I love pizza)

Playground

Test it here.

Collapse
 
kvharish profile image
K.V.Harish

What if the string is "Should I go?" or generally speaking when they come at the starting of a sentence?

Collapse
 
aminnairi profile image
Amin

You are right and this solution is not 100% bulletproof. Hence my little message just before saying that I'm curious about other answer.

What do you think we should do to resolve this issue?