Advertisement
ijontichy

butt.py

Apr 21st, 2014
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.14 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. def valuesFromFile(file):
  4.     values = {}
  5.      
  6.     for line in open(file):
  7.         keyVal = [i.strip() for i in line.split(":")]   # whitespace should not matter
  8.         if len(keyVal) != 2: continue                   # invalid line, was not in the form <key>:<value>
  9.         letter, val = keyVal                            # now we know there's exactly two values - key, and value
  10.  
  11.         # all values should be lowercase, so case doesn't matter on lookups
  12.         letter = letter.lower()
  13.      
  14.         # if we didn't get an integer in the string (or no string at all), just use 1
  15.         try: val = int(val)
  16.         except (TypeError, ValueError): val = 1
  17.      
  18.         values[letter] = val
  19.    
  20.     return values
  21.  
  22. class Scrabble:
  23.     def __init__(self, valueFile="letterValues.txt"):
  24.         # get a letter-value map - what it says on the tin
  25.         self.letterValues = valuesFromFile(valueFile)
  26.  
  27.     def getWordValue(self, word):
  28.         # keep running tally
  29.         totalValue = 0
  30.  
  31.         for i in word:  # i being the next character in word
  32.             i = i.lower() # case shouldn't matter
  33.             letterValue = self.letterValues.get(i, 0)  # if character not in values, assume default of 0
  34.             totalValue += letterValue
  35.  
  36.         return totalValue
  37.  
  38.     def getBestWord(self, words):
  39.         highest  = -1
  40.         best = ""
  41.        
  42.         for word in words:
  43.             # code reuse son
  44.             val = self.getWordValue(word)
  45.  
  46.             # higher values wins always
  47.             # lower length wins on ties
  48.             if val > highest or (val == highest and len(word) < len(best)):
  49.                 highest = val
  50.                 best = word
  51.  
  52.         return best
  53.  
  54.  
  55. def main():
  56.     import sys  # we need this for sys.stderr
  57.     import os   # we need this for os.path.isfile
  58.     pointFile = input("point-value file? (hit enter for default) ")
  59.  
  60.     # if we got an actual file, use it
  61.     if os.path.isfile(pointFile):
  62.         pointThingy = Scrabble(pointFile)
  63.  
  64.     else: # otherwise, just use the default
  65.  
  66.         # if the user gave a file path, tell him it doesn't exist
  67.         #  silence on errors is typically not a good decision
  68.         if len(pointFile) > 0:
  69.  
  70.             # {} gets replaced with pointFile here
  71.             # we print to sys.stderr because technically, this is an error
  72.             print("file \"{}\" does not exist, using default".format(pointFile), file=sys.stderr)
  73.  
  74.         pointThingy = Scrabble()
  75.  
  76.     wordsRaw = input("words? (seperate with comma) ")     # this is a string, no newline
  77.     wordList = [i.strip() for i in wordsRaw.split(",")]   # every word in wordsRaw, split on commas, with whitespace trimmed
  78.  
  79.     # self-explanatory
  80.     if len(wordList) == 1:
  81.         word = wordList[0]
  82.         # first {} gets replaced with word, second {} its value
  83.         print("value of \"{}\" is {}".format(word, pointThingy.getWordValue(word)))
  84.  
  85.     else:
  86.         best = pointThingy.getBestWord(wordList)
  87.         # {} replaced with best
  88.         print("best word in list is \"{}\"".format(best))
  89.  
  90.  
  91. # python trick: this is true if you're running from the command line or whatever,
  92. #  but false if you're importing
  93. if __name__ == "__main__":
  94.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement