ランダムな名前を作るためにランダムな単語を文字で作成するこのプログラムを作ったが、このプログラムは非常に遅く実行される。文字のランダムな文字列を生成し、それらが有効な名前であるかどうかを確認して、ユーザーが指定した名前と一致するかどうかを確認します。ランダムな文字の生成によるランダムな名前の生成が遅すぎる
私はCythonを試しましたが、CythonはPython 2.xしかサポートしていません。私はPython 3.xを使用します。ここで
は、コードは次のとおりです。
import sys
from random import randint
from datetime import datetime
lc_alphabet = "bcdfghjklmnpqrstvwxyz"
uc_alphabet = "BCDFGHJKLMNPQRSTVWXYZ"
lc_vocal = "aeiou"
uc_vocal = "AEIOU"
univ_list = []
verbose = False
def generator():
name_words = randint(4, 8)
finished_name = ""
for i in range(name_words):
io = randint(0, 3)
lc_alp_rand = randint(0, 20)
lc_vcl_rand = randint(0, 4)
lc_alp_i = lc_alphabet[lc_alp_rand]
lc_vcl_i = lc_vocal[lc_vcl_rand]
if io == 0 or io == 1:
finished_name += lc_alp_i
if io == 2 or io == 3:
finished_name += lc_vcl_i
return finished_name
def name_filtering():
while True:
this_name = generator()
VOCAL_WRONG = False
CONSONANT_WRONG = False
univ_wrong_point = 0
wrong_point = 0
for y in this_name:
if y in lc_vocal:
wrong_point += 1
if wrong_point >= len(this_name):
univ_wrong_point += 1
if verbose is True:
print("ALL VOCAL DETECTED " + this_name)
VOCAL_WRONG = True
wrong_point_2 = 0
for z in range(len(this_name)):
if this_name[z] in lc_alphabet:
wrong_point_2 += 1
if wrong_point_2 == len(this_name):
univ_wrong_point += 1
if verbose is True:
print("ALL CONSONANT DETECTED " + this_name)
CONSONANT_WRONG = True
for v in range(len(this_name)-1):
if this_name[v] in lc_vocal and this_name[v+1] in lc_vocal:
univ_wrong_point += 1
if verbose is True and VOCAL_WRONG is False:
print("VOCAL SIDE BY SIDE DETECTED " + this_name)
VOCAL_WRONG = True
if this_name[0] in lc_alphabet and this_name[1] in lc_alphabet and this_name[2] in lc_alphabet:
univ_wrong_point += 1
if verbose is True and CONSONANT_WRONG is False:
print("3 CONSONANT SIDE BY SIDE DETECTED " + this_name)
CONSONANT_WRONG = True
if this_name[1] in lc_alphabet and this_name[2] in lc_alphabet and this_name[3] in lc_alphabet:
univ_wrong_point += 1
if verbose is True and CONSONANT_WRONG is False:
print("3 CONSONANT SIDE BY SIDE DETECTED " + this_name)
CONSONANT_WRONG = True
if this_name[0] in lc_alphabet and this_name[1] in lc_alphabet and this_name[2] in lc_alphabet and \
this_name[3] in lc_alphabet:
univ_wrong_point += 1
if verbose is True and CONSONANT_WRONG is False:
print("4 CONSONANT SIDE BY SIDE DETECTED " + this_name)
CONSONANT_WRONG = True
if len(this_name) > 5:
if this_name[2] in lc_alphabet and this_name[3] in lc_alphabet and this_name[4] in lc_alphabet and \
this_name[5] in lc_alphabet:
univ_wrong_point += 1
if verbose is True and CONSONANT_WRONG is False:
print("4 CONSONANT SIDE BY SIDE DETECTED " + this_name)
CONSONANT_WRONG = True
if univ_wrong_point == 0:
return this_name
def search_name(txt):
disDate = str(datetime.now())
counter = 0
number = 0
while True:
name = name_filtering()
name_length = len(name)
std_space = 10
fin_space = std_space - name_length
the_space = " " * fin_space
if counter == 0:
print(str(number) + "| ", end="")
print(name + the_space, end="")
counter += 1
if counter == 10:
print()
counter = 0
number += 1
if name == txt:
print()
print()
print("Name " + txt + " FOUNDED on Number " + str(number))
print(disDate)
print(str(datetime.now()))
break
sys.stdout.flush()
def check_name(this_name):
univ_wrong_point = 0
wrong_point = 0
for y in this_name:
if y in lc_vocal:
wrong_point += 1
if wrong_point >= len(this_name):
univ_wrong_point += 1
if verbose is True:
print("ALL VOCAL DETECTED " + this_name)
wrong_point_2 = 0
for z in range(len(this_name)):
if this_name[z] in lc_alphabet:
wrong_point_2 += 1
if wrong_point_2 == len(this_name):
univ_wrong_point += 1
if verbose is True:
print("ALL CONSONANT DETECTED " + this_name)
for v in range(len(this_name) - 1):
if this_name[v] in lc_vocal and this_name[v + 1] in lc_vocal:
univ_wrong_point += 1
if verbose is True:
print("VOCAL SIDE BY SIDE DETECTED " + this_name)
if this_name[0] in lc_alphabet and this_name[1] in lc_alphabet and this_name[2] in lc_alphabet:
univ_wrong_point += 1
if verbose is True:
print("3 CONSONANT SIDE BY SIDE DETECTED " + this_name)
if this_name[1] in lc_alphabet and this_name[2] in lc_alphabet and this_name[3] in lc_alphabet:
univ_wrong_point += 1
if verbose is True:
print("3 CONSONANT SIDE BY SIDE DETECTED " + this_name)
if this_name[0] in lc_alphabet and this_name[1] in lc_alphabet and this_name[2] in lc_alphabet and \
this_name[3] in lc_alphabet:
univ_wrong_point += 1
if verbose is True:
print("4 CONSONANT SIDE BY SIDE DETECTED " + this_name)
if len(this_name) > 5:
if this_name[2] in lc_alphabet and this_name[3] in lc_alphabet and this_name[4] in lc_alphabet and \
this_name[5] in lc_alphabet:
univ_wrong_point += 1
if verbose is True:
print("4 CONSONANT SIDE BY SIDE DETECTED " + this_name)
if len(this_name) > 8:
print("TOO LONG (more than 8 letter)")
univ_wrong_point += 1
if len(this_name) < 4:
print("TOO FEW (fewer than 4 letter)")
univ_wrong_point += 1
if univ_wrong_point == 0:
print("this name match criteria")
else:
print("this name does not match criteria")
check_name(str(input("Check Name Criteria : ")))
search_name(str(input("Search Name in 4 words : ")))
私はこのコードを高速化することができる方法を見つけるために感謝することでしょう。私はname_filtering
がゆっくりと行っていると思う。
私はlc_alphabetはそう 'lc_alphabet = "bcdfghjklmnpqrstvwxyz" のような単なる変数で使用さ' – Wowotek
は 'set'のことを確認してください。少し改善することができ、それは簡単です。 –
@ Jean-FrançoisFabreそれは明らかにインデックス作成をサポートしていません。 – Wowotek