[英]Given a number, create a set of 2-tuples whose sum is equal to the provided number starting from (1, 1)
It gave me an example but how can I compute it? 它给了我一个例子,但是我该如何计算呢?
enter code here
create_dice_sets(6) --> ([(1, 5), (2, 4), (3, 3), (4, 2), (5, 1)])
create_dice_sets(2) --> [(1, 1)]
create_dice_sets(1) --> [()]
this is what i have given. 这就是我给的。
enter code here
def create_dice_sets(number):
#Fill your code here.
return result
print(create_dice_sets(6))
You can iterate over the range of values between 1 and the given number and create a tuple with the value i
and number - i
at every turn: 您可以遍历1到给定数字之间的值范围,并创建一个具有
i
和number - i
值的元组number - i
每次转动number - i
:
I think this is a homework so I assume you are not allowed to use any built-in function to do this. 我认为这是一项家庭作业,因此我假定您不允许使用任何内置函数来执行此操作。
>>> def create_dice_sets(number):
... result = []
... for i in range(1, number):
... result.append((i, number - i))
... return result
Btw, there is bug in the code for the given number being equal to 1. 顺便说一句,在给定数字等于1的代码中存在错误。
Demo: 演示:
>>> create_dice_sets(6)
[(1, 5), (2, 4), (3, 3), (4, 2), (5, 1)]
>>> create_dice_sets(2)
[(1, 1)]
>>> create_dice_sets(1)
[]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.