-
Notifications
You must be signed in to change notification settings - Fork 0
/
Giang_Wk6_Ex2.py
45 lines (37 loc) · 1.22 KB
/
Giang_Wk6_Ex2.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
# Alex Giang
# Professor Abolghasemi
# CIS 502
# 29 February 2024
# Week 6, Exercise 2
# This file is intended to demonstrate beginning knowledge in implementing
# iterator classes with customized behavior.
import collections.abc
class CapitalizeIterator(collections.abc.Iterator):
def __init__(self, words):
if isinstance(words, list):
pass
elif isinstance(words, str):
words = words.split()
else:
raise TypeError(words, 'Please pass CapitalizeIterator constructor either a'
' list of words or a string')
self.words = []
for w in words:
if len(w) >= 1:
self.words.append(w[:1].upper() + w[1:].lower())
def __next__(self):
if self.words:
return self.words.pop(0)
else:
raise StopIteration
word_list_list = [
["python", "is", "awesome"],
"returning each word capitalized",
["java", "wouldn't", "allow", "this"],
"ha ha ha hee ha ha"
]
for wl in word_list_list:
iterator = iter(CapitalizeIterator(wl))
print('\nCapitalized words:')
for wd in iterator:
print(wd, end=' ')