I created this for no reason other than to see it work. For now, it steams along to a certain point and isn't too flexibile with words containing the same character more than twice. It also breaks if there is more than one set of repeating characters. I also limited the possible amount of generated words to 100,000 because it can go on forever provided you input a word larger than 9 characters.
There are many commented out print statements -- an insight to my debugging process. Once I finalize the code, I'll remove all of those.
I want to build this out with the additional option of checking against a word-dictionary to save only actual words to the results -- all customizable of course. Definitely a work in progress.
import random
import sys
import collections
saved_words=[]def random_word_gen(word):
repeats = collections.Counter(word)
repeated_character=''
for c in repeats:
if repeats[c] > 1:
repeated_character=c
print('Repeated character: '+repeated_character)
break
x=0
wordlist=[]
for item in word:
wordlist.append(item)
print('Letters in word: '+str(wordlist))
while x != 100000:
new_word=''
while len(new_word) != len(word):
letter = random.choice(wordlist)
#print(letter)
if letter == repeated_character:
try:
test = collections.Counter(new_word)
count=0
for c2 in test:
if test[c2] > 1:
count=1
break
if count != 1:
new_word+=letterexcept Exception as e:
print(e)
if letter != repeated_character:
#print('does not equal character')
if letter in new_word:
pass
else:
new_word+=letter
#print(new_word)
#print('finished')
if new_word not in saved_words:
saved_words.append(new_word)
print(new_word)
else:
#print('')
pass
x+=1
with open('words_generated.txt', 'w') as f:
f.write(str(saved_words))if __name__=='__main__':
a = input("enter a single word"+'\n')
#print('\n')
random_word_gen(a)