使用循环为 python 中的连续结果添加更高的 score/penalty
Add higher score/penalty for consecutive result in python using loop
我正在尝试使用 python 编写计算最终得分的代码。我使用的初始代码是:
initial_score = 50
result = ['T','F','T','T','T','F']
final_score = initial_score
for i in (result):
if i == 'T':
final_score += 10
else:
final_score += -5
print(final_score)
结果是:
80
现在,如果列表中的结果是连续的,我想让 score/penalty 增加。如果结果连续为真 (T),则添加到 initial_point 的点将在每个结果(10、20、30 等)后增加。示例:
initial_score = 50
result = ['T','T','T','T','T']
因此列表中每个值的分数将为 [10,20,30,40,50]
,最终分数将为 200。对于 False (F) 也是如此,除了惩罚将为 (-5,-10,- 15 等)。我也想约束一下,如果连续的结果结束就从最新的score/penalty开始。示例:
initial_score = 10
result = ['T','T','F','F','T','T','F','F','F','F','T','T','T']
列表中的值为 [10,20,-5,-10,20,30,-10,-15,-20,-25,30,40,50]
,最终得分为 125。
我应该怎么做?
跟踪奖励和惩罚,在每个 right/wrong 答案上更新它,如果它与以前的答案相同。
def score(initial, result):
total = initial
award = 10
penalty = 5
for i, prev in zip(result, [None] + result):
if i == 'T':
if i == prev:
award += 10
total += award
else:
if i == prev:
penalty += 5
total -= penalty
return total
>>> score(50, ['T','T','T','T','T'])
200
>>> score(10, ['T','T','F','F','T','T','F','F','F','F','T','T','T'])
125
您可以使用 itertools.groupby()
和一点数学来计算每个 True/False 连胜的分值:
from itertools import groupby
t_score = 10
t_increment = 10
f_score = -5
f_increment = -5
total_score = initial_score
for key, items in groupby(result):
length = sum(1 for item in items)
if key == 'T':
total_score += t_score * length + (length * (length - 1)) // 2 * t_increment
t_score += t_increment * (length - 1)
else:
total_score += f_score * length + (length * (length - 1)) // 2 * f_increment
f_score += f_increment * (length - 1)
print(total_score)
这输出:
125
使用字典记录分数、奖励和惩罚。并循环遍历您的 result
列表以得出最终分数。
initial_score = 10
result = ['T','T','F','F','T','T','F','F','F','F','T','T','T']
point_dict = {'T':10, 'F':-5}
increment_dict = {'T':10, 'F':-5}
cycle_num = 0
for i in range(len(result)):
if not cycle_num:
initial_score += point_dict[result[i]]
else:
if result[i] == result[i-1]:
point_dict[result[i]] += increment_dict[result[i]]
initial_score += point_dict[result[i]]
else:
initial_score += point_dict[result[i]]
cycle_num += 1
print(point_dict)
print(initial_score)
我正在尝试使用 python 编写计算最终得分的代码。我使用的初始代码是:
initial_score = 50
result = ['T','F','T','T','T','F']
final_score = initial_score
for i in (result):
if i == 'T':
final_score += 10
else:
final_score += -5
print(final_score)
结果是:
80
现在,如果列表中的结果是连续的,我想让 score/penalty 增加。如果结果连续为真 (T),则添加到 initial_point 的点将在每个结果(10、20、30 等)后增加。示例:
initial_score = 50
result = ['T','T','T','T','T']
因此列表中每个值的分数将为 [10,20,30,40,50]
,最终分数将为 200。对于 False (F) 也是如此,除了惩罚将为 (-5,-10,- 15 等)。我也想约束一下,如果连续的结果结束就从最新的score/penalty开始。示例:
initial_score = 10
result = ['T','T','F','F','T','T','F','F','F','F','T','T','T']
列表中的值为 [10,20,-5,-10,20,30,-10,-15,-20,-25,30,40,50]
,最终得分为 125。
我应该怎么做?
跟踪奖励和惩罚,在每个 right/wrong 答案上更新它,如果它与以前的答案相同。
def score(initial, result):
total = initial
award = 10
penalty = 5
for i, prev in zip(result, [None] + result):
if i == 'T':
if i == prev:
award += 10
total += award
else:
if i == prev:
penalty += 5
total -= penalty
return total
>>> score(50, ['T','T','T','T','T'])
200
>>> score(10, ['T','T','F','F','T','T','F','F','F','F','T','T','T'])
125
您可以使用 itertools.groupby()
和一点数学来计算每个 True/False 连胜的分值:
from itertools import groupby
t_score = 10
t_increment = 10
f_score = -5
f_increment = -5
total_score = initial_score
for key, items in groupby(result):
length = sum(1 for item in items)
if key == 'T':
total_score += t_score * length + (length * (length - 1)) // 2 * t_increment
t_score += t_increment * (length - 1)
else:
total_score += f_score * length + (length * (length - 1)) // 2 * f_increment
f_score += f_increment * (length - 1)
print(total_score)
这输出:
125
使用字典记录分数、奖励和惩罚。并循环遍历您的 result
列表以得出最终分数。
initial_score = 10
result = ['T','T','F','F','T','T','F','F','F','F','T','T','T']
point_dict = {'T':10, 'F':-5}
increment_dict = {'T':10, 'F':-5}
cycle_num = 0
for i in range(len(result)):
if not cycle_num:
initial_score += point_dict[result[i]]
else:
if result[i] == result[i-1]:
point_dict[result[i]] += increment_dict[result[i]]
initial_score += point_dict[result[i]]
else:
initial_score += point_dict[result[i]]
cycle_num += 1
print(point_dict)
print(initial_score)