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 7748223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:43:38+00:00 2026-06-01T10:43:38+00:00

def partitions(n): # base case of recursion: zero is the sum of the empty

  • 0
def partitions(n):
    # base case of recursion: zero is the sum of the empty list
    if n == 0:
        yield []
        return

    # modify partitions of n-1 to form partitions of n
    for p in partitions(n-1):
        yield [1] + p
        if p and (len(p) < 2 or p[1] > p[0]):
            yield [p[0] + 1] + p[1:]

Explanation:
If you have a partition of n, you can reduce it to a partition of n-1 in a canonical way by subtracting one from the smallest item in the partition. E.g. 1+2+3 => 2+3, 2+4 => 1+4. This algorithm reverses the process: for each partition p of n-1, it finds the partitions of n that would be reduced to p by this process. Therefore, each partition of n is output exactly once, at the step when the partition of n-1 to which it reduces is considered.

This is code for getting all possible partitions of a number in Python. I am not good at Python. I would really appreciate if someone could just get it transformed into pseudocode(or detailed description) or in PHP. The explanation above creates a doubt in my mind about “subtracting one from the smallest item in the partition”. I can also subtract one from second smallest or some other element. So, why only smallest? If someone could explain me the whole idea, it would be really grateful.
Thanks.

  • 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-01T10:43:40+00:00Added an answer on June 1, 2026 at 10:43 am
    def partitions(n):
        # base case of recursion: zero is the sum of the empty list
        if n == 0:
            yield [] # yield empty array
            return # exit function
    
        # modify partitions of n-1 to form partitions of n
        for p in partitions(n-1): # recursive call, get n-1 partitions
            yield [1] + p # yield array [1, p...]
            if p and (len(p) < 2 or p[1] > p[0]): # p not empty, and length < 2 or p[1] > p[0]
                yield [p[0] + 1] + p[1:] # increment first item of p and yield p
    

    Here’s my try (afaik PHP does not have yield, so it might perform worse):

    function partitions($n) {
       # base case of recursion: zero is the sum of the empty list
       if(!$n) return array(array()); # return/"yield" empty array
    
       # modify partitions of n-1 to form partitions of n
       $a = array(); # will hold "yielded" values
       foreach(partitions($n-1) as $p) { # recursive call
         $a[] = array_merge(array(1), $p); # "yield" array [1, p...]
         if($p && (count($p) < 2 || $p[1] > $p[0])) { # p not empty, and length < 2 or p[1] > p[0]
           ++$p[0]; # increment first item of p
           $a[] = $p; # "yield" p
         }
       }
       return $a; # return all "yielded" values at once
    }
    

    (I don’t guarantee anything)

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

Sidebar

Related Questions

def any? if block_given? method_missing(:any?) { |*block_args| yield(*block_args) } else !empty? end end In
def makecounter(): return collections.defaultdict(int) class RankedIndex(object): def __init__(self): self._inverted_index = collections.defaultdict(list) self._documents = []
def common_elements(list1, list2): Return a list containing the elements which are in both list1
def update @album = Album.find(params[:id]) if @album.update_attributes(params[:album]) redirect_to(:action=>'list') else render(:action=>'edit') end end A Rails
def test_method [a, b, c].map {|i| yield(i) } end If I call test_method like
def self.get(server) return unless server server = server.to_s if klass = @handlers[server] obj =
def record return unless @supported klasses = profile_options[:formats].map { |f| RubyProf.const_get(#{f.to_s.camelize}Printer) }.compact klasses.each do
I have read the answers to the Slicing a list into n nearly-equal-length partitions
def insertion_sort(A): for j in range(1, len(A)): key = A[j] i = j -
def func(a, b = 100): return a + b func(a) func(a,b = 100) Is

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.