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

Here, we will figure out how to discover an arrangement in an array comprising of characters utilizing python program?

In this article, we would learn climate our array contains a referenced arrangement dislike a, b, c or 1, 2, 3 and so on.

Starting with a straightforward inquiry,

Question:

We are given with an array of char, return True if the succession of burn a, b, c shows up in the array someplace.

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

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

Clarification:

Here one can undoubtedly be confounded in the second line as we have taken range(len(char)- 2), in practically all inquiries we use – 1, yet here we have utilized – 2.

This can be clarified in light of the fact that with length-2, we can utilize i+1 and i+2 tuned in. As we need to discover a succession for three numbers

Further, the code is extremely basic as we need to compose an if articulation to check the three conditions and if all the three conditions fulfilled return True.

Leave a Comment