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

Here, we are going to learn how to find a series in an array consisting of characters using python program?
Submitted by Suryaveer Singh, on June 08, 2019

In this article, we would learn weather our array contains a mentioned series or not like a, b, c or 1, 2, 3 etc.

Beginning with a simple question,


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


    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


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']))




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.

Further, the code is very simple as we have to write an if statement to check the three conditions and if all the three conditions satisfied return True.

Python Array Programs »



Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.