我发现我的 python 代码真的很慢,使用 8 分钟完成,而 freebasic 需要 1 分钟,
任何人都可以建议一些优化
首先创建 digit_array["a","b","c","d","e","f","g","h","i","j"]。
除以,得到余数,一次又一次,将其存储到 array_index[] 中,1 作为最后一位。
else if 和 else 代码来处理未完成除法的最后一次迭代。
将最大索引存储为变量 i。
然后将array_index从最大值循环到1,将其映射到digit_array,
然后生成一个字符串(代码注释)
补充说:20120903
我这样做是为了用 digit_array 中的字母组合创建字符串,所以我可以跳过一些不常见的字母,如 'q' 'z' 或一些数字如 '4'
currpos 可以让我从上次开始
x 让我调整总数digit_array 中的字母,可以是 a-z+A-Z+0-9+' '
currpos_end 1024th 会给出,
如果 digit_array 是 ['0','1','2','3','4 ','5','6','7','8','9'] , x = 10
如果 digit_array 是 ['a','b','c','d',' e','f','g','h','i','j'] , x = 10
如果 digit_array 是 ['0','1','2','3', 则结果 1467, '4','5','6','7','8'] , x = 9
如果 digit_array 为 ['0','1','2','3','4','5','6', 则结果为 2000, '7'] , x = 8
现在我可以打印结果了,只是性能下降
python 3代码,
digit_array=[]
for i in range(0, 10): # range(10) ----> 0 1 2 3 4 ... 9
digit_array.append("")
digit_array[0] = "a"
digit_array[1] = "b"
digit_array[2] = "c"
digit_array[3] = "d"
digit_array[4] = "e"
digit_array[5] = "f"
digit_array[6] = "g"
digit_array[7] = "h"
digit_array[8] = "i"
digit_array[9] = "j"
array_index=[]
for i in range(0, 51):
array_index.append(0)
currpos = 0
currpos_end = pow((26+10+1),5)
#currpos_end = 1024
print (currpos)
currpos2 = currpos
x = 10 # ubound digit_array + 1, ("0","1") binary then x = 2
print ()
for currpos in range(currpos, currpos_end):
currpos2 = currpos
i = 1
while True :
if currpos2 > x:
array_index[i] = currpos2 % x
currpos2 = int(currpos2 / x)
elif currpos2 == x:
array_index[i] = 0
i += 1
array_index[i] = 1
break
else:
array_index[i] = currpos2
break
i += 1
print (i, " . ", currpos, currpos2)
for j in range(i , 1, -1):
# break #uncomment this and comment next 3 lines for speed testing
print (digit_array[array_index[j]], end='')
print (digit_array[array_index[1]], end='')
print ()
try:
input(".")
except:
pass
freebasic代码,
Dim digit_array(0 To 100) As String
digit_array(0) = "a"
digit_array(1) = "b"
digit_array(2) = "c"
digit_array(3) = "d"
digit_array(4) = "e"
digit_array(5) = "f"
digit_array(6) = "g"
digit_array(7) = "h"
digit_array(8) = "i"
digit_array(9) = "j"
Dim array_index(0 To 50) As ULongInt
For i As Integer = 0 To 50
array_index(i) = 0
Next
Dim As ULongInt currpos
Dim As ULongInt currpos_end
Dim As ULongInt currpos2
Dim As ULongInt x = 10 'ubound digit_array + 1, ("0","1") binary then x = 2
Dim As ULongInt i
currpos = 0
currpos_end = (26+10+1)^5
'currpos_end = 1024
Print currpos
currpos2 = currpos
Print
For currpos = currpos To currpos_end
currpos2 = currpos
i = 1
Do
If currpos2 > x Then
array_index(i) = currpos2 Mod x
currpos2 = CULngInt(currpos2 / x)
ElseIf currpos2 = x Then
array_index(i) = 0
i += 1
array_index(i) = 1
Exit Do
Else
array_index(i) = currpos2
Exit Do
EndIf
i += 1
Loop
'Print i; " . "; currpos; " "; currpos2
'''first = i Until 1 = last
For j As Integer = i To 1 Step -1
'Print digit_array(array_index(j));
Next
'Print
'Sleep
Next
Print "."
Sleep