-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAverageWordLength.py
More file actions
36 lines (28 loc) · 1.13 KB
/
Copy pathAverageWordLength.py
File metadata and controls
36 lines (28 loc) · 1.13 KB
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
import re
def calculateAverage(sentence):
sentence = re.sub(r'[^a-zA-Z\d\s]', u' ', sentence, flags=re.UNICODE)
sentence = re.split(' ', sentence)
sentence = list(filter(None, sentence))
wordLen = 0
for word in sentence:
if word != '':
wordLen = wordLen + len(word)
return round((wordLen / len(sentence)), 2)
# /////////////////////Test assignment example
def test_case_1():
input = 'The quick brown fox jumps over the lazy dog'
print('User input is:', input)
assert calculateAverage(input) == 3.89, "Test_case_1 Should be 3.89"
print('The average of word length is:', calculateAverage(input))
print('-------------------------------')
# /////////////////////Test assignment example
def test_case_2():
input = 'All sentences might...not be punctually, correct.'
print('User input is:', input)
assert calculateAverage(input) == 5.57, "Test_case_2 Should be 5.57"
print('The average of word length is:', calculateAverage(input))
print('-------------------------------')
if __name__ == "__main__":
test_case_1()
test_case_2()
print("Everything passed")