Advertisement
tom_enos

a3

Oct 7th, 2013
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.52 KB | None | 0 0
  1. """A board is a list of list of str. For example, the board
  2.    ANTT
  3.    XSOB
  4. is represented as the list
  5.    [['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']]
  6.  
  7. A word list is a list of str. For example, the list of words
  8.    ANT
  9.    BOX
  10.    SOB
  11.    TO
  12. is represented as the list
  13.    ['ANT', 'BOX', 'SOB', 'TO']
  14. """
  15.  
  16.  
  17. def is_valid_word(wordlist, word):
  18.     """ (list of str, str) -> bool
  19.  
  20.    Return True if and only if word is an element of wordlist.
  21.  
  22.    >>> is_valid_word(['ANT', 'BOX', 'SOB', 'TO'], 'TO')
  23.    True
  24.    """
  25.     return word in wordlist
  26.  
  27.  
  28. def make_str_from_row(board, row_index):
  29.     """ (list of list of str, int) -> str
  30.  
  31.    Return the characters from the row of the board with index row_index
  32.    as a single string.
  33.  
  34.    >>> make_str_from_row([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 0)
  35.    'ANTT'
  36.    """
  37.     return ''.join(board[row_index])
  38.  
  39. def make_str_from_column(board, column_index):
  40.     """ (list of list of str, int) -> str
  41.  
  42.    Return the characters from the column of the board with index column_index
  43.    as a single string.
  44.  
  45.    >>> make_str_from_column([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 1)
  46.    'NS'
  47.    """
  48.     return "".join([row[column_index] for row in board])
  49.  
  50.  
  51. def board_contains_word_in_row(board, word):
  52.     """ (list of list of str, str) -> bool
  53.  
  54.    Return True if and only if one or more of the rows of the board contains
  55.    word.
  56.  
  57.    Precondition: board has at least one row and one column, and word is a
  58.    valid word.
  59.  
  60.    >>> board_contains_word_in_row([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'SOB')
  61.    True
  62.    """
  63.     return (True in [word in make_str_from_row(board, row_index) for row_index in range(len(board))] and [True] or [False])[0]
  64.  
  65.  
  66. def board_contains_word_in_column(board, word):
  67.     """ (list of list of str, str) -> bool
  68.  
  69.    Return True if and only if one or more of the columns of the board
  70.    contains word.
  71.  
  72.    Precondition: board has at least one row and one column, and word is a
  73.    valid word.
  74.  
  75.    >>> board_contains_word_in_column([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'NO')
  76.    False
  77.    """
  78.     return (True in [word in make_str_from_column(board, column_index) for column_index in range(len(board[0]))] and [True] or [False])[0]
  79.  
  80.  
  81. def board_contains_word(board, word):
  82.     """ (list of list of str, str) -> bool
  83.  
  84.    Return True if and only if word appears in board.
  85.  
  86.    Precondition: board has at least one row and one column.
  87.  
  88.    >>> board_contains_word([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'ANT')
  89.    True
  90.    """
  91.     return board_contains_word_in_row(board, word) or board_contains_word_in_column(board, word)
  92.  
  93.  
  94. def word_score(word):
  95.     """ (str) -> int
  96.  
  97.    Return the point value the word earns.
  98.  
  99.    Word length: < 3: 0 points
  100.                 3-6: 1 point per character for all characters in word
  101.                 7-9: 2 points per character for all characters in word
  102.                 10+: 3 points per character for all characters in word
  103.  
  104.    >>> word_score('DRUDGERY')
  105.    16
  106.    """
  107.     return (2 < len(word) < 7 and [len(word) * 1] or [(6 < len(word) < 10 and [len(word) * 2] or [(len(word) > 9 and [len(word) * 3] or [0])[0]])[0]])[0]
  108.  
  109.  
  110. def update_score(player_info, word):
  111.     """ ([str, int] list, str) -> NoneType
  112.  
  113.    player_info is a list with the player's name and score. Update player_info
  114.    by adding the point value word earns to the player's score.
  115.  
  116.    >>> update_score(['Jonathan', 4], 'ANT')
  117.    """
  118.     player_info[1] += word_score(word)
  119.  
  120.  
  121. def num_words_on_board(board, words):
  122.     """ (list of list of str, list of str) -> int
  123.  
  124.    Return how many words appear on board.
  125.  
  126.    >>> num_words_on_board([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], ['ANT', 'BOX', 'SOB', 'TO'])
  127.    3
  128.    """
  129.     return sum([board_contains_word(board, word) for word in words])
  130.  
  131.  
  132. def read_words(words_file):
  133.     """ (file open for reading) -> list of str
  134.  
  135.    Return a list of all words (with newlines removed) from open file
  136.    words_file.
  137.  
  138.    Precondition: Each line of the file contains a word in uppercase characters
  139.    from the standard English alphabet.
  140.    """
  141.     return list(line.rstrip('\n') for line in words_file)
  142.  
  143.  
  144. def read_board(board_file):
  145.     """ (file open for reading) -> list of list of str
  146.  
  147.    Return a board read from open file board_file. The board file will contain
  148.    one row of the board per line. Newlines are not included in the board.
  149.    """
  150.     return list(list(line.rstrip('\n')) for line in board_file)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement