Professional Documents
Culture Documents
cs251 Quiz4 PDF
cs251 Quiz4 PDF
____________________________________________________________________________
q1 - Android
(13 Marks)
answers.txt
Specify your answers for the following questions in a txt file named answers.txt such that
every line has just one letter ranging from a, b, c and d. In case you don’t know the answer
to a question then put in a - (hyphen) for that respective line.
i.e. answers.txt should have content similar to the following, no extra spacing and all in capitals.
$ cat answers.txt
c
d
-
d
a
and so on …
This question will be auto-graded so make sure that you follow the submission format precisely.
Use Sublime Text for editing answers.txt so that you can match the respective question
number with the line number.
Marking Scheme:
+1 for every correct answer
-1 for every wrong answer
7. If you need to access the INTERNET in your android application, where would you
specify the need for it?
a. build.gradle file
b. AndroidManifest.xml
c. There is no need to specify such need, one can directly use it.
d. MainActivity.java
8. Let’s say you are on ActivityA of App1, then you clicked on the notification you received
of some WhatsApp message and now the WhatsApp has opened up. Which state of the
ActivityA of App1 be called.
a. onPause()
b. onStop()
c. onDestroy()
10. Which callback is fired when the system first creates the activity
a. onCreate()
b. onStart()
c. onResume()
11. The state in which the application interacts with the user
a. onCreate()
b. onStart()
c. onResume()
12. When you switch from ActivityA to ActivityB within the same application, in which state
would ActivityA be?
a. onResume()
b. onDestroy()
c. onStop()
d. onPause()
____________________________________________________________________________
Here in node.py you’re giving a simple node structure designed for a binary tree - a node either has a left
or right child - both nodes, or has None in place of them, indicating a leaf. No node in this binary tree has
just 1 child - either both or no children.
An internal node is defined as a node in the tree which has two children - these children may themselves
be a leaf or an internal node. Hence understand that by definition a leaf is not an internal node, and a node
is either an internal node or a leaf.
Henceforth, a tree is said to be of size n if it has n internal nodes. For a binary tree, this tree will have
in total 2n + 1 nodes: n + 1 of them will be leaves. A tree has at least one node as defined here - it’s head.
Definition: The mirror image of a tree is defined as given in figure 1 below. T1 is a mirror image of T2 if
one child of the head node of T1 is the mirror image of the other child of the head node of T2. For head
being a leaf - mirror image of a leaf head tree is itself.
Figure 1: Examples of trees that are mirror images of each other, not symmetrical
Function 2: Find all trees with ‘n’ internal nodes [10 marks]
Complete function def allTrees(n)using the explanation given below.
Don’t print anything. Do not change the name of or arguments to this function- it will be auto-graded.
Arguments (1): A single integer - number of internal nodes
Return value: List of heads of all unique trees that have n internal nodes. The order does not matter.
Limits - 0 <= n <= 11
Note that the list is exponential in n,
This function, on using list comprehension, can be completed with just 1 for loop, no nesting.
By a for loop, we mean an indented block that goes
for foo in bar:
blah
# end
...
Hence an instance of list comprehension is not a for loop
Hence you will be awarded full 10 marks for correct output and 1 for loop used, 8 marks if the output is
correct but two for loops have been used (nested or not), 6 marks if the output is correct but three loops
have been used (nested or not). 5 marks for correct output and more than 3 loops. Zero marks if the
output is incorrect.
Function 3: F
ind all trees with ‘n’ internal nodes that are symmetrical (i.e) they
exactly match their mirror images. [10 marks]
Complete function def allSymTrees(n)using the explanation given below.
Don’t print anything. Do not change the name of or arguments to this function- it will be auto-graded.
Arguments (1): A single integer - number of internal nodes
Return value: List of nodes that are heads to symmetrical trees. Again, the order does not matter.
This will be rather simple once you have completed allTrees
Definition of equality: The equality of two trees is equivalent to the equality of its head node, which has
been defined in the Node class. Read it carefully.
Definition of symmetry: if node is head of tree T, then T is symmetric iff node == mirror(node).
This is also exponential, 0 <= n <= 23
This function, on using list comprehension, can be completed without for loops.
Hence you will be awarded full 10 marks for correct output and no for loops and 7 marks if the output is
correct but one for loop has been used (nested or not). 5 marks for more than 1 loop. Zero marks if the
output is incorrect.
Hints for function 3:
1. What is the relation between all trees and all symmetric trees?
2. Even number of internal nodes - is such a symmetric tree possible?
3. What would happen if the left child of the head node is the mirror of the right child?
Just to reinforce what you might be thinking, and to make this question slightly easier, the following are
true. Use these to understand the approach for the last function.
a. len(allT rees(n)) == len(allSymT rees(2n + 1))
b. len(allSymT rees(n)) == 0
____________________________________________________________________________
Image resizing can be achieved by multiple techniques. What you have to implement here is Image
Resizing by Bilinear Interpolation. Go through this link to have an understanding of how bilinear
interpolation is done (the very first algorithm itself in the specified link should be enough for
understanding). You need to implement this resizing feature using numpy.
Fig. 1. An illustration of image resizing using bilinear interpolation. The four red dots show the data
points and the green dot is the point at which we want to interpolate.
You have been provided with two images barbara-input.png and barbara-output.png, two
python code files autograder.py and resizeImage.py and a numpy array file truth.npy in
quiz4-resources/q3/
Assume the pixel dimensions for the input image to be equal along both axes, i.e., assume an aspect ratio
of 1:1 for the axes. Consider the number of rows as M and the number of columns as N.
You need to resize the image to have the number of rows = 3M−2 and the number of columns = 2N−1,
such that the first and last rows, and the first and last columns, in the original and resized images,
represent the same data.
For this, you just need to code the function bilinearInterpolation() in resizeImage.py. Its
input and output are as follows:
Input: A square matrix in the form of 2D numpy arrays representing the original image
(barbara-input.png)
Output: The resized 2D numpy array with the specified number of rows and columns representing
the resultant image (barbara-output.png)
For testing your code, simply run autograder.py which will by itself call the function
bilinearInterpolation() and generate the resultant image. It will also report on the terminal
whether your output is correct or not. (DO NOT EDIT autograder.py)
____________________________________________________________________________
Models that assign probabilities to a sequence of words are called Language Models or LMs. In
this question, we will introduce to you the most simple model that assigns probabilities to
sentences or a sequence of words, the n-gram. An n-gram is a sequence of N words: a 2-gram
(or bigram) is a two-word sequence of words like “please submit”, “submit the” or “the quiz”,
and a 3-gram (or trigram) is a three-word sequence of words like “please submit the” or “submit
the quiz”. The final goal of this question is to get the unigram, bigram and trigram statistics of a
given corpus and use this information to predict the maximum likely next word given a sequence
of words and its probability.
You are provided with a data folder containing several text files (referred to as a corpus from
now on) in the q4 folder.
Eg:
body - I am a good boy and a bad guy
n=3
Some of the elements of the hashmap will be :
I -> 1
a -> 2
good boy -> 1
and a bad -> 1
etc.,
Note :
1. No need to clean the body
2. Split using whitespace (multiple whitespaces are considered
equivalent to single whitespace)
7. The function run in the file Worker.java. Worker class extends the Thread class. You
need to implement its run method (as described in ngrams_stats_mt.java). Its data
members are: [7 marks]
a. inqueue: it will be of type LinkedBlockingQueue<String>
b. outqueue: it will be of type LinkedBlockingQueue<HashMap<List<String>,
Integer> >
8. The function main in the file multi_threaded_lm.java.
It takes two command-line arguments : [8 marks]
a. the first argument is the path of the directory containing the corpus (remove
leading and trailing whitespaces)
b. the second argument is the path of the file containing queries (1 query on each
line)
Testing
We will use the frequency stats of the unigrams, bigrams and trigrams in the given corpus to
estimate the next word in a sequence i.e., given a sequence “I am going to ”, we need to predict
the next word in the sequence, it can be school, market or theatre, etc. For this, we will use an
advanced technique called Backoff and Interpolation in this question.
where λ1, λ2 and λ3 are parameters that need to be tuned based on the corpus provided.
λ1 = 0.0196
λ2 = 0.588
λ3 = 0.3924
Note that if anything in the denominator of the above formulae is 0 then set the corresponding
probability to 0.
Now your final task would be to find the word which maximizes the above probability and print
the word and the corresponding probability (tab-separated)
Output : argmaxwn P(wn|wn-1wn-2) maxwn P(wn|wn-1wn-2)
____________________________________________________________________________
General Instructions
● Make sure you know what you write, you might be asked to explain your code at
a later point in time.
● The submission will be graded automatically, so stick to the naming conventions
strictly.
● The deadline for the quiz is 10th November, Sunday at 1:15 PM.
Submission Instructions
The quiz4 directory to you looks exactly like this - except it is not named by your roll
number. Rename it as shown below and maintain this exact directory structure. Do not
move around files, add or delete any of them. We will pull this directory as required.
quiz4-<roll_number>/
├── q1
│ └── answers.txt
├── q2
│ └── node.py
├── q3
│ ├── autograder.py
│ ├── resizeImage.py
│ ├── barbara-input.png
│ ├── barbara-output.png
│ └── truth.npy
└── q4
├── data/
│ └── ... txt
├── getListOfFiles.java
├── readFile.java
├── Worker.java
├── ngrams_stats.java
├── ngrams_stats_st.java
├── ngrams_stats_mt.java
├── single_threaded_lm.java
├── multi_threaded_lm.java
├── getNgramsFromSingleString.java
├── output.txt
└── queries.txt