CSCI1105

View the Project on GitHub

Assignment 5 Question 1

Introduction

unable to get description

My Solution also hosted here

#########################
# CSCI 1105
# Assignment 5, Problem 1
# @author: ???

############################################
#### DO NOT CHANGE CODE INSIDE THIS BOX ####
############################################
# Direction constants                      #
DIR_U = 0                                  #
DIR_UR = 1                                 #
DIR_R = 2                                  #
DIR_DR = 3                                 #
DIR_D = 4                                  #
DIR_DL = 5                                 #
DIR_L = 6                                  #
DIR_UL = 7                                 #
                                           #
DIRS = [(-1,0),                            #
        (-1,1),                            #
        (0,1),                             #
        (1,1),                             #
        (1,0),                             #
        (1,-1),                            #
        (0,-1),                            #
        (-1,-1)]                           #
                                           #
# Create the puzzle table                  #
myPuzz = [input()]                         #
myPuzz[0] = myPuzz[0].split(" ")           #
for i in range(1,len(myPuzz[0])):          #
    myPuzz.append(input())                 #
    myPuzz[-1] = myPuzz[-1].split(" ")     #
                                           #
# Get the words to test                    #
wordList = []                              #
wordRead = input()                         #
while wordRead != "STOP!":                 #
    wordList.append(wordRead)              #
    wordRead = input()                     #
############################################



################## WRITE YOUR CODE AFTER THIS LINE ###################


