I'm trying to write code in the simplest and cleanest way possible. I've found a few ways to shorten and simplify my code through functions that I've never seen before or through using other methods. I'd like to expand my knowledge on writing code using various (but simple) methods, and also expand my function 'vocabulary'.
What I mainly want to know is if there are other ways of writing the for loop such that it is much easier to read, and is shorter and simpler. Just yesterday I found out that the enumerate function exists, which made my life so much easier! :)
On to the question-
Substring Counter: (I thought of this function as a programming challenge for myself)
A function that takes in a string and returns the amount of times 'A' ends with 'Z'. MUST ONLY CONTAIN 1 LOOP. Not sure how to explain these, but here are examples:
It is assumed that the input is a string and that all characters are alphabetical and capital. I know I can put checks for all these but I'm mainly focusing on the logical way the code is written.
substring_counter('AZAAZ') > 4
'AZAAZ' has 4 instances where it starts with 'A' and ends with 'Z'.
substring_counter('AZZ') > 2
'AZZ' has 2 instances where it starts with 'A' and ends with 'Z'.
1st method (using slicing):
def substring_counter(string): counter = 0 for num, x in enumerate(string): if x == 'A': if string[num:].find('Z') != -1: counter += string[num:].count('Z') return counter
2nd method (using counters):
def substring_counter(x): counter=0 word = 0 for i in x: if(i == 'A'): word += 1 elif(i == 'Z'): counter += word return counter
Which method is more understandable, or simpler? Which one should I be using?