Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python problem: How to use for loop single time to iterate two array ? Here is m

ID: 3572499 • Letter: P

Question

Python problem: How to use for loop single time to iterate two array ?

Here is my problem:

This time, write a function countTriples(str1) that returns the number of times a letter appears 3 or more times in a given string (does not have to be in a consecutive order).

STIPULATION: Only use a single for loop to iterate through the input string. And do not use list.count() inside of a for loop as that is a double nested for loop. Avoid using the Counter() class as well, rely on your own coding ability without needing to import extra libraries. (Just for this particular quiz, in general, always use libraries to make your life easier).

ex. countTriples("abcabcabc") == 3

Please,explain, Thank You!

Explanation / Answer

Use below logic my_list=[] history="" history_count=0 my_str="abcabcabc" for letter in my_str: if letter in history: my_list.remove((history,history_count)) history=letter history_count+=1 else: history_count=0 history_count+=1 history=letter my_list.append((history,history_count)) print my_list