Python Forum
Thread Rating:
  • 2 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Word generator
#10
(Jul-03-2017, 03:07 AM)tozqo Wrote: DeaD_EyE, what's wrong with your sample code? Is it because you are opening a word list instead of generating a word?

My code was the wrong answer and is not readable for beginners.

from functools import partial


def with_vowel(word, word_len):
   # hint: strip newline when comparing the size
   return len(word.strip()) == word_len and any(map(lambda vowel: vowel in word, 'aeiou'))


# trying to do it memory efficient and readable
def filter_text(infilename, outfilename, condition):
   with open(infilename) as infile, open(outfilename, 'w') as outfile:
       for word in infile:
           # for loop iterates over lines if the file was opened in text mode
           # word has a newline character inside
           if condition(word):
               outfile.write(word)


with_vowel_len4 = partial(with_vowel, word_len=4)
with_vowel_len5 = partial(with_vowel, word_len=5)
filter_text('wordlist.txt', 'filtered_words.txt', with_vowel_len4)
filter_text('wordlist.txt', 'filtered_words_len5.txt', with_vowel_len5)
But this was not the task. Additionally there are modules in pip for password generation if needed.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
Word generator - by jack1234 - Jul-02-2017, 02:58 PM
RE: Word generator - by buran - Jul-02-2017, 03:05 PM
RE: Word generator - by jack1234 - Jul-02-2017, 04:01 PM
RE: Word generator - by DeaD_EyE - Jul-02-2017, 04:40 PM
RE: Word generator - by Bass - Jul-02-2017, 04:44 PM
RE: Word generator - by buran - Jul-02-2017, 06:34 PM
RE: Word generator - by ichabod801 - Jul-02-2017, 08:48 PM
RE: Word generator - by DeaD_EyE - Jul-02-2017, 11:14 PM
RE: Word generator - by tozqo - Jul-03-2017, 03:07 AM
RE: Word generator - by DeaD_EyE - Jul-03-2017, 07:16 AM
RE: Word generator - by snippsat - Jul-03-2017, 09:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Random Generator: From Word to Numbers, from Numbers to n possibles Words Yamiyozx 2 1,463 Jan-02-2023, 05:08 PM
Last Post: deanhystad
  No output in 'english word generator' DeGerlash 14 6,591 Jan-10-2018, 12:23 PM
Last Post: squenson

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020