-
Notifications
You must be signed in to change notification settings - Fork 2
/
codechallenge_116.py
92 lines (79 loc) · 2.84 KB
/
codechallenge_116.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'''
Date: 05/04/2022
Problem description:
====================
This problem was asked by Nest.
Create a basic sentence checker that takes in a stream of characters and determines
whether they form valid sentences. If a sentence is valid, the program should print it out.
We can consider a sentence valid if it conforms to the following rules:
The sentence must start with a capital letter, followed by a lowercase letter or a space.
All other characters must be lowercase letters, separators (,,;,:) or terminal marks (.,?,!,‽).
There must be a single space between each word.
The sentence must end with a terminal mark immediately following a word.
Algorithm:
==========
1. Validate input
2. Check for single space between each word
3. Check for terminal mark at the end of the stream
4. Split the stream into array of characters. Traverse the array checking for capitalized 1st letter, alphanumeric, separators.
5. Return False if conditions not met, else print the sentence.
'''
import unittest
from msilib.schema import Error
import os, time, sys
def check_sentence(stream):
# check for single space between each word
if len(stream.split(' ')) > 1:
return False
# check if the sentence ends with a terminal mark
if list(stream)[-1] not in ['.','!','?','‽']:
return False
if list(stream)[0].isupper() == False:
return False
# Traverse the stream and check for the followings:
for i,c in enumerate(list(stream)):
# is first c a capital letter?
if i == 0:
continue
else: # i > 0
if c.isspace():
if (list(stream)[i+1].isspace()):
return False
elif (c.isalnum() and c.islower()) or (c in [',',';',':','.']):
continue
else:
return False
print(stream)
return True
class TestSentenceChecker(unittest.TestCase):
def test_sentence(self):
s = 'Hello world.'
self.assertTrue(check_sentence(s), 'Valid sentence')
s = 'Hello World. This is two sentences.'
self.assertFalse(check_sentence(s), 'Invalid sentence')
s = 'this is not a sentence...ay caramba!'
self.assertFalse(check_sentence(s), 'Invalid sentence')
#
# main driver
#
def main():
sentence = 'The quick brown fox jumps over the lazy dog.'
print(check_sentence(sentence))
not_sentence = 'ohhh, it is not a sentence!'
print(check_sentence(not_sentence))
s = 'this is not a sentence...ay caramba!'
print(check_sentence(s))
if __name__ == '__main__':
if os.environ.get('UNITTEST_ONLY') != 'True':
main()
else:
unittest.main()
'''
Run-time output:
===============
$ pipenv run python ./codechallenge_116.py
Loading .env environment variables...
The quick brown fox jumps over the lazy dog.
True
False
'''