# Examen parcial 2 ALS 2014-15
# coding=UTF-8
import unittest
import pickle
import json
# Pregunta 1 -----------------------------------------------------------------
class Pieza:
def __init__(self, id, nombre):
self.id = id
self.nombre = nombre
def __str__(self):
return str.format("{0}: {1}", self.id, self.nombre)
def save():
f = None
try:
f = open("piezas.dat", "wb")
pickle.dump(dictPiezas, f)
finally:
if f != None:
f.close()
def load():
dPiezas = {}
f = None
try:
f = open("piezas.dat", "rb")
dPiezas = pickle.load(f)
finally:
if f != None:
f.close()
return dPiezas
def piezas2str(dp):
toret = "\n"
for p in dp.values():
toret += str(p) + '\n'
return toret
# Pregunta 2 -----------------------------------------------------------------
class ListaOrdenada:
def __init__(self):
self.l = []
def to_list(self):
return self.l.copy()
def inserta(self, x):
i = 0
while( i < len(self.l)
and self.l[i] < x):
i += 1
self.l.insert(i, x)
return
def borraEn(self, i):
if (len(self.l) > i):
self.l.pop(i)
def __str__(self):
toret = "[ "
for x in l:
toret += str(x) + ' '
toret += ']'
return toret
class TestListaOrdenada(unittest.TestCase):
def setUp(self):
self.l = ListaOrdenada()
self.l.inserta(1)
self.l.inserta(4)
self.l.inserta(6)
def test_inserta(self):
resultado = [ 1, 2, 3, 4, 5, 6 ]
self.l.inserta(2)
self.l.inserta(3)
self.l.inserta(5)
self.assertEqual(resultado, self.l.to_list())
def test_borraEn(self):
resultado = [ 1, 6 ]
self.l.borraEn(1)
self.assertEqual(resultado, self.l.to_list())
# Pregunta 3 -----------------------------------------------------------------
collatz = lambda n: \
[] if n <= 1 else \
[(3 * n) + 1] + collatz((3 * n) + 1) if n % 2 != 0 else \
[n // 2] + collatz(n // 2)
# Pregunta 4 -----------------------------------------------------------------
def invoke(obj, strMth):
toret = None
m = getattr(obj, strMth)
if (m != None
and callable(m)):
toret = m()
return toret
# Pregunta 5 -----------------------------------------------------------------
class PuntosPartido:
def __init__(self):
self.puntos = {}
def inserta(self, dorsal, nombre):
dorsal = str(dorsal)
self.puntos[dorsal] = {"nombre": nombre, "faltas": 0, "puntos": 0}
def ponFalta(self, dorsal):
dorsal = str(dorsal)
self.puntos[dorsal]["faltas"] += 1
def ponCanasta(self, dorsal):
dorsal = str(dorsal)
self.puntos[dorsal]["puntos"] += 2
def getFaltas(self, dorsal):
dorsal = str(dorsal)
return self.puntos[dorsal]["faltas"]
def getPuntos(self, dorsal):
dorsal = str(dorsal)
return self.puntos[dorsal]["puntos"]
def getInfo(self, dorsal):
dorsal = str(dorsal)
return self.puntos[dorsal]
def save(self, nf):
f = open(nf, "w");
json.dump(self.puntos, f)
f.close()
@staticmethod
def load(nf):
toret = PuntosPartido()
f = open(nf, "r");
info = json.load(f)
f.close()
toret.puntos = info
return toret
# Demo -----------------------------------------------------------------------
class A:
def foo(self):
print("hola")
print("collatz(255): " + str(collatz(255)))
invoke(A(), "foo")
p = PuntosPartido()
p.inserta(1, "Fernando Martin")
p.inserta(15, "Petrovic")
p.ponCanasta(1)
p.ponCanasta(15)
p.ponFalta(15)
print(p.getInfo(1))
print(p.getInfo(15))
print("Saving as JSON...")
#p.save("partido.json")
#p = None
#print("Loading JSON...")
#p = PuntosPartido.load("partido.json")
#print(p.getInfo(1))
#print(p.getInfo(15))
IyBFeGFtZW4gcGFyY2lhbCAyIEFMUyAyMDE0LTE1CiMgY29kaW5nPVVURi04CgppbXBvcnQgdW5pdHRlc3QKaW1wb3J0IHBpY2tsZQppbXBvcnQganNvbgoKIyBQcmVndW50YSAxIC0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tCmNsYXNzIFBpZXphOgogICAgZGVmIF9faW5pdF9fKHNlbGYsIGlkLCBub21icmUpOgogICAgICAgIHNlbGYuaWQgPSBpZAogICAgICAgIHNlbGYubm9tYnJlID0gbm9tYnJlCiAgICAgICAgCiAgICBkZWYgX19zdHJfXyhzZWxmKToKICAgICAgICByZXR1cm4gc3RyLmZvcm1hdCgiezB9OiB7MX0iLCBzZWxmLmlkLCBzZWxmLm5vbWJyZSkKCQpkZWYgc2F2ZSgpOgogICAgZiA9IE5vbmUKICAgIHRyeToKICAgICAgICBmID0gb3BlbigicGllemFzLmRhdCIsICJ3YiIpCiAgICAgICAgcGlja2xlLmR1bXAoZGljdFBpZXphcywgZikKICAgIGZpbmFsbHk6CiAgICAgICAgaWYgZiAhPSBOb25lOgogICAgICAgICAgICBmLmNsb3NlKCkKICAgIApkZWYgbG9hZCgpOgogICAgZFBpZXphcyA9IHt9CiAgICBmID0gTm9uZQogICAgdHJ5OgogICAgICAgIGYgPSBvcGVuKCJwaWV6YXMuZGF0IiwgInJiIikKICAgICAgICBkUGllemFzID0gcGlja2xlLmxvYWQoZikKICAgIGZpbmFsbHk6CiAgICAgICAgaWYgZiAhPSBOb25lOgogICAgICAgICAgICBmLmNsb3NlKCkKICAgICAgICAgICAgCiAgICByZXR1cm4gZFBpZXphcwogICAgICAgICAgICAKZGVmIHBpZXphczJzdHIoZHApOgogICAgdG9yZXQgPSAiXG4iCiAgICBmb3IgcCBpbiBkcC52YWx1ZXMoKToKICAgICAgICB0b3JldCArPSBzdHIocCkgKyAnXG4nCiAgICAgICAgCiAgICByZXR1cm4gdG9yZXQKCiMgUHJlZ3VudGEgMiAtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLQpjbGFzcyBMaXN0YU9yZGVuYWRhOgogICAgZGVmIF9faW5pdF9fKHNlbGYpOgogICAgICAgIHNlbGYubCA9IFtdCiAgICAgICAgCiAgICBkZWYgdG9fbGlzdChzZWxmKToKICAgICAgICByZXR1cm4gc2VsZi5sLmNvcHkoKQogICAgCiAgICBkZWYgaW5zZXJ0YShzZWxmLCB4KToKICAgICAgICBpID0gMAogICAgICAgIAogICAgICAgIHdoaWxlKCBpIDwgbGVuKHNlbGYubCkKICAgICAgICAgICBhbmQgc2VsZi5sW2ldIDwgeCk6CiAgICAgICAgICAgIGkgKz0gMQogICAgICAgICAgICAKICAgICAgICBzZWxmLmwuaW5zZXJ0KGksIHgpCiAgICAgICAgcmV0dXJuCiAgICAKICAgIGRlZiBib3JyYUVuKHNlbGYsIGkpOgogICAgICAgIGlmIChsZW4oc2VsZi5sKSA+IGkpOgogICAgICAgICAgICBzZWxmLmwucG9wKGkpCiAgICAKICAgIGRlZiBfX3N0cl9fKHNlbGYpOgogICAgICAgIHRvcmV0ID0gIlsgIgogICAgICAgIGZvciB4IGluIGw6CiAgICAgICAgICAgIHRvcmV0ICs9IHN0cih4KSArICcgJwogICAgICAgIHRvcmV0ICs9ICddJwogICAgICAgIHJldHVybiB0b3JldAogICAgCmNsYXNzIFRlc3RMaXN0YU9yZGVuYWRhKHVuaXR0ZXN0LlRlc3RDYXNlKToKICAgIGRlZiBzZXRVcChzZWxmKToKICAgICAgICBzZWxmLmwgPSBMaXN0YU9yZGVuYWRhKCkKICAgICAgICBzZWxmLmwuaW5zZXJ0YSgxKQogICAgICAgIHNlbGYubC5pbnNlcnRhKDQpCiAgICAgICAgc2VsZi5sLmluc2VydGEoNikKICAgICAgICAKICAgIGRlZiB0ZXN0X2luc2VydGEoc2VsZik6CiAgICAgICAgcmVzdWx0YWRvID0gWyAxLCAyLCAzLCA0LCA1LCA2IF0KICAgICAgICBzZWxmLmwuaW5zZXJ0YSgyKQogICAgICAgIHNlbGYubC5pbnNlcnRhKDMpCiAgICAgICAgc2VsZi5sLmluc2VydGEoNSkKICAgICAgICAKICAgICAgICBzZWxmLmFzc2VydEVxdWFsKHJlc3VsdGFkbywgc2VsZi5sLnRvX2xpc3QoKSkKICAgICAgICAKICAgIGRlZiB0ZXN0X2JvcnJhRW4oc2VsZik6CiAgICAgICAgcmVzdWx0YWRvID0gWyAxLCA2IF0KICAgICAgICBzZWxmLmwuYm9ycmFFbigxKQogICAgICAgIHNlbGYuYXNzZXJ0RXF1YWwocmVzdWx0YWRvLCBzZWxmLmwudG9fbGlzdCgpKQoKIyBQcmVndW50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J0aW4iKQpwLmluc2VydGEoMTUsICJQZXRyb3ZpYyIpCnAucG9uQ2FuYXN0YSgxKQpwLnBvbkNhbmFzdGEoMTUpCnAucG9uRmFsdGEoMTUpCnByaW50KHAuZ2V0SW5mbygxKSkKcHJpbnQocC5nZXRJbmZvKDE1KSkKcHJpbnQoIlNhdmluZyBhcyBKU09OLi4uIikKI3Auc2F2ZSgicGFydGlkby5qc29uIikKI3AgPSBOb25lCgojcHJpbnQoIkxvYWRpbmcgSlNPTi4uLiIpCiNwID0gUHVudG9zUGFydGlkby5sb2FkKCJwYXJ0aWRvLmpzb24iKQojcHJpbnQocC5nZXRJbmZvKDEpKQojcHJpbnQocC5nZXRJbmZvKDE1KSkK
collatz(255): [766, 383, 1150, 575, 1726, 863, 2590, 1295, 3886, 1943, 5830, 2915, 8746, 4373, 13120, 6560, 3280, 1640, 820, 410, 205, 616, 308, 154, 77, 232, 116, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]
hola
{'nombre': 'Fernando Martin', 'faltas': 0, 'puntos': 2}
{'nombre': 'Petrovic', 'faltas': 1, 'puntos': 2}
Saving as JSON...