-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathum.py
More file actions
39 lines (28 loc) · 1.32 KB
/
um.py
File metadata and controls
39 lines (28 loc) · 1.32 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
37
38
39
"""
It’s not uncommon, in English, at least, to say “um” when trying to, um, think
of a word. The more you do it, though, the more noticeable it tends to be!
In a file called um.py, implement a function called count that expects a line of
text as input as a str and returns, as an int, the number of times that “um”
appears in that text, case-insensitively, as a word unto itself, not as a
substring of some other word. For instance, given text like hello, um, world,
the function should return 1. Given text like yummy, though, the function should
return 0.
Structure um.py as follows, wherein you’re welcome to modify main and/or
implement other functions as you see fit, but you may not import any other
libraries. You’re welcome, but not required, to use re and/or sys.
Either before or after you implement count in um.py, additionally implement, in
a file called test_um.py, three or more functions that collectively test your
implementation of count thoroughly, each of whose names should begin with test_
so that you can execute your tests with:
pytest test_um.py
"""
import re
def main():
print(count(input("Text: ")))
def count(s: str) -> int:
pattern = r"(\bum\b)"
if cleaned_string := re.findall(pattern, s, flags=re.I):
return len(cleaned_string)
return 0
if __name__ == "__main__":
main()