I am currently doing a python challenge in which I have to create certain codes. I am stuck on a particularly hard question and would appreciate some help. The problem is:

In 1989, a journalist asked Paul Boutin, an engineer at the Massachusetts Institue of Technology, "What do you think will be the biggest problem in computing in the 90s?" Paul replied: "There are only 17,000 three-letter acronyms."
To be precise, there are 263 = 17,576.
Write a program which takes in a TLA (a three letter abbreviation of the first letters of three words) and a line of text, and prints out all possible solutions for that TLA, all in lowercase.

An example is:

Abbreviation: TLA
Text: I really think lots about python . The long afternoons I've spent tinkering with concepts and truly lovely code have been well spent .
think lots about
the long afternoons

So far I have the code:

a = raw_input("Abbreviation: ")
a = a.lower()
b = raw_input("Text: ")
b = b.lower()
b = b.split()
c = 0
while c < len(b):
    d = c + 1
    e = d + 1
    if b[c].startswith(a[0]):
        if b[d].startswith(a[1]):
            if b[e].startswith(a[2]):
                f = b[c] + " " + b[d] + " " + b[e]
                print f
    c = c + 1

But when I enter TLAs where the first letter is the same as the third letter, and index error comes up on line 12. I don't understand where I am going wrong and desparately need the solution. Can anyone help me?

I think this way is clearer, and it is not limited then to three letter abbreviations:

abbreviation = raw_input("Abbreviation: ").lower()
abbreviation_length = len(abbreviation)
text = raw_input("Text: ").lower().split()
for position in range(len(text) - abbreviation_length + 1):
    if all(text[position + ind].startswith(abbreviation[ind]) for ind in range(abbreviation_length)):
        print(' '.join(text[position:position+abbreviation_length]))

Edited 4 Years Ago by pyTony

This question has already been answered. Start a new discussion instead.