2 Strings Lists Functions

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Ing. Fis.

2021/2022 Seconda esercitazione

You can view this report online at :

Full Name:

Email:
scored in Ing. Fis. 2021/2022
100%
Test Name: Ing. Fis. 2021/2022 Seconda esercitazione Seconda esercitazione in
45/45 105394 min 59 sec on 26 Oct
Taken On: 26 Oct 2021 15:50:46 CEST 2021 15:50:46 CEST
Time Taken: 105394 min 59 sec/ 90 min

Student Roll Number:

Invited by: Gabriele

Skills Score:

Tags Score: Lists 15/15

Strings 15/15
list 26/26

Recruiter/Team Comments:

No Comments.

Question Description Time Taken Score Status

Q1 In place list editing 5 Coding 25/ 25

Q2 List of strings manipulation Approximate Solution 15/ 15

Q3 Strings formatting Multiple Choice 1/ 1

Q4 Named arguments Multiple Choice 1/ 1

Q5 Return values Multiple Choice 1/ 1

Q6 List ordering Multiple Choice 1/ 1

Q7 Slices Multiple Choice 1/ 1

QUESTION 1 In place list editing 5 Coding list

Correct Answer
QUESTION DESCRIPTION

The goal of this assignment is to write the function edi t _ l i st ( l ) that takes in input a list of integer
Score 25
numbers: l

edi t _ l i st ( l ) must:

1. Normalize all the elements of l dividing them by the sum of all the initial values of l . Note:
1/6
1. This must be done editing l in place, not on a copy
2. if l is empty or the sum is zero you should not modify it.
2. Return a list containing the even elements of l . The order is not relevant.

For example:

l = [1,4,2,5,3,1,4]
res = edit_list(l)

after running edit_list then:

l = [0.05, 0.2, 0.1, 0.25, 0.15, 0.05, 0.2]


res = [4,2,4]

CANDIDATE ANSWER

Language used: Python 3

1 #
2 # Complete the 'edit_list' function below.
3 # Take in input a list of integer number (l)
4 #
5
6
7 def edit_list(l):
8 even = []
9 my_sum = 0
10 # Write your code here
11 for i in range(len(l)):
12 if l[i] % 2 == 0:
13 even.append(l[i])
14
15 for j in range(len(l)):
16 my_sum += l[j]
17 for k in range(len(l)):
18 l[k] = l[k]/my_sum
19
20
21 return even
22

TESTCASE DIFFICULTY TYPE STATUS SCORE TIME TAKEN MEMORY USED

Testcase 0 Easy Sample case  Success 2.5 0.0573 sec 9.44 KB

Testcase 1 Easy Sample case  Success 2.5 0.0507 sec 9.38 KB

Testcase 2 Easy Hidden case  Success 10 0.0603 sec 9.47 KB

Testcase 3 Easy Hidden case  Success 10 0.0581 sec 9.53 KB

No Comments

QUESTION 2 List of strings manipulation 



Approximate Solution Strings Lists

Correct Answer
QUESTION DESCRIPTION

The goal of this exercise is to implement a function called manipulate(str_list, k, c) .


Score 15
This function takes three parameters in input:
2/6
This function takes three parameters in input:
str_list , which is a list of strings (without punctuation, all lower case)
k , an integer
c , a character

It must perform two tasks:


1. Return a list of the first k words that start with the character in variable c (in the same order as
they are in the original list)
2. Return a list of the words that haven't been selected at the previous step.

For example if:


str_list = ['casa', 'rumore', 'vino', 'cane', 'pane', 'vecchio', 'polvere',
'tetto', 'camino'],
k=2
c='c'

then:

k_strings, other_strings = manipulate(str_list, k, c)


k_strings == ['casa', 'cane]
other_strings == ['rumore', 'vino', 'pane', 'vecchio', 'polvere', 'tetto',
'camino']

CANDIDATE ANSWER

Language used: Python 3

1 #
2 # Complete the 'manipulate' function below.
3 #
4 # The function is expected to return a 2D_STRING_ARRAY.
5 # The function accepts following parameters:
6 # 1. STRING_ARRAY str_list
7 # 2. INTEGER k
8 # 3. CHARACTER c
9 #
10
11 def manipulate(str_list, k, c):
12 k_words = []
13 other_words = []
14 counter = 0
15 # Write your code here
16 for i in range(len(str_list)):
17 if str_list[i][0] == c:
18 k_words.append(str_list[i])
19 if len(k_words) > k:
20 del k_words[k:len(k_words)]
21 for i in range(len(str_list)):
22 if str_list[i] not in k_words:
23 other_words.append(str_list[i])
24
25 return k_words, other_words

TESTCASE DIFFICULTY TYPE STATUS SCORE TIME TAKEN MEMORY USED

Testcase 0 Easy Sample case  Success 7.5 / 7.5 0.0512 sec 9.4 KB

Testcase 1 Easy Sample case  Success 7.5 / 7.5 0.065 sec 9.4 KB

No Comments

3/6
QUESTION 3 Strings formatting 

Multiple Choice

Correct Answer QUESTION DESCRIPTION

What is the format specifier to print a floating number with 3 units and 2 decimals?
Score 1

CANDIDATE ANSWER

Options: (Expected answer indicated with a tick)

:2.3f

2:3.f

:3.2f

3:2.f

No Comments

QUESTION 4 Named arguments 



Multiple Choice

Correct Answer QUESTION DESCRIPTION

Consider the following code, what is the value of res ?


Score 1

def func(a, b=2, c=3):


return a+b+c

res = func(10,c=4)

CANDIDATE ANSWER

Options: (Expected answer indicated with a tick)

17

16

15

14

No Comments

4/6
QUESTION 5 Return values 

Multiple Choice

Correct Answer
QUESTION DESCRIPTION

Consider the following code:


Score 1

def func():
return 1,2
a = func()

What is the value of a ?

CANDIDATE ANSWER

Options: (Expected answer indicated with a tick)

(1,2)

[1,2]

"1,2"

No Comments

QUESTION 6 List ordering 



Multiple Choice list

Correct Answer
QUESTION DESCRIPTION

What is the string printed by the following snippet of code?


Score 1

s = ['t','a','g','j','r']
print(s.sort())

CANDIDATE ANSWER

Options: (Expected answer indicated with a tick)

['a', 'g', 'j', 'r', 't']

['t', 'r', 'j', 'g','a']

None

ValueError

No Comments

5/6
QUESTION 7 Slices 

Multiple Choice

Correct Answer
QUESTION DESCRIPTION

Given that:
Score 1
a = [1,2,3,4,5]
b = [5,6,7]

what is the True option?

CANDIDATE ANSWER

Options: (Expected answer indicated with a tick)

a[5] == b[1]

a[7] == b[0]

a[-1] == b[-1]

a[4:7] == b[:1]

No Comments

PDF generated at: 24 May 2022 14:47:54 UTC

6/6

You might also like