Aryan PrajapatKnowledge Contributor
Write a one-liner in python that will count the number of capital letters in a file. Your code should work even if the file is too big to fit in memory.
Write a one-liner in python that will count the number of capital letters in a file. Your code should work even if the file is too big to fit in memory.
Let us first write a multiple line solution and then convert it to one-liner code.
with open(SOME_LARGE_FILE) as fh:
count = 0
text = fh.read()
for character in text:
if character.isupper():
count += 1
We will now try to transform this into a single line.
count sum(1 for line in fh for character in line if character.isupper())