Solució de l’examen parcial d’Ampliació d’Informàtica - Torn 2¶
- Organització:
Secció ETSEIB, Departament de Ciències de la Computació, UPC
- Data:
31 d’octubre de 2024
- Durada:
1 hora i 10 minuts
- Copyright:
Reconeixement-CompartirIgual 3.0 No adaptada de Creative Commons
Jocs de proves
Els exemples que compten per la nota són només els que van seguits de
# doctesttag: +TAG
Tests per a encavallament¶
>>> from tasques2 import encavallament
>>> from datetime import time
>>> encavallament(time(9, 00), time(13, 00), time(11, 00), time(17, 00)) # doctesttag: +TAG=1_encavallament
datetime.timedelta(seconds=7200)
>>> encavallament(time(9, 00), time(11, 00), time(10, 15), time(20, 00)) # doctesttag: +TAG=1_encavallament
datetime.timedelta(seconds=2700)
>>> encavallament(time(12, 00), time(13, 00), time(9, 00), time(14, 00)) # doctesttag: +TAG=1_encavallament
datetime.timedelta(seconds=3600)
>>> encavallament(time(9, 00), time(13, 00), time(10, 00), time(11, 22)) # doctesttag: +TAG=1_encavallament
datetime.timedelta(seconds=4920)
>>> encavallament(time(16, 00), time(17, 30), time(16, 10), time(16, 23)) # doctesttag: +TAG=1_encavallament
datetime.timedelta(seconds=780)
>>> encavallament(time(8, 00), time(13, 00), time(17, 00), time(21, 00)) # doctesttag: +TAG=1_encavallament
datetime.timedelta(0)
>>> encavallament(time(12, 00), time(13, 00), time(7, 00), time(12, 00)) # doctesttag: +TAG=1_encavallament
datetime.timedelta(0)
>>> encavallament(time(20, 22), time(23, 12), time(23, 12), time(23, 48)) # doctesttag: +TAG=1_encavallament
datetime.timedelta(0)
>>> for m in range(0, 60, 10):
... print(encavallament(time(12, 1), time(13, 4), time(12, m), time(13, m))) # doctesttag: +TAG=1_encavallament
0:59:00
0:54:00
0:44:00
0:34:00
0:24:00
0:14:00
>>> for m in range(0, 20, 3):
... print(encavallament(time(11, 0), time(12, 5), time(10, m), time(11, m*2))) # doctesttag: +TAG=1_encavallament
0:00:00
0:06:00
0:12:00
0:18:00
0:24:00
0:30:00
0:36:00
Tests per a Queviures¶
>>> from queviures import Queviures
>>> q = Queviures(50)
>>> q.capital, q.magatzem # doctesttag: +TAG=2_Queviures
(50, {})
>>> q.comprar('gruyere', 6, 3)
>>> q.capital, q.magatzem # doctesttag: +TAG=2_Queviures
(32, {'gruyere': 6})
>>> q.comprar('cafè', 3, 4)
>>> q.capital, q.magatzem == {'gruyere':6, 'cafè':3} # doctesttag: +TAG=2_Queviures
(20, True)
>>> q.nombre_productes() # doctesttag: +TAG=2_Queviures
9
>>> len(q) # doctesttag: +TAG=2_Queviures
2
>>> q['cafè'], q['gruyere'], q['sal'] # doctesttag: +TAG=2_Queviures
(3, 6, 0)
>>> q.vendre('gruyere', 8)
>>> q.capital, len(q), q.nombre_productes() # doctesttag: +TAG=2_Queviures
(68, 1, 3)
>>> q['cafè'], q['gruyere'], q['llet'] # doctesttag: +TAG=2_Queviures
(3, 0, 0)
>>> q.comprar('sardines', 5, 5)
>>> q.comprar('ratafia', 3, 20)
>>> q.vendre('sobrassada', 15)
>>> q.capital, len(q), q.nombre_productes() # doctesttag: +TAG=2_Queviures
(43, 2, 8)
>>> for prod in ['cafè', 'gruyere', 'sal', 'sardines', 'ratafia']:
... print(q[prod], end=',') # doctesttag: +TAG=2_Queviures
3,0,0,5,0,
>>> q = Queviures(800)
>>> q.comprar('roquefort', 30, 5)
>>> q.comprar('caviar', 10, 1000)
>>> q.comprar('sardines', 10, 5)
>>> q.comprar('ratafia', 20, 15)
>>> q.comprar('llet', 100, 1)
>>> q.comprar('sardines', 6, 12)
>>> q.comprar('roquefort', 20, 7)
>>> len(q), q.nombre_productes(), q.capital # doctesttag: +TAG=2_Queviures
(4, 166, 128)
>>> for prod in ['caviar', 'ratafia', 'llet', 'pa', 'roquefort', 'sardines', 'tonyina']:
... print(q[prod], end=',') # doctesttag: +TAG=2_Queviures
0,20,100,0,30,16,0,
>>> for prod, euros in zip(['caviar', 'roquefort', 'llet', 'pa', 'tonyina'], [120, 18, 4, 2, 22]):
... q.vendre(prod, euros)
... print(len(q), q.nombre_productes(), q.capital) # doctesttag: +TAG=2_Queviures
4 166 128
3 136 668
2 36 1068
2 36 1068
2 36 1068
>>> q.magatzem == {'sardines': 16, 'ratafia': 20} # doctesttag: +TAG=2_Queviures
True
>>> q.comprar('caviar', 1, 1068)
>>> q.magatzem == {'sardines': 16, 'ratafia': 20, 'caviar': 1} # doctesttag: +TAG=2_Queviures
True
>>> q.vendre('sardines', 18)
>>> q.magatzem == {'ratafia': 20, 'caviar': 1} # doctesttag: +TAG=2_Queviures
True
>>> len(q), q.nombre_productes(), q.capital # doctesttag: +TAG=2_Queviures
(2, 21, 288)
>>> q = Queviures(6000)
>>> for prod, qt, euros in zip('ABCDEFGHIJKLMNOPQRSTUVWXYZ', range(3, 40), range(3, 80, 2)):
... q.comprar(prod, qt, euros)
... print(len(q), q.nombre_productes(), q.capital) # doctesttag: +TAG=2_Queviures
1 3 5991
2 7 5971
3 12 5936
4 18 5882
5 25 5805
6 33 5701
7 42 5566
8 52 5396
9 63 5187
10 75 4935
11 88 4636
12 102 4286
13 117 3881
14 133 3417
15 150 2890
16 168 2296
17 187 1631
18 207 891
19 228 72
19 228 72
19 228 72
19 228 72
19 228 72
19 228 72
19 228 72
19 228 72
>>> q.vendre('patates', 5)
>>> q.magatzem == {'A': 3, 'B': 4, 'C': 5, 'D': 6, 'E': 7, 'F': 8, 'G': 9, 'H': 10, 'I': 11, 'J': 12, 'K': 13, 'L': 14, 'M': 15, 'N': 16, 'O': 17, 'P': 18, 'Q': 19, 'R': 20, 'S': 21} # doctesttag: +TAG=2_Queviures
True
>>> for prod, qt, euros in zip(['sal']*10, range(1,10), [1]*10):
... q.comprar(prod, qt, euros)
... print(len(q), q.nombre_productes(), q.capital) # doctesttag: +TAG=2_Queviures
20 229 71
20 231 69
20 234 66
20 238 62
20 243 57
20 249 51
20 256 44
20 264 36
20 273 27
Tests per a QueviuresLimit¶
>>> from queviures_limit import QueviuresLimit
>>> q = QueviuresLimit(200, 100)
>>> q.capital, q.magatzem, q.espai_lliure() # doctesttag: +TAG=3_QueviuresLimit
(200, {}, 100)
>>> q.comprar('galetes', 150, 1) # no hi ha prou espai lliure
>>> q.comprar('emmental', 10, 7) # doctesttag: +TAG=2_Queviures
>>> q.capital, q.magatzem, q.espai_lliure() # doctesttag: +TAG=3_QueviuresLimit
(130, {'emmental': 10}, 90)
>>> q.comprar('cafè', 20, 3)
>>> q.capital, q.espai_lliure(), len(q), q.nombre_productes() # doctesttag: +TAG=3_QueviuresLimit
(70, 70, 2, 30)
>>> q.comprar('salmó fumat', 5, 100) # no hi ha prou capital per comprar-ho
>>> q.capital, q.magatzem == {'emmental': 10, 'cafè': 20}, q.espai_lliure(), len(q) # doctesttag: +TAG=3_QueviuresLimit
(70, True, 70, 2)
>>> q['cafè'], q['emmental'], q['galetes'], q['salmó fumat'] # doctesttag: +TAG=3_QueviuresLimit
(20, 10, 0, 0)
>>> q.vendre('emmental', 8)
>>> q.capital, q.espai_lliure(), q.nombre_productes() # doctesttag: +TAG=3_QueviuresLimit
(150, 80, 20)
>>> q['cafè'], q['emmental']
(20, 0)
>>> q.comprar('sardines', 10, 3)
>>> q.comprar('ratafia', 3, 20)
>>> q.comprar('caramels', 25, 1)
>>> q.comprar('taronges', 60, 2)
>>> q.vendre('sobrassada', 15)
>>> q.vendre('ratafia', 32)
>>> q.capital, q.espai_lliure() # doctesttag: +TAG=3_QueviuresLimit
(131, 45)
>>> for prod in ['cafè', 'emmental', 'caramels', 'sardines', 'ratafia', 'taronges']:
... print(q[prod], end=',') # doctesttag: +TAG=3_QueviuresLimit
20,0,25,10,0,0,
>>> q.nombre_productes(), len(q) # doctesttag: +TAG=3_QueviuresLimit
(55, 3)
>>> q = QueviuresLimit(2000, 50) # doctesttag: +TAG=3_QueviuresLimit
>>> for prod, qt, preu in zip('ABCDEFGHI', range(10, 50), range(50, 15, -1)):
... q.comprar(prod, qt, preu)
... print(q.capital, q.espai_lliure(), q.nombre_productes(), end=' -> ')
... if qt < 15: q.vendre(prod, preu*2)
... print(q.capital, q.espai_lliure(), q.nombre_productes())
1500 40 10 -> 2500 50 0
1961 39 11 -> 3039 50 0
2463 38 12 -> 3615 50 0
3004 37 13 -> 4226 50 0
3582 36 14 -> 4870 50 0
4195 35 15 -> 4195 35 15
3491 19 31 -> 3491 19 31
2760 2 48 -> 2760 2 48
2760 2 48 -> 2760 2 48
>>> q.magatzem == {'F': 15, 'G': 16, 'H': 17} # doctesttag: +TAG=3_QueviuresLimit
True
>>> for prod, qt, preu in zip('FGHABCDDAABCBABC', range(1, 40), range(10, 1, -1)): # doctesttag: +TAG=3_QueviuresLimit
... if qt < 10: q.comprar(prod, qt, preu)
... print(q.capital, q.espai_lliure(), q.nombre_productes(), end=' -> ')
... q.vendre(prod, preu*2)
... print(q.capital, q.espai_lliure(), q.nombre_productes())
2750 1 49 -> 3070 17 33
3052 15 35 -> 3376 33 17
3352 30 20 -> 3672 50 0
3644 46 4 -> 3700 50 0
3670 45 5 -> 3730 50 0
3700 44 6 -> 3760 50 0
3732 43 7 -> 3788 50 0
3764 42 8 -> 3812 50 0
3794 41 9 -> 3830 50 0
>>> q.magatzem # doctesttag: +TAG=3_QueviuresLimit
{}
>>> q.comprar('Nevera', 1, 3794)
>>> q.magatzem # doctesttag: +TAG=3_QueviuresLimit
{'Nevera': 1}