4 Replies - 1505 Views - Last Post: 28 October 2013 - 04:02 PM Rate Topic: -----

#1 i_am_become_time  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-July 13

Circus Tower Problem

Posted 27 October 2013 - 03:13 PM

It's one of those days where I'm feeling like a complete idiot since I can't seem to figure this out. The problem:


A circus is designing a tower routine consisting of people standing atop one another’s shoulders. For practical and aesthetic reasons, each person must be both shorter and lighter than the person below him or her. Given the heights and weights of each person in the circus, write a method to compute the largest possible number of people in such a tower EXAMPLE: Input (ht, wt): (65, 100) (70, 150) (56, 90) (75, 190) (60, 95) (68, 110) Output: The longest tower is length 6 and includes from top to bottom: (56, 90) (60,95) (65,100) (68,110) (70,150) (75,190)
My code:

def circus_tower(data)
  tower_height = 0
  sorted = data.sort_by {|x| x.first}
  for i in (0..sorted.size)
    if (sorted[i].last > sorted[i+1].last)
      sorted[i], sorted[i+1] = sorted[i+1], sorted[i]
      tower_height += 1
    end
  end
end



I'm passing in the data as an array of arrays. I keep getting this error: circus_tower': undefined methodlast' for nil:NilClass (NoMethodError). I've tried to go about comparing the second element of each array in the sorted array a number of different ways(e.g., at(1), [-1]) and I keep getting a variation of the same error. Can anyone tell me what's going on?

Is This A Good Question/Topic? 0
  • +

Replies To: Circus Tower Problem

#2 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1383
  • View blog
  • Posts: 3,515
  • Joined: 28-November 09

Re: Circus Tower Problem

Posted 27 October 2013 - 04:44 PM

First issue is that you're using a for loop in Ruby. Use blocks instead:
(1..5).to_a.each do |x|
  code
end

(1..5).to_a.each{ |x| code }



Contrived examples, but you get the idea.

Now this is a classic problem known as the Tower of Hanoi, and I would highly suggest looking into recursive solutions to this task, as that's probably the point of this assignment.

This post has been edited by Lemur: 27 October 2013 - 04:46 PM

Was This Post Helpful? 0
  • +
  • -

#3 i_am_become_time  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-July 13

Re: Circus Tower Problem

Posted 27 October 2013 - 04:59 PM

I figured out the issue (or someone did for me). sorted[x+1] goes out of range on the last iteration, so I added an "if sorted[x+1]" to ensure that doesn't happen. However, tower_height isn't being incremented and I haven't been able to deduce why.
Was This Post Helpful? 0
  • +
  • -

#4 i_am_become_time  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 25-July 13

Re: Circus Tower Problem

Posted 27 October 2013 - 05:05 PM

View PostLemur, on 27 October 2013 - 04:44 PM, said:

First issue is that you're using a for loop in Ruby. Use blocks instead:
(1..5).to_a.each do |x|
  code
end

(1..5).to_a.each{ |x| code }



Contrived examples, but you get the idea.

Now this is a classic problem known as the Tower of Hanoi, and I would highly suggest looking into recursive solutions to this task, as that's probably the point of this assignment.


Any particular reason that you would need to change the range to an array before running the block?
Was This Post Helpful? 0
  • +
  • -

#5 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1383
  • View blog
  • Posts: 3,515
  • Joined: 28-November 09

Re: Circus Tower Problem

Posted 28 October 2013 - 04:02 PM

....lack of sleep. Yeah, not necessary. Still, you're going to want to find out about the recursive solutions to that problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1