Advertisement
wjzijderveld

FHC2012: Alphabet Soup

Jan 24th, 2012
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1. '''
  2. Created on Jan 23, 2012
  3.  
  4. @author: willemjan
  5. '''
  6.  
  7. import io
  8. from root.helper import getInputPath
  9. from pprint import pprint
  10. live = True
  11.  
  12. file = open(getInputPath(live) + '/alphabet_soup.txt', 'r')
  13. resultFile = open(getInputPath(live) + '/alphabet_soup_out.txt', 'w')
  14. cntCases = int(file.readline())
  15.  
  16. def hasResult(dictionary):
  17.     usedLetters = {}
  18.     # Loop thru letters in testString
  19.     for letter in list("HACKERCUP"):
  20.         # If letter not in string or no longer in dictionary, return false
  21.         if letter not in dictionary or dictionary[letter] <= 0:
  22.             return False
  23.         else:
  24.         # else add letter to usedLetters to make the delta later on
  25.             if letter not in usedLetters:
  26.                 usedLetters[letter] = 1
  27.             else:
  28.                 usedLetters[letter] += 1
  29.            
  30.     # Word found, perform delta
  31.     for foundLetter in usedLetters:
  32.         dictionary[foundLetter] -= 1
  33.    
  34.     return True
  35.        
  36.  
  37. i = 0
  38. while i < cntCases:
  39.     i+=1
  40.     line = file.readline().strip()
  41.     dictionary = {}
  42.     letters = list(line)
  43.     for x in letters:
  44.        
  45.         if x not in dictionary:
  46.             dictionary[x] = 0;
  47.            
  48.         dictionary[x] += 1
  49.        
  50.     result = 0
  51.     while True:
  52.         if hasResult(dictionary):
  53.             result += 1
  54.         else:
  55.             break
  56.        
  57.     resultFile.write('Case #' + str(i) + ': ' + str(result) + '\n')
  58.  
  59. resultFile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement