Pattern Matching Algorithm In Python Step