I would like to know how to improve the execution speed of the code below.
The context : n is an integer read from the stdin, and represents the number of cases. Each case is represented by a sequence of characters (can only be uppercase alphabet letters), that are read from stdin. For each case, if the sequence doesn't contain the sub-string "CD", we add 1 to the variable wins
.
Finally, we print out wins
: the number of strings not containing the string "CD".
n = int(raw_input()) #number of cases
wins = 0
for i in range(n):
if "CD" not in raw_input(): #count number of strings that doesn't contain "CD"
wins += 1
print wins
Note: Official Python performance tips suggest using map
or list comprehension instead of for
loop to speed up, but I couldn't implement those without the for
loop.
What other ways to speed the execution speed, knowing that every thing read from the stdin is necessary, and that the typing speed (I use python filename.py <input.txt
method) isn't an issue (I'm looking for a way pertaining to the code structure, to speed the execution itself) ?
python file.py < something or other
that you deleted from the comments on the last question? \$\endgroup\$ – Peilonrayz Sep 29 '17 at 20:19