fork(3) download
  1. class Romans():
  2. """docstring for Romanos"""
  3. def __init__(self):
  4. self.eq = [("M",1000), ("CM", 900), ("D", 500), ("CD", 400), ("C", 100), ("XC", 90),
  5. ("L", 50), ("XL", 40), ("X", 10), ("IX", 9), ("V", 5), ("IV", 4), ("I", 1)]
  6. self.dic = {"M":1000, "D":500, "C":100, "L":50, "X":10, "V":5, "I": 1}
  7.  
  8. def to_roman(self, n):
  9. ans = ""
  10. for r,d in self.eq:
  11. for i in range(0,3):
  12. if n >= d:
  13. ans += r
  14. n -= d
  15. else: break
  16. return ans
  17.  
  18. def to_decimal(self, rom):
  19. ans = 0
  20. last = 0
  21. for c in rom:
  22. if (last > 0) and (last < self.dic[c]):
  23. ans += self.dic[c] - last
  24. last = 0
  25. else:
  26. ans += last
  27. last = self.dic[c]
  28. return ans+last
  29.  
  30. class Parents():
  31. """docstring for Parents"""
  32. def __init__(self):
  33. self.a = ["{", "(", "["]
  34. self.b = ["}", ")", "]"]
  35.  
  36. def validar(self, line):
  37. st = []
  38. for c in line:
  39. if c in self.a:
  40. st.append(c)
  41. elif c in self.b:
  42. if len(st) == 0:
  43. return False
  44. ind = self.b.index(c)
  45. if st.pop() != self.a[ind]:
  46. return False
  47. return True
  48.  
  49. class Subconjs():
  50. def subs(self, lista):
  51. l = len(lista)
  52. pl = (1<<l)
  53. ans = []
  54. for mask in range(0, pl):
  55. cmb = []
  56. for i in range(0, l):
  57. if (mask&(1<<i)) > 0:
  58. cmb.append(lista[i])
  59. ans.append(cmb)
  60. return ans
  61.  
  62.  
  63. prueba1 = Romans()
  64. print(prueba1.to_roman(39))
  65. print(prueba1.to_decimal('XXXIX'))
  66.  
  67. prueba2 = Parents()
  68. print(prueba2.validar("()[]{}"))
  69. print(prueba2.validar("({[)]"))
  70.  
  71. prueba3 = Subconjs()
  72. print(prueba3.subs([4, 5, 6]))
  73.  
Success #stdin #stdout 0.02s 9984KB
stdin
Standard input is empty
stdout
XXXIX
39
True
False
[[], [4], [5], [4, 5], [6], [4, 6], [5, 6], [4, 5, 6]]