Q:

Python program to find a series in an array consisting of characters

belongs to collection: Python Array Programs

0

We are given with an array of char, return True if the sequence of char a, b, c appears in the array somewhere.

Example:

    Array_abc(['a', 'x', 'a', 'b', 'c']) = True
    Array_abc(['f', 'x', 'a', 'i', 'c', 't']) = True
    Array_abc(['k', 'x', 'a', 'e', 'c']) = True

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Code:

def Array_abc(char):
    for i in range(len(char) - 2):
        if char[i] == 'a' and char[i + 1] == 'b' and char[i + 2] == 'c':
            return True
    return False


print (Array_abc(['a', 'x', 'a', 'b', 'c']))

Output

True

Explanation:

Here one can easily be confused in the second line as we have taken range(len(char)-2), in almost all questions we use -1, but here we have used -2. This can be explained because with length-2, we can use i+1 and i+2 in the loop. As we have to find a sequence for three numbers.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python program to find the occurrence of a particu... >>
<< Python program to find the sum of all elements of ...