Help on algorithm to calculate combinations

Discussion in 'Ruby' started by Alexander Antonakakis, Feb 25, 2010.

  1. Hello all
    I would like to find an algorithm to caclulate all options on the
    following problem.
    Lets suppose we have a room of max capacity of 4 persons.
    Which are the combinations of man - child in this room?
    some of them will be:
    empty room (none inside)
    1 man
    1 child
    2 men
    2 children
    2 men 1 children
    2 men 2 children
    Alexander Antonakakis, Feb 25, 2010
    #1
    1. Advertising

  2. [Note: parts of this message were removed to make it a legal post.]

    On 25 February 2010 09:40, Alexander Antonakakis <> wrote:

    > Hello all
    > I would like to find an algorithm to caclulate all options on the
    > following problem.
    > Lets suppose we have a room of max capacity of 4 persons.
    > Which are the combinations of man - child in this room?
    >


    0 children / 0 men
    0 children / 1 men
    0 children / 2 men
    0 children / 3 men
    0 children / 4 men
    1 children / 0 men
    1 children / 1 men
    1 children / 2 men
    1 children / 3 men
    2 children / 0 men
    2 children / 1 men
    2 children / 2 men
    3 children / 0 men
    3 children / 1 men
    4 children / 0 men

    --
    Thomas Preymesser

    http://thopre.googlepages.com/
    http://thopre.wordpress.com/
    Thomas Preymesser, Feb 25, 2010
    #2
    1. Advertising

  3. Thomas Preymesser wrote:
    > On 25 February 2010 09:40, Alexander Antonakakis <>
    > wrote:
    >
    >> Hello all
    >> I would like to find an algorithm to caclulate all options on the
    >> following problem.
    >> Lets suppose we have a room of max capacity of 4 persons.
    >> Which are the combinations of man - child in this room?
    >>

    >
    > 0 children / 0 men
    > 0 children / 1 men
    > 0 children / 2 men
    > 0 children / 3 men
    > 0 children / 4 men
    > 1 children / 0 men
    > 1 children / 1 men
    > 1 children / 2 men
    > 1 children / 3 men
    > 2 children / 0 men
    > 2 children / 1 men
    > 2 children / 2 men
    > 3 children / 0 men
    > 3 children / 1 men


    max = 4
    @result = []
    (0..max).each do |x|
    (0..(max - x)).each do |y|
    @combination = []
    x.times { @combination << "children" }
    y.times { @combination << "men" }
    @result << @combination
    end
    end

    Thank you very much
    > 4 children / 0 men


    --
    Posted via http://www.ruby-forum.com/.
    Alexander Antonakakis, Feb 25, 2010
    #3

  4. > max = 4
    > @result = []
    > (0..max).each do |x|
    > (0..(max - x)).each do |y|
    > @combination = []
    > x.times { @combination << "children" }
    > y.times { @combination << "men" }
    > @result << @combination
    > end
    > end
    >
    > Thank you very much
    >> 4 children / 0 men


    it doesn't produce 1 children (for instance)

    results = []
    0.upto(4) do |n|
    0.upto(2**n - 1) do |i|
    s = i.to_s(2).rjust(n,'0').split('').sort.join
    results << s unless results.include?(s)
    end
    end

    p results.map! {|e| e.tr('01','cm')}

    --
    Posted via http://www.ruby-forum.com/.
    Giampiero Zanchi, Feb 25, 2010
    #4
  5. Giampiero Zanchi wrote:
    > it doesn't produce 1 children (for instance)
    >
    > results = []
    > 0.upto(4) do |n|
    > 0.upto(2**n - 1) do |i|
    > s = i.to_s(2).rjust(n,'0').split('').sort.join
    > results << s unless results.include?(s)
    > end
    > end
    >
    > p results.map! {|e| e.tr('01','cm')}


    It produces the 1 children option plus the empty room one. We have the
    same results 14 for you that don't iclure the empty room combination and
    15 for me with the empty room.
    --
    Posted via http://www.ruby-forum.com/.
    Alexander Antonakakis, Feb 25, 2010
    #5
  6. Alexander Antonakakis

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    Sorry, computer lagged and I hit 'send' rather than clicking in the window
    to edit.

    Here is a solution I meant to post, which has the correct inflections
    http://pastie.org/842110

    def people_in_room(occupants)
    for men in 0..occupants
    for children in 0..occupants - men
    yield men , children
    end
    end
    end

    puts "in a room with 4 people, you could occupy it in the following ways:"
    people_in_room 4 do |men,children|
    to_print = Array.new
    to_print << "#{men} #{ men == 1 ? 'man' : 'men' }" unless
    men.zero?
    to_print << "#{children} #{ children == 1 ? 'child' : 'children' }" unless
    children.zero?
    puts to_print.join(' ')
    end
    Josh Cheek, Feb 25, 2010
    #6
  7. Alexander Antonakakis

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Thu, Feb 25, 2010 at 2:40 AM, Alexander Antonakakis <>wrote:

    > Hello all
    > I would like to find an algorithm to caclulate all options on the
    > following problem.
    > Lets suppose we have a room of max capacity of 4 persons.
    > Which are the combinations of man - child in this room?
    > some of them will be:
    > empty room (none inside)
    > 1 man
    > 1 child
    > 2 men
    > 2 children
    > 2 men 1 children
    > 2 men 2 children
    > .
    > .
    > 4 men
    > 4 children
    >
    > I appreciate your help
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    def people_in_room(occupants)
    for men in 0..occupants
    for children in 0..occupants - men
    yield men , children
    end
    end
    end

    puts "in a room with 4 people, you could occupy it in the following ways:"
    people_in_room 4 do |men,children|
    puts "men: #{men} , children: #{children}"
    end
    Josh Cheek, Feb 25, 2010
    #7
  8. Josh Cheek wrote:
    > Sorry, computer lagged and I hit 'send' rather than clicking in the
    > window
    > to edit.


    Thanks Josh :)
    --
    Posted via http://www.ruby-forum.com/.
    Alexander Antonakakis, Feb 25, 2010
    #8
  9. Alexander Antonakakis

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Thu, Feb 25, 2010 at 6:43 AM, Alexander Antonakakis <>wrote:

    > Josh Cheek wrote:
    > > Sorry, computer lagged and I hit 'send' rather than clicking in the
    > > window
    > > to edit.

    >
    > Thanks Josh :)
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    lol, you're welcome. But I just saw that it prints an empty line for the
    empty room. That's what I get for not testing. Try adding this line to the
    beginning of the block:
    next( puts "empty room" ) if men.zero? && children.zero?
    Josh Cheek, Feb 25, 2010
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jose luis fernandez diaz

    Combinations/permutations algorithm in C++

    jose luis fernandez diaz, Apr 13, 2004, in forum: C++
    Replies:
    6
    Views:
    14,090
    Leor Zolman
    Apr 13, 2004
  2. Peter R
    Replies:
    2
    Views:
    7,392
    Alex Vinokur
    May 11, 2004
  3. Replies:
    0
    Views:
    3,103
  4. deancoo
    Replies:
    2
    Views:
    416
    deancoo
    Feb 25, 2005
  5. cayblood
    Replies:
    8
    Views:
    503
    Victor Bazarov
    Nov 2, 2005
Loading...

Share This Page