-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter8_exercise8_5.py
79 lines (54 loc) · 2.48 KB
/
chapter8_exercise8_5.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'''
str.islower()
Return True if all cased characters in the string are lowercase and there is at least one cased character,
False otherwise.
'''
def any_lowercase1(s):
for c in s:
if c.islower():
return True
else:
return False
'''Here, the funtion takes the first character, if it is Upper case,
it returns false, if it is lower case, it returns true. it does not iterate over other characters in the string s'''
# -----------------------------------------------------------------------------------------------------------------------
def any_lowercase2(s):
for c in s:
if 'c'.islower():
return 'True'
else:
return 'False'
'''this function checks only the string 'c' is lower, which always returns True'''
# -----------------------------------------------------------------------------------------------------------------------
def any_lowercase3(s):
for c in s:
flag = c.islower()
return flag
'''This would have been a nice code except that it only returns after iterating completly.
Hence, the output is based on the last letter of the string you're checking, it ignores the state of other characters
but only return its last check '''
# -----------------------------------------------------------------------------------------------------------------------
def any_lowercase4(s):
flag = False
for c in s:
flag = flag or c.islower()
return flag
'''This is the perfect code. During the iteration, once a lowercase is met, the flag changes to True.
if another lower case is met, c.islower changes to false but the flag still maintains True because if the
"or" gate'''
# -----------------------------------------------------------------------------------------------------------------------
def any_lowercase5(s):
for c in s:
if not c.islower():
return False
return True
'''This code returns True by default but, it will only work when the string being tested has not more that one
upper case letter. Once it encounters an upper case letter, it returns a value and the if statement doesnt run
anymore.
'''
# -----------------------------------------------------------------------------------------------------------------------
print(any_lowercase1('WaSEe'))
print(any_lowercase2('CSEc'))
print(any_lowercase3('ccSEc'))
print(any_lowercase4('CSddE'))
print(any_lowercase5('EeE'))