-
Notifications
You must be signed in to change notification settings - Fork 0
/
TRowTest.py
52 lines (40 loc) · 1.64 KB
/
TRowTest.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
import string
import re
def long_word_counter(sentence='The cow jumped over the moon.'):
"""
This method looks for the longest word in a sentence. The return
is a list containing the longest word and its length as an int.
Special characters are removed from being counted.
Example: long_word_counter(sentence='The cow jumped over the moon.')
"""
sentence = re.sub(r"[,.;@#?!&$]+", '', sentence).split() #remove all punctuations from sentences
count = 0
lword = ''
wordlength = 0
for i in sentence:
count += 1
if len(i) > wordlength:
lword = i
wordlength = len(i)
return [lword, wordlength]
def short_word_counter(sentence = 'The cow jumped over the moon.'):
"""
This method looks for the shortest word in a sentence. The return
is a list containing the shortest word and its length as an int. The
words "the" and "a" are intentionally ignored. Special characters are
removed from being counted.
Example: short_word_counter(sentence='The cow jumped over the moon.')
"""
sentence = re.sub(r"[,.;@#?!&$]+", '', sentence).split() #remove all punctuations from sentences
lword = ''
wordlength = 0
for i in sentence:
if (i.lower() == 'the') or (i.lower() == 'a'): # ignore the word "the" from counting... typical use case
continue
elif len(i) < wordlength:
lword = i
wordlength = len(i)
elif wordlength == 0:
lword = i
wordlength = len(i)
return [lword, wordlength]