Skip to content

Team A Question Submission #4

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@
PLEASE FILL IN THE FOLLOWING!

## Full Name
John Johnson
Juan P. Villalobos Rujano

## UWindsor Email
john.johnson@uwindsor.ca
villaloj@uwindsor.ca

## Application Form
- [ ] I certify I have submitted the [application form](https://forms.office.com/r/R4A1JyB3Xf)
- [x] I certify I have submitted the [application form](https://forms.office.com/r/R4A1JyB3Xf)

## Briefly explain how your solution works and how to run it
## Briefly explain how your main works and how to run it
When the main file is run, it will construct a 2D array of the same dimensions as the examples given (3x4) using user input. The input must be entered into the console and must be a row of 4 char values separated by spaces, three such rows must be entered; Each row is converted into a char array and assigned to the 2d array at array[i]. Lastly, the programs asks for a word from the user and passes both word and array[][] variables into solution().

My solution...
In solution() we first construct a Board obj which has the properties a[][] to store the 2D array, explored to keep track of the stage of exploration for a given point in a, and path[] to store the path numbers used to spell the word. A path number is a unique identifier for each element in a[][] that is calculated as row_number*row_size + column_number. The search function will iterate through a[][] at a specified starting point and if it finds a value of a[i][j] that equals the 3rd parameter passed into search(), char c, it will return the page number. Otherwise, it returns -1.

available() iterates through path[] to determine if it contains a specific page number, this helps us determine if a specific element in a[][] has already been passed through once.

Returning to solution(), we find the pathNum of the first char in word and assign it to path[0], then we initialize boolean terminate and enter a do-while loop. The loop will continue until path[] is full or terminate becomes true. In the loop we enter a switch that takes board.explored as its parameter. If explored is equal to -1, the algorithm has reached a dead-end and will set terminate equal to true and return false. If case is between 0-3, it will check a cardinal direction of from the current position in the path and check if that neighbour is both equal to the next letter and has not been passed through in path[].

If explored reverts to default in the switch, a given position has checked all four of its neighbours and none of them are the next letter. Therefore, we must either revert to the previous position in the path and check the next direction, we must search for another element in a[][] to be the first letter in the path if the old path was a dead-end, or we must terminate the loop if there are no more candidates for path[].

When the loop ends, we either return false if terminate = true or we return true if it doesn't as that would mean a viable path that spells the 'word' has been found.

The solution() function has been tested using the test cases in the solutionTest.java file.
45 changes: 45 additions & 0 deletions Board.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.util.Arrays;
public class Board {
public char[][] a;//2d array of char inputs
public int path[];//the collection of the points selected by the algorithm to spell the word
public int explored = 0;//current stage of exploration at a given a[i][j]
private int len;//length of word we must find in a[][]

public Board(char a[][], int len){//board constructor
this.a = a;
path = new int[len];
Arrays.fill(path, -1);//initialize all values of path to -1 to avoid confusion with a[0][0]
this.len = len;
}

/**the search function will take indexes for a row and column of the array a and search for the first c it finds. Later on, we may use the first two
* parameters to avoid double counting a character. Search returns a unique index for each element in a[][], it is calculated as 4*i + j. If a character is
*not found it returns -1
*/
public int search(int row, int col, char c){
for(int i = row; i < 3; i++){
for(int j = col; j < 4; j++){
if(a[i][j] == c){
return 4*i + j;
}
}
}
return -1;
}

/**The available function takes a pathNum and then iterates through path[] to locate it, if it does, it returns false; otherwise, it will return
* true
*/
public boolean available(int pathNum){
for(int i = 0; i < len; i++){
if(path[i] == pathNum){
return false;
}
}
return true;
}

public int getRow(int pathNum){return (pathNum - pathNum%4)/4;}//returns i by deriving it from a pathNum

public int getCol(int pathNum){return pathNum - (pathNum - pathNum%4);}//returns j by deriving it from a pathNum
}
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ Be sure to also complete the [application form](https://forms.office.com/r/R4A1J

# Grading

Your solutions will be tested with many test cases. We will look at algorithm correctness against these test cases as well as time complexity when assessing your solution.
Your solutions will be tested with many main cases. We will look at algorithm correctness against these main cases as well as time complexity when assessing your main.

# Submitting

Fork this repository to your personal account, then push your solutions to your fork on the master branch. You may use any programming language of your choosing. Please save the file that we should run as a file named `solution` (e.g. `solution.py`, `solution.c`).
Fork this repository to your personal account, then push your solutions to your fork on the master branch. You may use any programming language of your choosing. Please save the file that we should run as a file named `main` (e.g. `main.py`, `main.c`).

Next, **create a pull request** to lock in your submission, **explaining how your program works and how to run it**.

Expand All @@ -25,7 +25,7 @@ The word can be constructed from letters of sequentially adjacent cell, where "a

### Example:
```
board =
Board =
[
['A','B','C','E'],
['S','F','C','S'],
Expand All @@ -41,8 +41,8 @@ Given word = "ABCB", return false.


### Input:
1) `board` - 2D array of characters
2) `word` - String word that you are looking for in the board
1) `Board` - 2D array of characters
2) `word` - String word that you are looking for in the Board

Assume inputs are separated by spaces and new lines. So for Team A it will look like:
```
Expand All @@ -55,4 +55,4 @@ word


### Output:
Boolean value representing whether the word can be found (true) in the board or not (false). Your function should return this value.
Boolean value representing whether the word can be found (true) in the Board or not (false). Your function should return this value.
115 changes: 115 additions & 0 deletions main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import java.util.Scanner;

public class main {
static boolean solution(String word, char[][] a){
Board b = new Board(a, word.length());//create board object
char c = word.charAt(0);//get first char in word
int col = 0;//col & row are 0 so that b.search looks for c from the start of a[][] to the end
int row = 0;
b.path[0] = b.search(row, col, c);//assign the pathNum to path[0]
boolean terminate = false;//if this becomes true, we are unable to locate a valid char in a[][] that would continue the word. Therefore, the function returns false
int i = 1;//index i represents the index of the current letter in the word we are attempting to locate
do{
if(b.path[i - 1] >= 0){//if the previous path index is less than zero, we must skip to the next else-statement
c = word.charAt(i);
row = b.getRow(b.path[i - 1]);
col = b.getCol(b.path[i - 1]);
}
else{//set explored = -1
b.explored = -1;
}
switch (b.explored){
case -1:
terminate = true;
break;
case 0://if there is a character above the character path[i - 1], and it equals c, and it is not present anywhere else in the path
if(row > 0 && b.a[row - 1][col] == c && b.available(b.search(row - 1, col, c))){
b.path[i] = b.search(row - 1, col, c);
b.explored = -1;
i++;
}
b.explored++;
break;
case 1://if there is a character to the right of the character path[i - 1], and it equals c, and it is not present anywhere else in the path
if(col < 3 && b.a[row][col + 1] == c && b.available(b.search(row, col + 1, c))){
b.path[i] = b.search(row, col + 1, c);
b.explored = -1;
i++;
}
b.explored++;
break;
case 2://if there is a character below the character path[i - 1], and it equals c, and it is not present anywhere else in the path
if(row < 2 && b.a[row + 1][col] == c && b.available(b.search(row + 1, col, c))){
b.path[i] = b.search(row + 1, col, c);
b.explored = -1;
i++;
}
b.explored++;
break;
case 3://if there is a character to the left of the character path[i - 1], and it equals c, and it is not present anywhere else in the path
if(col > 0 && b.a[row][col - 1] == c && b.available(b.search(row, col - 1, c))){
b.path[i] = b.search(row, col - 1, c);
b.explored = -1;
i++;
}
b.explored++;
break;
default://if all cardinal directions of path[i-1] have been checked, and we still haven't found c
if(i == 1){//if path only contains the identifier of the first char in word
c = word.charAt(0);
row = b.getRow(b.path[0]);
col = b.getCol(b.path[0]) + 1;
int temp = b.search(row, col, c);//attempt to look for another candidate for path[0]
if(temp == -1){//if a candidate is not found
terminate = true;
}
else{//otherwise
b.path[0] = temp;
b.explored = 0;
}
}
if(i > 1){//if there are more than 1 chars in path[], check the previous two pathNums to determine which direction was taken from path[i - 2] to reach path[i - 1]
if(b.path[i - 2] == b.path[i - 1] + 4*b.getRow(b.path[i - 1])){//if the algorithm went up
b.explored = 1;
}
else if(b.path[i - 2] == b.path[i - 1] - 1){//if it went right
b.explored = 2;
}
else if(b.path[i - 2] == b.path[i - 1] - 4*b.getRow(b.path[i - 1])){//if it went down
b.explored = 3;
}
else{//else, it went left
b.explored = 4;
if(i == 2){
b.explored = -1;
}
}
i--;
}
break;
}
}while(i < word.length() && terminate == false);//while the loop has no found a pathNum for all indexes in path[] AND while terminate is false
if(terminate == true){//path not found
return false;
}
else{//word can be made
return true;
}
}

public static void main(String args[]){
//initialize Scanner and 2D array
Scanner in = new Scanner(System.in);
char[][] array = new char[3][4];

//Read three lines of input from console, erase the spaces from the string and convert each string into a char array. This array is then assigned to array[i]
for(int i = 0; i < 3; i++){
array[i] = in.nextLine().replaceAll(" ","").toCharArray();
}

//take the word input and pass it along with the array into the parameters of solution()
System.out.println("What word would you like to find? Enter it unbroken below: ");
String word = in.nextLine();
System.out.println(solution(word, array));
}
}
114 changes: 114 additions & 0 deletions solutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class solutionTest {

@Test
void test0() {
char[][] a = {{'A','S','E','R'},
{'Y','U','T','O'},
{'G','H','I','L'}};
String word = "SEROLITUHGYA";
assertTrue(main.solution(word, a));
}

@Test
void test1() {
char[][] a = {{'A','S','E','R'},
{'Y','U','T','O'},
{'G','H','I','L'}};
String word = "ASER";
assertTrue(main.solution(word, a));
}

@Test
void test2() {
char[][] a = {{'A','S','E','R'},
{'Y','U','T','O'},
{'G','H','I','L'}};
String word = "CAT";
assertFalse(main.solution(word, a));
}

@Test
void test3() {
char[][] a = {{'A','A','A','A'},
{'A','T','A','A'},
{'A','A','B','A'}};
String word = "BAT";
assertTrue(main.solution(word, a));
}

@Test
void test4() {
char[][] a = {{'A','A','A','A'},
{'T','A','A','A'},
{'A','A','B','A'}};
String word = "BAT";
assertFalse(main.solution(word, a));
}

@Test
void test5() {
char[][] a = {{'A','A','A','A'},
{'T','A','A','A'},
{'A','A','B','A'}};
String word = "BAAT";
assertTrue(main.solution(word, a));
}

@Test
void test6() {
char[][] a = {{'T','T','A','T'},
{'T','A','B','A'},
{'T','T','A','T'}};
String word = "BAT";
assertTrue(main.solution(word, a));
}

@Test
void test7() {
char[][] a = {{'A','S','E','R'},
{'Y','U','T','O'},
{'G','H','I','L'}};
String word = "AYA";
assertFalse(main.solution(word, a));
}

@Test
void test8() {
char[][] a = {{'A','S','E','R'},
{'Y','U','T','O'},
{'G','H','I','L'}};
String word = "ASA";
assertFalse(main.solution(word, a));
}

@Test
void test9() {
char[][] a = {{'A','S','E','R'},
{'Y','U','T','O'},
{'G','H','I','L'}};
String word = "SER";
assertTrue(main.solution(word, a));
}

@Test
void test10() {
char[][] a = {{'A','S','A','R'},
{'Y','U','T','O'},
{'G','H','I','L'}};
String word = "AR";
assertTrue(main.solution(word, a));
}

@Test
void test11() {
char[][] a = {{'A','F','U','S'},
{'D','U','Y','I'},
{'F','N','M','P'}};
String word = "FUN";
assertTrue(main.solution(word, a));
}
}