迭代函数以找到起始值以在一定数量的步骤中完成序列
Iterative function to find start value to complete sequence in a certain number of steps
我目前正在尝试在 python 中编写一个函数,该函数采用两个值 goal
和 count
并迭代搜索该函数应从以下数量开始的值count
给出的步骤,直到找到等于或大于 goal
.
的数字
感谢 Sprizgola,这是我目前所拥有的。
def find_start_forward(goal, count):
"""
Function that iteratively searches forward from an initial
start value of 0, computes the sequence until the last value is greater than or equal
to the goal, and returns the start value of the sequence
:param goal: The last number in the sequence
:param count: How many steps to reach the goal
:pre-conditions: goal >= 0 and count >= 0
:return:
"""
if count == 0:
goal = goal
return goal
else:
number = 0
count = 0
while True:
number = number + 1
goal_end = number * 2 + 5
if goal_end >= goal:
return number
count += 1
当前输出:
print(find_start_forward(100,1))
>> 48
print(find_start_forward(7,2))
>> 1
我希望最终输出如下所示:
find_start_forward(100,1) It would find 48 is the first to follow the rules: * 2 + 5 is >= 100
48
find_start_forward(7,2) Finds what 2 steps from the start is >= 7
0
函数返回 None
因为它没有在循环结束前进入 if 子句。我用 while 循环修改了 for 循环,并将计数添加到每次迭代中:
def find_start_forward(goal, count):
"""
Function that iteratively searches forward from an initial
start value of 0, computes the sequence until the last value is greater than or equal
to the goal, and returns the start value of the sequence
:param goal: The last number in the sequence
:param count: How many steps to reach the goal
:pre-conditions: goal >= 0 and count >= 0
:return:
"""
if count == 0:
goal = goal
return goal
else:
start_value = 0
while True:
sequence_sum = start_value
for i in range(count):
goal_end = sequence_sum * 2 + 5
if goal_end >= goal:
return start_value
sequence_sum = goal_end
start_value += 1
这些是输出
print(find_start_forward(100,1))
>> 48
print(find_start_forward(7,2))
>> 0
print(find_start_forward(100,4))
>> 2
编辑 2:
在这里!基本上我已经创建了一个嵌套循环:一个 while 循环,它保持递增起始值和 for 循环,它搜索以小于 count
的值完成序列的数字。文末一句你posted有助于理解题意
我目前正在尝试在 python 中编写一个函数,该函数采用两个值 goal
和 count
并迭代搜索该函数应从以下数量开始的值count
给出的步骤,直到找到等于或大于 goal
.
感谢 Sprizgola,这是我目前所拥有的。
def find_start_forward(goal, count):
"""
Function that iteratively searches forward from an initial
start value of 0, computes the sequence until the last value is greater than or equal
to the goal, and returns the start value of the sequence
:param goal: The last number in the sequence
:param count: How many steps to reach the goal
:pre-conditions: goal >= 0 and count >= 0
:return:
"""
if count == 0:
goal = goal
return goal
else:
number = 0
count = 0
while True:
number = number + 1
goal_end = number * 2 + 5
if goal_end >= goal:
return number
count += 1
当前输出:
print(find_start_forward(100,1))
>> 48
print(find_start_forward(7,2))
>> 1
我希望最终输出如下所示:
find_start_forward(100,1) It would find 48 is the first to follow the rules: * 2 + 5 is >= 100
48
find_start_forward(7,2) Finds what 2 steps from the start is >= 7
0
函数返回 None
因为它没有在循环结束前进入 if 子句。我用 while 循环修改了 for 循环,并将计数添加到每次迭代中:
def find_start_forward(goal, count):
"""
Function that iteratively searches forward from an initial
start value of 0, computes the sequence until the last value is greater than or equal
to the goal, and returns the start value of the sequence
:param goal: The last number in the sequence
:param count: How many steps to reach the goal
:pre-conditions: goal >= 0 and count >= 0
:return:
"""
if count == 0:
goal = goal
return goal
else:
start_value = 0
while True:
sequence_sum = start_value
for i in range(count):
goal_end = sequence_sum * 2 + 5
if goal_end >= goal:
return start_value
sequence_sum = goal_end
start_value += 1
这些是输出
print(find_start_forward(100,1))
>> 48
print(find_start_forward(7,2))
>> 0
print(find_start_forward(100,4))
>> 2
编辑 2:
在这里!基本上我已经创建了一个嵌套循环:一个 while 循环,它保持递增起始值和 for 循环,它搜索以小于 count
的值完成序列的数字。文末一句你posted有助于理解题意