Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Submission _ Ehsan Salimi #8

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Conversation

EhsanSl
Copy link

@EhsanSl EhsanSl commented Nov 29, 2022

Solution for team A

** instructions : **
you can simply run it on a text editor or any IDLE, First you must enter each row of the matrix ( seperate each char by **space** ) after you are done adding rows to the matrix , press 'Enter' (input empty) then you can enter the **Key word**

Explanation
There are two main functions and a couple of utility functions to keep the code clean.
the two main fucntions are **identifier() & scavenger() ** ,

Scavenger
this is recursive function and this function is being called from the identifier at the first place for every index in the matrix, and the i , j indexes of the matrix as well as the keyword and a counter representing the # of matched characters are being passed to it as arguments.

  • the base case of this recursive function is if the counter == length of the word
  • in case the ith character of the keyword is not matching with the mtx[i][j] character of the matrix or any of the indexes exceed the boundaries of the matrix, the function will return False,
  • otherwise, the function will keep calling itself by incrementing ith and jth indexes to traverse the matrix in all 4 different directions( left, right, top, down) until one of the indexes goes out of bounds.
  • worth mentioning that when we pass the if the condition that fails when the wrd[count] != mtx[i][j] , we know that we are at a step where the countth character is matching and so we replace it by - before calling the recursive functions so we would not visit it again, but we switch it back to its original character, incase if we failed to match the rest of the sentence.

identifier() is being called from the main , and at the end returns the boolean value to the main
the identifier at first checks for the validity of the input matrix and keyword and if they were acceptable, runs a for loop on each index of the matrix, and in each iteration calls the scavenger by passing corresponding attributes to it.
if the result of scavenger() is true , the true value is returned, otherwise the loop continues until we run out of indexes in the matrix and then returns false.

** the comments are pretty descriptive and they explain each step of the process**

PS: the way I implemented the count was not my idea, I originally wanted to create a boolean matrix with the same size as the original one and keep the track of each index separately, but then I realized recursion would not be possible ( or not simple) and in order for that to work, I had to approach the iterative method and so the time complexity would have been way worse. that is why you may also see some commented lines holding variable bool_matrix

The base of the main , accepting input and separating it to matrix and keyWord.
added the wrd_stack , and updated the main, defined find_first_char() and identifier() , working on the logic now..
added a plan about how to implement 2 stacks to keep track of the progress
nothing special
explained the main skeleton  of the code in the comments  section
- created a function to return the dimension of the input array
- defined  a function to create the boolean_array to keep track of visited nodes
updated the skeleton
@EhsanSl EhsanSl changed the title Submission Submission _ Ehsan Salimi Dec 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant