You are on page 1of 2

Esquema de recorregut genèric Esquema de cerca genèric (amb booleà i sense)

obtenir_primer_element trobat = False


while not darrer_element : obtenir_primer_element
tractar_element while not darrer_element_ja_tractat and not trobat :
obtenir_següent_element if element_compleix_condició :
tractar_darrer_element trobat = True
tractament_final else :
obtenir_següent_element
if trobat :
tractament_trobat
else :
tractament_no_trobat
obtenir_primer_element
while not darrer_element and not el_compleix_cond :
obtenir_següent_element
if el_compleix_cond :
tractament_trobat
else :
tractament_no_trobat

Esquemes de recorregut sobre llistes Esquema de cerca sobre llistes (amb booleà i sense)
(amb l’estructura while) trobat = False
índex = 0 i =0
while índex < len(llista) : while i<len(l) and not trobat :
tractar_element llista[índex] if compleix_condició_l[i] :
índex = índex + 1 trobat = True
tractament_final else :
i = i + 1
if trobat :
tractament_trobat
else :
tractament_no_trobat
(amb l’estructura for-in i accés als índexs) i =0
for índex in range(len(llista)) : while i < len(l)-1 and not compleix_cond_l[i] :
tractar llista[índex] i = i + 1
tractament_final if compleix_cond_l[i] :
tractament_trobat
(amb l’estructura for-in sense accés als índexs) else :
for element in llista : tractament_no_trobat
tractar element
tractament_final

Esquemes de recorregut i cerca (amb booleà) sobre matrius:


for i in range(len(matriu)) : trobat = False
# Per cada fila amb índex i i = 0
for j in range(len(matriu[i])) : while i<len(matriu) and not trobat :
# Per cada columna j dins de la fila i j = 0
tractar matriu[i][j] while j<len(matriu[i]) and not trobat :
tractament_final if compleix_condició_matriu[i][j] :
trobat = True
else :
j=j+1
if not trobat :
i=i+1
if trobat :
tractament_trobat
else :
tractament_no_trobat

Esquemes de recorregut i cerca (amb booleà) en fluxos de dades:


obtenir_primer_element trobat = False
while not element_sentinella : obtenir_primer_element
tractar_element while not element_sentinella and not trobat :
obtenir_següent_element if element_compleix_condició :
tractament_final trobat = True
else :
obtenir_següent_element
if trobat :
tractament_trobat
else :
tractament_no_trobat

You might also like