Homework 1
This first homework will introduce you to the basics of writing a larger program in Perl. The homework is in two parts. Part A can be completed with the knowledge learned in the first and second lecture only. Part B will require knowledge from the third lecture.
Part A
Prompt the user for the path to a file to open for reading. Open that file. The file contains three lines. The first line contains two numbers, separated by a comma. The second line contains a directory name. The third line contains a file name in all caps.
Create the directory specified by the second line (you may assume it does not already exist, and that you have permissions to create it). Create a file named by the third line after converting all but the first character to lowercase, inside the new directory. Write to this file the sum, product, difference, quotient, and exponentiation of the two numbers.
For example, if the file contained:
5,2 foo BAR.TXT
Then you would create the directory foo, and open a new file
for writing named foo/Bar.txt. You would print the following
lines to that file:
Sum: 7 Product: 10 Difference: 3 Quotient: 2.5 Exponentiation: 25
Note that if the second number is 0, you should instead print a message to the file that you can't divide by 0, rather than attempting to print the quotient (which would cause your program to crash).
Part B
Play a guessing game with the user. The user will enter three numbers on the command line. The first number is how many letters the secret word has. The second number is which word of that length to choose. The third number is how many chances the user has to guess the word.
When your program starts, read from the file /usr/dict/words to find the correct
file. For example, if the user's command line options were 5 3 10 then the
user would have 10 chances to guess the 3rd 5-letter word in /usr/dict/words (which
happens to be "aback")
To guess, the user enters a letter. If that letter is in the word, tell the user in which position(s) it's in. If not, the number of chances left decreases. If that was the last chance, the game is over. Show what the secret word was. If the user enters a full word, the game ends immediately, and you print out whether or not that guess is correct (if it wasn't, show what the secret word was).
Here is a sample execution of your Part B:
You have 10 chances to guess 5-letter word #3: c 'c' is in the word at position(s): 4 You have 10 chances to guess 5-letter word #3: d 'd' is not in the word You have 9 chances to guess 5-letter word #3: a 'a' is in the word at position(s): 1, 3 You have 9 chances to guess 5-letter word #3: alpha 'alpha' is not the correct word The correct word was 'aback'
You may assume that all command line options entered are infact integers (because we haven't yet covered the method for testing that). However, entering more or less arguments than are required is an error, as is entering a length of 0 or 1, or a word number or chances number of 0. All of these errors should be checked for and dealt with accordingly (see below)
Combining the parts
Your submissions must be contained in one single file. If your file is executed without any command line arguments present, it will run Part A. If there are any command line arguments given, it will run Part B.
Grading Criteria
| A: Open file specified by user | 5 |
|---|---|
| A: Read in two numbers | 5 |
| A: Read in directory and file | 5 |
| A: Create directory | 2.5 |
| A: Create file | 10 |
| A: Print computations | 10 |
| B: Parse Command line | 2.5 |
| B: Obtain proper secret word | 12.5 |
| B: Get user guess | 2.5 |
| B: Print position(s) of guess | 10 |
| B: End when out of chances | 5 |
| B: Check full word guess | 5 |
| Combining the Parts | 5 |
| No Warnings | 5 |
| Error Checking | 5 |
| Code Style | 5 |
| Output Style | 5 |
Penalties
- Late
- A submission turned in within 16 hours past the deadline will lose 20 points. A submission turned in more than 16 hours past the deadline will be graded a 0
- Compilation
- If your program fails to compile, it will be graded subjectively based on the code written, and then lose 50% of the remaining points.
No Warnings
No compilation nor runtime warnings can be generated by Perl evaluating your code. That is,
I should never see "use of uninitialized value" or "illegal division by 0" or similar.
(You are, of course, welcome and encouraged to use the warn
and die functions to tell the user when he/she has done something wrong). Your
code will be executed with warnings enabled, even if you don't explicitly
use warnings in your program.
Error Checking
You must check to make sure the user has not done something wrong. "Wrong" in this case means making sure files and paths exist, directory creation and file creation is successful, files in Part A have the right number of lines, the first line contains two values, proper command line arguments are given in Part B, etc. Your program should never crash due to unexpected input. A sensible error message should be printed to the user, telling him/her what went wrong.
Code Style
Your code must be easily read by a human being. Most important are three
facets: consistent indentation, meaningful variable names, and explanatory
comments. For a larger guide to writing well-styled Perl code, please
read perldoc perlstyle
Output Style
Your output must be easily read by a human being. Values and data should be labeled. Prompts should be explicit. White space and newlines should be used for visual distinction. Debugging statements should be removed before submitting.
Submission Instructions
Your program must be executable on either rcs-sun4.rpi.edu
or solaris.remote.cs.rpi.edu. Whichever you chooose, run
the program ~lallip/public/submit.pl and follow the prompts.
Your submission is due at 11:59:59pm on Tuesday, February 5, 2008. You may submit infinite times. Only the last submission will be graded.
