0

I am trying to extend the code from the top answer here:

Find all the possible N-length anagrams - fast alternatives

I need to do the same thing, but to yield all sub-anagrams (without repetition).

Most questions on this subject treat only input strings like "ABCD" where the characters are all unique.

EXAMPLE

Input = "abb"

Expected output is an iterator that yields from

["a", "b", "ab", "ba", "bb", "abb", "bab", "bba"]

So, for instance, "bba" isn't yielded twice as a result of there being two "b" occurences in the input string.

The following code does what I want:

from itertools import permutations, chain
from typing import Iterator

def shuffler(word: str) -> Iterator[str]:
    yield from set(chain(
        *map(
            lambda x:
            permutations(word, x), range(1, len(word) + 1)
        )
    ))

This code first creates a set, however. As I will be using this function many thousands of times, the code will be slower than if a "pure" iterator were returned from the function.

Frank
  • 223
  • 3
  • 11
  • 2
    Not sure what your question is as you have a working solution. – DisappointedByUnaccountableMod Dec 23 '20 at 18:50
  • 1
    @barny the solution is phony as it first creates a set and yields from it. so the "iterator" is fake. I appreciate I didn't make this clear. – Frank Dec 23 '20 at 19:32
  • 1
    Still don’t understand the problem - does it produce the output you need? The set ensures results are unique, isn’t that what you want? Edit your question so it’s clear what the exact problem is with your code - i.e. why the output isn’t what you need. – DisappointedByUnaccountableMod Dec 23 '20 at 19:34
  • 1
    @barny Thanks for your feedback. I've edited the question. My main concern is performance. I could be wrong, but I suspect that, in creating a set, and then "yielding" from it, the resulting code (when calling the function many times) will be slower than if a proper iterator were returned from the function. I've not had to worry about optimization before so I may be misunderstanding something. – Frank Dec 23 '20 at 20:12
  • 1
    Hmm, premature optimisation is the root of a lot of software development problems. Basically, unless you know your code *is* too slow then unnecessarily optimising it ‘just in case’ is in fact a waste of your valuable time which could be spent more productively doing pretty much anything else. – DisappointedByUnaccountableMod Dec 23 '20 at 20:17

0 Answers0