\documentclass{article}

%% \usepackage{lgrind}

\setlength{\topmargin}          {-0.5in}
\setlength{\evensidemargin}     {0.0in}
\setlength{\oddsidemargin}      {0.0in}

%%\setlength{\leftmargin} {-0.5in}
\setlength{\textwidth}  {6.5in}
\setlength{\textheight} {9in}
\newcommand{\ret}{\framebox{\tiny RETURN}}
\newcommand{\ctrld}{\framebox{\tiny Ctrl-D}}
\newcommand{\ctrlc}{\framebox{\tiny Ctrl-C}}

\begin{document}

\begin{center}

{\em Programming in Perl, Spring 98} \\
\underline{Homework 1} \\
{\bf Due : Monday, April 6 at midnight} \\
{\bf Weight : } 10\% of course grade \\
Instructor : Louis Ziantz

\end{center}


This assignment consists of a series of small Perl programs.  It
should be submitted according to the {\em Submission Guidelines}.
Don't forget to comment your program, providing a general description
of the Perl script along with inline comments for important or complex
portions of code.  

%% Please get started on the assignment as soon as possible.  

\begin{itemize}

%  \item [$\,$] {\bf Problem 1} The following is the initial part of
%  the sequence of {\em Fibonacci numbers} : 0, 1, 1, 2, 3, 5, 8, 13,
%  21, \ldots , where each number after the first two is the sum of the
%  two preceding numbers.  The ratios of consecutive Fibonacci numbers
%  (0/1, 1/1, 1/2, 2/3, 3/5, 5/8, 8/13, \ldots) approach the ``golden
%  ratio'' $\frac{\sqrt{5} - 1}{2}$.  Write a Perl script called
%  \underline{golden.plx} that calculates all the Fibonacci numbers
%  less than 5000 and the decimal values of the ratios of consecutive
%  Fibonacci numbers.  The program should keep a count of the ratios
%  and, as each ratio is computed, output the count, the ratio, and the
%  golden ratio, each separated by two tabs.  One line should be
%  printed for each ratio computed.  Before any of the lines are
%  printed a header should be printed consisting of ``Count'', two
%  tabs, ``Ratio'', two tabs, and ``Golden Ratio'' on a single line.


  \item [$\,$] {\bf Problem 1} Write a Perl script that finds the
  median(s) of a list.  Informally, a median of a list is an item
  which is ``halfway'' down the list (that is, there are as many items
  above and below the median in the list).  More precisely, if there
  are $n$ items in a ordered list and $n$ is odd, there is a single
  median at location $\frac{n+1}{2}-1$.  If $n$ is even, there are two
  medians at locations $\frac{n}{2}-1$ and $\frac{n}{2}$.  Though the
  term median is normally applied to numerical data, your script
  \underline{median.plx} should find the median of a list of words.
  The script should allow the user to enter a series of words (or
  other lines of text), each followed by a newline, with the entire
  list terminated by a \ctrld.  It should read the lines into an array
  and sort them based on ASCII ordering.  The program should then
  print the median or medians of the list.  To make testing easier,
  you can generate a list of words using the editor of your choice.
  If the resulting file is named \underline{words.dat}, the script may
  then be run as {\tt median.plx < words.dat}.

  \item [$\,$] {\bf Problem 2} Write a Perl script called \underline{
  histogram.plx} that prints a histogram for a set of integers ranging
  from 0 to 99.  The histogram will be divided into ten intervals,
  each with a ten point range.  That is, the zeroth interval represents
  values from 0-9 while the ninth interval represents values from
  90-99.  An array can be used to maintain the number of values that
  fall in each interval of the histogram.  The script should process
  integers until a -1 is encountered.  As each value is read from
  STDIN, the appropriate location in the array should be incremented.
  Remember that in Perl division always yields a decimal value.  If
  \verb+$x+ is a variable holding a floating-point value, it may be
% $ ;; because Latex mode in emacs wants to see another dollar sign
  truncated to an integer value using the Perl builtin function
  \verb+int+ (that is, \verb+int($x)+).  Once all data have been input, the
% $ ;; because Latex mode in emacs wants to see another dollar sign
  histogram should be printed.  Histogram intervals should be output
  one to a line.  Each line should begin with the interval number,
  followed by a space, a colon, and another space.  An asterisk
  (\verb+*+) should be printed for each value falling in an interval.
  Thus, if the array indicates that the zeroth interval contains 6
  values, \verb+0 : ******+ should be printed as the first line.  Given a
  file \underline{integers.dat} with one number to a line (and -1 as
  the last value), the script may be run as {\tt histogram.plx <
  integers.dat}.

   \item [$\,$] {\bf Problem 3} A small mail-order compact disc
company uses the price codes and corresponding prices as given below
on the left and charges shipping and handling based on the total
number of items ordered as indicated below on the right:

  \begin{center}
  \begin{tabular}{|l|r|c|l|r|} \cline{1-2} \cline{4-5}
Code  &  Price           & \mbox{\hspace{2in}} &   CDs & Charge \\ \cline{1-2} \cline{4-5}
CL1   &  12.98           &            &   1--3 & 3.49 \\
HR1   &  11.98           &            &   4--6 & 6.49 \\
CL2   &  10.98           &            &   7--9 & 8.99 \\
SR1   &  11.98           &            &   10+ & 10.99 \\ \cline{4-5}
SR2   &  14.98                        \\
SR3   &  24.98                        \\ \cline{1-2}
  \end{tabular}
  \end{center}

Store the price information as a hash called \verb+%code_to_price+
using the price codes as keys and the prices as values.  The shipping
and handling charges should be stored in an array.  A nested if
structure can be used to select appropriate elements of the array when
calculating shipping and handling.

%%Shipping and handling is charged based :
%%
%  \begin{center}
%  \begin{tabular}{|l|r|} \hline
%CDs & Charge \\ \hline
%1--3 & 3.49 \\
%4--6 & 6.49 \\
%7--9 & 8.99 \\
%10+ & 10.99 \\ \hline
%  \end{tabular}
%  \end{center}

Write a Perl script \underline{cd.plx} that processes a customer's
order.  The script should first prompt for a customer's account
number.  You may assume any account number entered is valid.  The
program should then display the following menu:
\begin{verbatim}
P -- Place a CD on order.
R -- Remove a CD from on order.
T -- Total bill and list CDs ordered.
E -- Exit.
Type choice and hit enter:
\end{verbatim}
The script should respond correctly to either a lower or uppercase
character.  The actions to be taken for each choice are as follows:


   \begin{enumerate}

     \item[P ] 
     
     \begin{itemize}

       \item prompt the user to enter a code for the CD to be
       purchased 
   
       \item read the cd code input (you may assume any code entered is
       valid) 

       \item prompt the user to enter a price code

       \item read the price code input

       \item use the price code entered to index the 
       \verb+%code_to_price+ hash.  If the corresponding value is
       defined, use the cd code as a key to a hash called
       \verb+%purchase+ and store the price as a value of
       \verb+%purchase+.  If there is no value in
       \verb+%code_to_price+ for the price code entered, print a
       message that an invalid price code was input and do not modify
       the \verb+%purchase+ hash (do not reprompt; display the main
       menu again).

    \end{itemize}

     \item[R ] 
     
     \begin{itemize}

       \item prompt the user to enter a code for the CD to be
       purchased 
   
       \item read the cd code input (you may assume any code entered is
       valid) 

       \item if there is no value corresponding to the cd code in
       \verb+%purchase+, print an appropriate message (do not
       reprompt); otherwise, delete the location in \verb+%purchase+
       associated with the cd code entered

    \end{itemize}

     \item[T ] 
     
     \begin{itemize}

       \item output a line indicating the customer's account number
   
       \item output a header for two columns that will be printed;
       it should consist of ``CD code'', two tabs, and ``Price''

       \item print the cd code and price for each order stored in
       \verb+%purchase+ (separate them by two tabs)

       \item use each price to total the charges for the
       customer.  In addition, count the number of items purchased. 

       \item use the number of items purchased to determine shipping
       and handling charges and add this to the total bill (ignore
       possible taxes)

       \item print the number of items purchased and the total cost to
       the customer

       \item some example output:
\begin{verbatim}
Purchases for account number M21C:
CD code         Price 
CL3456          $12.98
CL3367          $10.98
SR2345          $11.98
SR8889          $24.98
Total items on order: 4
Total due (includes shipping and handling): $67.41
\end{verbatim}
% $ ;; because Latex mode in emacs wants to see another dollar sign
     \end{itemize}

     \item[other] 
     
     \begin{itemize}

       \item indicate that the choice entered was invalid
   
     \end{itemize}

  \end{enumerate}

The script should continue prompting for choices until an 'E' or 'e' is input
by the user.

\end{itemize}

\vspace{0.2in}

\noindent
Recall from the first lecture that in order to run a Perl script, you
need to do the following:

\begin{enumerate}
 
   \item place \verb+#!/usr/local/bin/perl -w+ at the beginning of the
   script file on a line by itself (assume the file is named
   \underline{script.plx})

   \item execute the following command at a UNIX prompt: \verb|chmod a+x script.plx|

   \item run the script by typing the following at a UNIX prompt:
   \verb+script.plx+

\end{enumerate}

\end{document}




   The paycodes are as follows:

   \begin{enumerate}

     \item[1 --] hourly worker

     \begin{itemize}

       \item {\em data needed for pay computation:} hours worked and hourly
       wage

       \item {\em pay computed as:} hourly wage * hours worked if hours
       worked $<$= 40; hourly wage * 40 + (1.5 * hourly wage * hours
       beyond 40 worked) otherwise

    \end{itemize}

    \item[2 --] commission worker

     \begin{itemize}

       \item {\em data needed for pay computation:} gross weekly sales

       \item {\em pay computed as:} \$250 + 5.7\% of gross weekly sales

    \end{itemize}

    \item[3 --] pieceworker

     \begin{itemize}

       \item {\em data needed for pay computation:} pay per item and number
       of items made

       \item {\em pay computed as:} pay per item * number
       of items made

    \end{itemize}

  \end{enumerate}

    For example, if {\tt Bob Smith} is entered as the employee name,
    and {\tt 1} is entered as the paycode (hourly worker), then the
    script should prompt for hourly wage (suppose {\tt 8.00} is
    entered) and hours worked (suppose {\tt 50} is entered).  The
    script would then print {\tt The weekly pay for Bob Smith is
    \$440.} and prompt for the next employee, exiting if just a \ret\
    is entered.

\pagebreak


  \item [$\,$] {\bf Problem 5} Write a Perl script called
  \underline{seven.pl} that prints out {\em The Seven Lectures of
  Perl}.  You will need to initialize an array {\tt @num\_to\_day}
  with a list of {\tt 'First'}, {\tt 'Second'}, on up to, {\tt
  'Seventh'}.  You can then use {\tt @num\_to\_day} to initialize hash
  {\tt \%day\_to\_num} using a hash slice so that {\tt \%day\_to\_num}
  has key-value pairs ('First',0), ('Second', 1), and so forth to,
  ('Seventh', 6).  You should also create a hash {\tt \%seven\_days}
  with the following keys and values:

  \begin{center}
  \begin{tabular}{ll}
'First' & 'Literals and scalars' \\
'Second' & 'Arrays and control structures' \\
'Third' & 'Hashes and regular expressions' \\
'Fourth' & 'Whatever we fit in after the test' \\
'Fifth' & 'Simple CGI programming' \\
'Sixth' & 'Whatever we fit in before the exam' \\
'Seventh' & 'What the exam actually looked like'
  \end{tabular}
  \end{center}

  For each {\tt \$key} in the hash {\tt \%seven\_days}, 

  \begin{itemize}

    \item print \verb+"By the $key day of Perl class, we learned about...\n"+

    \item use {\tt \$key} on the hash {\tt \%day\_to\_num} to get a
    number, call it {\tt \$bound}

    \item iterate from {\tt \$bound} downto 0 (call the loop variable
 {\tt \$i}),

    \begin{itemize}

      \item  use {\tt $@$num\_to\_day} and {\tt \$i} to create an
appropriate {\tt \$daykey} into the hash {\tt \%seven\_days}

      \item print a tab followed by the result of accessing {\tt \%seven\_days}
with {\tt \$daykey} followed by a newline

    \end{itemize}

  \end{itemize}

  So, if {\tt \$key} is {\tt 'Third'}, we get the following:
\begin{verbatim}
By the Third day of Perl class, we learned about...
        Hashes and regular expressions
        Arrays and control structures
        Literals and scalars
\end{verbatim}

  Since we are iterating over a hash, will we be printing the "By the
  \$key day of..." lines out in order from First to Second to
  ... Seventh?  Think about it.

\end{itemize}

\end{document}


	Literals and scalars
        Arrays and control structures
        Hashes and regular expressions
        Whatever we fit in after the test
        Simple CGI programming
        Whatever we fit in before the exam
        What the exam actually looked like



        Twelve lords leaping
        Eleven ladies dancing
        Ten pipers piping
        Nine drummers drumming
        Eight maids milking
        Seven swans swimming
        Six geese laying
        Five gold rings
        Four colly birds
        Three french hens
        Two turtle doves and
        A partridge in a pear tree 

Files : 

  list of secret words
  list of contestant files
  contestant files

Store secret words in an array
Store contestants in an array or hash

foreach contestant
  open the contestant file
  for i = 1 to numwords
    search for word in the file
    if it matches,
      increment wordcount 
    endif
    ``rewind'' file
  endfor
  if wordcount == numwords
    output ``Congratulations, <name>, you have won the prize!''
  else

    


