Solució de l’examen final torn 1 d’Ampliació d’Informàtica

Organització:

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

Data:

8 de gener de 2024

Durada:

90 minuts

Copyright:

Reconeixement-CompartirIgual 4.0 No adaptada de Creative Commons

Jocs de proves

Aquests jocs de proves necessiten el fitxer meteo2.csv.

Els exemples que compten per la nota són només els que van seguits de # doctesttag: +TAG


Esquema del sistema fluvial del joc de proves:

A +–B | +–C | | +–D | | +–E | | +–F | +–G | | +–H | | | +–I | +–J | +–K +–L . +–M . +–N . +–O . +–W

P +–Q | +–R | +–S | +–T | +–U | +–V . +–X . +–Y . +–Z


>>> lrius = [ 'BA', 'CB', 'DC', 'ED', 'FE',
...           'GB', 'HG', 'IB', 'JI', 'KI',
...           'LA', 'ML', 'NL', 'OL', 'WL',
...           'QP', 'RQ', 'SP', 'TS', 'US',
...           'VP', 'XV', 'YX', 'ZX' ]
>>> import networkx as nx
>>> g1 = nx.DiGraph(lrius)
>>> g2 = nx.DiGraph(lrius[:10])

Test confluencia

>>> from rius import confluencia
>>> confluencia(g1, 'A', 'B')  # doctesttag: +TAG=1_confluencia
0
>>> confluencia(g1, 'B', 'A')  # doctesttag: +TAG=1_confluencia
1
>>> confluencia(g1, 'E', 'A')  # doctesttag: +TAG=1_confluencia
2
>>> confluencia(g1, 'U', 'P')  # doctesttag: +TAG=1_confluencia
2
>>> [confluencia(g1, x, 'A') for x in 'CDEFGHIJKLM'] # doctesttag: +TAG=1_confluencia
[2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2]
>>> [confluencia(g1, x, 'B') for x in 'CDEFGHIJKLM'] # doctesttag: +TAG=1_confluencia
[1, 2, 2, 2, 1, 2, 1, 2, 2, 0, 0]
>>> [confluencia(g1, x, 'P') for x in 'QRSTUVXC'] # doctesttag: +TAG=1_confluencia
[1, 2, 1, 2, 2, 1, 2, 0]
>>> [confluencia(g1, x, 'V') for x in 'ABVXYZP'] # doctesttag: +TAG=1_confluencia
[0, 0, 2, 1, 2, 2,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 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 1, 0, 2]

Test recorregut

>>> from rius import recorregut
>>> g1 = nx.DiGraph(lrius)
>>> recorregut(g1, 'A')  # doctesttag: +TAG=1_recorregut
['A']
>>> recorregut(g1, 'C')  # doctesttag: +TAG=1_recorregut
['C', 'B', 'A']
>>> recorregut(g1, 'F')  # doctesttag: +TAG=1_recorregut
['F', 'E', 'D', 'C', 'B', 'A']
>>> recorregut(g1, 'G')  # doctesttag: +TAG=1_recorregut
['G', 'B', 'A']
>>> recorregut(g1, 'L')  # doctesttag: +TAG=1_recorregut
['L', 'A']
>>> recorregut(g1, 'P')  # doctesttag: +TAG=1_recorregut
['P']
>>> recorregut(g1, 'Q')  # doctesttag: +TAG=1_recorregut
['Q', 'P']
>>> recorregut(g1, 'T')  # doctesttag: +TAG=1_recorregut
['T', 'S', 'P']
>>> recorregut(g1, 'Y')  # doctesttag: +TAG=1_recorregut
['Y', 'X', 'V', 'P']
>>> [recorregut(g1, a) for a in g1] # doctesttag: +TAG=1_recorregut
[['B', 'A'], ['A'], ['C', 'B', 'A'], ['D', 'C', 'B', 'A'], ['E', 'D', 'C', 'B', 'A'], ['F', 'E', 'D', 'C', 'B', 'A'], ['G', 'B', 'A'], ['H', 'G', 'B', 'A'], ['I', 'B', 'A'], ['J', 'I', 'B', 'A'], ['K', 'I', 'B', 'A'], ['L', 'A'], ['M', 'L', 'A'], ['N', 'L', 'A'], ['O', 'L', 'A'], ['W', 'L', 'A'], ['Q', 'P'], ['P'], ['R', 'Q', 'P'], ['S', 'P'], ['T', 'S', 'P'], ['U', 'S', 'P'], ['V', 'P'], ['X', 'V', 'P'], ['Y', 'X', 'V', 'P'], ['Z', 'X', 'V', 'P']]

