-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounting_words.py
More file actions
31 lines (24 loc) · 802 Bytes
/
counting_words.py
File metadata and controls
31 lines (24 loc) · 802 Bytes
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
# Function that takes as input a string and returns the number of words in the string.
def count_words(a_string):
counter = 0
ready = True
for char in a_string:
if char == ' ':
ready = True
else:
if ready:
counter += 1
ready = False
return counter
def main():
passage =("The number of orderings of the 52 cards in a deck of cards "
"is so great that if every one of the almost 7 billion people alive "
"today dealt one ordering of the cards per second, it would take "
"2.5 * 10**40 times the age of the universe to order the cards in every "
"possible way.")
s = ''
print(count_words(passage))
print(count_words(s))
#>>>56
if __name__ == '__main__':
main()