r/programmingchallenges • • Oct 11 '18

Random word generator program

I'm trying to make a big coded message for a D&D campaign. Basically I want to be able to input a string of letters and return a random word for each letter. Just wondering if there are any programs like that out there, if not, where can I find some sort of library with a list of all/ a large number of English words?

Didn't know where else to post this

4 Upvotes

4 comments sorted by

View all comments

1

u/[deleted] Oct 12 '18

my python 3 solution inspired by u/lgastako 's.

import itertools as it
from operator import itemgetter
import random
import sys


def encrypt(word, words):
    return [random.choice(words[letter]) for letter in word]


def gen_dict():
    with open("/usr/share/dict/words") as f:
        words = {k: list(v) for k, v in it.groupby(map(str.strip, f), itemgetter(0))}
    return words


def main():
    words = gen_dict()
    for word in sys.argv[1:]:
        print(" ".join(encrypt(word, words)))


if __name__ == "__main__":
    main()