loading...

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

thepracticaldev profile image dev.to staff ・1 min read

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 your speech using the following words:

are - aren't
can - can't
could - couldn't
did - didn't
do - don't
had - hadn't
has - hasn't
have - haven't
is - isn't
might - mightn't
must - mustn't
should - shouldn't
was - wasn't
were - weren't
would - wouldn't

Add or remove the suffix n't where needed. Beware of the word "can" and extra suffixes like 've.

speech:
I do like pizza.
I haven't seen you wearing that hat before.
I could see why you would say that.
I didn't say it! It wasn't me!
YES, WE CAN

Good luck!


This challenge comes from dinglemouse on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Posted on by:

thepracticaldev profile

dev.to staff

@thepracticaldev

The hardworking team behind dev.to ❤️

Discussion

pic
Editor guide
 

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.

 

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

 

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?