- The challenge consists in five code challenges about Java, SQL and NoSQL
- Each code challenge must have its own production branch which will be merged to dev branch and then to main branch
Question | Jump to it |
---|---|
Question 1 | Link |
Question 2 | Link |
Question 3 | Link |
Question 4 | Link |
Question 5 | Link |
- Main language: JAVA
- Tasks
- folder creation
- branch creation
- start code production
- question done
Must output the student name that failed in the class respecting the quantity of problems solved and the name ordered alphabetically
First tiebraker => number of problems solved
Second tiebraker => last name alphabetically sorted
- Create one Array List to store the students name and another one to store how many problems were solved;
- With an Array List will be easier remove data, add data and comparing it. The methods most used in the code.
ArrayList<Integer> problemSolved = new ArrayList<Integer>(); ArrayList<String> studentsName = new ArrayList<String>();
- Instantiate a Scanner Class to receive the data input;
Scanner source = new Scanner(System.in);
- With a loop that could be a for because we already know the number of students, each of them will be inserted in a different line, we will add:
- Every single integer with the method .nextInt() (of the Scanner Class) to the problems Array List;
- Every single string with the method .next() (of the Scanner Class) to the students name Array List;
for (int i = 0; i < studentsQuantity; i++) { studentsName.add(source.next()); problemSolved.add(source.nextInt()); }
- HERE COMES THE MAGIC, respecting the tiebreakers:
- Using the j and i variables, the code will compare the values in the indexes that j and i points to
for (int j = problemSolved.size() - 1, i = 0; j > 0; j--)
- The first if statement will remove from students name Array List and from the problems solved Array List the value in the i index if it is greater than the value in the j index
if (problemSolved.get(i) > problemSolved.get(j)) { problemSolved.remove(i); studentsName.remove(i); }
- In line 22, if the value in the i index is lower than the value in the j index, the values in the j index will be removed from both Array Lists.
else if (problemSolved.get(i) < problemSolved.get(j)) { problemSolved.remove(j); studentsName.remove(j); }
- The last and most important, the piece of code below checks if both values (i and j) are equal to each other.
else if (Objects.equals(problemSolved.get(i), problemSolved.get(j)))
- If so, it means that both students got the same number of problems solved, and we will go for the second tiebraker;
- The method str1.compareTo(str2) can return 3 distinct values, which can be:
- An int value of 0 if the string is equal to the other string.
- A case which will not happen because there are no homonyms
else { System.out.println("Both students have the same name"); break; }
- An int value lower than 0 if the string is lexicographically less than the other string
else if (studentsName.get(i).compareTo(studentsName.get(j)) < 0) { studentsName.remove(i); problemSolved.remove(i); }
- An int value greater than 0 if the string is lexicographically greater than the other string (more characters)
else if (studentsName.get(i).compareTo(studentsName.get(j)) < 0) { studentsName.remove(i); problemSolved.remove(i); }
- At the end, the student with the least number os problems solved and with the last name alphabetically sorted will be printed out
System.out.println(studentsName.get(0));
- An int value of 0 if the string is equal to the other string.
- With a loop that could be a for because we already know the number of students, each of them will be inserted in a different line, we will add:
- Main language: JAVA
- Tasks
- folder creation
- branch creation
- start code production
- question done
Must output one of the next messages
- "Fun" => if the amount happy faces is greater than the amount of sad faces
- "Neutral" => if the amount of happy faces is equal than the amount of sad faces
- "Sad" => if the amount of happy faces is lower than the amount of happy faces
- Create an array (elements) to store each element from the string (line) separated by whitespaces using the method .split( );
- Instatiate a Scanner Class to read the input;
- With a string (line) store each .nextLine( )
- With two int variables
Scanner source = new Scanner(System.in); String line = source.nextLine(); String[] elements = line.split(" "); int upsetCount = 0, funCount = 0;
- A for each loop is used to count how many
":-("
and":-)"
are on the array (elements)for (String word : elements) { if (word.equals(":-(")){ upsetCount++ ; } else if (word.equals(":-)")){ funCount++; } }
- Finally, an if statement to check the numeric values of
upsetCount
andfunCount
;if (upsetCount==funCount){ System.out.println("Neutral"); } else if (upsetCount > funCount) { System.out.println("Upset"); } else { System.out.println("Fun"); }
- Main language: JAVA
- Tasks
- folder creation
- branch creation
- start code production
- question done
Must output the result of an equation
- Instantiate a Scanner Class to read the input;
- Create an array(results) to store each equation's result and then print it out
Scanner source = new Scanner(System.in); int result = 0; List<Integer> results = new ArrayList<>(); int k = 1;;
- A while loop to run through every line
while (source.hasNext())
- Some if statement to check how many number are on the input and if it respects the constraints
if (operandsQuantity < 1 || operandsQuantity > 100) { break; } if (operandsQuantity != 0) { String equation = source.next();
- This particular for loop was made to store the signs presented in the equationOperator.
for (int i = 0; i < equationFormatted.length(); i++) { equationOperator.add(equationFormatted.charAt(i)); }
- Another if statement to check other constraints related to the numbers of operands and how many of them were inserted
if (operandsQuantity < equationCounter.size()) { System.out.println("Exceeded number of operands, you must have inserted " + operandsQuantity + " operands"); } else if (operandsQuantity > equationCounter.size()) { System.out.println("You must have inserted " + operandsQuantity + " operands");
- The last part of the code was developed to add or to subtract the value of the result
- Then this value is stored in the array(results)
} else{
for(int j=0;j<equationCounter.size();j++){
if(equationOperator.get(j).hashCode()=="-".hashCode()){
result=result-Integer.parseInt((String)equationCounter.get(j));
}else{
result=Integer.parseInt((String)equationCounter.get(j))+result;
}
}
results.add(result);
}
- A for each loop to print each result stored in the array(results)
for (int i : results) { System.out.println("Test: " + k); System.out.println(i); k++;
- Main language: SQL
- Tasks
- folder creation
- branch creation
- start code production
- question done
Must write a scritp for PostgreSQL creating in this order:
- Create a database;
- Create a table to store addresses with
- address_id
- zip code
- street name
- complement for the address
- neighborhood
- city name
- state name
- country name;
- Create a table to store persons with
- person_id
- name
- age
- phone
- height
- cpf
- date of birth
- address_id
Note: The address_id in Person Table should be a foreign key which refers to the address_id in the Address Table
- Using the SQL language create the first database;
- Then create the Address Table to first store the address to be referenced by the Person Table
- Finally, create the Person Table.
- Main language: NoSQL
- Tasks
- folder creation
- branch creation
- start code production
- question done
Must create a script for MondoDB inserting a Person Document containing:
- Object;
- Name;
- Age;
- Phone;
- Height;
- Email;
- CPF;
- Date of birth;
- Address > - Zip code;
- Street name;
- Complement;
- Neighborhood;
- City;
- State;
- Country
- Download Mongosh, you can find more about it here;
- Start the executable file in .zip folder;
- Connect to your MongoDB server;
- And then follow the written code.