Boa tarde.
Peguei alguns exercícios pra fazer de estrutura de dados e me deparei com esse desafio. Iniciei, porém estou com dificuldade de desenvolver o raciocínio.
Chain Validator
There are two kind of lists, each for a different type of values. First type: a list of one digit and two-digit numbers, second type: a list of words and single characters consist of a-z letters (only lower case). A list can either be of the first or second type, but not mixed with both.
Multiple digits/letters objects have dependency on single digit/letters objects which are they consist of. For example, the number 24 is dependent on two objects: 2 and 4, the word “aba” is dependent on two objects: “a” and “b”.
Write java code for the following: Implement a method boolean validate(List nodes) which will determine if a chain is valid, such that multi characters dependency is satisfied by other single characters numbers in the list. Same method should work for both kind of Node types.
The class/interface Node is yours to implement. Class should represent the two types of data. Implement the class in a way that will help you implement the validate method.
You can add additional classes as you wish.
Examples: Valid chain: 36, 6, 24, 4, 47, 7, 2, 3, 27Valid chain: “p”, “aba”, ”a”, ”b”, “perso”, “o”, “r”, “e”, “s” Not valid chain: 35, 5, 65, 6, 24, 4, (number 2 is missing). You don’t have to take care of: “a”, 5, “asd” (or any mixed values list).
Pointers: Give a correct answer. Write generic and easy to modify code. Consider time complexity. Utilize data structure to make the solution efficient.
Criei uma classe Node e uma classe Test.
package node;
import java.util.Arrays;
import java.util.List;
public class Node {
int[] numbers = {36, 6, 24, 4, 47, 7, 2, 3, 27};
String[] letters = {"p", "aba", "a", "b", "perso", "o", "r", "e", "s"};
public Node(int[] numbers, String[] letters) {
this.numbers = numbers;
this.letters = letters;
}
protected boolean validate(List<Node> nodes) {
for (int i = 0; i <= numbers.length; i++) {
for (int j = 0; j < letters.length; i++) {
if (Integer.toString(numbers[i]) == letters[j]) {
return true;
}
}
}
return false;
}
public int[] getNumbers() {
return numbers;
}
public void setNumbers(int[] numbers) {
this.numbers = numbers;
}
public String[] getLetters() {
return letters;
}
public void setLetters(String[] letters) {
this.letters = letters;
}
@Override
public String toString() {
return Arrays.toString(numbers) + Arrays.toString(letters);
}
}
package node;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class NodeTest {
public static void main(String[] args) {
Node a = new Node(new int[0], new String[0]);
List<Node> nodes = new LinkedList<>();
nodes.addAll(a);
a.validate(nodes);
System.out.println(a);
}
}
Minha dúvida está em como implementar essa parte: such that multi characters dependency is satisfied by other single characters numbers in the list.