I don't know what you're expecting the code to return?
I expect the code to return: university was found, for example.
My approach:
1. Find or write a function that finds substring A in a string B reading left to right or right to left. Let's call it find_substring.
2. Write a function that looks for your words in a given string. Let's call it find_words. It will use find_substring.
3. Top level function:
For each row: put the row into a string, call find_words.
For each column: put the column into a string, call find_words.
For each bottom left to top right diagonal: put the diagonal into a string, call find_words.
For each top left to bottom right diagonal: put the diagonal into a string, call find_words.
Edit: obviously the first step in the top level function is to read the puzzle into a 2D array.
Thank you lev888.
1. Do you mean that I have to create 4 horizontal strings, 10 vertical strings, and 26 diagonal strings? Then find_substring will go through all 40 strings to find the required words?
What I mean here is that, for example, we have two diagonal strings for sign, one is "sign" from top to bottom and one is "ngis" from bottom to top. Does the job of find_substring to check "n", "ng", "ngi", and "ngis"? Then, it will check the reverse which is "s", "si", "sig", and "sign". Then it will say
sign was found. Is that what you mean by the function find_substring?
By the way the two diagonal strings "sign" and "ngis" are just two diagonal strings out of 26.
2. If this is the purpose of the function find_substring, I don't see a reason to create the function find_words because I can directly compare what the function find_substring found with the words that I have found, university, unit, year, and sign.
3. I have done this step in 1.
Edit: obviously the first step in the top level function is to read the puzzle into a 2D array.
The matrix that I have created in my post #4 can be stored in a 2D array where the first element, 00, for example, can be found by two loops which have the indices i = 0, and j = 0 in the first round, respectively.
In this way, I can check element by element while walking horizontally in the array by concatenating letters together to make words. This job can be done easily for the horizontal words, but I cannot make the loops to walk vertically or diagonally.
What would the code return? The location of the word?
My interpretation of the question is to write the code to return these words. It's also apparent that those words the OP found are not exhaustive. Those are just some that s/he found.
My question to
@mario99 : how would the code know it found a word? Unless you give it a dictionary to check against.
The location is important, but we don't have to mention it. The purpose is so far just to find the word in that location and say it was found. The words which I have found are the dictionary to check against.
I think the intent is to solve the sort of "word search" puzzle in which you are given a letter square, and a list of words to find in it. You "solve" it by circling the words in place. Like here:
Fun printable worksheet for students of all ages features 39 african animals to hunt down.
www.puzzles-to-print.com
So, yes, it presumably has to return the location of each word, in some form; and the words to find will not be exhaustive -- you are, in essence, given a (very small) dictionary.
Am I right? Of course, I don't see how this is a math problem.
Thank you Dr.Peterson
You are exactly correct. The main purpose is "word search". In reality, we want the code to solve the puzzle itself by giving it a real dictionary which probably contains all the words in the world which seems it will take a lot of time to find a single word. Therefore, the second purpose of this code is performance. I will make a code and John smith will make another code. The fastest code to find the words wins. For example, at first trial, I may make a code which can find the words in 40 seconds. I have extra trials to improve the performance. Improving the algorithm in a smart way will speed up the searching process.
The real challenge is that, is it possible to create an algorithm to solve the puzzle in 1 second? This is what the instructor said, we would be surprised in chapter 10 that our algorithm would be developed in such a way that it would able to solve a real word puzzle in no time. I can't wait to see that!
Am I right? Of course, I don't see how this is a math problem.
I don't know what to say here. But the instructor once said, if you know math, you can create a code of 2 lines, while another code with the same purpose done by another person without the knowledge of math can cost 10s of lines. Therefore, it is a math problem indirectly.
When doing a word-search puzzle, you'd need to return 2 things: (1) the word it found and (2) the location of the word.
I'm questioning the OP about the first requirement. Without doing the word search by hand first (because this defeats the purpose of automation), how are you expecting the code to return the requirement (1)?
For example, how would it know "in", a real English word, vs "ni"?
The OP seems to be doing requirement (1) by hand, then using those as inputs to find requirement (2).
It's 100% not a math question, just want to make sure the OP understands the necessary requirements for doing this assignment.
I agree that solving the problem in the way I explained in post #1 defeats the purpose of automation. The funny thing is what I explained was the purpose of the code. I have explained to you what the instructor had explained to us.
I am afraid that I will get zero mark if I solved the word puzzle problem by searching in a real dictionary instead of the small dictionary that I have created. If you think that the instructor has interrupted the problem in a faulty way, feel free to share the correctness with me to share it with him. Otherwise, let us just surrender to the instructor call.
What we need to see is the actual wording of the assignment, assuming that's what this is. The OP might be misunderstanding it.
If it's as I am assuming, then he is just giving an example he's made up. The puzzle constructor makes both the square and the word list (and probably the latter comes first, contrary to what he's said).
If all he was given was the square, then you are right.
I wish if I have made this up.