lab3.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env python
  2. # valid_pizza() checks if a pizza is valid, returning True if it is.
  3. # A valid pizza is a dict with
  4. # 'toppings', a list of toppings
  5. # each topping is a 2-tuple with the name of the topping and a 0, 1, or 2
  6. # 0 means that the topping covers the whole pizza
  7. # 1 means that the topping covers one half of the pizza
  8. # 2 means that the topping covers the other half of the pizza
  9. # 'num_toppings', a number equal to the number of toppings
  10. # 'size', one of 6, 8, 10, or 12 (representing inches)
  11. # Each half of the pizza must have at least one topping (so just a 0 is OK, a 1
  12. # and a 2 is OK, but only 1's are not OK).
  13. # up to 8 points
  14. def valid_pizza(pizza):
  15. if pizza['toppings'][0][0] == 'pepperoni':
  16. return True
  17. # Every year, pizza shops hold a competition. The format of the tournament is
  18. # round-robin and each shop plays every other shop the same number of times.
  19. # best_record() takes the scores of the tournament and returns the index of the
  20. # shop with the most wins and the number of wins that shop has.
  21. # The scores are a list of lists (think of it as a 2D array or matrix) such
  22. # that score[0][1] is the number of times shop 0 beat shop 1. score[i][j] +
  23. # score[j][i] is always the same number (the number of games every pair of
  24. # shops is required to play) and score[i][i] is always 0.
  25. # up to 6 points
  26. def best_record(score):
  27. return 0, 3
  28. # order_pizza() parses a file of orders and returns the total cost in cents of
  29. # the valid pizzas in that file.
  30. # The file is like a CSV file. Each order is on its own line and each field is
  31. # comma-separated. There are pairs of fields representing toppings and where
  32. # they go on the pizza (0, 1, or 2). The last field is a number representing
  33. # the size. (There is no field for num_toppings). Be careful, as some lines are
  34. # invalid pizzas.
  35. # The cost of a pizza is $1 for each inch, $0.50 for a whole-pizza topping, and
  36. # $0.25 for a half-pizza topping. Remember that the cost is in cents.
  37. # up to 6 points
  38. def order_pizza(filename):
  39. f = open(filename, 'r')
  40. for line in f.readlines():
  41. pass
  42. f.close()
  43. return 0
  44. def main():
  45. import random
  46. pizza1 = {'num_toppings': 1, 'toppings': [('pepperoni', 0)], 'size': 12}
  47. pizza2 = {'num_toppings': 2, 'toppings': [('biribiri', 0)], 'size': 10}
  48. pizza3 = {
  49. 'num_toppings': 2,
  50. 'toppings': [
  51. ('tomato', 1),
  52. ('sauce', 2),
  53. ],
  54. 'size': 9,
  55. }
  56. # string concatenation, ternary operator
  57. print str(pizza1) + ' was ' + ('valid' if valid_pizza(pizza1) else 'invalid')
  58. print pizza2, 'was', 'valid' if valid_pizza(pizza2) else 'invalid'
  59. # string formatting
  60. print '%s was %s' % (pizza3, 'valid' if valid_pizza(pizza3) else 'invalid')
  61. teams = 5
  62. games = 3
  63. # initialize a list of lists that is teams x teams wide to 0's
  64. score = [[0] * teams for _ in xrange(teams)]
  65. for i in xrange(teams):
  66. for j in xrange(i+1, teams):
  67. score[i][j] = random.randint(0, games)
  68. score[j][i] = games - score[i][j]
  69. print
  70. # I could print score, but that's ugly
  71. for s in score:
  72. print s
  73. print 'Best record:', best_record(score)
  74. print
  75. # download http://pyc.raylu.net/static/lab3.piz
  76. print 'Total cost:', order_pizza('lab3.piz')
  77. if __name__ == '__main__':
  78. main()