Listing 12-02: Recognizing LDC

##############################################################################
# Python From Scratch
# Autor: Nilo Ney Coutinho Menezes
# Editora Novatec (c) 2010-2024
# Site: https://pythonfromscratch.com
#
# File: listing\chapter 12\12.02 - Recognizing LDC.py
# Description: Recognizing LDC
##############################################################################

sentence = "Buy it for $50.72. Call (92) 5431-2201 now before 10/12/2033."
output = []
phone = []
def ldc(entry):
    state = 0
    ldc_code = []
    for position, character in enumerate(entry):
        if state == 0 and character == "(":
            state = 1
            ldc_code.append(character)
        elif state == 1 and character.isnumeric() and position <= 3:
            ldc_code.append(character)
        elif state == 1 and character == ")":
            state = 2
            ldc_code.append(character)
            return True, 0, position
        else:
            break
    return False, -1, -1
for position in range(len(sentence)):
    found, start, end = ldc(sentence[position:])
    if found:
        print(f"LDC found in positions: {position+start} to {position+end}")
        print(sentence[position + start: position + end + 1])
Click here to download the file