You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

20次骰子投掷随机模拟:连续相同值括号包裹功能问题求助

Fixing Dice Roll Formatting: Wrapping Consecutive Duplicates in Parentheses

Hey there! Let's sort out that formatting issue with your dice roll simulation. The problem you're hitting is that your current logic is incorrectly grouping non-consecutive or non-matching numbers in parentheses—we need to adjust it to only wrap runs of the same consecutive number instead.

Here's a complete, working solution:

from random import randint

def main():
    # Generate 20 random dice rolls (1-6)
    dice_rolls = [randint(1, 6) for _ in range(20)]
    print("Raw dice rolls:", dice_rolls)
    
    if not dice_rolls:
        return
    
    # Initialize variables to track consecutive runs
    formatted_output = []
    current_number = dice_rolls[0]
    consecutive_count = 1
    
    # Iterate through each roll after the first
    for roll in dice_rolls[1:]:
        if roll == current_number:
            # Same number as current run—increase count
            consecutive_count += 1
        else:
            # Different number: process the current run first
            if consecutive_count == 1:
                # Single number, add directly
                formatted_output.append(str(current_number))
            else:
                # Multiple consecutive numbers: wrap in parentheses
                run_str = " ".join([str(current_number)] * consecutive_count)
                formatted_output.append(f"( {run_str} )")
            # Reset for the new run
            current_number = roll
            consecutive_count = 1
    
    # Don't forget to process the final run after the loop ends
    if consecutive_count == 1:
        formatted_output.append(str(current_number))
    else:
        run_str = " ".join([str(current_number)] * consecutive_count)
        formatted_output.append(f"( {run_str} )")
    
    # Combine all parts into the final formatted string
    final_result = " ".join(formatted_output)
    print("Formatted result:", final_result)

if __name__ == "__main__":
    main()

How this works:

  • We first generate your 20 random dice rolls and store them in a list.
  • We track the current number we're watching (current_number) and how many times it's appeared consecutively (consecutive_count).
  • As we loop through each roll:
    • If the roll matches our current number, we increment the consecutive count.
    • If it doesn't match, we handle the run we were tracking:
      • Single numbers get added directly to the output list.
      • Longer runs get joined into a string and wrapped in parentheses before being added.
    • We then reset our tracker variables to start watching the new number.
  • After the loop finishes, we have to handle the last run (since the loop ends before processing it).
  • Finally, we join all the formatted parts into a single string for output.

Example Output:

If your raw rolls are [1, 4, 1, 2, 3, 6, 1, 4, 3, 2, 6, 6, 6, 5, 6, 2, 1, 3, 5, 3], the formatted result will be:

1 4 1 2 3 6 1 4 3 2 ( 6 6 6 ) 5 6 2 1 3 5 3

Which matches your desired correct format perfectly!

内容的提问来源于stack exchange,提问作者GERRR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:17:51