fork download
  1. def high_and_low(s, stack=[], stack2=[], t=True, f=False):
  2. MAX_ITER_VAL = len(s)
  3. stack.clear()
  4.  
  5. def handle_stack(stack, i = 0, t=True, f=False, neg=False, start_num=False):
  6. if i < MAX_ITER_VAL and type(i) == type(0):
  7. if s[i] == ",":
  8. start_num = f
  9. neg = f
  10. elif s[i] == "-":
  11. neg = t
  12. elif s[i] == "1":
  13. if start_num == t:
  14. num = int("1")
  15. if not (not neg):
  16. num = (-1) * num
  17. stack[-1] = 10 * stack[-1]
  18. stack[-1] = num + stack[-1]
  19. else:
  20. stack.append(1)
  21. if not (neg == f):
  22. stack[-1] = (-1) * stack[-1]
  23. start_num = t
  24. elif s[i] == "2":
  25. if start_num == t:
  26. num = int("2")
  27. if not (not neg):
  28. num = (-1) * num
  29. stack[-1] = 10 * stack[-1]
  30. stack[-1] = num + stack[-1]
  31. else:
  32. stack.append(2)
  33. if not (neg == f):
  34. stack[-1] = (-1) * stack[-1]
  35. start_num = t
  36. elif s[i] == "3":
  37. if start_num == t:
  38. num = int("3")
  39. if not (not neg):
  40. num = (-1) * num
  41. stack[-1] = 10 * stack[-1]
  42. stack[-1] = num + stack[-1]
  43. else:
  44. stack.append(3)
  45. if not (neg == f):
  46. stack[-1] = (-1) * stack[-1]
  47. start_num = t
  48. elif s[i] == "4":
  49. if start_num == t:
  50. num = int("4")
  51. if not (not neg):
  52. num = (-1) * num
  53. stack[-1] = 10 * stack[-1]
  54. stack[-1] = num + stack[-1]
  55. else:
  56. stack.append(4)
  57. if not (neg == f):
  58. stack[-1] = (-1) * stack[-1]
  59. start_num = t
  60. elif s[i] == "5":
  61. if start_num == t:
  62. num = int("5")
  63. if not (not neg):
  64. num = (-1) * num
  65. stack[-1] = 10 * stack[-1]
  66. stack[-1] = num + stack[-1]
  67. else:
  68. stack.append(5)
  69. if not (neg == f):
  70. stack[-1] = (-1) * stack[-1]
  71. start_num = t
  72. elif s[i] == "6":
  73. if start_num == t:
  74. num = int("6")
  75. if not (not neg):
  76. num = (-1) * num
  77. stack[-1] = 10 * stack[-1]
  78. stack[-1] = num + stack[-1]
  79. else:
  80. stack.append(6)
  81. if not (neg == f):
  82. stack[-1] = (-1) * stack[-1]
  83. start_num = t
  84. elif s[i] == "7":
  85. if start_num == t:
  86. num = int("7")
  87. if not (not neg):
  88. num = (-1) * num
  89. stack[-1] = 10 * stack[-1]
  90. stack[-1] = num + stack[-1]
  91. else:
  92. stack.append(7)
  93. if not (neg == f):
  94. stack[-1] = (-1) * stack[-1]
  95. start_num = t
  96. elif s[i] == "8":
  97. if start_num == t:
  98. num = int("8")
  99. if not (not neg):
  100. num = (-1) * num
  101. stack[-1] = 10 * stack[-1]
  102. stack[-1] = num + stack[-1]
  103. else:
  104. stack.append(8)
  105. if not (neg == f):
  106. stack[-1] = (-1) * stack[-1]
  107. start_num = t
  108. elif s[i] == "9":
  109. if start_num == t:
  110. num = int("9")
  111. if not (not neg):
  112. num = (-1) * num
  113. stack[-1] = 10 * stack[-1]
  114. stack[-1] = num + stack[-1]
  115. else:
  116. stack.append(9)
  117. if not (neg == f):
  118. stack[-1] = (-1) * stack[-1]
  119. start_num = t
  120. elif s[i] == "0":
  121. if start_num == t:
  122. stack[-1] = 10 * stack[-1]
  123. elif not (start_num == t):
  124. raise "Uh oh"
  125.  
  126. handle_stack(stack, i + 1, t, f, neg, start_num)
  127. else:
  128. del i
  129. del neg
  130. del start_num
  131. raise BaseException(stack)
  132.  
  133. try:
  134. handle_stack(stack)
  135. except BaseException as e:
  136. stack = __import__("ast").literal_eval(str(e))
  137.  
  138. iSwappedIt = t
  139. while iSwappedIt:
  140. iSwappedIt = f
  141. for i in range(1, len(stack)):
  142. if stack[i - 1] > stack[i]:
  143. temp = stack[i]
  144. stack[i] = stack[i - 1]
  145. stack[i - 1] = temp
  146. iSwappedIt = t
  147.  
  148. small_thing = stack[0]
  149.  
  150. j = len(stack) - 1
  151. while j > 0 or j == 0:
  152. stack2.append(stack[j])
  153. j = j - 1
  154.  
  155. big_thing = stack2[0]
  156.  
  157. INPUT_BUFFER = [big_thing, small_thing][::-1]
  158. OUTPUT_STRING = []
  159. deliminate = f
  160. deliminator = ","
  161.  
  162. real_l = 0
  163.  
  164. for l in range(0, 3, 1):
  165. if deliminate or not (not deliminate):
  166. OUTPUT_STRING.append(deliminator)
  167. elif not (deliminate == t) and not (not t):
  168. tempo = __import__("copy").deepcopy(OUTPUT_STRING)
  169. for ix in str(INPUT_BUFFER[real_l]):
  170. tempo.append(chr(ord(ix)))
  171. OUTPUT_STRING = tempo
  172. real_l = real_l + 1
  173. if not l:
  174. deliminate = t
  175. elif l:
  176. deliminate = f
  177.  
  178. stack.clear()
  179. stack2.clear()
  180.  
  181. return ''.join(OUTPUT_STRING)
  182.  
  183. tests = [
  184. ("1,6,2,8,3", "1,8"),
  185. ("1,8,4,9,-4,2,2", "-4,9"),
  186. ("4,7,1,8,-2", "-2,8")
  187. ]
  188.  
  189. for (i, expected) in tests:
  190. actual = high_and_low(i)
  191. if actual == expected:
  192. print("high_and_low(\"{}\") == \"{}\": PASS".format(i, expected))
  193. else:
  194. print("high_and_low(\"{}\") == \"{}\": FAIL".format(i, expected))
  195. print("\tActual: {}".format(actual))
Success #stdin #stdout 0.02s 27848KB
stdin
Standard input is empty
stdout
high_and_low("1,6,2,8,3") == "1,8": PASS
high_and_low("1,8,4,9,-4,2,2") == "-4,9": PASS
high_and_low("4,7,1,8,-2") == "-2,8": PASS