# Examen ALS Parcial 1 - 2015
__author__ = "jbgarcia@uvigo.es"
def ordena( l) :
"""Devuelve una nueva lista ordenada.
:param l: La lista a ordenar
:return: Una nueva lista, como l, pero ordenada.
"""
toret = [ ]
for x in l:
# Find position in target list
i = 0
while ( i < len ( toret)
and toret[ i] < x) :
i += 1
toret.insert ( i, x)
return toret
def histograma( l) :
"""Muestra un histograma por pantalla, usando '*'
:param l: La lista de datos a mostrar.
"""
def dibuja_barra( x) :
print ( "*{0}" .format ( "*" * x) )
maxi = max ( l)
mini = min ( l)
diff = maxi - mini
for x in l:
dibuja_barra( ( x * 10 ) // diff)
def resta_conjuntos( c1, c2) :
"""Devuelve un nuevo conjunto con el resultado de restar c2 de c1
:param c1: El conjunto del que restar
:param c2: El conjunto a restar
:return: Un nuevo conjunto resultado.
"""
toret = set ( )
for x in c1:
if x not in c2:
toret.add ( x)
return toret
class Estudiante:
def __init__ ( self , d, n) :
self .__nombre = n
self .__dni = d
@ property
def nombre( self ) :
return self .__nombre
@ property
def dni( self ) :
return self .__dni
def __str__ ( self ) :
return "{0}: {1}" .format ( self .dni , self .nombre )
class Nota:
def __init__ ( self , d, v) :
self .__dni = d
self .valor = v
@ property
def dni( self ) :
return self .__dni
@ property
def valor( self ) :
return self .__valor
@ valor.setter
def valor( self , x) :
self .__valor = x
def __str__ ( self ) :
return "{0}, {1}" .format ( self .dni , self .valor )
class Asignatura:
def __init__ ( self , n) :
self .__nombre = n
self .__estudiantes = { }
self .__notas = { }
@ property
def nombre( self ) :
return self .__nombre
def matricula( self , e) :
self .__estudiantes[ e.dni ] = e
def califica( self , n) :
self .__notas[ n.dni ] = n
def __str__ ( self ) :
toret = ""
for dni, estudiante in self .__estudiantes.items ( ) :
nota = self .__notas.get ( dni)
if nota != None :
toret += "{0}: {1}\n " .format ( estudiante, nota.valor )
else :
toret += "{0}\n " .format ( estudiante)
return toret
class Aparato:
def __init__ ( self , c, m, n) :
self .__codigo = c
self .__nombre = n
self .__modelo = m
@ property
def codigo( self ) :
return self .__codigo
@ property
def modelo( self ) :
return self .__modelo
@ property
def nombre( self ) :
return self .__nombre
def __str__ ( self ) :
return "{0}: {1} {2}" .format ( self .codigo , self .modelo , self .nombre )
class AparatoNacional( Aparato) :
def __init__ ( self , c, m, n, p) :
Aparato.__init__ ( self , c, m, n)
self .__provincia = p
@ property
def provincia( self ) :
return self .__provincia
def __str__ ( self ) :
return Aparato.__str__ ( self ) + " provincia: " + self .provincia
class AparatoImportacion( Aparato) :
def __init__ ( self , c, m, n, p) :
super ( ) .__init__ ( c, m, n)
self .__pais = p
@ property
def pais( self ) :
return self .__pais
def __str__ ( self ) :
return super ( ) .__str__ ( ) + " pais: " + self .pais
# Demo ejercicio 1
print ( ordena( [ 7 , 5 , 9 , 2 , 4 , 8 , 6 , 3 , 1 ] ) )
# Demo ejercicio 2
histograma( [ 0 , 40 , 20 , 99 ] )
# Demo ejercicio 3
print ( resta_conjuntos( { 1 , 2 , 3 } , { 2 } ) )
# Demo ejercicio 4
e1 = Estudiante( 12345678 , "Palotes, Perico de los" )
e2 = Estudiante( 12345679 , "Con Tomate, Pan" )
print ( "{0}\n {1}\n " .format ( e1, e2) )
a1 = Asignatura( "ALS" )
a1.matricula ( e1)
a1.matricula ( e2)
a1.califica ( Nota( e1.dni , 7.5 ) )
print ( a1)
# Demo ejercicio 5
ap1 = AparatoNacional( 1 , "BlueSens" , "Web TV" , "Madrid" )
ap2 = AparatoImportacion( 2 , "Apple" , "On-Line TV" , "EEUU" )
print ( ap1)
print ( ap2)
IyBFeGFtZW4gQUxTIFBhcmNpYWwgMSAtIDIwMTUKX19hdXRob3JfXyA9ICJqYmdhcmNpYUB1dmlnby5lcyIKCmRlZiBvcmRlbmEobCk6CgkiIiJEZXZ1ZWx2ZSB1bmEgbnVldmEgbGlzdGEgb3JkZW5hZGEuCgkKCQk6cGFyYW0gbDogTGEgbGlzdGEgYSBvcmRlbmFyCgkJOnJldHVybjogVW5hIG51ZXZhIGxpc3RhLCBjb21vIGw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