简体   繁体   中英

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:

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.

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)
[]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM