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

On Thu, Feb 25, 2010 at 2:40 AM, Alexander Antonakakis <alexis(a)maich.gr>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

From: Alexander Antonakakis on
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/.

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

On Thu, Feb 25, 2010 at 6:43 AM, Alexander Antonakakis <alexis(a)maich.gr>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?