Вы находитесь на странице: 1из 3

BIRLA INSTITUTE OF TECHNOLOGY & SCIENCE, PILANI

PILANI CAMPUS
FIRST SEMESTER 2016 2017
PRINCIPLES OF PROGRAMMING LANGUAGES (CS F301)
ASSIGNMENT
Date: 28.10.2016
Submission Due: 12.11.2016

Weightage: 10 % (20 M)
Type: Open Book

Instructions:
1. Answers of Q1 must be typed in a Word file in .docx and the name of your file must be your complete ID followed
by a dot(.) and docx [Example 2014A7PS019P.docx]. For Q2, deliverable should be in the form of a single RKT
file named as ID.rkt [Example 2014A7PS019P.rkt]. Submit both the files on Nalanda.
2. All submissions will be subjected to plagiarism check using Turnitin Plagiarism Detection software which not
only checks the complete collection of files submitted by a teacher, it also checks against all internet and book
resources and amongst the submitted student files.
3. If a submission is found plagiarized, then it will be penalized with negative marking equal to the weightage of the
question whose answer is found plagiarized. (Example: If the question is of 5 marks, there will be negative
marking of 5 marks for plagiarized answer).

Q1.

[2 + 1 + 3 + 1.5 + 1 + 1.5 = 10 M]
Write EBNF descriptions for the following:
i. A Java class definition header statement
ii. A Java method call statement
iii. A C switch statement
iv. A C union definition
v. C float literals
b) Analyze and write a comparison of Cs malloc and free functions with C++s new and delete operators. Use
safety as the primary consideration in the comparison.
c) Rewrite the following code segment using a multiple-selection statement in the following languages:
a)

if ((k == 1) || (k == 2)) j = 2 * k - 1
if ((k == 3) || (k == 5)) j = 3 * k + 1
if (k == 4) j = 4 * k - 1
if ((k == 6) || (k == 7) || (k == 8)) j = k 2
i.
Fortran 95
ii.
Ada
iii.
C
iv.
C++
v.
Java
vi.
Python
vii.
Ruby
Assume all variables are integer type. Discuss the relative merits of the use of these languages for this
particular code.
d) Design and implement a C++ program that defines a base class A, which has a subclass B, which itself has a
subclass C. The A class must implement a method, which is overridden in both B and C. You must also write
a test class that instantiates A, B, and C and includes three calls to the method. One of the calls must be
statically bound to As method. One call must be dynamically bound to Bs method, and one must be
dynamically bound to Cs method. All of the method calls must be through a pointer to class A.

e)

Write a Prolog program that implements quicksort.

f)

Suppose you are given a collection of parent" facts. For example,


Parent (George, Edward). // A parent of George is Edward
Parent (Edward, Victoria). // A parent of Edward is Victoria
Parent (Edward, Albert).
// A parent of Edward is Albert
Consider the following two PROLOG programs (x, y, and z are variables). The only difference is that the
clauses in the tail of the second rule are reversed.
Ancestor(x,y) :- Parent(x,y).
Ancestor(x,y) :- Parent(x,z), Ancestor(z,y).
Ancestor(x,y) :- Parent(x,y).
Ancestor(x,y) :- Ancestor(z,y), Parent(x,z)
What difference does the reversal make in respect to the search space of a query? In particular, are there any
differences in the solutions? Which is a better PROLOG program and why?

Q2. Implement given algorithm in Scheme programming language for clustering words given in a file.

[10 M]

Representation:
Each cluster is represented by a triplet (id, member, distances), where id is a unique number, member is a list of
words assigned to this cluster, and distances is list of pairs where first element is the id of another cluster and second
element is distance of that cluster from current cluster.
For example, (3, (is it in), ((1, 1/6), (2, 1/8), (4, 1/3), (5, 1/20))) represents,
id of this cluster = 3
member words = "is", "it", "in"
distances =
distance from cluster with id 1 = 1/6
distance from cluster with id 2 = 1/8
distance from cluster with id 4 = 1/3
distance from cluster with id 5 = 1/20
Algorithm:
1. Read all words of a file in a list wordlist.
2. Create a list clusterList by assigning each word to a new cluster. For example, a cluster containing word "is" will
be represented as (1, ("is"), ()). The distance tuple of all new clusters will be empty.
3. Calculate pair wise distance between each cluster by using function CtoCdistance(), and update the distance element
of each cluster. Represent self distance with value 100.
4. Repeat until more than 8 clusters are left.
i. (a, b) <-- Find two clusters which are nearest to each other
ii. Create a new cluster list by removing a and b and inserting a new cluster c which should be created as
follows:
a. Create a unique id for c
b. Take union of member word list of a and b to create member word list of c.
c. For each distance entry (id, a_dist) in a and (id, b_dist) in b, the distance entry for id in c should
contain minimum distance of a_dist and b_dist i.e. in c it should be (id, min(a_dist, b_dist)).
d. Distances list of all remaining clusters should be updated to represent distance from new cluster
c instead of a and b. For each entry in distance list of cluster x,
If id of first element in distance list is not a or b, then insert the distance tuple as such.
For tuples, containing id of a or b, a new tuple entry should be done such that id is id of
cluster c and distance is minimum of distance from a and b of that cluster x, i.e. it should
be (c_id, min(a_dist, b_dist)).
5. Display all clusters such that each cluster is printed on a new line and all member words are separated by a space.

