简体   繁体   English

给定一个数字,创建一个2元组,其总和等于从(1,1)开始的提供的数字

[英]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到给定数字之间的值范围,并创建一个具有inumber - 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.

相关问题 从2元组列表中生成3元组的最大数量 - Generating maximum number of 3-tuples from a list of 2-tuples select 来自 pandas 列的 n 个元素,其总和等于提供的数字 - select n elements from a pandas column whose sum is equal to a provided number 如何打印列表的元素,其总和等于 python 中的给定数字? - how to print elements of a list whose sum is equal to a given number in python? 从4个给定的数组(未排序)中,找到每个数组的元素,其总和等于某个数字X. - From 4 given arrays(not sorted), find the elements from each array whose sum is equal to some number X 从列表中创建2元组 - Create 2-tuples from a list 从一组2元组生成3元组 - Generating 3-tuples from a set of 2-tuples Python:创建新列等于从列号9开始的所有列的总和 - Python: Create New Column Equal to the Sum of all Columns Starting from Column Number 9 给定一个由 N 个整数组成的数组和一个整数 K,找出该数组中总和等于 K 的元素对的数量 - Given an array of N integers, and an integer K, find the number of pairs of elements in the array whose sum is equal to K 列表中总和等于或小于给定数字的元素列表 - list of elements from a list that sum is equal or less than to a given number 如何找到它们的总和等于给定数字的数字? - How to find the numbers that their sum equal to a given number?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM