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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:27:08+00:00 2026-05-12T22:27:08+00:00

What would be the correct translation of the following Python method to Ruby? def

  • 0

What would be the correct translation of the following Python method to Ruby?

def uniqueCombinations(self, items, n):
    """ 
    items: list of elements
    n: number in a group
    """
    if n == 0:
        yield []
    else:
        for i in range(len(items)-n+1):
            for cc in uniqueCombinations(items[i+1:],n-1):
                yield [items[i]]+cc

What I want to do is call

uniqueCombinations(['01', '02', '03'], 2) 

and get

[['01', '02'], ['01', '03'], ['02', '03']]

This is what I have so far.

def uniq_c(items, n)
  if n == 0
    yield []
  else
    puts items.inspect
    range_max = items.length-n+1
    for i in (0...range_max)
      u = uniq_c(items[(i+1)..-1], n-1) { |x| x }
      u.each do |cc|
        yield [items[i]] + cc
      end
    end
  end
end

but I get this:

in `+': can't convert Fixnum into Array (TypeError)
  • 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-12T22:27:08+00:00Added an answer on May 12, 2026 at 10:27 pm

    First, it’d be better to do this as a method of the Array class.

    Second, you can fix your error by changing [items[i]] + cc to [items[i]] + [cc].

    Third, here’s a different implementation I had lying around, see what you think. It creates an array of permutations represented by indices, then maps each index to its value.

    class Array
      def combinations(n)
        perms = []
        a = (0...n).to_a
        while perms.push a.dup
          break if a.increment!(a.length - 1, self.length-1).nil?
        end
        perms.map {|p| p.map {|i| self[i]} }
      end
      def increment!(i, max)
        self[i] += 1
        while self[i] > max
          return nil if i <= 0
          return nil if self.increment!(i - 1, max).nil?
          self[i] = self[i - 1] + 1
        end
        self.dup
      end
    end
    
    [1,2,3].combinations 3 # => [[1, 2, 3]]
    [1,2,3].combinations 2 # => [[1, 2], [1, 3], [2, 3]]
    [1,2,3].combinations 1 # => [[1], [2], [3]]
    [:foo,:bar,:baz,:quux,:wibble].combinations 3
    # => [[:foo, :bar, :baz],
    #     [:foo, :bar, :quux],
    #     [:foo, :bar, :wibble],
    #     [:foo, :baz, :quux],
    #     [:foo, :baz, :wibble],
    #     [:foo, :quux, :wibble],
    #     [:bar, :baz, :quux],
    #     [:bar, :baz, :wibble],
    #     [:bar, :quux, :wibble],
    #     [:baz, :quux, :wibble]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 528k
  • Answers 527k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Looks like you want to access a user defined property,… May 16, 2026 at 10:52 pm
  • Editorial Team
    Editorial Team added an answer This runs for 7000 iterations. So if your runtime is… May 16, 2026 at 10:52 pm
  • Editorial Team
    Editorial Team added an answer This is a great resource for tutorials. http://www.raywenderlich.com May 16, 2026 at 10:52 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have the following transaction: SQL inserts a 1 new record into a table
I am trying to convert the following stored proc to a LinqToSql call (this
I have class foo that contains a std::auto_ptr member that I would like to
I'm working on a view-based iPhone app that has the following flow: search ->
I remember running some tests a some months ago with gettext and the following
I am attempting to fix some bilingual xml files using regular expressions to match
When I call a SQL statement via JDBC on the DB2 and the statement
I'm new to WCF services and wondered what the best way to tackle the
I'm designing a new protocol called DITP. It is a connection oriented protocol that
There seem to be very different opinions about using transactions for reading from a

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.