CtoCdistance(a, b):
1. Create a list by calculating distance between each pair of words in cluster a and b
i.e.
distList = {d, such that d is WtoWdistance(aa, bb) where aa belongs to membership list of a and bb belongs to
membership list of b}
2. Return minimum of distList as the distance between a and b.
WtoWdistance(aa, bb):
1. dist = [ 1 - (number of characters in intersection of characters of aa and bb)/(number of characters in union of
characters of aa and bb)],where words aa and bb are treated as set of characters. Matching should be case insensitive.

Instructions:
1. Deliverable should be in form of a single RKT file named as ID.rkt (Use full BITS ID like 2014A7XXYYYP.rkt).
2. This file should have 5 identifiers named as step1, step2, step3, step4, and step5 which should display the output
after each of the 5 steps of algorithm. For example, if the input file contains following text: "this is fun that then
those in it can ran run", for the following driver program:
(display step1) (display step2) (display step3)
Following should be the output:
(this is fun that then those in it can ran run)((1 (this) ()) (2 (is) ()) (3 (fun) ()) (4 (that) ()) (5 (then) ())
(6 (those) ()) (7 (in) ()) (8 (it) ()) (9 (can) ()) (10 (ran) ()) (11 (run) ()))((1 (this) ((1 100) (2 1/2) (3 1)
(4 3/5) (5 2/3) (6 1/2) (7 4/5) (8 1/2) (9 1) (10 1) (11 1))) (2 (is) ((1 1/2) (2 100) (3 1) (4 1) (5 1) (6
5/6) (7 2/3) (8 2/3) (9 1) (10 1) (11 1))) (3 (fun) ((1 1) (2 1) (3 100) (4 1) (5 5/6) (6 1) (7 3/4) (8 1) (9
4/5) (10 4/5) (11 1/2))) (4 (that) ((1 3/5) (2 1) (3 1) (4 100) (5 3/5) (6 2/3) (7 1) (8 3/4) (9 4/5) (10 4/5)
(11 1))) (5 (then) ((1 2/3) (2 1) (3 5/6) (4 3/5) (5 100) (6 1/2) (7 4/5) (8 4/5) (9 5/6) (10 5/6) (11 5/6)))
(6 (those) ((1 1/2) (2 5/6) (3 1) (4 2/3) (5 1/2) (6 100) (7 1) (8 5/6) (9 1) (10 1) (11 1))) (7 (in) ((1 4/5)
(2 2/3) (3 3/4) (4 1) (5 4/5) (6 1) (7 100) (8 2/3) (9 3/4) (10 3/4) (11 3/4))) (8 (it) ((1 1/2) (2 2/3) (3 1)
(4 3/4) (5 4/5) (6 5/6) (7 2/3) (8 100) (9 1) (10 1) (11 1))) (9 (can) ((1 1) (2 1) (3 4/5) (4 4/5) (5 5/6) (6
1) (7 3/4) (8 1) (9 100) (10 1/2) (11 4/5))) (10 (ran) ((1 1) (2 1) (3 4/5) (4 4/5) (5 5/6) (6 1) (7 3/4) (8
1) (9 1/2) (10 100) (11 1/2))) (11 (run) ((1 1) (2 1) (3 1/2) (4 1) (5 5/6) (6 1) (7 3/4) (8 1) (9 4/5) (10
1/2) (11 100))))

2. Language selected in DrRacket should be "Determine language from source" and first line of program should be
"#lang racket". Code should be strictly compatible with version 6.7 of DrRacket.
3. Second line of program should be a definition for file name inputFile and rest of the code should use this variable
to read input words i.e.
(define inputFile "/home/fduser/ppl/input.txt")
4. Do not use any library which involves explicit inclusion i.e. using require keyword
5. Write pure functional program as much as possible. Do not use mutable lists. Minimize use of mutable values.
6. Input test files will not have any special characters.
7. Follow the instructions strictly, so that your submission doesn't get rejected by "Automated Evaluater Script".
8. ID of cluster should be continuously increasing integer starting from 1. Whenever two clusters are merged, the ID
of new cluster should be the next integer not yet taken. Hint: Create a global counter procedure, and call it every
time to get a new ID, whenever clusters are merged.
9. In case, multiple cluster pairs are at same nearest distance, only merge the pair which appears first in cluster list
i.e. first pair returning minimum value.

END

Вам также может понравиться