TransactCharlie

Problem B. Dancing With the Googlers

Apr 15th, 2012
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. __author__ = 'charles'
  2.  
  3. inputFile = "B-large.in"
  4. outputFile = inputFile + ".out"
  5.  
  6. class Dance(object):
  7.  
  8.     def __init__(self, surprises, targetBestScore, danceCards):
  9.         self.surprises = int(surprises)
  10.         self.targetBestScore = int(targetBestScore)
  11.         self.danceCards = danceCards
  12.  
  13.     def DisplayCase(self):
  14.  
  15.         print "Case : [surprises:" + str(self.surprises) + "] - [target:" + str(self.targetBestScore) + "]"
  16.         print ",".join(self.danceCards)
  17.  
  18.     def SolveCase(self):
  19.         matched = 0
  20.         for card in self.danceCards:
  21.  
  22.  
  23.             cardAv = int(card) / 3
  24.  
  25.             maxWithoutSurprise = 0
  26.             maxWithSurprise = 0
  27.  
  28.             if int(card) == 0:
  29.                 maxWithoutSurprise = 0
  30.                 maxWithSurprise = 0
  31.  
  32.             elif int(card) == 1:
  33.                 maxWithoutSurprise = 1
  34.                 maxWithSurprise = 1
  35.  
  36.             elif int(card) == 2:
  37.                 maxWithoutSurprise = 1
  38.                 maxWithSurprise = 2
  39.  
  40.             elif int(card) % 3 == 0:
  41.                 maxWithoutSurprise = cardAv
  42.                 maxWithSurprise = cardAv + 1
  43.  
  44.             elif int(card) % 3 == 1:
  45.                 maxWithoutSurprise = cardAv + 1
  46.                 maxWithSurprise = cardAv + 1
  47.  
  48.             elif int(card) % 3 == 2:
  49.                 maxWithoutSurprise = cardAv + 1
  50.                 maxWithSurprise = cardAv + 2
  51.  
  52.             if maxWithoutSurprise >= self.targetBestScore:
  53.                 matched += 1
  54.  
  55.             elif maxWithSurprise >= self.targetBestScore and self.surprises > 0:
  56.                 matched += 1
  57.                 self.surprises -= 1
  58.  
  59.             print card, cardAv, maxWithoutSurprise, maxWithSurprise, matched, self.surprises
  60.         return str(matched)
  61.  
  62.  
  63. def extractTestCases(filename):
  64.  
  65.     with open(filename, 'r') as f:
  66.         fileContents = (f.read().splitlines())
  67.         f.close()
  68.  
  69.     cases = []
  70.     for c in range(0,int(fileContents[0])):
  71.         dd = fileContents[c+1].split(" ")
  72.         cases.append(Dance(dd[1], dd[2], dd[3:]))
  73.  
  74.     return cases
  75.  
  76.  
  77. def main():
  78.  
  79.     cases = extractTestCases(inputFile)
  80.  
  81.  
  82.    # for case in cases:
  83.     #    print case.DisplayCase()
  84.  
  85.  
  86.     with open(outputFile, "w") as w:
  87.         for n in range(0, len(cases)):
  88.             case = cases[n]
  89.             w.write("Case #" + str(n + 1) + ": " + case.SolveCase() + "\n")
  90.  
  91. if __name__ == "__main__":
  92.     main()
Add Comment
Please, Sign In to add comment