Listing 12-04: Program modified to recognize sequences

##############################################################################
# Python From Scratch
# Autor: Nilo Ney Coutinho Menezes
# Editora Novatec (c) 2010-2024
# Site: https://pythonfromscratch.com
#
# File: listing\chapter 12\12.04 - Program modified to recognize sequences.py
# Description: Program modified to recognize sequences
##############################################################################

sentence = "Buy it for $50.72. Call (92) 5431-2201 before 10/12/2033."
def number(entry, qmin, qmax):
    num = 0
    for character in entry:
        if character.isnumeric():
            num += 1
        else:
            break
    if qmin <= num <= qmax:
        return num, 0, num - 1
    else:
        return -1, -1, -1
def sequence(entry, pattern):
    position, maximum_position = 0, len(pattern)
    for character in entry:
        if character == pattern[position]:
            position += 1  # Same characters, test the next character
        else:
            break  # Exited the sequence
        if position == maximum_position:  # Found the entire sequence
            return 1, 0, position - 1
    return -1, -1, -1
def ldc(entry):
    found, _, _ = sequence(entry[0], "(")
    if found > 0:
        found, _, n_end = number(entry[1:], 2, 3)
        if found > 0:
            found, _, _ = sequence(entry[n_end + 2], ")")
            if found > 0:
                return 1, 0, found + 2
    return -1, -1, -1
for position in range(len(sequence)):
    found, start, end = ldc(sequence[position:])
    if found > 0:
        print(f"LDC found in positions: {position+start} to {position+end}")
        print(sequence[position + start: position + end + 1])
Click here to download the file