我正在嘗試撰寫一個函式,該函式列印出一個字典,該字典顯示同面骰子的組合可能滾動的特定數量的值。
三個6 面骰子的示例輸出
input: 6,3
Desired output: {3: 1, 4: 3, 5: 6, 6: 10, 7: 15, 8: 21, 9: 25, 10: 27, 11: 27, 12: 25, 13: 21, 14: 15, 15: 10, 16: 6, 17: 3, 18: 1}
四個8 面骰子的示例輸出
input: 8,4
desired output: {4: 1, 5: 4, 6: 10, 7: 20, 8: 35, 9: 56, 10: 84, 11: 120, 12: 161, 13: 204, 14: 246, 15: 284, 16: 315, 17: 336, 18: 344, 19: 336, 20: 315, 21: 284, 22: 246, 23: 204, 24: 161, 25: 120, 26: 84, 27: 56, 28: 35, 29: 20, 30: 10, 31: 4, 32: 1}
這是我的函式,它為每個組合創建一個空字典:
def dice_range(dice_type,dice_count):
dice_dict = {i 1:0 for i in range(dice_type*dice_count) if i 1 >= dice_count}
return dice_dict
input: dice_range(8,3)
actual output: {3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0, 13: 0, 14: 0, 15: 0, 16: 0, 17: 0, 18: 0, 19: 0, 20: 0, 21: 0, 22: 0, 23: 0, 24: 0}
有了這個,我就能弄清楚如何解決任何 dice_type(# of side) 的特定數量的 dice_count 以下是前幾個示例的解決方案:
dice_type = 6
dice_count = 3
temp = dice_range(dice_type,dice_count)
# for 3 dice of any number of sides
for x in range(1,dice_type 1):
for i in range(1,dice_type 1):
for j in range(1,dice_type 1):
for k,v in temp.items():
if x i j == k:
temp[k] =1
dice_type = 8
dice_count = 4
temp = dice_range(dice_type,dice_count)
# for 4 dice of any number of sides
for y in range(1,dice_type 1):
for x in range(1,dice_type 1):
for i in range(1,dice_type 1):
for j in range(1,dice_type 1):
for k,v in temp.items():
if y x i j == k:
temp[k] =1
我希望這是這個問題的足夠背景。
這是我嘗試過的,但由于某種原因,遞回沒有按我的預期作業。
def dice_range(dice_type,dice_count):
dice_dict = {i 1:0 for i in range(dice_type*dice_count) if i 1 >= dice_count}
return dice_dict
def dice_value_calculator(dice_type,dice_count):
PREDICTION = dice_range(dice_type,dice_count)
# Tallies the number by 1 everytime it shows up in the loop.
def fill_dict(total):
for k in PREDICTION.keys():
if total == k:
PREDICTION[k] = 1
def loop(temp_dice_count,loop_total = 0):
for loop_i in range(1,(dice_type 1)):
# Base Case | if the number of dice(dice_count) reaches 0 that means it reached the end of the recursion
if temp_dice_count == 0:
fill_dict(loop_total)
print(PREDICTION)
loop_total = 0
# General Case / Recursive
else:
loop_total = loop_i
temp_dice_count -= 1
loop(temp_dice_count, loop_total)
loop(dice_count) # calls the loop function
return PREDICTION # returns the temp dictionary
print(dice_value_calculator(6,3))
This is the output I'm getting for three 6-sided dice
{3: 2, 4: 4, 5: 0, 6: 2, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0, 13: 0, 14: 0, 15: 0, 16: 0, 17: 0, 18: 0}
Both the fill_dict() function and the dice_range() function work well on their own so it must be how I'm calling loop()
Any tips would be greatly appreciated. Please let me know if I need to clarify anything, this is my first time asking a question so I may have missed something.
Thanks in advance
uj5u.com熱心網友回復:
這個問題至少有四種可能的方法。
“如何獲得動態控制for回圈次數的效果?”
為此,
Here, grouping is done based on thinking that those indistinguishable items are ones(as we do in partitions)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/437639.html
