lab3.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env python
  2. import sys
  3. sys.path.append('/home/dotcloud/env/lib/python2.6/site-packages')
  4. from sandbox import Sandbox, SandboxConfig
  5. import os, traceback
  6. importname = sys.argv[1]
  7. if importname.endswith('.py'):
  8. importname = importname[:len(importname)-3]
  9. else:
  10. print >> sys.stderr, 'Filename did not end with .py for some reason...'
  11. sys.exit(2)
  12. os.chdir(sys.path[0])
  13. sys.path.insert(0, os.path.expanduser('~/submissions'))
  14. sys.dont_write_bytecode = True
  15. from random import seed, randint
  16. seed(1)
  17. def wins_best_record(teams, games):
  18. wins = [[0] * teams for _ in range(teams)]
  19. for i in xrange(teams):
  20. for j in xrange(teams):
  21. wins[i][j] = randint(0, games)
  22. wins[j][i] = games - wins[i][j]
  23. sums = map(sum, wins)
  24. largest_idx = 0
  25. for i in xrange(1, len(sums)):
  26. if sums[i] > sums[largest_idx]:
  27. largest_idx = i
  28. return wins, largest_idx, sums[largest_idx]
  29. pizfiles = [('handout.piz', 3950), ('invalid.piz', 1050)]
  30. output = ''
  31. score = 0
  32. def run():
  33. submission = __import__(importname)
  34. global output, score
  35. pizzas = [
  36. {'num_toppings': 1, 'toppings': [('topping', 0)], 'size': 10},
  37. {'num_toppings': 2, 'toppings': [('one', 1), ('two', 2)], 'size': 12},
  38. {'num_toppings': 0, 'toppings': [], 'size': 8},
  39. {'num_toppings': 1, 'toppings': [('topping', 0)], 'size': 7},
  40. {'num_toppings': 2, 'toppings': [('topping one', 1), ('topping two', 1)], 'size': 6},
  41. {'num_toppings': 3, 'toppings': [('one', 2), ('two', 1), ('three', 0)], 'size': 12},
  42. {'toppings': [('', 0)], 'size': 10},
  43. {'num_toppings': 1, 'toppings': [('topping', 0)]},
  44. ]
  45. validity = [True, True, False, False, False, True, False, False]
  46. for i in range(len(pizzas)):
  47. if submission.valid_pizza(pizzas[i]) == validity[i]:
  48. score += 1
  49. else:
  50. output += '%s should be %s\n' % (pizzas[i], 'valid' if validity[i] else 'invalid')
  51. wbr_args = [(5, 3), (5, 3), (16, 5)]
  52. for args in wbr_args:
  53. wins, b_team, b_number = wins_best_record(*args)
  54. team, number = submission.best_record(wins)
  55. if team == b_team:
  56. score += 1
  57. else:
  58. output += 'Best record of\n'
  59. for l in wins:
  60. output += ' %s\n' % l
  61. output += ' should be team #%d\n' % b_team
  62. if number == b_number:
  63. score += 1
  64. else:
  65. output += 'Best record of\n'
  66. for l in wins:
  67. output += ' %s\n' % l
  68. output += ' should be %d wins\n' % b_number
  69. for pizfile in pizfiles:
  70. result = submission.order_pizza(pizfile[0])
  71. if result == pizfile[1]:
  72. score += 3
  73. else:
  74. output += 'Order was incorrect for %s; got %d (answer is hidden)\n' % (pizfile[0], result)
  75. return score
  76. config = SandboxConfig()
  77. config.timeout = 5
  78. config.allowModule(importname, 'valid_pizza', 'best_record', 'order_pizza')
  79. for pizfile in pizfiles:
  80. config.allowPath(pizfile[0])
  81. sandbox = Sandbox(config)
  82. try:
  83. score = sandbox.call(run)
  84. except:
  85. traceback.print_exc(20, sys.stderr)
  86. print >> sys.stderr
  87. print score
  88. print >> sys.stderr, output