#!/usr/bin/perl
use strict;
use warnings;
####################
#
# Final exam
# Intro programming in Perl
# Fall 2003
#
# This is an open book exam; you may use any books, references,
# online static pages or perl compiler you like.  
#
# Do *not* ask other people for help - this must be your own work.
# If you have questions you can ask me.
#
# Do let me know which resources you used for each question.
#
# You may print out your answers and hand them to me,
# or email me your final result.
#
# The material we've covered this semester includes
#   *  documentation
#   *  tests and debugging
#   *  input/output : command line, user typing, files
#   *  looping: for, if, while, boolean logic
#   *  subroutines and functions
#   *  regular expressions
#   *  scalars, arrays, hashes, and more elaborate data structures
#   *  references and objects
#   *  packages, modules, CPAN
#
# All submitted code will be evaluated on whether it compiles,
# whether it works, its documentation and built-in tests, and 
# how it it shows you understand what's going on.
#
# The order of the 6 questions roughly follows what we've done over
# the term, progressing from loops to subroutines to objects.  
#
# Partial credit *is* given; if you can't see how to do a question,
# then do some piece of it.  That's a good idea in any case, usually:
# start simple and work your way up.
#
# Good luck.
#
###################

# ---    1    -----------------------

# Write a program that counts backwards from 99 to 11.
# For each number, it should print a line that gives the number,
# and then checks to see if the digits add up to 13.
#
# Part of the output would look something like
#
#    67 : 6 + 7 is 13
#    66 : nope
#    65 : nope
#

# ---     2      --------------

# This is a syntax debugging question.
# (I suggest you pull this code apart and test pieces of it 
# individually to understand it and answer the question.)
# 
# Consider the subroutine

 sub doit {
    our $x = shift and print "x is $x\n"
 }

# and these lines that invoke it :

 doit 1;
 doit 0;

# Explain what this subroutine does, 
# and why it behaves differently in these two cases.

# ---    3    -----------------------

# Write a program which asks the user for 5 words,
# and then prints all possible pairs of those words.
# Include error checking for the user input.

# Part of a sample dialog might look like this.
# (You can input the words one by one if you like.)
#
#   $ ./pairs
#   What are the five words? cat dog boy girl 
#   Oops - you only entered four words.  Please try again.
#   What are the five words? cat dog boy girl candy
#    cat dog
#    cat boy
#    cat girl
#    cat candy
#    dog boy
#    ...
# and so on

# For extra brownie points, modify it to ask the user
# for a number N, then N words, then a number M, 
# and then have it print all possible M-tuples of those words.

# ---    4    -----------------------

# Write a program which will read in an HTML file and 
# summarize the images that it links to.
#
# For example, if the file contains
#
#  <html><head><title>Sample</title></head>
#  <body>
#  <ul>p
#   <li><img src="this/picture.jpg"> 
#   <li><img src="other.png">
#  </ul>
#  </body></html>
#
# then the program would output something like
#
#     Images found  :
#          this/picture.jpg
#          other.png
#
# The program will have to 
#  (1) read the file, 
#  (2) use a regular expression to find any images,
#  (3) report the results
# 
# Be clear about any assumptions you make about the form of the input file.

# ---    5    -----------------------

# Here is a data structure.

my $person = {
	      name => "Jim",
	      age  => 44,
	      brothers => [ 'Matt', 'Bill', 'Paul' ],
	      sisters  => [ 'Marie', 'Annie', 'Aileen' ]
	     };

# (a)
# Write a subroutine that prints out a description of one of these.
# Specifically,

 summarize($person);

# should produce something like
#
#     The person 'Jim' is 44 years old.
#     Brothers are Matt, Bill, Paul.
#     Sisters are Marie, Annie, Aileen.

sub summarize {
  # ADD YOUR CODE HERE
}

# (b)
# Write another subroutine that adds another brother.
# Specifically,

 addBrother($person, 'Joe');
 summarize($person);

 sub AddBrother {
   # ADD YOUR CODE HERE
 }


# should give
#     The person 'Jim' is 44 years old.
#     Brothers are Matt, Bill, Paul, Joe.
#     Sisters are Marie, Annie, Aileen.

# ---    6    -----------------------

# Finally, turn the "person" data structure into 
# an object, including a "new" subroutine to make 
# one, so that we can write code like

my $fred = Person->new( name=>'Fred', age=>23 );
$fred->addBrother('Alfred');
$fred->summarize;

# which would print

#     The person 'Fred' is 23 years old.
#     Brothers are Alfred.

# -----------------------------------

# That's it.

 
