-
Notifications
You must be signed in to change notification settings - Fork 891
/
problem_263.py
34 lines (23 loc) · 855 Bytes
/
problem_263.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
SEPARATORS = {',', ';', ':'}
TERM_MARKS = {'.', '?', '!'}
def is_valid(context, char, next_chars):
curr_valid = True
if not context and not char.istitle():
return False
if len(context) == 1:
if char == ' ' or not char.istitle():
pass
else:
return False
if char in TERM_MARKS:
return context[-1] not in (SEPARATORS | TERM_MARKS)
if not next_chars:
return char in TERM_MARKS and curr_valid
return is_valid(context + char, next_chars[0], next_chars[1:]) if curr_valid else False
def is_valid_sentence(sentence):
return is_valid("", sentence[0], sentence[1:])
# Test
assert is_valid_sentence("Valid sentence.")
assert not is_valid_sentence("Invalid sentence")
assert not is_valid_sentence("INvalid sentence.")
assert is_valid_sentence("A valid sentence.")