2 Replies - 1539 Views - Last Post: 10 November 2012 - 08:07 AM Rate Topic: -----

#1 rajxss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 25-July 09

Problem with the sets of functions

Posted 10 November 2012 - 06:44 AM

I have problem with the last part of the function!
def is_valid_word(wordlist, word):
    ''' (list of str, str) -> bool

    Return True if and only if word is an element of wordlist.

    >>> is_valid_word(['ANT', 'BOX', 'SOB', 'TO'], 'TO')
    True
    '''
    return word in wordlist
    
  

def make_str_from_row(board, row_index):
    ''' (list of list of str, int) -> str

    Return the characters from the row of the board with index row_index
    as a single string.

    >>> make_str_from_row([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 0)
    'ANTT'
    '''
    mk_str=''
    for letter in board[row_index]:
        mk_str +=letter
    return mk_str
    

def make_str_from_column(board, column_index):
    ''' (list of list of str, int) -> str

    Return the characters from the column of the board with index column_index
    as a single string.

    >>> make_str_from_column([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 1)
    'NS'
    '''
    mk_c=''
    for i in board:
        mk_c += i[column_index]
    return mk_c
    


def board_contains_word_in_row(board, word):
    ''' (list of list of str, str) -> bool

    Return True if and only if one or more of the rows of the board contains
    word.

    Precondition: board has at least one row and one column, and word is a
    valid word.

    >>> board_contains_word_in_row([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'SOB')
    True
    '''

    for row_index in range(len(board)):
        if word in make_str_from_row(board, row_index):
            return True

    return False


def board_contains_word_in_column(board, word):
    ''' (list of list of str, str) -> bool

    Return True if and only if one or more of the columns of the board
    contains word.

    Precondition: board has at least one row and one column, and word is a
    valid word.

    >>> board_contains_word_in_column([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'NO')
    False
    '''
    for column_index in range(len(board[0])):
        if word in make_str_from_column(board, column_index):
            return True

    return False


def board_contains_word(board, word):
    '''(list of list of str, str) -> bool

    Return True if and only if word appears in board.
    checks whether a word that a player guessed occurs
    in any of the rows or columns of the board.

    Precondition: board has at least one row and one column.

    >>> board_contains_word([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'ANT')
    True
    '''
    mk_str= make_str_from_row(board, row_index)
    for i in word:
        if word[i]== mk_str:
            return True
    return False



Can anybody guide me through this!

def board_contains_word(board, word):
    '''(list of list of str, str) -> bool

    Return True if and only if word appears in board.
    checks whether a word that a player guessed occurs
    in any of the rows or columns of the board.

    Precondition: board has at least one row and one column.

    >>> board_contains_word([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 'ANT')
    True
    '''
    mk_str= make_str_from_row(board, row_index)
    for i in word:
        if word[i]== mk_str:
            return True
    return False



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with the sets of functions

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,672
  • Joined: 13-March 10

Re: Problem with the sets of functions

Posted 10 November 2012 - 07:41 AM

I would do it like this, but my way will only work if the rows are of the same length.
You need to test it though:


def boardContainsWord(word, board):
    ''' lets imaging board is:   
    [ ['A', 'B', 'B', 'A'], ['P','Y', 'T', 'O', 'N']  ]
    '''
    
    return checkRows(word, board) or checkColumns(word, board)

    
def checkRows(word, board):
    #lets get the lists into strings
    for lst in board:
        value = "".join(lst)
        #check whether word is a substring of value
        if word in value:
            return True
    
    return False


def checkColumns(word, board):
    #now we need to do the same thing for the columns
    rows = len(board)
    columns = len(board[0])
    
  
    for i in range(columns):
        value = ""
        for j in range(rows):
            value = value + board[j][i]
        
        if word in value:
            return True
        
    return False

            


Was This Post Helpful? 1
  • +
  • -

#3 rajxss  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 25-July 09

Re: Problem with the sets of functions

Posted 10 November 2012 - 08:07 AM

Thank You!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1