0 votes
in Python Flask by
Can you write an efficient code to count the number of capital letters in a file?

1 Answer

0 votes
by
The normal solution for this problem statement would be as follows:

with open(SOME_LARGE_FILE) as countletter:

count = 0

text = countletter.read()

for character in text:

if character.isupper():

count += 1

To make this code more efficient, the whole code block can be converted into a one-liner code using the feature called generator expression. With this, the equivalent code line of the above code block would be as follows:

count sum(1 for line in countletter for character in line if character.isupper())

Related questions

0 votes
asked Aug 22, 2022 in Python by Robindeniel
0 votes
asked Sep 25, 2021 in Python by john ganales
...