def findWord(puzzle, word):
    # just a placeholder for if statement (doesn't matter in the end)
    availability = False
    
    # made a variable for number of rows and columns along with length of the word 
    row = len(puzzle)
    col = len(puzzle[0])
    length = len(word)
    
    # created an empty dictionary for the word to be added if found
    dictionary = {}
    
    # made a for loop that searches for the word horizontally to the right
    # first for loop goes through every row to find the word
    for i in range(row):
        
        # second for loop goes through zeroth column to the column where beginning 
        of the word can appear which is col-length and the increment is one
        for j in range(0, col-length+1, 1):
            
            # created a test variable that tests if all letter of the word is found 
            which is used in the if statement below
            test = []
            
            # used an if statement to find the word
            # if the first letter of the word is found somewhere in the puzzle
            if puzzle[i][j] == word[0]:
                
                # used for loop to see if the letters to the right of that spot 
                also matches with the word 
                for k in range(length):
                    
                    # it tests each one and if it matches, it puts yeah into the 
                    list called "test" or "nah"
                    if puzzle[i][j+k] == word[k]:
                        test.append("yeah")
                    else:
                        test.append("nah")
                
                # if a single "nah" is found in the list called test, availability 
                becomes false but otherwise it adds the location and the direction 
                into the dictionary
                if "nah" in test:
                    availability = False
                else: 
                    dictionary["row"] = i
                    dictionary["col"] = j
                    
                    # I do not know why the mimir takes 2 as opposed to "DIR_R" but 
                    I guess it happened as the prof made an adjustment
                    dictionary["direction"] = 2
            
            # cleared the list that was used to test all letter for upcoming other 
            test (just in case)
            test.clear()
    
    # this test does the confirmation if the word can be found horizontally but in 
    opposite way
    # for loop to test each row
    for i in range(row):
        
        # for loop to test each column (parameter adjusted to weird counting that 
        python uses with an increment of -1 since going opposite way)
        for j in range(col-1, length-2, -1):
            
            # list to test the letters
            test = []
            
            # same as the previous test to see if the first letter of the word is 
            found in the puzzle
            if puzzle[i][j] == word[0]:
                
                # if the first letter is found it goes through the opposite way to 
                test the remaining letter using for loop
                for k in range(length):
                    
                    # the j-k goes backward to test the letters and adds the "yeah" 
                    or "nah" to the list as appropriate
                    if puzzle[i][j-k] == word[k]:
                        test.append("yeah")
                    else:
                        test.append("nah")
                
                # if single "nah" is found in the test, availability is False or 
                if it finds it, the location and the direction of 6 is added
                if "nah" in test:
                    availability = False
                else: 
                    dictionary["row"] = i
                    dictionary["col"] = j
                    dictionary["direction"] = 6
            
            # clears the list used for testing (just in case again)
            test.clear() 
    
    # created a for loop to find the word vertically going down with proper parameter 
    with the increment of 1 for going down for each row
    for i in range(0, row-length, 1):
        
        # created a for loop to find the word for each column
        for j in range(col):
            
            # created a list for testing purposes
            test = []
            
            # if the first letter of the word is found in the puzzle it goes through 
            this if statement
            if puzzle[i][j] == word[0]:
                
                # since it found the matching first letter of the word, it goes 
                through for loop to see if the remaining letters match
                for k in range(length):
                    
                    # and it puts the result of "yeah" or "nah" in the list called 
                    test
                    if puzzle[i+k][j] == word[k]:
                        test.append("yeah")
                    else:
                        test.append("nah")
                
                # if a single "nah" is found in the test, availability is False 
                but if it doesn't the location and the direction of 4 is added to 
                the dictionary
                if "nah" in test:
                    availability = False
                else: 
                    dictionary["row"] = i
                    dictionary["col"] = j
                    dictionary["direction"] = 4
            
            # clears the list that was used for testing (just in case)
            test.clear()
    
    # second test to find the word vertically but going up this time with the 
    right parameter (due to weird counting that python uses with an increment of 
    -1 for going up)        
    for i in range(row-1, length-2, -1):
        
        # second for loop to test each column
        for j in range(col):
            
            # created a list for testing purposes
            test = []
            
            # if the first letter of the word is found in the puzzle, it goes 
            through this if statement
            if puzzle[i][j] == word[0]:
                
                # since the first letter of the word is found, it goes through 
                a for loop to test the remaining letter
                for k in range(length):
                    
                    # if the letter matches, it gives "yeah" or "nah" and puts 
                    it in the list
                    if puzzle[i-k][j] == word[k]:
                        test.append("yeah")
                    else:
                        test.append("nah")
                
                # if single "nah" is found in the test, the availability is 
                False but if it doesn't the location and the direction is 
                added to the dictionary
                if "nah" in test:
                    availability = False
                else: 
                    dictionary["row"] = i
                    dictionary["col"] = j
                    dictionary["direction"] = 0
            
            # clears the list that used to test (just in case again for the 
            last time)
            test.clear()
    
    # now goes diagonally going rightward down
    for i in range(0, row-length+1, 1):
        
        # second for loop goes through zeroth column to the column where 
        beginning of the word can appear which is col-length and the increment is one
        for j in range(0, col-length+1, 1):
            
            # created a test variable that tests if all letter of the word is 
            found which is used in the if statement below
            test = []
            
            # used an if statement to find the word
            # if the first letter of the word is found somewhere in the puzzle
            if puzzle[i][j] == word[0]:
                
                # used for loop to see if the letters to the right of that spot
                also matches with the word 
                for k in range(length):
                    
                    # it tests each one and if it matches, it puts yeah into the
                    list called "test" or "nah"
                    if puzzle[i+k][j+k] == word[k]:
                        test.append("yeah")
                    else:
                        test.append("nah")
                
                # if a single "nah" is found in the list called test, availability
                becomes false but otherwise it adds the location and the direction 
                into the dictionary
                if "nah" in test:
                    availability = False
                else: 
                    dictionary["row"] = i
                    dictionary["col"] = j
                    
                    # I do not know why the mimir takes 2 as opposed to "DIR_R" 
                    but I guess it happened as the prof made an adjustment
                    dictionary["direction"] = 3
            
            # cleared the list that was used to test all letter for upcoming 
            other test (just in case)
            test.clear()
    
    # this test does the confirmation if the word can be found horizontally 
    but in opposite way
    # for loop to test each row
    for i in range(0, row-length+1, 1):
        
        # for loop to test each column (parameter adjusted to weird counting 
        that python uses with an increment of -1 since going opposite way)
        for j in range(length-1, col, 1):
            
            # list to test the letters
            test = []
            
            # same as the previous test to see if the first letter of the word 
            is found in the puzzle
            if puzzle[i][j] == word[0]:
                
                # if the first letter is found it goes through the opposite 
                way to test the remaining letter using for loop
                for k in range(length):
                    
                    # the j-k goes backward to test the letters and adds the 
                    "yeah" or "nah" to the list as appropriate
                    if puzzle[i+k][j-k] == word[k]:
                        test.append("yeah")
                    else:
                        test.append("nah")
                
                # if single "nah" is found in the test, availability is False 
                or if it finds it, the location and the direction of 6 is added
                if "nah" in test:
                    availability = False
                else: 
                    dictionary["row"] = i
                    dictionary["col"] = j
                    dictionary["direction"] = 5
            
            # clears the list used for testing (just in case again)
            test.clear() 
            
    # now goes diagonally going upward left
    for i in range(length-1, row-1, 1):
        
        # second for loop goes through zeroth column to the column where beginning
        of the word can appear which is col-length and the increment is one
        for j in range(0, col-length, 1):
            
            # created a test variable that tests if all letter of the word is found
            which is used in the if statement below
            test = []
            
            # used an if statement to find the word
            # if the first letter of the word is found somewhere in the puzzle
            if puzzle[i][j] == word[0]:
                
                # used for loop to see if the letters to the right of that spot also
                matches with the word 
                for k in range(length):
                    
                    # it tests each one and if it matches, it puts yeah into the 
                    list called "test" or "nah"
                    if puzzle[i-k][j+k] == word[k]:
                        test.append("yeah")
                    else:
                        test.append("nah")
                
                # if a single "nah" is found in the list called test, availability
                becomes false but otherwise it adds the location and the direction
                into the dictionary
                if "nah" in test:
                    availability = False
                else: 
                    dictionary["row"] = i
                    dictionary["col"] = j
                    
                    # I do not know why the mimir takes 2 as opposed to "DIR_R" 
                    but I guess it happened as the prof made an adjustment
                    dictionary["direction"] = 1
            
            # cleared the list that was used to test all letter for upcoming 
            other test (just in case)
            test.clear()
    
    # this test does the confirmation if the word can be found horizontally 
    but in opposite way
    # for loop to test each row
    for i in range(col-1, length-2, -1):
        
        # for loop to test each column (parameter adjusted to weird counting 
        that python uses with an increment of -1 since going opposite way)
        for j in range(col-1, length-2, -1):
            
            # list to test the letters
            test = []
            
            # same as the previous test to see if the first letter of the 
            word is found in the puzzle
            if puzzle[i][j] == word[0]:
                
                # if the first letter is found it goes through the opposite 
                way to test the remaining letter using for loop
                for k in range(length):
                    
                    # the j-k goes backward to test the letters and adds the 
                    "yeah" or "nah" to the list as appropriate
                    if puzzle[i-k][j-k] == word[k]:
                        test.append("yeah")
                    else:
                        test.append("nah")
                
                # if single "nah" is found in the test, availability is False 
                or if it finds it, the location and the direction of 6 is added
                if "nah" in test:
                    availability = False
                else: 
                    dictionary["row"] = i
                    dictionary["col"] = j
                    dictionary["direction"] = 7
            
            # clears the list used for testing (just in case again)
            test.clear()
            
    # if the dictionary remains empty due to availability being False for both
    test, the final is none or otherwise it gives the dictionary containing the
    location and the direcion      
    if dictionary == {}:
        final = "None"
    else:
        final = dictionary
    return(final)
    
   


################## WRITE YOUR CODE ABOVE THIS LINE ###################



##################################################
####### DO NOT CHANGE CODE INSIDE THIS BOX #######
##################################################
for word in wordList:                            #
    print(word, findWord(myPuzz, word))          #
                                                 #
for rowList in myPuzz:                           #
    for char in rowList:                         #
        print(char+" ", end="")                  #
    print()                                      #
##################################################