In: Computer Science
In Python Please
Samwell Tarly knows that in the world of Westeros, some names are
better than others. Some like Aegon Targaryen and Tyrion Lannister.
He and Citadel believe that a correct name should have the same
number of capital letters as words, for example: Bran Stark is
correct. Whereas Brienne of Tarth is not. Your task is to help read
the books and determine the correct names. Each name is strictly
composed of letters and names separated by single spaces. Input
Format Several test cases, one per line, each containing the name
of a person. No name contains more than 20 words and each word does
not contain more than 30 characters. (End of file loop) Constraints
(End of file loop)
Output Format For each name, you must print “Correct” if the name is approved, otherwise you must print “No”. Sample Input 0
Robert Baratheon
Daenerys Targaryen
Cersei lannister
Theon Greyjoy
Brienne of Tarth
Arya Stark
Melisandre
Gray worm
Jon snow
Daario NaHaris
CODE -
# Open file for reading
file = open("names.txt")
# Read names in the file line by line
for name in file:
# Count no. of words in the name
noOfWords = len(name.split())
# Initialize the no. of uppercase characters in the name with 0
noOfUpper = 0
# Loop to count no. of uppercase characters in the name
for letter in name:
if letter >= 'A' and letter <= 'Z':
noOfUpper += 1
# Print "Correct" if no of capital letters in name is equal to no of words
if noOfUpper == noOfWords:
print("Correct")
# Print "No" if no of capital letters in name is not equal to no of words
else:
print("No")
SCREENSHOTS -
INPUT TEXT FILE -
CODE WITH OUTPUT -
If you have any doubt regarding the solution, then do
comment.
Do upvote.