Tests repetit

>>> from repetit import primer_repe
>>> primer_repe([1, 3, 4, 6, 4, 5])  # doctesttag: +TAG=2_repetit
(True, 4)
>>> primer_repe(list('indivisible'))  # doctesttag: +TAG=2_repetit
(True, 'i')
>>> primer_repe(list('animia'))  # doctesttag: +TAG=2_repetit
(True, 'a')
>>> primer_repe('la perdiu diu a la guatlla que hi fas tu guatlla aquí?'.split())  # doctesttag: +TAG=2_repetit
(True, 'la')
>>> primer_repe(list('abcdefghijkl'))  # doctesttag: +TAG=2_repetit
(False, None)
>>> primer_repe(list('esternocleidomastoideu'))  # doctesttag: +TAG=2_repetit
(True, 'e')
>>> primer_repe(list('12345678987654321'))  # doctesttag: +TAG=2_repetit
(True, '1')
>>> primer_repe(list('1234578989'))  # doctesttag: +TAG=2_repetit
(True, '8')
>>> primer_repe(list('xaabbccxdd'))  # doctesttag: +TAG=2_repetit
(True, 'x')
>>> primer_repe(list(range(500)))  # doctesttag: +TAG=2_repetit
(False, None)

Tests vent_fluix

>>> import pandas as pd
>>> df = pd.read_csv('meteo2.csv', index_col=0)
>>> from meteo import vent_fluix
>>> vent_fluix(df, 5)   # doctesttag: +TAG=3_vent_fluix
26
>>> vent_fluix(df, 10)   # doctesttag: +TAG=3_vent_fluix
7
>>> vent_fluix(df, 20)   # doctesttag: +TAG=3_vent_fluix
-1
>>> vent_fluix(df.loc[:20], 4)   # doctesttag: +TAG=3_vent_fluix
7
>>> vent_fluix(df.loc[10:], 12)   # doctesttag: +TAG=3_vent_fluix
31
>>> vent_fluix(df.loc[10:], 30)   # doctesttag: +TAG=3_vent_fluix
-1
>>> vent_fluix(df.loc[:30], 15)   # doctesttag: +TAG=3_vent_fluix
-1
>>> vent_fluix(df.loc[10:20], 5)   # doctesttag: +TAG=3_vent_fluix
18
>>> [vent_fluix(df, x) for x in range(1,20,3)]  # doctesttag: +TAG=3_vent_fluix
[26, 26, 7, 7, 7, -1, -1]
>>> [vent_fluix(df.loc[x:x+5], 1) for x in range(1,25)]  # doctesttag: +TAG=3_vent_fluix
[1, 7, 7, 7, 7, 7, 7, 8, 9, 12, 12, 12, 18, 18, 18, 18, 18, 18, 24, 24, 26, 26, 26, 26]

Tests dir_vent_pluja

>>> df = pd.read_csv('meteo2.csv', index_col=0)
>>> from meteo import dir_vent_pluja
>>> dir_vent_pluja(df) == {'E': 0.0, 'N': 19.0, 'NO': 2.5, 'O': 0.0, 'ONO': 10.25, 'S': 0.0, 'SE': 0.0, 'SEE': 2.25, 'SO': 1.0}  # doctesttag: +TAG=3_dir_vent_pluja
True
>>> dir_vent_pluja(df.loc[:10])  == {'NO': 0.25, 'O': 0.0, 'ONO': 0.25, 'S': 0.0, 'SE': 0.0, 'SEE': 0.25, 'SO': 1.0}  # doctesttag: +TAG=3_dir_vent_pluja
True
>>> dir_vent_pluja(df.loc[20:]) == {'N': 13.5, 'NO': 2.25, 'ONO': 10.0}  # doctesttag: +TAG=3_dir_vent_pluja
True
>>> dir_vent_pluja(df.loc[::2]) == {'E': 0.0, 'N': 9.5, 'NO': 0.25, 'ONO': 7.25, 'SE': 0.0, 'SO': 1.0}  # doctesttag: +TAG=3_dir_vent_pluja
True
>>> dir_vent_pluja(df.loc[::3]) == {'E': 0.0, 'NO': 2.5, 'ONO': 2.75, 'SE': 0.0, 'SEE': 0.25, 'SO': 1.0}  # doctesttag: +TAG=3_dir_vent_pluja
True