![](/img/trans.png)
[英]Code Executes As Python Script Correctly…but when converted into an exe file does not give appropriate dynamic output as expected
[英]Cantor set code does not give expected output
我在 cantor set 代碼中有一些我找不到的錯誤。 這個想法是給定的數字數組你遞歸地刪除中間元素的 1/3 並在兩側遞歸並重復直到某個閾值。
這是代碼
import math
full = list(range(80))
l = list(range(80))
threshold = 2
def printl():
for i in full:
if i in l:
print("*", end='')
else:
print(" ", end='')
print()
def cantor(lo, hi):
lowi = math.ceil( (hi-lo) * 1/3 + lo)
highi = int( (hi-lo) * 2/3 + lo)
if len(l[lo:hi]) < threshold:
return
printl()
del l[lowi:highi]
cantor(lo, lowi)
cantor(lowi+1, hi)
康托爾(0,len(l)-1)
我得到了非常奇怪的結果,不知道為什么......
一個棘手的問題——我計算了這些部分並將它們遞歸地壓縮到一個數組中,然后我將其轉儲到終端:
from math import log
def cantor(length, threshold):
if length >= threshold:
sequence = cantor(length // 3, threshold)
blanks = [" " * (length // 3)] * int(log(length, 3) + 1) # estimate and let zip toss extras
return ["*" * length] + [a + b + c for a, b, c in zip(sequence, blanks, sequence)]
return []
length = 81
threshold = 1
print(*cantor(length, threshold), sep='\n')
輸出
> python3 test.py
*********************************************************************************
*************************** ***************************
********* ********* ********* *********
*** *** *** *** *** *** *** ***
* * * * * * * * * * * * * * * *
>
我認為您應該將切片的處理順序從最后一個更改為第一個,因為遞歸到較低的索引將使先前遞歸對 lowi 和 hi 的位置無效:
cantor(lowi+1, hi)
cantor(lo, lowi)
cantor(0, len(l)-1)
您可能需要檢查下標中的范圍的結尾,因為范圍的結尾是獨占的。 即 l[2:7] 不包括索引 #7。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.