Advertisement
TransactCharlie

Problem C. Recycled Numbers

Apr 15th, 2012
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.47 KB | None | 0 0
  1. __author__ = 'charles'
  2.  
  3. inputFile = "C-large.in"
  4. outputFile = inputFile + ".out"
  5.  
  6.  
  7. class Recylce(object):
  8.  
  9.     def __init__(self, l, h):
  10.         self.l = l
  11.         self.h = h
  12.  
  13.     def DisplayCase(self):
  14.         print "Case : min(" + str(self.l) + ") max(" + str(self.h) + ")"
  15.  
  16.     def SolveCase(self):
  17.         hits = set()
  18.         for i in range(self.l, self.h - ((self.h - self.l) / 2)):
  19.             for c in range (1, len(str(i))):
  20.                 pre = str(i)[len(str(i)) - c: len(str(i))]
  21.                 post = str(i)[0:len(str(i)) - c]
  22.                 checkint = int(pre + post)
  23.  
  24.                 if checkint > i and checkint >= self.l and checkint <= self.h:
  25.                    hits.add((i, checkint))
  26.  
  27.         return str(len(hits))
  28.  
  29. def extractTestCases(filename):
  30.  
  31.     with open(filename, 'r') as f:
  32.         fileContents = (f.read().splitlines())
  33.         f.close()
  34.  
  35.     cases = []
  36.     for c in range(0,int(fileContents[0])):
  37.         l, h = fileContents[c+1].split(" ")
  38.         cases.append(Recylce(int(l), int(h)))
  39.  
  40.     return cases
  41.  
  42.  
  43. def main():
  44.  
  45.     cases = extractTestCases(inputFile)
  46.  
  47.  
  48.     for case in cases:
  49.         print case.DisplayCase()
  50.  
  51.  
  52.     with open(outputFile, "w") as w:
  53.         for n in range(0, len(cases)):
  54.             case = cases[n]
  55.             print("Case #" + str(n + 1) + ": " + case.SolveCase() + "\n"),
  56.             w.write("Case #" + str(n + 1) + ": " + case.SolveCase() + "\n")
  57.  
  58. if __name__ == "__main__":
  59.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement