fork download
  1. import math
  2.  
  3.  
  4. class Punto:
  5. # Atributos estáticos
  6. ORG_X = 0
  7. ORG_Y = 0
  8.  
  9. # Obj static
  10. ORG = None
  11.  
  12. @staticmethod
  13. def GET_ORG():
  14. if not Punto.ORG:
  15. Punto.ORG = Punto(Punto.ORG_X, Punto.ORG_Y)
  16.  
  17. return Punto.ORG
  18.  
  19. def __init__(self, x, y):
  20. # Atributos normales
  21. self._x = x
  22. self._y = y
  23.  
  24. @property
  25. def x(self):
  26. return self._x
  27.  
  28. @property
  29. def y(self):
  30. return self._y
  31.  
  32. @x.setter
  33. def x(self, v):
  34. self._x = v
  35.  
  36. @y.setter
  37. def y(self, v):
  38. self._y = v
  39.  
  40. def __add__(self, p2):
  41. return Punto(self.x + p2.x, self.y + p2.y)
  42.  
  43. def __eq__(self, p2):
  44. return self.x == p2.x and self.y == p2.y
  45.  
  46. def __str__(self):
  47. return f"{self._x}, {self._y}"
  48.  
  49.  
  50. class Persona:
  51. def __init__(self, nombre, edad):
  52. self._nombre = nombre
  53. self._edad = edad
  54.  
  55. @property
  56. def nombre(self):
  57. return self._nombre
  58.  
  59. @property
  60. def edad(self):
  61. return self._edad
  62.  
  63. def __str__(self):
  64. return f"{self.nombre} ({self.edad} años)"
  65.  
  66.  
  67. class Empleado(Persona):
  68. def __init__(self, nombre, edad, empresa, salario):
  69. super().__init__(nombre, edad)
  70. self._empresa = empresa
  71. self._salario = salario
  72.  
  73. @property
  74. def empresa(self):
  75. return self._empresa
  76.  
  77. @property
  78. def salario(self):
  79. return self._salario
  80.  
  81. def __str__(self):
  82. return super().__str__() + ": " + self.empresa + " = " + str(self.salario)
  83.  
  84.  
  85. # Lambdas
  86.  
  87. doble = lambda x: 2 * x
  88. car = lambda l: l[0] if len(l) > 0 else None
  89. cdr = lambda l: l[1:] if len(l) > 1 else []
  90.  
  91. reverse = lambda l: ([] if l == [] else
  92. [car(l)] if len(l) == 1 else
  93. reverse(cdr(l)) + [car(l)])
  94.  
  95. # Write a lambda that finds the minimum element in a list
  96. find_min_aux = lambda l, mn: (
  97. mn if l == [] else
  98. find_min_aux(cdr(l), car(l)) if car(l) < mn else
  99. find_min_aux(cdr(l), mn))
  100.  
  101. find_min = lambda l: (find_min_aux(cdr(l), car(l))
  102. if len(l) > 1 else None)
  103.  
  104. # Map, filter y reduce
  105. # Map(f, l) -> [f(x) for x in l]
  106. map = lambda f, l:(
  107. [] if l == [] else
  108. [f(car(l))] + map(f, cdr(l)))
  109.  
  110. # Filter(f, l) -> [x for x in l if f(x)]
  111. filter = lambda f, l: (
  112. [] if l == [] else
  113. filter(f, cdr(l)) if not f(car(l)) else
  114. [car(l)] + filter(f, cdr(l)))
  115.  
  116. # Reduce(f, l) -> reduce(lambda x, y: x + y, [1,2,3]) == 6
  117. reduce = lambda f, l, default=lambda x: 0 if x is None else x: (
  118. default(None) if l == [] else
  119. default(car(l)) if len(l) == 1 else
  120. f(default(car(l)), default(reduce(f, cdr(l), default))))
  121.  
  122.  
  123. print("doble(5) =", doble(5))
  124. print("reverse([1, 2, 3]) =", reverse([1, 2, 3]))
  125. print("find_min([5, 2, 1, 3, 9]) =", find_min([5, 2, 1, 3, 9]))
  126. print("map(lambda x: x * 2, [1, 2, 3, 4, 5, 6]) =",
  127. map(lambda x: x * 2, [1, 2, 3, 4, 5, 6]))
  128. print("filter(lambda x: x % 2 == 0, [1, 2, 3, 4, 5, 6, 7 ,8]) =",
  129. filter(lambda x: x % 2 == 0, [1, 2, 3, 4, 5, 6, 7 ,8]))
  130. print("reduce(lambda x, y: x + y, [1, 2, 3, 4, 5, 6, 7, 8]) =",
  131. reduce(lambda x, y: x + y, [1, 2, 3, 4, 5, 6, 7, 8]))
  132. print("reduce(lambda x, y: x * y, [1, 2, 3, 4, 5]) =",
  133. reduce(lambda x, y: x * y, [1, 2, 3, 4, 5]))
  134. print("reduce(lambda x, y: x * y, []) =",
  135. reduce(lambda x, y: x * y, [], default=lambda x: 1 if x is None else x))
  136.  
  137. lps = [Punto(0, 0), Punto(5, 6), Punto(7, 8), Punto(11, 22)]
  138.  
  139. # Show points in lps with a distance to origin < 30
  140. lps_dist_menor = map(str,
  141. filter(
  142. lambda p: math.sqrt((p.x ** 2) + (p.y ** 2)) < 10,
  143. lps))
  144.  
  145. print(str.join(" - ", [str(p) for p in lps]))
  146. print("Puntos dist origen < 10:", lps_dist_menor)
  147.  
  148. # Jubilación anticipada
  149. # How much money will we save if we retire people
  150. # with age > 60?
  151. lista_empleados = [
  152. Empleado("Baltasar", 21, "UVigo", 1500),
  153. Empleado("Lourdes", 61, "UVigo", 1500),
  154. Empleado("Pedro", 57, "UVigo", 1500),
  155. Empleado("Nanny", 48, "UVigo", 1500),
  156. Empleado("Rosalía", 63, "UVigo", 1500),
  157. Empleado("Reyes", 60, "UVigo", 1500),
  158. Empleado("Juan Carlos", 64, "UVigo", 1500),
  159. ]
  160.  
  161. print("Ahorro retiro:",
  162. reduce(lambda x, y: x + y,
  163. filter(lambda e: e.edad > 60, lista_empleados),
  164. default=lambda e: e.salario if isinstance(e, Empleado) else e))
  165.  
Success #stdin #stdout 0.02s 9300KB
stdin
Standard input is empty
stdout
doble(5) = 10
reverse([1, 2, 3]) = [3, 2, 1]
find_min([5, 2, 1, 3, 9]) = 1
map(lambda x: x * 2, [1, 2, 3, 4, 5, 6]) = [2, 4, 6, 8, 10, 12]
filter(lambda x: x % 2 == 0, [1, 2, 3, 4, 5, 6, 7 ,8]) = [2, 4, 6, 8]
reduce(lambda x, y: x + y, [1, 2, 3, 4, 5, 6, 7, 8]) = 36
reduce(lambda x, y: x * y, [1, 2, 3, 4, 5]) = 120
reduce(lambda x, y: x * y, []) = 1
0, 0 - 5, 6 - 7, 8 - 11, 22
Puntos dist origen < 10: ['0, 0', '5, 6']
Ahorro retiro: 4500