-
Notifications
You must be signed in to change notification settings - Fork 1
/
Detect_capital.py
43 lines (42 loc) · 1.4 KB
/
Detect_capital.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
class Solution(object):
def detectCapitalUse(self, word):
"""
:type word: str
:rtype: bool
"""
'''
either:
- all cap
- all lower
- only 1st is cap
'''
# If a one letter word, capital is used correctly
if len(word) == 1:
return True
# Otherwise, let's find out!
else:
isTrue = True
# If a word starts off without a capital, the rest must be uncapitalized
if word[0].isupper() == False:
for i in range(len(word))[1:]:
if word[i].isupper() == True:
isTrue = False
break
else:
isTrue = True
return isTrue
# If a word starts ff with a capital, must be all capital, or just the first letter
if word[0].isupper() == True:
restCap = 0
for i in word[1:]:
if i.isupper() == True:
restCap += 1
if restCap == 0:
# Rest of the word is lowercase
return True
elif len(word[1:]) == restCap:
# Entire word is uppercase
return True
else:
# Some jumblled up word
return False