0

I have been trying to make a program that runs over a dictionary which makes anagrams from an inputted string. I found most of the code to this problem here Algorithm to generate anagrams but i need to have the program only output a line of max amount of words instead of only using words of a set length (MIN_WORD_SIZE).

If the input was: python Anagram.py "Finding Anagrams" 3 dictionary.txt. The output would be "Gaming fans nadir" because it only uses 3 words at most to create the anagram. ?

Thomas Youngson
  • 181
  • 3
  • 14

2 Answers2

1

This is a slight work around, however if you only want to filter out words that have a length shorter than a specified argument, then you could make a change in your programs main.

Your program currently says:

for word in words.anagram(letters):
   print word

You could change your program to say:

for word in words.anagram(letters):
    if len(word.split()) <= int(argv[2]):
        print word

Not the most elegant answer but, I hope this helps!

oreid
  • 1,336
  • 2
  • 13
  • 25
-1

I don't like to make programs for programmers, usually. To the best of my understanding:

import random

word = "scramble"

scramble = list(word)

for i in range(len(scramble)):
    char = scramble.pop(i)
    scramble.insert(random.randint(0,len(word)),char)
print(''.join(scramble))

This will scramble the word variable.

Hope this helps!

Thanks :)

Coolq B
  • 344
  • 5
  • 10