Professional Documents
Culture Documents
Winter 2017
Exam 1
09-Nov-2017
Time Limit: 30 Minutes Teaching Assistant
This exam contains 9 pages (including this cover page) and 12 questions.
Total of points is 80. This test is open book, open notes, closed internet.
Each question is about R and Python unless otherwise mentioned. Kindly write the codes
in both the languages. This is an open book/notes, closed laptop test.The last question is
open laptop, closed internet test. Kindly hand over the answer sheet before opening your
laptop. The question paper will be in your mail box [for the last question] just for which
mail [net] can be used.
Abbreviations used are:
WARP : Write a R program
WAPP : Write a Python Program
WAR&APP : Write a R & a Python program.
1. (2 points) What’s the difference between tuple and dictionary in Python? Which one is
mutable?
Programming-Python & R Exam 1 - Page 2 of 9 09-Nov-2017
2. (2 points) In R, the rpart package, and the rpart function provide recursive partition-
ing solutions for both classification and regression. What determines whether rpart
function will perform a classification analysis or a regression analysis?
3. (2 points) Name a clustering method that requires you to specify the number of clusters
in its solution. Name a clustering method that does not require you to specify the
number of clusters.
6. (10 points) Consider the following vector of values stored in a variable called x:
Programming-Python & R Exam 1 - Page 3 of 9 09-Nov-2017
> x
> 3 99 56 4 8 1 NA 8 1 0 43 NA 8 2 3
(c) (2 points) WAR&APP to replace the missing values in x with the mean of the
non-missing values in x
(d) (2 points) Write a R function that, when passed a vector, will return a vector with
the missing values in the vector replaced by the mean of the non-missing values of
the vector
(e) (2 points) WARP to extract lines from a vector names thestring which have more
than one double quoted strings. For example, these lines
this "line" has "quoted" "strings"
"one" more to follow "two"
"a""b"
Programming-Python & R Exam 1 - Page 4 of 9 09-Nov-2017
(f) (2 points) WARP that will remove multiple blanks from before and after a vector of
strings called fullstrings. For example, " hello, Python " should be converted
to “hello, Python”, and “Telco R ” should be converted to “Telco R”.
7. (10 points) Consider a vector called book, each element of which contains the text of one
sentence of a book. For the purposes of this question, consider a word as text separated
from other text by one or more blanks.
(a) (2 points) WAR&APP to find the average number of characters in each sentence
including the blanks, and another program to find the average number of characters
in each sentence not including the blanks.
(b) (2 points) WAR&APP to to find the average number of words in each line of the
book.
Programming-Python & R Exam 1 - Page 5 of 9 09-Nov-2017
(c) (2 points) WAR&APP m to find the line in the book with the most characters.
8. (16 points) For each of the short R program below, state the approximate value that z
will hold after executing the code.
(a) (2 points) R code:
x = seq ( 1 , 11 , 2 )
dim ( x ) = c ( 3 , 2 )
z = x [ , dim ( x ) [ 2 ] ]
x = seq ( 1 0 0 ) %% 2
y = c (1 , 2)
z = sum ( x ∗ y )
x = array ( c ( 1 , 2 , 2 , 1 ) , c ( 2 , 2 ) )
z = apply ( x , 1 , sum ) − apply ( x , 2 , sum )
Programming-Python & R Exam 1 - Page 6 of 9 09-Nov-2017
x = seq ( 6 )
z = ( 2 ∗ x < 1 0)
x = 1
f = function ( x ) c ( 2 ∗ x , x )
z = f ( f (x ) )
n = 10000
x = runif ( n , −1 , 1 )
y = runif ( n , −1 , 1 )
z = mean ( x ∗ x + y ∗ y <= 1 )
Programming-Python & R Exam 1 - Page 7 of 9 09-Nov-2017
10. (15 points) A classic puzzle called the Towers of Hanoi is a game that consists of three
rods, and a number of disks of different sizes which can slide onto any rod. The puzzle
starts with the disks in a neat stack in ascending order of size on one rod, the smallest
at the top, thus making a conical shape.
The objective of the puzzle is to move the entire stack to another rod, obeying the fol-
lowing rules:
L = [ ’ Ford ’ , ’ C h e v r o l e t ’ , ’ Toyota ’ , ’ N i s s a n ’ , ’ T e s l a ’ ]
12. (11 points) The waiting time of the nth customer in a single server queue. Suppose
customers labelled C0 , C1 , . . . Cn arrive at times τ = 0, τ1 , . . . , τn for service by a single
server. The interarrival times A1 = τ1 − τ0 , . . . , An = τn − τn−1 are independently and
identically distributed (iid) random variables (rv) with the exponential density
λα e−λα x ∀x ≥ 0 (1)
The service times S0 , S1 , . . . Sn are iid rv which are also independent of the interarrival
times with the exponential density
λs e−λs x ∀x ≥ 0 (2)
Let Wj denote the waiting time of customer Cj . Hence customer Cj leaves at time
τj + Wj + Sj . If this time is greater than τj+1 then the next customer, Cj+1 must wait
for the time τj + Wj + Sj − τ j + 1. Hence we have the recurrent relation
W0 = 0 (3)