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

2.2 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
698a1a73ade5ac0e19180fa5 Challenge 202: Add Punctuation 29 challenge-202

--description--

Given a string of sentences with missing periods, add a period (".") in the following places:

  • Before each space that comes immediately before an uppercase letter
  • And at the end of the string

Return the resulting string.

--hints--

add_punctuation("Hello world") should return "Hello world."

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(add_punctuation("Hello world"), "Hello world.")`)
}})

add_punctuation("Hello world It's nice today") should return "Hello world. It's nice today."

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(add_punctuation("Hello world It's nice today"), "Hello world. It's nice today.")`)
}})

add_punctuation("JavaScript is great Sometimes") should return "JavaScript is great. Sometimes."

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(add_punctuation("JavaScript is great Sometimes"), "JavaScript is great. Sometimes.")`)
}})

add_punctuation("A b c D e F g h I J k L m n o P Q r S t U v w X Y Z") should return "A b c. D e. F g h. I. J k. L m n o. P. Q r. S t. U v w. X. Y. Z."

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(add_punctuation("A b c D e F g h I J k L m n o P Q r S t U v w X Y Z"), "A b c. D e. F g h. I. J k. L m n o. P. Q r. S t. U v w. X. Y. Z.")`)
}})

add_punctuation("Wait.. For it") should return "Wait... For it."

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(add_punctuation("Wait.. For it"), "Wait... For it.")`)
}})

--seed--

--seed-contents--

def add_punctuation(sentences):

    return sentences

--solutions--

def add_punctuation(sentences):
    result = ""
    length = len(sentences)

    for i, c in enumerate(sentences):
        if c == " " and i + 1 < length and sentences[i + 1].isupper():
            result += "."
        result += c

    if not result.endswith("."):
        result += "."

    return result