Ctrl K

Well-Formed Words in a Line

Easy
The string line consists of lowercase English letters, digits, hyphens -, the marks !, ., , and spaces. Spaces (one or more in a row) split it into tokens. A token is a well-formed word if all of the following hold: - it contains no digits; - it has at most one hyphen, and if there is one, it has a letter on each side ("a-b" is fine, "-ab" and "ab-" are not); - it has at most one punctuation mark, and if there is one, it comes last ("ok," and "!" are fine, "o!k" and "a.," are not). Return the number of well-formed words in the line.
Example 1
line="well-known  facts, x2 ok!" → 3

"well-known", "facts," and "ok!" are well formed, while "x2" contains a digit. The answer is 3.

Example 2
line="-up mid- a-b-c .. !" → 1

In "-up" and "mid-" the hyphen sits at an edge, "a-b-c" has two hyphens, ".." has two punctuation marks. Only "!" qualifies, so the answer is 1.

Example 3
line="go. go, go!" → 3
Constraints
  • 1 ≤ len(line) ≤ 1000
  • line consists of lowercase English letters, digits, ' ', '-', '!', '.', ','
  • The line contains at least one token
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsValue of a Roman Numeral EasyShared Start of Words EasyBracket Balance EasyWhere the Fragment Starts EasyLength of the Final Word Easy
loading Python…
Input
line="well-known  facts, x2 ok!"
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 323 tests on the server, hidden ones included.