0% found this document useful (0 votes)
8 views17 pages

11 Revision

The document contains a series of programming exercises and their corresponding solutions in Python. It includes functions for finding intersections of dictionaries, rounding numbers, calculating column averages from a file, identifying missing numbers in a list, and counting zeros, even, and odd numbers in a list. Additionally, it provides outputs for various code snippets demonstrating basic Python operations.

Uploaded by

nivido4274
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views17 pages

11 Revision

The document contains a series of programming exercises and their corresponding solutions in Python. It includes functions for finding intersections of dictionaries, rounding numbers, calculating column averages from a file, identifying missing numbers in a list, and counting zeros, even, and odd numbers in a list. Additionally, it provides outputs for various code snippets demonstrating basic Python operations.

Uploaded by

nivido4274
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Introduction to

“Revision”
1
Q1
• Write a function Intersec(D1, D2) that takes two
dictionaries D1 and D2, and returns a new
dictionary with the common keys in D1 and D2.
• Example:
• D1 = {1:'a', 2:'b', 3:'c'}
• D2 = {2:'x', 4:'y', 1:'z'}
• Intersec(D1, D2) => {1: ['a', 'z'], 2: ['b', 'x']}

2
Answer
def Intersec(D1, D2):
res = {}
for i in D1:
if i in D2:
res[i] = [D1[i],D2[i]]
return res

D1 = {1:'a', 2:'b', 3:'c'}


D2 = {2:'x', 4:'y', 1:'z'} 3

print( Intersec(D1, D2))


Q2
• Write a function Round(n) that takes a float number n and
returns n rounded to the nearest integer.
• Note: you are not allowed to use the round built-in function
• Example:
• Round (3.5) => 4
• Round (2.2) => 2

4
Answer
def Round(n):
if n - int(n) >= 0.5:
return int(n) + 1
return int(n)

print (Round(3.5))

5
Q3
•Consider a text file where each line contains 3 integers
separated by commas. For each line, the 1st integer is at
position 0, the 2nd integer is at position 1, and the 3rd
integer is at position 2. A column of data is defined as all
integers in the file with the same position.
Write a function ColAvg(F) that takes a file name
F of the above format and returns a list of the averages of
each column.
Example: numbers.txt
3,2,6
2,3,3
1,10,15
6
ColAvg (“numbers.txt”) => [2, 5, 8]
Q3 answer
Answer:
def ColAvg(F):
fin = open(F,'r')
res = [0,0,0]
lines = fin.readlines()
i=0
for line in lines:
for num in line.split(','):
res[i] += int(num)
i += 1
i=0
for i in range(len(res)):
res[i] = int(res[i]/len(lines))
return res 7

print (ColAvg("numbers.txt"))
Q4
• Write a function missing(L,n) that takes a list L and
an integer n. The list contains numbers from 1 to n
except a missing number. Your function should
return this missing number.
• Example:
• missing([1,2,4,5], 5) => [3]
• missing([1,2,3,4,5], 6) => [6]
• missing([1,2,4],6) =>[3,5,6]
8
Q4 answer
def missing(L, n):
set1=set(L)
L2=list(range(1,n+1))
set2=set(L2)
diff=list(set2-set1)

return diff
print(missing([1,2,4,5], 5))
9
Q5
• Write a function CountInList(L) that takes a list L to
count the number of zeros, even and odd
numbers from a List of numbers
• Example:
CountInList([0,33,25,64,58,0,66,94,23,47,45,0,29,
28])
• Output :
• Number of Zeros: 3
• Number of evens: 5
• Number of odds: 6 10
Q5 answer
def CountInList(L):
count_zeros=0
count_odd=0
count_even=0
for i in L:
if i==0:
count_zeros+=1
elif i%2==0:
count_even+=1
else:
count_odd+=1
return count_zeros,count_even,count_odd
zeros,evens,odds=CountInList([0,33,25,64,58,0,66,94,23,47,45,0,29,28])
11
print("Number of Zeros: ",zeros)
print("Number of evens: ",evens)
print("Number of odds: ",odds)
Write the output of the
following programs
x = [1, 2, 3]
for item in x:
item = item + 1
print (x)

[1, 2, 3]

12
Write the output of the
following programs
x = [[1, 2], [], [8, 9, 3]]
for i in range(len(x)):
x[i].append(5)
print(x)

[[1, 2, 5], [5], [8, 9, 3, 5]]

13
Write the output of the
following programs
D={' Tunisia':[1,3,5,0], 'Egypt':[2,4,6,4]}
for i in D.values():
S=0
for x in i:
S+=x
print (S/len(i))

2.25
14
4.0
Write the output of the
following programs
ph = 3
if ph < 7:
print (“It’s acidic”)
elif ph < 4:
print (“It’s a strong acid”)

It's acidic
15
Write the output of the
following programs
x = 1256
i = 10
while i < = 1000:
print (x // i)
i = i * 10

125
12
1 16
Write the output of the
following programs
Points=(3,4)
Points[0]=5
Print(points)

It will give you an error as tuples are


immutable 17

You might also like