Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

3.9 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
6a151d32d772271dd2448c2c Challenge 310: British to American 29 challenge-310

--description--

Given a sentence, convert any British English spellings to their American English equivalents using the following lookup table and return the updated sentence:

British American
"colour" "color"
"flavour" "flavor"
"honour" "honor"
"neighbour" "neighbor"
"labour" "labor"
"humour" "humor"
"centre" "center"
"fibre" "fiber"
"defence" "defense"
"offence" "offense"
"organise" "organize"
"recognise" "recognize"
"analyse" "analyze"
  • Replacements should be case-insensitive. For example, "Colour" should become "Color".
  • The input may contain words that build on the exact spelling of a root in the table that also need to be changed. For example, "colouring" should become "coloring", and "disorganised" should become "disorganized".

--hints--

british_to_american("I love the colour blue.") should return "I love the color blue."

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(british_to_american("I love the colour blue."), "I love the color blue.")`)
}})

british_to_american("The fibre optic cable is new.") should return "The fiber optic cable is new."

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(british_to_american("The fibre optic cable is new."), "The fiber optic cable is new.")`)
}})

british_to_american("It's an honour to meet someone with such humour.") should return "It's an honor to meet someone with such humor."

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(british_to_american("It's an honour to meet someone with such humour."), "It's an honor to meet someone with such humor.")`)
}})

british_to_american("The unrecognised artist analysed his colour palette at the centre.") should return "The unrecognized artist analyzed his color palette at the center."

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(british_to_american("The unrecognised artist analysed his colour palette at the centre."), "The unrecognized artist analyzed his color palette at the center.")`)
}})

british_to_american("The offence analysed, with organisation, the defence centre and recognised that the neighbouring labouror was humourous, flavourful, and colourful.") should return "The offense analyzed, with organisation, the defense center and recognized that the neighboring laboror was humorous, flavorful, and colorful."

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(british_to_american("The offence analysed, with organisation, the defence centre and recognised that the neighbouring labouror was humourous, flavourful, and colourful."), "The offense analyzed, with organisation, the defense center and recognized that the neighboring laboror was humorous, flavorful, and colorful.")`)
}})

--seed--

--seed-contents--

def british_to_american(sentence):

    return sentence

--solutions--

import re

def british_to_american(sentence):
    lookup = {
        "colour": "color",
        "flavour": "flavor",
        "honour": "honor",
        "neighbour": "neighbor",
        "labour": "labor",
        "humour": "humor",
        "centre": "center",
        "fibre": "fiber",
        "defence": "defense",
        "offence": "offense",
        "organise": "organize",
        "recognise": "recognize",
        "analyse": "analyze"
    }

    result = sentence
    for british, american in lookup.items():
        def replace(match, american=american):
            return american[0].upper() + american[1:] if match.group()[0].isupper() else american
        result = re.sub(british, replace, result, flags=re.IGNORECASE)

    return result