Main Content

Romeo and Julie

Archive - Originally posted on "The Horse's Mouth" - 2008-12-04 01:00:35 - Graham Ellis

Romeo wants to take his girlfriend Julie out to The Jolly Judge tomorrow night, but Julie's Dad (who really doesn't approve of young Romeo) has shut her in her room on the first floor of their home, which as you can see is on the top of a substantial mound.

Being a resourceful lad, Romeo is going to nip down to his local B & Q Store and nick a couple of ladders (it's this habit of his of nicking things that has put Julie's Dad off him, by the way) to get her out.

Your task ... if you're on our Perl Course ... is to write a program to help Romeo work out how long the two ladders he'll need will be:

• The mount is 12 metres tall, and there's a good grounding about 5 metres away from the base

• the window of Julie's room is 4 metres up from the ground, but Dad has placed a line of holly bushes just under Julie's window, so Romeo is going to have to foot the ladder 3 metres out.

The algorithm you need is:
   $ladlen = sqrt($w * $w + $h * $h);
and you're asked to write that in a separate sub / in a separate file, so that you can easily re-use the same code later if you need too (since Romeo is a bit of a jack-the-lad by all accounts, it would be a shame to write code just for him rather than have it available for re-use, wouldn't it?)


There's a sample answer - source code of the main program here, and you can find the file that contains the sub here. If you ARE here because you're a Perl programmer, do have a look at the source which is rather thoroughly commented - and contains examples of things like my v our, package definition, and the use of the wantarray function.