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

  • Home
  • SEARCH
  • 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 565647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:53:39+00:00 2026-05-13T12:53:39+00:00

I have the following python function to recursively find all partitions of a set:

  • 0

I have the following python function to recursively find all partitions of a set:

def partitions(set_):
    if not set_:
        yield []
        return
    for i in xrange(2**len(set_)/2):
        parts = [set(), set()]
        for item in set_:
            parts[i&1].add(item)
            i >>= 1
        for b in partitions(parts[1]):
            yield [parts[0]]+b

for p in partitions(["a", "b", "c", "d"]):
print(p)

Can someone help me to translate this into ruby? This is what I have so far:

def partitions(set)
  if not set
    yield []
    return
  end
  (0...2**set.size/2).each { |i|
    parts = [Set.new, Set.new]
    set.each { |item|
      parts[i&1] << item
      i >>= 1
    }
    partitions(parts[1]).each { |b|
      yield [parts[0]] << b
    }
  }
end

p partitions([1, 2, 3, 4].to_set)

I get the error “LocalJumpError: no block given”. I guess this is because the yield functions work differently in Python and Ruby.

  • 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-05-13T12:53:39+00:00Added an answer on May 13, 2026 at 12:53 pm
    #!/usr/bin/ruby1.8
    
    def partitions(set)
      yield [] if set.empty?
      (0 ... 2 ** set.size / 2).each do |i|
        parts = [[], []]
        set.each do |item|
          parts[i & 1] << item
          i >>= 1
        end
        partitions(parts[1]) do |b|
          result = [parts[0]] + b
          result = result.reject do |e|
            e.empty?
          end
          yield result
        end
      end
    end
    
    partitions([1, 2, 3, 4]) do |e|
      p e
    end
    
    # => [[1, 2, 3, 4]]
    # => [[2, 3, 4], [1]]
    # => [[1, 3, 4], [2]]
    # => [[3, 4], [1, 2]]
    # => [[3, 4], [2], [1]]
    # => [[1, 2, 4], [3]]
    # => [[2, 4], [1, 3]]
    # => [[2, 4], [3], [1]]
    # => [[1, 4], [2, 3]]
    # => [[1, 4], [3], [2]]
    # => [[4], [1, 2, 3]]
    # => [[4], [2, 3], [1]]
    # => [[4], [1, 3], [2]]
    # => [[4], [3], [1, 2]]
    # => [[4], [3], [2], [1]]
    

    What’s different:

    • The guard calls set.empty? instead of
      (implicitly) testing for set.nil?
    • Leave out the .each when calling
      partitions
    • Use Array instead of Set
    • Filter empty sets out of the yielded
      result
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Python compiled regex patterns have a findall method that does the following: Return
how do i round off a function. i have the following code in python
I have the following Python code: import xml.dom.minidom import xml.parsers.expat try: domTree = ml.dom.minidom.parse(myXMLFileName)
I have a question about using os.execvp in Python. I have the following bit
I am a newbie to Python and have come across the following example in
When using IF statements in Python, you have to do the following to make
I have defined a .dir-locals.el file with the following content: ((python-mode . ((cr/virtualenv-name .
I have a need for a high-performance string hashing function in python that produces
in python 3 (with tkinter) i have the following images: SqImg1 = PhotoImage(file='SqImg1.gif') SqImg2
I have following situation: I have loged user, standard authentication with DB table $authAdapter

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.