This Laboratory may be done individually or in pairs. There is a lot of work here; you have until the end of the week to complete it.
Again, be careful in laboratories! Avoid cables, avoid electrocuting yourself, change position every 30 minutes.
Again, let me know if you can't login to Windows (or Mac OS X) or if you can't login to grue (grue.ict.griffith.edu.au) using the ssh client putty (or the ssh command from the Terminal application). For this exercise, you can use dwarf (dwarf.ict.griffith.edu.au) or grue (grue.ict.griffith.edu.au). If you can't login to grue or dwarf, work with someone who can, until you can.
Again, print my Linux command summary in the Resources page, study it, and keep it handy for future reference. If you are using vi on Linux, print my Vi command summary. If you are using Mac OS X, read Apple's Switch 101.
Complete all tasks in Exercise 1. In particular, make sure that your HTML page has all the elements requested, and that it validates as an HTML5 document.
Reread the lecture notes on Python and read Sections 3 to 6 of the Python Tutorial.
Create a Python module fibo.py in your labs directory
on grue containing the following
function definition:
def fib(n):
"""Print the Fibonacci sequence up to n."""
a, b = 0, 1
while b < n:
print b,
a, b = b, a+b
print
Be precise with indentation! Use spaces not tabs for indentation. Configure your editor to do this automatically. Learn to use your editor's indent and outdent shortcuts.
Run the python interpreter and import function fib from the
module using the command:
>>> from fibo import fib
Call the function fib as follows:
>>> fib(100) 1 1 2 3 5 8 13 21 34 55 89
Add a second function fib2 to the module fibo.py. A
function call fib2(n) should return a list containing the
Fibonacci sequence up to n. When called, your new function should behave
as follows:
>>> fib2(100) [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
To be clear, function fib2 does not contain any
print statements; it simply creates and returns a list of Fibonacci
numbers. Instead of printing Fibonacci numbers, it appends them to the
list that it eventually returns. Remember to give every Python function
(and class) a short, clear, complete documentation string.
Hint. Study the way lists are used in the factorisation example.
Copy the module frequency.py to your
labs directory on grue. Copy the small text file preamble.txt to
the same directory. (This is the preamble to the Universal Declaration of
Human Rights.). Run the Python interpreter, import the module, and call
function count on the text file. It should print the number of
occurrences of each word in the
file.
Study the source code of the module until you understand every aspect of it!
Call function count again, first to print the number of
occurrences of all words in your text file in decreasing frequency, and
then to print the frequencies of the 10 most frequent words only.
Copy the content of module frequency.py into a new module
concordance.py in your labs directory. Modify the
module so that it prints a concordance: the list of line numbers
where every word in a text file occurs. So, if word "the" occurs in lines,
1, 2 and 4, and word "course" occurs in lines 2 and 3, the module should
print:
course: 2 3 the: 1 2 4
Words should be printed in alphabetical order.
Hint. Modify the variable frequencies in module
frequency.py to map words to lists of line numbers
(instead of to counts). Whenever a word occurs on a new line, append the
line number to the list associated with that word.
Note. Some students might find this a difficult exercise. But you only learn by pushing yourself! And it's OK to ask for help.
(See Exerecise 11 in Laboratory 1.) Create a
Python module items.py that contains a definition of a class
Item. Each item should contain a name which is a string and a
price which is a real. Define a function __str__(self) in class
Item to display instances of class Item.
Import this module. Create instances of class Item.
(See the Student-Course-Enrolment example in Lecture 2.)
Create a Python module students.py that contains definitions of
classes that allow you to model students (ids, names and addresses),
courses (codes, titles, convenors and prequisites) and enrolments
(students, courses, years, grades). Note that the enrolment relationship
between courses and students is a many-to-many relationship and the
prerequisite relationship between courses and courses is also a
many-to-many relationship. Define a function __str__(self) in
every class. Import this module. Create
and display instances of all three classes.
Extend the previous question to store all instances of class
Student in a list that is a class attribute of class
Student, and similarly for classes Courseand
Enrolment. Using the Python interpreter, load the extended
module, create instances of classes Student, Course and
Enrolment, and give expressions to find all courses that I (for
example) convene and all students enrolled this year in course
7401ICT. (This question may be not so easy, but is easier in
Django.)
Note. You must write documentation strings for every Python module, class and function that you define. More generally, you must follow the Python style guidelines.