如果您投掷3个,6个骰子,则多达3次?答案是一个置换?您将如何设置一个函数来解决此问题?

发布于 2025-02-04 19:23:29 字数 183 浏览 3 评论 0原文

def permutations(numberof_sides,sum_of_dice)
    for i in range(numberof_sides):
         for j in range(sum_of_dice):
             x= ???

我在这里正确的轨道吗?

def permutations(numberof_sides,sum_of_dice)
    for i in range(numberof_sides):
         for j in range(sum_of_dice):
             x= ???

Am I on the right track here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空城缀染半城烟沙 2025-02-11 19:23:29

不确定这是否回答了您的问题,但是我们可以使用itertools.permutations来创建排列列表,然后循环循环以查看它们是否正确总结

一个基本示例可能会如下:

from itertools import permutations

# creates repeats of lists, to communicate
# that we have a certain number of dice
def num_list(sides, count):
    modified_list = []
    for i in range(count):
        #appends the same list as many times as is called for
        modified_list += list(range(1, sides + 1))
    return modified_list

# returns sum of elements in list
def sum(list):
    running_sum = 0
    for element in list:
        running_sum += element
    return running_sum

def permutations_basic(num_of_dice, dice_sides, desired_sum):

    # creates a set, to prevent repeats of identical permutations
    output = set()

    # itertools.permutations does most of the heavy lifting here, so
    # we only need to loop through the permutations it generates
    for perm in list(permutations(num_list(dice_sides, num_of_dice), num_of_dice)):

        if sum(perm) == desired_sum:
            output.add(perm)

    return output


# all permutations for 3 D6, that add to 7
for item in permutations_basic(3, 6, 7):
    print(item)

edit:edit:修复了评论中的错字

Not sure if this answers your question, but we can use itertools.permutations to create the list of permutations, then loop through them to see if they sum up properly

A basic example might look like this:

from itertools import permutations

# creates repeats of lists, to communicate
# that we have a certain number of dice
def num_list(sides, count):
    modified_list = []
    for i in range(count):
        #appends the same list as many times as is called for
        modified_list += list(range(1, sides + 1))
    return modified_list

# returns sum of elements in list
def sum(list):
    running_sum = 0
    for element in list:
        running_sum += element
    return running_sum

def permutations_basic(num_of_dice, dice_sides, desired_sum):

    # creates a set, to prevent repeats of identical permutations
    output = set()

    # itertools.permutations does most of the heavy lifting here, so
    # we only need to loop through the permutations it generates
    for perm in list(permutations(num_list(dice_sides, num_of_dice), num_of_dice)):

        if sum(perm) == desired_sum:
            output.add(perm)

    return output


# all permutations for 3 D6, that add to 7
for item in permutations_basic(3, 6, 7):
    print(item)

Edit: fixed typo in comment

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文