fork download
  1. # If you wish you can import any modules from the standard library
  2. # Do not use modules that are not in the standard library
  3. import sys
  4.  
  5.  
  6. def processArray(array):
  7. '''Modify this function to process `array` as indicated
  8. in the question. At the end, return the appropriate
  9. value.
  10.  
  11. Please create appropriate classes, and use appropriate
  12. data structures as necessary.
  13.  
  14. Do not print anything in this function.
  15.  
  16. Submit this entire program (not just this function)
  17. as your answer
  18. '''
  19. if len(array)==1:
  20. return 1
  21.  
  22. if len(array)==0:
  23. return 0
  24.  
  25. start = 0
  26. i=0
  27. while i!=len(array):
  28. if array[i]%3==0:
  29. sum_accum = 0
  30. while array[i]%3==0:
  31. sum_accum = sum_accum + array[i]
  32. i = i + 1
  33. array[start] = sum_accum
  34. sum_accum = 0
  35. else:
  36. array[start] = array[i]
  37. i = i + 1
  38. start = start + 1
  39.  
  40.  
  41. res = start + 1
  42. sys.stdout.write(str(res))
  43. return res # change this appropriately, if necessary
  44.  
  45. def run():
  46. array = []
  47. for line in sys.stdin:
  48. try:
  49. intval = int(line)
  50. except ValueError:
  51. # If line is not exactly one integer, ignore it
  52. pass
  53. if intval == 0:
  54. break
  55. array.append(intval)
  56. newlen = processArray(array)
  57. for i in range(newlen):
  58. print(array[i])
  59.  
  60. if __name__ == '__main__':
  61. run()
Success #stdin #stdout 0.07s 14176KB
stdin
2
6
36
62
121
66
63
39
661
6
-1
stdout
92
42
62
121
168
661
6
-1
661