Advertisement
abbarnes

Word Wrangler [codeskulptor]

Dec 18th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.69 KB | None | 0 0
  1. """
  2. Student code for Word Wrangler game
  3. """
  4. #http://www.codeskulptor.org/#user43_aMTfqwQh6UpMic3_8.py
  5.  
  6. import urllib2
  7. import codeskulptor
  8. import poc_wrangler_provided as provided
  9. import random
  10.  
  11. WORDFILE = "assets_scrabble_words3.txt"
  12.  
  13.  
  14. # Functions to manipulate ordered word lists
  15.  
  16. def remove_duplicates(list1):
  17.     """
  18.    Eliminate duplicates in a sorted list.
  19.  
  20.    Returns a new sorted list with the same elements in list1, but
  21.    with no duplicates.
  22.  
  23.    This function can be iterative.
  24.    """
  25.     if not list1:
  26.         return list1
  27.     ans = [list1[0]]
  28.     for item in list1:
  29.         if item != ans[-1]:
  30.             ans.append(item)
  31.     return ans
  32.  
  33. def intersect(list1, list2):
  34.     """
  35.    Compute the intersection of two sorted lists.
  36.  
  37.    Returns a new sorted list containing only elements that are in
  38.    both list1 and list2.
  39.  
  40.    This function can be iterative.
  41.    """
  42.     #Filter works, but is too slow for large lists like the dictionary
  43.     #filter(lambda x: x in list2, list1)
  44.    
  45.     ans = []
  46.     first = list(list1)
  47.     second = list(list2)
  48.     while first and second:
  49.         if first[0] == second[0]:
  50.             ans.append(first.pop(0))
  51.             second.pop(0)
  52.         elif first[0] < second[0]:
  53.             first.pop(0)
  54.         elif first[0] > second[0]:
  55.             second.pop(0)
  56.     return ans
  57.    
  58.  
  59. # Functions to perform merge sort
  60.  
  61. def merge(list1, list2):
  62.     """
  63.    Merge two sorted lists.
  64.  
  65.    Returns a new sorted list containing those elements that are in
  66.    either list1 or list2.
  67.  
  68.    This function can be iterative.
  69.    """  
  70.     merged = []
  71.     first = list(list1)
  72.     second = list(list2)
  73.     while len(merged) < len(list1) + len(list2) and first and second:
  74.         if first[0] < second[0]:
  75.             merged.append(first.pop(0))
  76.         else:
  77.             merged.append(second.pop(0))
  78.     merged += first
  79.     merged += second
  80.     return merged
  81.                
  82. def merge_sort(list1):
  83.     """
  84.    Sort the elements of list1.
  85.  
  86.    Return a new sorted list with the same elements as list1.
  87.  
  88.    This function should be recursive.
  89.    """
  90.     if len(list1) <= 1:
  91.         return list1
  92.     half = len(list1)/2
  93.     return merge(merge_sort(list1[0:half]), merge_sort(list1[half:]))
  94.  
  95. # Function to generate all strings for the word wrangler game
  96.  
  97. def gen_all_strings(word):
  98.     """
  99.    Generate all strings that can be composed from the letters in word
  100.    in any order.
  101.  
  102.    Returns a list of all strings that can be formed from the letters
  103.    in word.
  104.  
  105.    This function should be recursive.
  106.    """
  107.     if not word:
  108.         return [""]
  109.     if len(word) == 0:
  110.         return [word]
  111.     first = word[0]
  112.     rest = word[1:]
  113.     old_all_strings = gen_all_strings(rest)
  114.     new_strings = []
  115.     for item in old_all_strings:
  116.         for index in range(len(item)+1):
  117.             new_strings.append(first.join([item[0:index], item[index:]]))
  118.     all_strings = old_all_strings + new_strings
  119.     return all_strings
  120.  
  121. # Function to load words from a file
  122.  
  123. def load_words(filename):
  124.     """
  125.    Load word list from the file named filename.
  126.  
  127.    Returns a list of strings.
  128.    """
  129.     url = codeskulptor.file2url(filename)
  130.     netfile = urllib2.urlopen(url)
  131.     words = []
  132.     for line in netfile.readlines():
  133.         words.append(line.strip())
  134.     return words
  135.  
  136. def run():
  137.     """
  138.    Run game.
  139.    """
  140.     words = load_words(WORDFILE)
  141.     wrangler = provided.WordWrangler(words, remove_duplicates,
  142.                                      intersect, merge_sort,
  143.                                      gen_all_strings)
  144.     provided.run_game(wrangler)
  145.  
  146. # Uncomment when you are ready to try the game
  147. #run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement