CT240 lab10
http://geminga.it.nuigalway.ie/~gettrick/courses/CT240/labs/l10.html
-
For this lab you must submit a printout of all source code (python
files), and the result(s) of running the program on (a few)
test cases.
The source code must
be well presented (indenting, spaces, reasonable variable/function names,
etc.) and must include comments (as a rough guideline - aim to have nearly as
many comments as lines of code). Any questions asked should be answered on a plain sheet of paper.
-
The above material should be given to Paul or left in the cardboard box marked CT240 in the mail room (room IT415) on the 4th floor of the IT Building
before
the deadline of 5pm
Monday 1st. December 2008. You will lose 20% for each day
(or part of day) the lab is late. If you have a genuine reason for
submitting a late lab, please contact Paul before the lab
due date/time.
-
Plagiarism (the unattributed copying of work from other sources
(internet, fellow students,....)) will not be tolerated. Please see
http://www.nuigalway.ie/engineering/documents/plagiarism_guide_students
_v4.pdf. You risk getting zero for your lab if it is found to be
plagiarized.
In this lab, you should run PYTHON from the command line
(i.e. do not run IDLE). In Windows, click on the start menu, and
select "Visual Studio 2008 Command Prompt" [or: select firstly
"Microsoft Visual Studio" followed by
"Visual Studio 2008 Command Prompt" in a sub-menu].
Alternatively select the menus MAIN --> UTILITIES --> Command Line.
Then you should type in
cd C:\Python25. Now in your current folder you have the PYTHON
executable. So you can type
- python
- python file.py to run the program file.py
- python file.py arg1 arg2 arg3 ...
to run the program file.py with some command line arguments.
Note that in practice file.py may be C:\file.py or F:\file.py or
U:\file.py etc. depending on where you have the program (e.g. on USB
stick).
Note also: If you can't find PYTHON executable: Go to SEARCH in the
main menu, and type in python in the search.
Write a program in PYTHON that searches a (text) file for a given
string. The user should be allowed to put the name of the file, and
the name of the string to be searched for, as
command line arguments. Your program should also use 3 flags:
- -h (meaning help), which does not
run the search but just prints out a help message.
- -s which searches for a string that
occurs anywhere (i.e. as part of a word or on its own).
- -w which searches for a word,
i.e. a string surrounded by whitespace (space, tab, newline,...)
Moreover, your program should output to the user, the line numbers
in the text file where the string occurs.
An example follows: suppose the text file is called carol.txt,
containing the 4 lines:
Good King Wenceslas looked out
on the Feast of Stephen,
When the snow lay round about,
deep and crisp and even.
and suppose you name your PYTHON program search.py: Then
-
python search.py -s out carol.txt
should return the numbers 1 and 3 (where the string "out" occurs)
-
python search.py -s ee carol.txt
should return the number 4
-
python search.py -w out carol.txt
should return the number 1 only.
You should use the find() method of the string object to do
the search.
©
NUI, Galway