好的,我用 python 寫了這個然后重寫,因為我不喜歡 if else 陳述句。第一個版本完美運行。我的第二個失敗了,最終比我的第一個版本占用了更多的行數。我的問題是,我只是愚蠢嗎?有沒有更好的方法來做到這一點,還是我應該接受 if else 宣告的需要?
對不起代碼轉儲,我第一次嘗試要瘋了
#number we are starting with
num = 1
#what we are going to apply equation loop to
result = num
#how many times goes through equation loop before reaching 1
count = 0
# list of numbers used in equation loop
num_in_loop = [result]
#end equation loop function.
running = True
# equation loop
def eqation_loop(running, num_in_loop, count, num, result):
while running == True:
if (result % 2) == 0:
result = result /2
count =1
num_in_loop.append(result)
elif result == 1.0:
print(num, "took", count ," loops to get to 1: numbers in loop = ", num_in_loop, file=open('3x 1/result.txt','a'))
num =1
print(num)
result = num
num_in_loop = [result]
count = 0
elif num == 100:
running = False
elif (result % 2) != 0:
result = result * 3 1
count =1
num_in_loop.append(result)
eqation_loop(running, num_in_loop, count, num, result)
第二次嘗試:
#number we are starting with
num = 1
#what we are going to apply equation loop to
result = num
#how many times goes through equation loop before reaching 1
count = 0
# list of numbers used in equation loop
num_in_loop = [result]
limit = int(input("range you want to try: " ))
def update_var(num_in_loop,result,count):
count =1
num_in_loop.append(result)
return equation_loop(limit,num, result)
def reset_var(num_in_loop, count, limit,num, result):
print(num, "took", count ," loops to get to 1: numbers in loop = ", num_in_loop, file=open('3x 1/test.txt','a'))
num =1
result = num
num_in_loop = [result]
count = 0
return equation_loop(limit,num, result)
def equation_loop(limit,num, result):
if num == limit:
return
elif result == 1:
return reset_var(num_in_loop, count, limit,num, result)
elif (result % 2) == 0:
result = result /2
return update_var(num_in_loop,result,count)
elif (result % 2) != 0:
result = result *3 1
return update_var(num_in_loop,result,count)
equation_loop(limit,num, result)
uj5u.com熱心網友回復:
你不能在沒有任何if/else陳述的情況下寫這個(好吧,如果你真的知道你在做什么,你可以通過嚴重濫用while,但你不應該),但這里有一個簡化的版本,希望包含一些有用的例子使您的代碼更易于撰寫(和閱讀!):
def equation_loop(num: int) -> list[int]:
"""
Repeatedly applies the magic equation trying to
reach 1.0, starting with num. Returns all the results.
"""
nums = [num]
while True:
if num == 1:
return nums
if num % 2:
num = num * 3 1
else:
num = num // 2
nums.append(num)
for num in range(1, 100):
results = equation_loop(num)
print(f"{num} took {len(results)} loops to get to 1: {results}")
這里的關鍵是您不需要這么多變數!單個回圈只需要它的起點 ( num) 并且只需要回傳結果串列(您可以從中獲得count,因為它將是串列的長度)。減少冗余變數的數量消除了許多不必要的代碼行,它們只是來回復制狀態。傳遞一個 like 的值running = True是不必要的,因為它會一直持續True到結束回圈的時候——而不是在你完成時使用while True:andreturn或。break
最大的收獲是,如果您有兩個始終具有相同值的變數(或者甚至兩個始終以完全相同的方式相關的值,例如串列及其長度),您可能只需要其中一個。
您還可以通過分離兩個嵌套回圈來簡化代碼——對于一個給定的回圈,num您想要回圈直到數字達到 1,所以這是一個回圈。您還想遍歷所有nums 直到 99(我花了一段時間才弄清楚代碼在做什么;我必須運行它并查看輸出以查看其中一些額外的狀態用于在單個回圈中實作嵌套回圈)。在兩個不同的回圈中執行這些操作很容易,您可以將其中一個放在一個漂亮的簡潔函式中(我使用了您的equation_loop名字,盡管它比您的原始版本做的作業更少),它需要開始num并回傳串列該起點的結果。然后可以以更簡單的方式呼叫這個簡單的函式for回圈遍歷nums 并列印每個結果。
請注意,我int使用 int 除法 ( //) 將所有內容都保留為 s —— 測驗浮點數是否完全相等通常很危險,因為浮點數并不完全精確!您的方程式始終使用整數值(因為如果它是偶數,您只能除以 2),因此使用 int 除法更有意義,甚至不用擔心浮點值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/454872.html
