Dieses Programm ist eindeutig zu langsam; hat jemand eine zündende Beschleunigungsidee?
Code: Alles auswählen
#!/usr/bin/env python
# coding: utf-8 -*-
#-------------------------------------------------------------------------------
# Name: scrabble.py
# Purpose: create german words with 7 example letters
#
# Author: Thomas Graeber
#
# Created: 12/28/2020
# License: n/a
#-------------------------------------------------------------------------------
"""
create german words with 7 example letters
and print them out for use in the board game "Scrabble"
contains functions:
convert - converts a list of letters to a word
calc_permutations - calculates all possible orders of the letters
and thus create possible words
"""
from itertools import chain, permutations
def convert(s):
"""
converts a list of letters to a word
In:
s - word-list
Out:
word
"""
word = ""
for x in s:
word += x
return word
def calc_permutations(example_letters):
"""
calculates all possible orders of the letters
and thus create possible words
In:
example_letters
Out:
all_permutations
"""
all_permutations = list(
chain.from_iterable(
permutations(example_letters, length)
for length in range(1, len(example_letters) + 1)
)
)
return all_permutations
def main():
all_permutations = calc_permutations(['a', 'e', 'b', 'k', 'l', 'm', 'e'])
# initialize list to avoid double entries
results = []
# initialize counter to count the results
counter = 0
for item in all_permutations:
possible_word = convert(item)
if possible_word[0] == 'a' or possible_word[0] == 'A':
wordlist = open('wordbooks/german_a.dic', 'r')
elif possible_word[0] == 'b' or possible_word[0] == 'B':
wordlist = open('wordbooks/german_b.dic', 'r')
elif possible_word[0] == 'c' or possible_word[0] == 'C':
wordlist = open('wordbooks/german_c.dic', 'r')
elif possible_word[0] == 'd' or possible_word[0] == 'D':
wordlist = open('wordbooks/german_d.dic', 'r')
elif possible_word[0] == 'e' or possible_word[0] == 'E':
wordlist = open('wordbooks/german_e.dic', 'r')
elif possible_word[0] == 'f' or possible_word[0] == 'F':
wordlist = open('wordbooks/german_f.dic', 'r')
elif possible_word[0] == 'g' or possible_word[0] == 'G':
wordlist = open('wordbooks/german_g.dic', 'r')
elif possible_word[0] == 'h' or possible_word[0] == 'H':
wordlist = open('wordbooks/german_h.dic', 'r')
elif possible_word[0] == 'i' or possible_word[0] == 'I':
wordlist = open('wordbooks/german_i.dic', 'r')
elif possible_word[0] == 'j' or possible_word[0] == 'J':
wordlist = open('wordbooks/german_j.dic', 'r')
elif possible_word[0] == 'k' or possible_word[0] == 'K':
wordlist = open('wordbooks/german_k.dic', 'r')
elif possible_word[0] == 'l' or possible_word[0] == 'L':
wordlist = open('wordbooks/german_l.dic', 'r')
elif possible_word[0] == 'm' or possible_word[0] == 'M':
wordlist = open('wordbooks/german_m.dic', 'r')
elif possible_word[0] == 'n' or possible_word[0] == 'N':
wordlist = open('wordbooks/german_n.dic', 'r')
elif possible_word[0] == 'o' or possible_word[0] == 'O':
wordlist = open('wordbooks/german_o.dic', 'r')
elif possible_word[0] == 'p' or possible_word[0] == 'P':
wordlist = open('wordbooks/german_p.dic', 'r')
elif possible_word[0] == 'q' or possible_word[0] == 'Q':
wordlist = open('wordbooks/german_q.dic', 'r')
elif possible_word[0] == 'r' or possible_word[0] == 'R':
wordlist = open('wordbooks/german_r.dic', 'r')
elif possible_word[0] == 's' or possible_word[0] == 'S':
wordlist = open('wordbooks/german_s.dic', 'r')
elif possible_word[0] == 't' or possible_word[0] == 'T':
wordlist = open('wordbooks/german_t.dic', 'r')
elif possible_word[0] == 'u' or possible_word[0] == 'U':
wordlist = open('wordbooks/german_u.dic', 'r')
elif possible_word[0] == 'v' or possible_word[0] == 'V':
wordlist = open('wordbooks/german_v.dic', 'r')
elif possible_word[0] == 'w' or possible_word[0] == 'W':
wordlist = open('wordbooks/german_w.dic', 'r')
elif possible_word[0] == 'x' or possible_word[0] == 'X':
wordlist = open('wordbooks/german_x.dic', 'r')
elif possible_word[0] == 'y' or possible_word[0] == 'Y':
wordlist = open('wordbooks/german_y.dic', 'r')
elif possible_word[0] == 'z' or possible_word[0] == 'Z':
wordlist = open('wordbooks/german_z.dic', 'r')
elif possible_word[0] == 'ä' or possible_word[0] == 'Ä':
wordlist = open('wordbooks/german_ae.dic', 'r')
elif possible_word[0] == 'ö' or possible_word[0] == 'Ö':
wordlist = open('wordbooks/german_oe.dic', 'r')
elif possible_word[0] == 'ü' or possible_word[0] == 'Ü':
wordlist = open('wordbooks/german_ue.dic', 'r')
for zeile in wordlist:
# [:-1] erases the line feed
if zeile[:-1] == possible_word or zeile[:-1] == possible_word.capitalize():
# avoid double entries
if zeile not in results:
counter += 1
print(str(counter) + ": " + zeile)
results.append(zeile)
wordlist.close()
if __name__ == "__main__":
main()