Guest User

Blackone

a guest
Nov 15th, 2009
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1. #!/usr/bin/python
  2. # Source: 3nplus1.py
  3. # Author: Jeffrey Cartagena
  4. # Date: April, 29, 2009
  5.  
  6.  
  7. # readFile function
  8. def readFile(inFile):
  9.    
  10.     line = inFile.readline().rstrip()
  11.     if not line:
  12.         return 0
  13.  
  14.     nums = line.split()
  15.     return nums    
  16. # End of readFile function
  17.  
  18.  
  19.  
  20. # iterations function
  21. def iterations(value):
  22.     count = 1
  23.  
  24.     while value != 1:
  25.         if value % 2 == 0:
  26.             value = value / 2
  27.         else:
  28.             value = value * 3 + 1
  29.  
  30.         count += 1
  31.  
  32.     return count
  33. # End of iterations function
  34.  
  35.  
  36.  
  37. # This function call the function iterations to check
  38. # the iterations of each number then compare them tu return the maximum
  39. def checkIterations(flag):
  40.    
  41.     intMax = 1
  42.  
  43.     for i in range(int(flag[0]), int(flag[1])):
  44.         temp = iterations(i)
  45.  
  46.         if temp > intMax:
  47.             intMax = temp
  48.  
  49.     return intMax    
  50. # End of checkIterations
  51.  
  52.  
  53.  
  54. # writeFile function
  55. def writeFile(flag, intMax, outFile):
  56.     outFile.write(str(flag[0]) + " " + str(flag[1]) + " " + str(intMax) + '\n')
  57. # End of writeFile function
  58.  
  59.  
  60.  
  61.  
  62. # Main Routine
  63. try:
  64.     # Opening the files input.in, output.out
  65.     inFile = open("input.in")
  66.     outFile = open("output.out", 'w')
  67. except IOError:
  68.     raw_input("Files cannot be opened\n")
  69.     exit
  70. except:
  71.     raw_input("Something unexpected happened\n")
  72.     exit
  73.  
  74. # Variable Declaration
  75.  
  76. flag = readFile(inFile)
  77.  
  78. while flag:
  79.     intMax = checkIterations(flag)
  80.     writeFile(flag, intMax, outFile)
  81.     flag = readFile(inFile)
  82.  
  83. inFile.close()
  84. outFile.close()
  85. raw_input("Finishing the problem\n")
  86. # End of Main Routine
  87.  
Advertisement
Add Comment
Please, Sign In to add comment