Вы находитесь на странице: 1из 3

INTRODUCTION TO CODING - 18 Dec 2018

Surname and Name ___________________________________________________

Matriculation number ___________________________________________________

EMAIL ___________________________________________________

1. Transform the following numbers, expressed in positional notation with different bases B (see the
subscript of each number for the different bases), into the decimal format (B=10):
12, 112, 2F16, 2A16, 378.
Sum also a unit to the numbers above and give the results with the same original base (2, 8, or 16).
(Remember that in base B we use a number B of distinct symbols, i.e., from 0 to B-1. For example,
in base B=8 we use symbols 0,1,2,3,4,5,6,7, while in base B=16 we use symbols: 0,1,…,9,A,B,C,D,E,F,
where A stands for 10, B for 11, etc.)
SOLUTION:
12 = 110
112 = 310
2F16 = 32 + 15 = 4710
2A16 = 32 + 10 = 4210
378 = 48 + 7 = 5510.
By summing 1:
12 + 1 = 102
112 + 1 = 1002
2F16 + 1 = 3016
2A16 + 1 = 2B16
378 + 1 = 408.

2. An image has a number of pixels 1024 x 2048 (210 = 1024 = K), where a pixel is associated with a
number, quantized on 1, 2 or 4 bytes, representing a gray level. How many kilobytes (210 B = KB) or
megabytes (220 B = MB) are needed for the three cases of quantization?
How many images can be stored on a memory support (a pen) of 64 gigabytes (230 = 1 G).
SOLUTION:
An image is composed of 1024 x 2048 pixels = 221 pixels = 2 x 220 pixels = 2 M pixels
With 1 Byte per pixel, a picture is large: 221 B = 2 x 220 B = 2 MB
With 2 Bytes per pixel, a picture is large: 2 x 221 B = 222 B = 4 MB
With 4 Bytes per pixel, a picture is large: 4 x 221 B = 223 B = 8 MB

With a memory support of 64 GB = 236 B:


1 Byte per pixel: 236 B / 2 MB = 236 / 221 = 215 = 215 pictures.
2 Bytes per pixel: 236 B / 4 MB = 236 / 222 = 214 = 214 pictures.
4 Bytes per pixel: 236 B / 8 MB = 236 / 223 = 213 = 213 pictures.

3. Define a function that given an integer x (e.g.: x=50) and a string y of numerical characters (e.g.:
y=”25”), check if x is a multiple of y. Return either the number of times y is contained in x, or -1
otherwise (ex: return 2 for x=50 and y=”25”).
SOLUTION:
def chk(x, y):
if x % int(y) == 0:
return x // int(y)
else:
return -1
4. Trace the execution of the following program step by step (showing the values assumed by the
variables involved for line executed), detect the run-time error, and finally fix the program.
Determine the final value of sum (once corrected the error):
1. products = 'beer soap wine oil tomato'
2. warehouse = {'beer':10, 'wine':100, 'oil':25}
3. listp = products.split()
4. sum = 0
5. for i in range(len(listp)):
6. prod = listp[i]
7. sum = sum + warehouse[prod]
8. print(sum)

SOLUTION:
The trace is: 1,2,3,4,5,6,7,5,6,7, error
When the control reaches line 7 for the second time, we have an error, because the key
“soap” is not contained in dictionary warehouse.
When the error is raised: sum=10, i=1, prod='soap',
listp = ['beer','soap','wine','oil','tomato']
The code without the bug is the following, where we first check the presence of a key in the
dictionary, before modifying the variable sum by accessing warehouse.
At the end, we have: sum = 135.

products = 'beer soap wine oil tomato'


warehouse = {'beer':10, 'wine':100, 'oil':25}
listp = products.split()
sum = 0
for i in range(len(listp)):
prod = listp[i]
if prod in warehouse:
sum = sum + warehouse[prod]
print(sum)

5. This list represents a series of 6 side die rolls:


rolls = [1,5,2,3,5,4,4,3,1,1,1,2,3,1,5,6,2]
Write a program that analyzes the rolls and computes the following: (1) The total number of rolls,
(2) The total value of all rolls (i.e. 1+5+2+3…), (3) The average value of all the rolls.
Re-write another program where we have a string instead of a list as input:
rolls = "1,5,2,3,5,4,4,3,1,1,1,2,3,1,5,6,2"

SOLUTION:
rolls = [1,5,2,3,5,4,4,3,1,1,1,2,3,1,5,6,2]
s = 0
for d in rolls:
s += d
print("Tot rolls:", len(rolls))
print("Sum rolls:", s)
print("Avg rolls:", s/len(rolls))

###############################################
srolls = "1,5,2,3,5,4,4,3,1,1,1,2,3,1,5,6,2"
l = srolls.split(',')
s = 0
for d in l:
s += int(d) # element d is a string!
print("Tot rolls:", len(l))
print("Sum rolls:", s)
print("Avg rolls:", s/len(l))
6. Write a function chk(n, lst) that, taken a number n and a list of numbers lst, returns a new list of
the same length of lst, where each entry of index i is True if lst[i] is a multiple of n.
Example: given n=10 and lst = [100, 12, 13, 10, 8], the function has to return the
list: [True, False, False, True, False].

SOLUTION:
def chk(n, lst):
newl = []
for el in lst:
if el % n == 0:
newl.append(True)
else:
newl.append(False)
return(newl)

print(chk(3, [23,18,8,7,14,9])) # example of call

7. Write a function that, given an input number n, print n lines as follows:


1*
5 ++
10 ***
15 ++++
20 *****
….
Consider that a string of k occurrences of the same symbol ’-’ can be written as: k*’-’
Example: 10*’-’ is equal to: ’---------’

SOLUTION:
def p_tree(n):
if n < 1:
return # no tree is displayed

# n >= 1
num = 1
print(num, "*")
num = 5
for i in range(2,n+1): # from 2 to n
if i % 2 == 0:
print(num, i * '+')
else:
print(num, i * '*')
num = num+5

p_tree(7) # example of call

Вам также может понравиться