Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8968009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:22:28+00:00 2026-06-15T17:22:28+00:00

I’m trying to make a simple recursive function that will generate a list of

  • 0

I’m trying to make a simple recursive function that will generate a list of nested lists in Python. The end result will represent a single elimination tournament bracket. I’m hoping that creating a list like this will make it easy for me to generate what I need. This will later be used to create models for tournament matches.

So if there is a tournament of 4 participants:

[[1,4],[2,3]]

Tournament of 7 participants:

[[1,[4,5]],[[2,7],[3,6]]]

Or a tournament of 8 participants:

[[[1,8],[4,5]],[[2,7],[3,6]]]

I haven’t had an algorithms class yet (I’m hoping the class will end up helping with things like this) so I’m not completely sure how to approach this problem. Below is my attempt so far.

def decide_rounds(list_to_fill, player_nums):
    if len(player_nums) < 3:
        for num in player_nums:
            list_to_fill.append(num)
        return

    left = []
    decide_rounds(left, ??????) #Tried passing various things to these with no avail.
    list_to_fill.append(left)
    right = []
    decide_rounds(right, ???????)
    list_to_fill.append(right)

Any help or explanation on how to approach this would be greatly appreciated!

Edit: Currently I am calling the function like this:

rounds = []
decide_rounds(rounds, range(1, size +1))
print rounds
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:22:29+00:00Added an answer on June 15, 2026 at 5:22 pm

    Try this:

    def divide(arr, depth, m):
        if len(complements) <= depth:
            complements.append(2 ** (depth + 2) + 1)
        complement = complements[depth]
        for i in range(2):
            if complement - arr[i] <= m:
                arr[i] = [arr[i], complement - arr[i]]
                divide(arr[i], depth + 1, m)
    
    m = int(raw_input())
    
    arr = [1, 2]
    complements = []
    
    divide(arr, 0, m)
    print arr
    

    We notice that for a bracket with 2^n players, the sum of every pair is the same number. For every pair, the right term is determined by the left element and the depth of the recursion, so we can just proceed by generating the array depth first. We memoize the complements to improve runtime just a bit. It works for any m > 1 as it stops recursing once the complement is too large.

    See it in action: http://ideone.com/26G1fB

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.