0

I am making a multiword anagram generator, and for this I need to insert a number of spaces in random positions throughout a given string. The inserted spaces must also not be adjacent to one another, nor can they be at the end or beginning of the string. If someone could show me how to do this, I would very much appreciate it.

Here is my current code:

import enchant
import itertools
d = enchant.Dict("en_US")

while True:
    text = input('Enter text to be anagrammed: ')

    perms = set([''.join(p) for p in itertools.permutations(text.replace(' ', ''))])

    anagrams = []
    for i in perms:
       if d.check(i) == True:
            anagrams.append(i)

    print(anagrams)

For example, for the input 'fartshoes', if I wanted to insert two spaces in it, a possible output would be 'far tsho es'.

  • 1
    Example would be nice, also what did you try? – Ofer Sadan Jun 18 '18 at 21:15
  • 1
    What have you tried so far? If you show us your current code and what the problem is in it, we can help better. What does the expected input and output look like? It would be helpful to include an example. – Stuti Rastogi Jun 18 '18 at 21:16
  • I have added my code. So far it only works for single-word anagrams. There is no problem in it; I simply have no idea where to start for what I am trying to do. – Ryan Meier Jun 18 '18 at 21:20
  • How many spaces do you want inserted? Also, see https://stackoverflow.com/q/21439011/2564301 to split something in random-length chunks; then join with spaces – which, incidentally, neatly takes care of "not be adjacent to one another, nor can they be at the end or beginning of the string". – Jongware Jun 18 '18 at 21:22
  • Whatever the user specifies, but I can handle getting the user input. The number of spaces inserted would be the number of words the user wishes for there to be in the anagrams minus 1. If the user wants 2 words, for example, there would be 1 space inserted. – Ryan Meier Jun 18 '18 at 21:25

1 Answers1

1

Divide and conquer:

Step 1: Generate the set of indices where spaces will be inserted. These indices range from 1 to k-1 where k is the length of the input string. When you pick an index then you must remove it and also its adjacent indices from the setof possible indices.

Step 2: Build the final string by insertion of a space into the selected positions.

There is no need to generate all possible combinations or use any combinatoric methods.

import random

s = "weholdthesetruthstobeself-evident"
n = 3

possible_spaces = set(range(1, len(s)))
spaces = set()

while len(spaces) < n and possible_spaces:
    space = random.choice(list(possible_spaces))
    spaces.add(space)
    for x in (space-1, space, space+1):
        possible_spaces.discard(x)

output = ''.join((" "+c if n in spaces else c) for n, c in enumerate(s))

print(output)

Output from successive runs:

we holdthesetru thstobeself-ev ident
weh oldthesetruthstobe self-e vident
w eholdthe setruthstobe self-evident
weholdthese tru thstobeself -evident
weholdthesetruth stobe se lf-evident
weho ld thesetruthstobe self-evident
Paul Cornelius
  • 9,245
  • 1
  • 15
  • 24