我正在使用 python 中的遞回。我有兩個函式 r(coord) 回傳以給定坐標為圓心的圓的半徑,而 inside(coord) 回傳直接在給定圓心的圓內的所有圓的中心集。
我想計算所有圓的表面,我想回傳最小圓的中心。我已經為表面撰寫了函式:
def surface(coord):
return sum([surface(x) for x in inside(coord)], r(coord) * r(coord) * pi)
現在我想用一次遞回回傳最小圓的中心。這是我寫的:
def smallest(coord):
return min(inside(coord))
但是這行代碼輸出的結果與我期望的不同。
Expected :(20, 10.625) <-- The one I get
Actual :(16.25, 12.5) <-- The result I want the smallest circle
我被困在這個功能上,我不太確定如何解決這個問題或解決方案是什么。
這是我的全部代碼
def r(coord):
return {(0, 10): 7.5,
(0, 6): 2.5,
(3.75, 12.5): 2.5,
(3.75, 11.25): 1.25,
(-3.75, 12.5): 2.5,
(-5, 12.5): 1.25,
(20, 10): 7.5, (20, 6): 2.5,
(23.75, 12.5): 2.5, (23.75, 11.25): 1.25,
(16.25, 12.5): 2.5, (15, 12.5): 1.25,
(20, 10.625): 0.625,
(-2.5, -8.75): 8.75,
(-8.125, -4.375): 0.625,
(-1.875, -9.375): 6.875,
(-1.25, -10): 5,
(-1.25, -9.25): 3.75,
(-0.625, -10.625): 1.875,
(21.25, -16.25): 11.25,
(21.25, -7.5): 2.5,
(22.5, -7.5): 1.25,
(25.625, -13.125): 3.125,
(26.875, -11.875): 0.625,
(26.25, -21.25): 3.75,
(16.25, -21.25): 3.75,
(16.25, -22.25): 2.5,
(16.125, -22.875): 0.875,
(16.25, -12.5): 3.75
}[coord]
def inside(coord):
inside1 = {(0, 10): [(0, 6), (3.75, 12.5), (-3.75, 12.5)],
(0, 6): [],
(3.75, 12.5): [(3.75, 11.25)],
(3.75, 11.25): [],
(-3.75, 12.5): [(-5, 12.5)],
(-5, 12.5): [],
(20, 10): [(20, 6), (23.75, 12.5), (16.25, 12.5), (20, 10.625)],
(20, 6): [],
(23.75, 12.5): [(23.75, 11.25)],
(23.75, 11.25): [],
(16.25, 12.5): [(15, 12.5)],
(15, 12.5): [],
(20, 10.625): [],
(-2.5, -8.75): [(-8.125, -4.375), (-1.875, -9.375)],
(-8.125, -4.375): [],
(-1.875, -9.375): [(-1.25, -10)],
(-1.25, -10): [(-1.25, -9.25)],
(-1.25, -9.25): [(-0.625, -10.625)],
(-0.625, -10.625): [],
(21.25, -16.25): [(21.25, -7.5), (25.625, -13.125),
(26.25, -21.25), (16.25, -21.25),
(16.25, -12.5)],
(21.25, -7.5): [(22.5, -7.5)],
(22.5, -7.5): [],
(25.625, -13.125): [(26.875, -11.875)],
(26.875, -11.875): [],
(26.25, -21.25): [],
(16.25, -21.25): [(16.25, -22.25)],
(16.25, -22.25): [(16.125, -22.875)],
(16.125, -22.875): [],
(16.25, -12.5): []}[coord]
random.shuffle(inside1)
return inside1
def surface(coord):
return sum([surface(x) for x in inside(coord)], r(coord) * r(coord) * pi)
def smallest(coord):
return min(inside(coord)) <-- wrong output
uj5u.com熱心網友回復:
return min([coord] [smallest(x) for x in inside(coord)], key=r)
我試過這個,它對我有用。
uj5u.com熱心網友回復:
我找到了解決我的問題的方法。它輸出了正確的坐標。它遍歷所有圓圈并找到最小的,然后將它們存盤坐標。這是代碼:
return min([coord] [smallest(x) for x in inside(coord)], key=r)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/398748.html
