1

I am trying to print "YES" or "NO" (quotes for clarity) depending on whether any permutation of the pattern exists in the text string.

from itertools import permutations


i = int(input())

for j in range(i):
    m = input()
    n = input()

    lst = []
    for k in permutations(m):
        lst.append("".join(k))

    for l in lst:
        if l in n:
            print("YES")
            break
        else:
            print("NO")
            break

    print(lst)

This is the sample input:

3
hack
indiahacks
code
eddy
coder
iamredoc

This is the expected output

YES
NO
YES

For first two inputs, output is coming correctly and the third output is the one which fails.

/home/ajay/.pyenv/versions/3.4.1/bin/python3.4 /home/ajay/PycharmProjects/LearnPython/test.py
1
coder
iamredoc
NO
['coder', 'codre', 'coedr', 'coerd', 'corde', 'cored', 'cdoer', 'cdore', 'cdeor', 'cdero', 'cdroe', 'cdreo', 'ceodr', 'ceord', 'cedor', 'cedro', 'cerod', 'cerdo', 'crode', 'croed', 'crdoe', 'crdeo', 'creod', 'credo', 'ocder', 'ocdre', 'ocedr', 'ocerd', 'ocrde', 'ocred', 'odcer', 'odcre', 'odecr', 'oderc', 'odrce', 'odrec', 'oecdr', 'oecrd', 'oedcr', 'oedrc', 'oercd', 'oerdc', 'orcde', 'orced', 'ordce', 'ordec', 'orecd', 'oredc', 'dcoer', 'dcore', 'dceor', 'dcero', 'dcroe', 'dcreo', 'docer', 'docre', 'doecr', 'doerc', 'dorce', 'dorec', 'decor', 'decro', 'deocr', 'deorc', 'derco', 'deroc', 'drcoe', 'drceo', 'droce', 'droec', 'dreco', 'dreoc', 'ecodr', 'ecord', 'ecdor', 'ecdro', 'ecrod', 'ecrdo', 'eocdr', 'eocrd', 'eodcr', 'eodrc', 'eorcd', 'eordc', 'edcor', 'edcro', 'edocr', 'edorc', 'edrco', 'edroc', 'ercod', 'ercdo', 'erocd', 'erodc', 'erdco', 'erdoc', 'rcode', 'rcoed', 'rcdoe', 'rcdeo', 'rceod', 'rcedo', 'rocde', 'roced', 'rodce', 'rodec', 'roecd', 'roedc', 'rdcoe', 'rdceo', 'rdoce', 'rdoec', 'rdeco', 'rdeoc', 'recod', 'recdo', 'reocd', 'reodc', 'redco', 'redoc']

Process finished with exit code 0

You can see that redoc is present in the list of permutations but why the python in keyword is not recognizing it and thus producing NO output.

1
  • So if my answer helps, you'll add 2 points to your rep by accepting it. By the way, I did plus one this one for being interesting. Commented Sep 15, 2014 at 13:05

1 Answer 1

4

You're breaking out of the loop over your list immediately after checking the first item in the list.

instead of:

for l in lst:
    if l in n:
        print("YES")
        break
    else...

If you must use a loop structure, you can rehabilitate your code with:

for l in lst:
    if l in n:
        print("YES")
        break
else: # this will run after (and only if) exhausting your loop.
    print("NO")

Raymond Hettinger thought this usage of else should be called nobreak and I think that's good intuition for it.


This is a usage of the else clause on loops, and is documented here.

Sign up to request clarification or add additional context in comments.

4 Comments

It works thanks :) Can you explain why this particular else out of the loop works. i m not getting it exactly.
It's control flow that only runs if the loop ends without breaking. That means you didn't find the substring, so you want NO printed there.
The time limit may exceed in this case. what if we want to do it within 2 sec.
Nothing in the question about a time limit, but if you want to guard against that, you'll have to build it into your control flow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.