You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

foundation.py 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. class AlgoDatValue:
  2. memory = 0
  3. read = 0
  4. write = 0
  5. compare = 0
  6. add_operation = 0
  7. sub_operation = 0
  8. mul_operation = 0
  9. div_operation = 0
  10. bit_operation = 0
  11. @staticmethod
  12. def summary():
  13. print("*** Summary ***")
  14. print(f"Memory: {AlgoDatValue.memory}")
  15. print(f"Read: {AlgoDatValue.read}")
  16. print(f"Write: {AlgoDatValue.write}")
  17. print(f"Compare: {AlgoDatValue.compare}")
  18. print(f"Add: {AlgoDatValue.add_operation}")
  19. print(f"Sub: {AlgoDatValue.sub_operation}")
  20. print(f"Mul: {AlgoDatValue.mul_operation}")
  21. print(f"Div: {AlgoDatValue.div_operation}")
  22. print(f"Bit: {AlgoDatValue.bit_operation}")
  23. def __init__(self, value):
  24. AlgoDatValue.memory += 1
  25. AlgoDatValue.write += 1
  26. self.value = value
  27. def __str__(self):
  28. AlgoDatValue.read += 1
  29. return f"{self.value}"
  30. def __repr__(self):
  31. AlgoDatValue.read += 1
  32. return f"{self.value}"
  33. def __eq__(self, other):
  34. AlgoDatValue.compare += 1
  35. AlgoDatValue.read += 2
  36. if not isinstance(other, AlgoDatValue):
  37. return self.value == other
  38. return self.value == other.value
  39. def __lt__(self, other):
  40. AlgoDatValue.compare += 1
  41. AlgoDatValue.read += 2
  42. if not isinstance(other, AlgoDatValue):
  43. return self.value < other
  44. return self.value < other.value
  45. def __le__(self, other):
  46. AlgoDatValue.compare += 1
  47. AlgoDatValue.read += 2
  48. if not isinstance(other, AlgoDatValue):
  49. return self.value <= other
  50. return self.value <= other.value
  51. def __gt__(self, other):
  52. AlgoDatValue.compare += 1
  53. AlgoDatValue.read += 2
  54. if not isinstance(other, AlgoDatValue):
  55. return self.value > other
  56. return self.value > other.value
  57. def __ge__(self, other):
  58. AlgoDatValue.compare += 1
  59. AlgoDatValue.read += 2
  60. if not isinstance(other, AlgoDatValue):
  61. return self.value >= other
  62. return self.value >= other.value
  63. def __add__(self, other):
  64. AlgoDatValue.add_operation += 1
  65. AlgoDatValue.read += 2
  66. return self.value + other.value
  67. def __sub__(self, other):
  68. AlgoDatValue.sub_operation += 1
  69. AlgoDatValue.read += 2
  70. return self.value - other.value
  71. def __mul__(self, other):
  72. AlgoDatValue.mul_operation += 1
  73. AlgoDatValue.read += 2
  74. return self.value * other.value
  75. def __truediv__(self, other):
  76. AlgoDatValue.div_operation += 1
  77. AlgoDatValue.read += 2
  78. return self.value / other.value
  79. def __floordiv__(self, other):
  80. AlgoDatValue.div_operation += 1
  81. AlgoDatValue.read += 2
  82. return self.value // other.value
  83. def __mod__(self, other):
  84. AlgoDatValue.div_operation += 1
  85. AlgoDatValue.read += 2
  86. return self.value % other.value
  87. def __iadd__(self, other):
  88. AlgoDatValue.add_operation += 1
  89. AlgoDatValue.read += 1
  90. AlgoDatValue.write += 1
  91. self.value += other.value
  92. return self
  93. def __isub__(self, other):
  94. AlgoDatValue.sub_operation += 1
  95. AlgoDatValue.read += 1
  96. AlgoDatValue.write += 1
  97. self.value -= other.value
  98. return self
  99. def __imul__(self, other):
  100. AlgoDatValue.mul_operation += 1
  101. AlgoDatValue.read += 1
  102. AlgoDatValue.write += 1
  103. self.value *= other.value
  104. return self
  105. def __itruediv__(self, other):
  106. AlgoDatValue.div_operation += 1
  107. AlgoDatValue.read += 1
  108. AlgoDatValue.write += 1
  109. self.value /= other.value
  110. return self
  111. def __ifloordiv__(self, other):
  112. AlgoDatValue.div_operation += 1
  113. AlgoDatValue.read += 1
  114. AlgoDatValue.write += 1
  115. self.value //= other.value
  116. return self
  117. def __imod__(self, other):
  118. AlgoDatValue.div_operation += 1
  119. AlgoDatValue.read += 1
  120. AlgoDatValue.write += 1
  121. self.value %= other.value
  122. return self
  123. def __neg__(self):
  124. return -self.value
  125. def __pos__(self):
  126. return +self.value
  127. def __abs__(self):
  128. return abs(self.value)
  129. def __invert__(self):
  130. return ~self.value
  131. def __lshift__(self, other):
  132. AlgoDatValue.bit_operation += 1
  133. AlgoDatValue.read += 2
  134. return self.value << other.value
  135. def __rshift__(self, other):
  136. AlgoDatValue.bit_operation += 1
  137. AlgoDatValue.read += 2
  138. return self.value >> other.value
  139. def __and__(self, other):
  140. AlgoDatValue.bit_operation += 1
  141. AlgoDatValue.read += 2
  142. return self.value & other.value
  143. def __xor__(self, other):
  144. AlgoDatValue.bit_operation += 1
  145. AlgoDatValue.read += 2
  146. return self.value ^ other.value
  147. def __or__(self, other):
  148. AlgoDatValue.bit_operation += 1
  149. AlgoDatValue.read += 2
  150. return self.value | other.value
  151. def __ilshift__(self, other):
  152. AlgoDatValue.bit_operation += 1
  153. AlgoDatValue.read += 2
  154. AlgoDatValue.write += 1
  155. self.value <<= other.value
  156. return self
  157. def __irshift__(self, other):
  158. AlgoDatValue.bit_operation += 1
  159. AlgoDatValue.read += 2
  160. AlgoDatValue.write += 1
  161. self.value >>= other.value
  162. return self
  163. def __iand__(self, other):
  164. AlgoDatValue.bit_operation += 1
  165. AlgoDatValue.read += 2
  166. AlgoDatValue.write += 1
  167. self.value &= other.value
  168. return self
  169. def __ixor__(self, other):
  170. AlgoDatValue.bit_operation += 1
  171. AlgoDatValue.read += 2
  172. AlgoDatValue.write += 1
  173. self.value ^= other.value
  174. return self
  175. def __ior__(self, other):
  176. AlgoDatValue.bit_operation += 1
  177. AlgoDatValue.read += 2
  178. AlgoDatValue.write += 1
  179. self.value |= other.value
  180. return self
  181. def __int__(self):
  182. return int(self.value)
  183. def __float__(self):
  184. return float(self.value)
  185. def __complex__(self):
  186. return complex(self.value)
  187. def __round__(self, n=0):
  188. return round(self.value, n)
  189. class AlgoDatArray:
  190. def __init__(self, size):
  191. self.size = size
  192. self.array = [AlgoDatValue(None)] * size
  193. def set(self, index, value):
  194. assert isinstance(value, AlgoDatValue)
  195. assert isinstance(index, int)
  196. self.array[index] = value
  197. def get(self, index):
  198. assert isinstance(index, int)
  199. return self.array[index]
  200. def __str__(self):
  201. return str(self.array)
  202. def __len__(self):
  203. return len(self.array)
  204. def __iter__(self):
  205. return iter(self.array)
  206. def __getitem__(self, index):
  207. assert isinstance(index, int)
  208. return self.array[index]
  209. def __setitem__(self, index, value):
  210. assert isinstance(index, int)
  211. assert isinstance(value, AlgoDatValue)
  212. self.array[index] = value
  213. def ReadIntSequence(filename):
  214. with open(filename, "r") as file:
  215. l = list(map(int, file.read().split()))
  216. a = AlgoDatArray(len(l))
  217. for i in range(len(l)):
  218. a.set(i, AlgoDatValue(l[i]))
  219. return a