fork download
  1. # your code goes here
  2.  
  3. import numpy as np, time
  4.  
  5. # simply check
  6. def every(arr, val):
  7. for e in arr:
  8. if e != val:
  9. return False
  10. return True
  11.  
  12. # check with premature
  13. def every_plus(arr, val):
  14. for i in xrange(0, len(arr)/2+1):
  15. if arr[i] != val or arr[-1 - i] != val:
  16. return False
  17. return True
  18.  
  19. count = 10**5
  20.  
  21. values = np.empty((count), dtype=np.uint8)
  22. values.fill(0)
  23.  
  24. print values
  25. ## tests with filled by nulls
  26. start_time = time.time()
  27. print every(values, 0)
  28. print time.time() - start_time, "seconds of non optimased check"
  29. start_time = time.time()
  30. print every_plus(values, 0)
  31. print time.time() - start_time, "seconds of optimased check"
  32. print len(filter(lambda x: x != 0, values)) == 0
  33. print time.time() - start_time, "seconds of filter-based check"
  34.  
  35. values[count-1] = 1
  36.  
  37. print values
  38. ## tests with one in the end
  39. start_time = time.time()
  40. print every(values, 0)
  41. print time.time() - start_time, "seconds of non optimased check"
  42. start_time = time.time()
  43. print every_plus(values, 0)
  44. print time.time() - start_time, "seconds of optimased check"
  45. print len(filter(lambda x: x == 0, values)) == 0
  46. print time.time() - start_time, "seconds of filter-based check"
  47.  
  48. values[count-1] = 0
  49. values[count/2] = 1
  50.  
  51. print values
  52. ## tests with one in the middle
  53. start_time = time.time()
  54. print every(values, 0)
  55. print time.time() - start_time, "seconds of non optimased check"
  56. start_time = time.time()
  57. print every_plus(values, 0)
  58. print time.time() - start_time, "seconds of optimased check"
  59. print len(filter(lambda x: x == 0, values)) == 0
  60. print time.time() - start_time, "seconds of filter-based check"
Success #stdin #stdout 4.29s 16672KB
stdin
Standard input is empty
stdout
[0 0 0 ..., 0 0 0]
True
0.532950162888 seconds of non optimased check
True
0.546085119247 seconds of optimased check
True
1.10429215431 seconds of filter-based check
[0 0 0 ..., 0 0 1]
False
0.535746097565 seconds of non optimased check
False
1.69277191162e-05 seconds of optimased check
False
0.592010974884 seconds of filter-based check
[0 0 0 ..., 0 0 0]
False
0.269232988358 seconds of non optimased check
False
0.544636964798 seconds of optimased check
False
1.13299608231 seconds of filter-based check