Solució del lliurament 1 G50 d’Ampliació d’Informàtica

Organització:

Secció ETSEIB, Departament de Ciències de la Computació, UPC

Data:

2 d’octubre de 2024

Durada:

30 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=

>>> from itertools import *

Tests per a afegeix_intercalant

>>> from afegeix import afegeix_intercalant
>>> it1 = iter('casa')
>>> it2 = map(lambda x: str(x), iter(range(10)))
>>> it = afegeix_intercalant(it1, it2, lambda x: x!='a')
>>> next(it) # doctesttag: +TAG=1_afegeix_intercalant
'c'
>>> next(it) # doctesttag: +TAG=1_afegeix_intercalant
'0'
>>> for x in it:           # doctesttag: +TAG=1_afegeix_intercalant
...    print(x, end='-')
a-s-1-a-
>>> it1 = iter(['casal', 'por', 'festa', 'patata', 'sol', 'escola', 'llampa'])
>>> it2 = iter(['X', 'XX', 'XXX', 'XXXX', 'XXXXX', 'XXXXXX', 'XXXXXXX'])
>>> it = afegeix_intercalant(it1, it2, lambda x: len(x)%4==0)
>>> for x in it:         # doctesttag: +TAG=1_afegeix_intercalant
...    print(x, end='-')
casal-por-festa-patata-sol-escola-llampa-
>>> it1 = iter(['casal', 'por', 'festa', 'patata', 'sol', 'escola', 'llampa'])
>>> it2 = iter(['X', 'XX', 'XXX', 'XXXX', 'XXXXX', 'XXXXXX', 'XXXXXXX'])
>>> it = afegeix_intercalant(it1, it2, lambda x: True)
>>> for x in it:         # doctesttag: +TAG=1_afegeix_intercalant
...    print(x, end='-')
casal-X-por-XX-festa-XXX-patata-XXXX-sol-XXXXX-escola-XXXXXX-llampa-XXXXXXX-
>>> it1=(x for x in range(30))
>>> it2=  iter(['X', 'XX', 'XXX', 'XXXX', 'XXXXX', 'XXXXXX', 'XXXXXXX'])
>>> it = afegeix_intercalant(it1, it2, lambda x: x%8==0)
>>> for x in it:                   # doctesttag: +TAG=1_afegeix_intercalant
...    print(x, end='-')
0-X-1-2-3-4-5-6-7-8-XX-9-10-11-12-13-14-15-16-XXX-17-18-19-20-21-22-23-24-XXXX-25-26-27-28-29-

Iteradors infinits

>>> def iter1(n):
...     i = 0
...     while True:
...         if i %n == 1 or i %n== 2:
...              yield i
...         i = i+1
>>> it1 = iter1(6)
>>> it2 = iter1(7)
>>> it = afegeix_intercalant(it1, it2, lambda x: True)
>>> l =[next(it) for i in range(20)]
>>> l              # doctesttag: +TAG=1_afegeix_intercalant
[1, 1, 2, 2, 7, 8, 8, 9, 13, 15, 14, 16, 19, 22, 20, 23, 25, 29, 26, 30]
>>> it1 = iter1(4)
>>> it2 = iter1(7)
>>> it = afegeix_intercalant(it1, it2, lambda x: len(str(x))%2==0)
>>> l =[next(it) for i in range(20)]
>>> l                                 # doctesttag: +TAG=1_afegeix_intercalant
[1, 2, 5, 6, 9, 10, 1, 13, 2, 14, 8, 17, 9, 18, 15, 21, 16, 22, 22, 25]

Un iterador finit i l’altre infinit

>>> it2 = iter(['X', 'XX', 'XXX', 'XXXX', 'XXXXX', 'XXXXXX', 'XXXXXXX']*4)
>>> it = afegeix_intercalant(it1, it2, lambda x: len(str(x))< 3 and x%3 !=0)
>>> next(it)            # doctesttag: +TAG=1_afegeix_intercalant
26
>>> next(it)             # doctesttag: +TAG=1_afegeix_intercalant
'X'
>>> next(it)            # doctesttag: +TAG=1_afegeix_intercalant
29
>>> next(it)             # doctesttag: +TAG=1_afegeix_intercalant
'XX'
>>> for i in range(10):          # doctesttag: +TAG=1_afegeix_intercalant
...    print(next(it), end='-')
30-33-34-XXX-37-XXXX-38-XXXXX-41-XXXXXX-

Tests per a pagament de nómines

>>> from pagament import pagament_nomines
>>> it_p2=iter([('pere', 40), ('pau', 36), ('mar', 70), ('ramon', 55), ('josep', 50)])
>>> it1_p2, it2_p2, total= pagament_nomines(it_p2, 14, 600)
>>> total       # doctesttag: +TAG=2_pagament_nomines
3514
>>> list(it1_p2)      # doctesttag: +TAG=2_pagament_nomines
[('pere', 560), ('pau', 504)]
>>> list(it2_p2)             # doctesttag: +TAG=2_pagament_nomines
[('mar', 980), ('ramon', 770), ('josep', 700)]
>>> it_p2=iter([('pere', 40), ('pau', 36), ('mar', 70), ('josep', 50)])
>>> it1_p2, it2_p2, total= pagament_nomines(it_p2, 10, 100)
>>> total              # doctesttag: +TAG=2_pagament_nomines
1960
>>> list(it1_p2)         # doctesttag: +TAG=2_pagament_nomines
[]
>>> list(it2_p2)                     # doctesttag: +TAG=2_pagament_nomines
[('pere', 400), ('pau', 360), ('mar', 700), ('josep', 500)]
>>> it_p2=iter([('a', 40), ('b', 36), ('c', 70), ('d', 55), ('e', 50)])
>>> it1_p2, it2_p2, total= pagament_nomines(it_p2, 20, 900)
>>> total                      # doctesttag: +TAG=2_pagament_nomines
5020
>>> next(it1_p2, 'Acabada')        # doctesttag: +TAG=2_pagament_nomines
('a', 800)
>>> list(it2_p2)                        # doctesttag: +TAG=2_pagament_nomines
[('c', 1400), ('d', 1100), ('e', 1000)]