Assignment 5. CS330 Programming Languages, Spring 2004 

PLEASE READ CAREFULLY
MISSING
DETAILS MAY COST YOU

(due date April 2)

Details

A random 25% of students will be orally interviewed by me in person
regarding their submission. Make sure you have written everything you
submit. For any function you are trying to write, you can request
what is the desired output for a particular input by email. This is assignment
will be done individually.

Late assignment policy:  If the assignment is submitted
within three days from when it was due, you will get half the grade you
would get if you had submitted on time. You will not be able
to submit after three days. (exceptions to this rule only
if you have a VERY important reason).


IMPORTANT: The careful design and documentation (i.e comments) of
your code will be important factors in your grade. If there is a bug
it's better to report it than hide it. Be honest, precise and clear and
you shall be rewarded. NEVER (at least in this class) sacrifice
clarity for efficiency of execution.

(10% of final grade - 10 points)

Astronomy in Prolog (3 points)

An astronomer provides you with the following information about celestial objects in English:
Sun, sirius and betelgeuse are stars.
Mercury, venus, earth, and mars orbitthe sun.
Moon orbits the earth.
Phobos and deimos orbit mars.

A planet is a celestial object that orbits the sun.
A satellite is a celestial object that orbits a planet.
A celestial object is part of the solar system if it is the sun, or a planet or a satellite of a planet.


Encode these facts in Prolog building a system that can answer queries about celestial objects.
Some examples that you will need to support are:

orbits(mars, sun)    ?          (yes)
orbits(moon, sun)   ?          (no)
orbits(phobos, B)   ?           (mars)
orbits(B, mars)        ?          (phobos and deimos)
planet(mars)           ?           (yes)
planet(P)                 ?           (mercury, venus, earth)
satellite(phobos )     ?          (yes)
satellite(S)                ?          (moon, phobos, deimos)
solar(sun)                 ?         (yes)
solar(sirius)             ?          (no)
solar(B)                   ?          ( sun, mercury, venus, earth, mars, moon, phobos, deimos)


To run prolog, type in your rules and facts in a file with extension .pl
Some example code can be found here:
prologExamples.tar

Type grolog and at the prompt you can load
the facts/rules by typing
['append.p'].

Then you are ready to issue queries. Remember the distinction between variables (capital letters)
and relations,

List stuff in Prolog (3 points)

As we showed in class, a definition of the relation member for lists in Prolog is the following:

mymember(X, [X|T]). 
mymember(X, [Y|T]) :- mymember(X,T) 

Define a relation multiple that is the property of being a list with multiple
occurances of some element. (hint: use mymember) For example:
multiple([1,2,3]) is false and multiple([1,2,2,3]) is true.

Define a relation last such that last(X,L) is true if X is the last element of list L.
For example last(1,[1,2,3]) is flase and last(3,[1,2,3]) is true.

Define a relation drop such that if drop(L1, N, L2) is true L2 is obtained
by dropping every Nth element from L1. For example:
drop([1,2,3,4,5,6], 2, [1,3,5]) is true.
(hint: define a relation dropaux(L1, N, L2, K) where L2 is obtained from L1 by first copying K-1 elements
   and then dropping an element and, from then on, dropping every N'th element. Use dropaux
    to define drop)
(hint to evaluate you have to use the is keyword:    for example   K1 is K -1 if K is 5 then K1 will become 4)




Scripting in Python (4 points) 

A common task in scripting language is to take as input a text file (examples could be apache log files, protein data in bioinformatics,
student grades etc) and summarize/report the information contained in the file in some non-trivial way. Another important feature
that scripting languages have is the ability to use regular expression to find strings. The following code snippets illustrates
the use of regular expressions in Python:

import re

p = re.compile('Hello(.*)') # regular expression for strings with Hello as a prefix
if p.match('HelloWorld'):
print 'It matches'
else:
print 'No match'

if p.match('HeloWorld'): 
print 'It matches'
else:
print 'No match'


Your task is to write a python program to summarize a text file with data from
assignment 1. The text file contains lines of the form:

student_id       language     linesofcode


Your task it to output the number of students/average loc for each programming language.
In addition you can select a subset of languages to be summarized by providing a prefix
using the -p command line argument. By default all the languages are printed.
In addition the -i, -o options are used to specify the input and output file
(the output file should contain the same results that are printed directly to stdout)

You can use the following skeleton code as a starting point  (it just copies
the input file to the output file):
asn5.py

The data file used for input is the following:
data.txt


Here is the output of 3 runs of the script with different options:

[gtzan@arion assn4]$ summary.py -i data.txt -o output.txt
Language/Number of students
C# 1
C 2
PHP 1
Java 26
Python 1
Scheme 1
C++ 1
Perl 39

Language/Lines of code
C# 132
C 1164
PHP 326
Java 6213
Python 152
Scheme 87
C++ 113
Perl 5039


[gtzan@arion assn4]$ summary.py -i data.txt -o output.txt -p P
Language/Number of students
Python 1
PHP 1
Perl 39

Language/Lines of code
Python 152
PHP 326
Perl 5039

[gtzan@arion assn4]$ summary.py -i data.txt -o output.txt -p Py
Language/Number of students
Python 1

Language/Lines of code
Python 152






Extra credit 

Implement any of the previous assignments or parts of them in Python/Prolog.
Implementations in Python will get 1/2 of the original credit for that part.
Implementations in Prolog will be worth 3/4 of the original credit.
For example turtle in Python will be worth 2 points and turtle
in Prolog will be worth 3 points.