Docsity

Scientific Programming Exam: R, Python, and Concurrent Programming, Prove d'esame di Programmazione Avanzata

Toggle sidebar
bg1
Scientific Programming
Politecnico di Milano, DEIB
Rosario M. Piro, Pietro Pinoli
Final exam (90 min), 14th June 2022
PLEASE WRITE LEGIBLY!
Name: Immatriculation number:
Qapla’!
1.
( points) Some R basics
Consider the following instructions:
[1]
if (!requireNamespace("BiocManager", quietly = TRUE))
[2]
install.packages("BiocManager")
[3]
BiocManager::install("edgeR")
(a)
( points) Describe the meaning of :: in the last command.
(b)
( points) What will the following simple expressions return when they are executed?
1.
as.numeric(is.numeric(0.45))
2.
is.nan(NA)
3.
a=1; b=5; a-1:b
A1
A2
A3
A4
A5
A6
A7
(Bonus)
Σ
(6)
60
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

Anteprima parziale del testo

Scarica Scientific Programming Exam: R, Python, and Concurrent Programming e più Prove d'esame in PDF di Programmazione Avanzata solo su Docsity!

Scientific Programming

Politecnico di Milano, DEIB Rosario M. Piro, Pietro Pinoli

Final exam (90 min), 14 th^ June 2022

PLEASE WRITE LEGIBLY!

Name: Immatriculation number:

Qapla’!

  1. ( points) Some R basics Consider the following instructions:

[1] if (!requireNamespace("BiocManager", quietly = TRUE)) [2] install.packages("BiocManager") [3] BiocManager::install("edgeR")

(a) ( points) Describe the meaning of :: in the last command.

(b) ( points) What will the following simple expressions return when they are executed?

  1. as.numeric(is.numeric(0.45))
  2. is.nan(NA)
  3. a=1; b=5; a-1:b

A1 A2 A3 A4 A5 A6 A7 (Bonus) Σ

  1. ( points) Factors and lists in R

(a) ( points) Given the following factor: myfac <- factor(1:4, 3)

  • What do you obtain for: myfac != 3
  • How to obtain the levels?
  • What happens if myfac[6] <- 5

(b) ( points) Given a list named “mylist” which contains three numeric matrices, e.g., $mat [,1] [,2] [1,] 1 3 [2,] 2 4

$mat [,1] [,2] [,3] [1,] 7 9 11 [2,] 8 10 12

$mat [,1] [,2] [1,] 0.1 0. [2,] 0.2 0.

  • What do you obtain for length(unlist(mylist))?
  • How do I obtain the second matrix?
  • Get the sum of all elements for each matrix without using a for - loop (or any other loop structure). Briefly describe the output you obtain (of what type is it?)
  1. ( points) Concurrent programming

a) What is GIL? How does it prevent a Python Script to be parallelized?

b)What is task switchng? Is it correlated with concurrent programming? Is it equivalent to true hardware parallelism?

  1. ( points) Python, Pandas, numpy

(a) ( points) How do numpy’s multidimensional arrays reduce memory usage?

(b) ( points) What is a Pandas series? How is it related to a numpy array?

  1. ( points) BONUS

In the following multiple choice questions (2 points each), you have to choose the single best answer.

(a) Input and Output of getSeq?     Input: Bsgenome, GRanges / Output: DNAStringSet

(b) Using the testthat framework for unit testing, how can you check whether two numeric vectors vecA and vecB contain exactly the same numbers? ◦ expect_match(vecA, vecB) ◦ expect_match(vecA, vecB, each_element=TRUE) ◦ expect_equal(vecA, vecB) ◦ expect_equal(vecA, vecB, each_element=TRUE)

(c) Consider the following type of object.Which of this object can contain multiple elements? XStringSet, GRanges, GRangesList, CompressedVCF

 all of them  XStringSet, GRanges, GRangesList  GRanges, GRangesList, CompressedVCF  XstringSet, GRangesList

Scientific Programming

Politecnico di Milano, DEIB Rosario M. Piro, Pietro Pinoli

Final exam (90 min), 14th^ June 2022

PLEASE WRITE LEGIBLY!

Name: Immatriculation number:

Qapla’!

A1 A2 A3 A4 A5 A6 A7 (Bonus)

P

  1. (9 points) Some R basics Consider the following instructions:

[1] if (!requireNamespace("BiocManager", quietly = TRUE)) [2] install.packages("BiocManager") [3] BiocManager::install("edgeR")

(a) (6 points) Describe what each of these three lines mean and what exactly happens if they are exe- cuted!

(b) (3 points) What will the following simple expressions return when they are executed?

  1. as.numeric(is.numeric(0.45))
  2. is.na(NaN)
  3. a=1; b=5; a:b-
  1. (10 points) R class systems

Consider the differences between the S4 and Reference class systems. For each of the class systems imagine a class “student” with the data items “name” (string), “university” (string) and “id” (number). Let s4 be an object of the S4 class and sR an object of the Reference class. (a) (6 points) Define the classes as necessary and create the two objects s4 (S4 class) and sR (Reference class) with the values “Gigi”, “PoliMI” and ID 112.

(b) (4 points) Make examples for the following tasks: Task S4 class Reference class

Set the name to “Eva”

Verify that the object is of class “student”

Make a full copy of the object

If possible, verify that the object is of the re- spective class system (S4/Reference)

  1. (7 points) R packages

Consider the following files and folders (in alphabetical order):

decompT2S decompT2S.R decompT2S.Rd decompT2S.Rmd DESCRIPTION man NAMESPACE R test-decomp.R testthat testthat.R tests vignettes

Distribute them in the following figure such that it reflects the correct structure of an R package named “decompT2S” (folders have a gray background):

  1. (8 points) Python, Pandas, numpy

(a) (4 points) Why are numpy’s multidimensional arrays preferred over Python’s built-in structures for scientific programming, in particular when a high performance is needed?

(b) (4 points) What is a Pandas series? How is it related to a numpy array?

  1. (10 points) Network programming

Use the Python module “socket” to write a very simple server that waits for incoming connections without doing anything in particular, except closing each connection immediately (no need for concur- rency and spawning new processes). You will need the functions (in alphabetical order):

accept(), bind(), close(), listen(), socket()

Additional hints:

  • You can use the function gethostname() from the same module to determine the hostname of the server.
  • An infinite while-loop can be used to wait for connections, such that the server does not end the program after having received the first connection.

Scientific Programming

Politecnico di Milano, DEIB Rosario M. Piro, Pietro Pinoli

Final exam (90 min), 1 st^ July 2022

PLEASE WRITE LEGIBLY!

Name: Immatriculation number:

Qapla’!

1. (9 points) Some R basics

(a) What is the difference between load and attach a package and in which case NameSpace is used?

(b) What is masking, what happens and how to call the masked function?

A1 A2 A3 A4 A5 A6 A7 (Bonus) 9 10 10 8 6 8 9 (6) (^) Σ

60

2. (10 points) Matrices in R

4. (8 points) Network programming

Given the following functions from the Python module socket (in alphabetical order) accept(), bind(), close(), connect(), listen(), recv(), send(), socket()

Using this functions, describe the typical initialization and data flow both on the client and the server side by

filling the following figure with the appropriate function names.

5. (6 points) Concurrent programming

What is the GIL (Global Interpreter Lock)? How does it prevent speed-up gain when trying to parallelize a Python script? What is the most common solution?