Skip to content

Instantly share code, notes, and snippets.

@youssef3wi
Last active October 12, 2024 10:58
Show Gist options
  • Select an option

  • Save youssef3wi/b63132bc4a481ca9967197c89b4d6894 to your computer and use it in GitHub Desktop.

Select an option

Save youssef3wi/b63132bc4a481ca9967197c89b4d6894 to your computer and use it in GitHub Desktop.
You are tasked with validating student records from an array RecArray, where each element is in the format <name>:<roll_no>:<marks>.
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* <b>Unique Array</b>
* <p>
* You are tasked with validating student records from an array {@code RecArray}, where each element
* is in the format {@code <name>:<roll_no>:<marks>}.
* <p>
* The validation rules are given below:
* <ul>
* <li>{@code Name}: the name should consist only of English alphabets, which words separated
* by underscore.
* <p>
* Each word must start with an uppercase letter, followed by lowercase letters.
* </li>
* <li>{@code Roll Number}: The roll number must be unique and a prime number.</li>
* <li>{@code Marks}: The marks should be a positive decimal number with exactly two digits
* after the decimal point.</li>
* </ul>
* <p>
* Implement a method that should function as a filter for student records,
* throwing exceptions whenever an invalid record is encountered.
* <p>
* <b>Print the count of exceptions that would be thrown while validating the entire set of student records.</b>
* <p>
* <u><b>Note</b></u>
* <p>
* The array {@code RecArray} is a collection of strings only separated by a space.
* <p>
* A unique roll number means that no 2 students can have the same roll number.
* <p>
* For unique roll numbers, only check the valid student records only.
* <p>
* The uppercase character in the name may not be followed by a lowercase character.
* <p>
* In case of the same roll number, all students after the first student will throw an exception.
* <p>
* <u><b>Function description</b></u>
* <p>
* In the provided code snippet, implement the provided {@code UniqueArray(...)} method
* to print the count of exceptions that would be thrown while validating the entire set of student records.
* You can write your code in the space below the phrase {@literal "WRITE YOUR LOGIC HERE"}
* <p>
* There will be multiple test cases running, so the input and output should match exactly as provided.
* <p>
* The base Output variable {@code result} is set to a default value {@code -404} which can be modified.
* <p>
* Additionally, you can add or remove these output variables.
* <p>
* <u><b>Input format</b></u>
* <p>
* The first line contains the array {@code RecArray}, where each element
* is in the format {@code <name>:<roll_no>:<marks>}.
* <p>
* <u><b>Sample input</b></u>
* <p>
* {@code A:3:10.22 Abc_Def:13:12.11 Abc_def:13:11.1} -- denotes <b>RecArray</b>
* <p>
* <u><b>Constraints</b></u>
* <p>
* 1 < = {@code RecArray.length} < = 10000
* <p>
* 0 < {@code <name>.length} < 100
* <p>
* 0 < = {@code <roll no>} < = 10000
* <p>
* 0.00 < = {@code <marks>} < = 100000.00
* <p>
* <u><b>Output format</b></u>
* <p>
* The output contains an integer denoting the count of exceptions thrown while validating
* the entire set of student records.
* <p>
* <u><b>Sample output</b></u>
* <p>
* {@code 1}
* <p>
* <u><b>Explanation</b></u>
* <p>
* For 3rd student record
* <p>
* Name: Abc_def (Invalid: 'def' should be 'Def' for proper format)
* <p>
* Roll number: 13 (valid prime number but previously used)
* <p>
* Marks: 11.1 (Invalid format: should have two decimal places)
* <p>
* So, there is only 1 exception.
*/
public class StudentChecker {
private static final Pattern RECORD_PATTERN = Pattern.compile("^[A-Z][a-z]*(_[A-Z][a-z]*)?:[0-9]{0,4}:[0-9]{0,5}\\.[0-9]{2}$");
/**
* Checks if a given number is a prime number.
*
* @param number The integer to be checked for primality. It should be a non-negative integer.
* @return Returns true if the number is prime, otherwise returns false.
*/
private static boolean isPrime(int number) {
if (number <= 1) {
return false;
}
for (int idx = 2; idx <= Math.sqrt(number); idx++) {
if (number % idx == 0) {
return false;
}
}
return true;
}
public static int UniqueArray(List<String> RecArray) {
int record = 0;
// WRITE YOUR LOGIC HERE
Set<String> rollNumbers = new HashSet<>();
for (String student : RecArray) {
String[] parts = student.split(":");
if (parts.length != 3) {
record++;
continue;
}
Matcher matcher = RECORD_PATTERN.matcher(student);
if (!matcher.matches()) {
record++;
} else {
//roll number check
// unique ?
if (rollNumbers.contains(parts[1])) {
record++;
continue;
}
// prime ?
if (!isPrime(Integer.parseInt(parts[1]))) {
record++;
continue;
}
rollNumbers.add(parts[1]);
}
}
return record == 0 ? -404 : record;
}
public static void main(String[] args) {
// INPUT
List<String> students = new ArrayList<>();
students.add("A:3:10.22");
students.add("Abc_Def:13:12.11");
students.add("Abc_def:13:11.1");
// Call the function with the input array
int output = UniqueArray(students);
// Output
System.out.println(output);
}
}

Student checker

Unique Array

You are tasked with validating student records from an array RecArray, where each element is in the format <name>:<roll_no>:<marks>.

The validation rules are given below:

  • Name: the name should consist only of English alphabets, which words separated by underscore.

    Each word must start with an uppercase letter, followed by lowercase letters.

  • Roll Number: The roll number must be unique and a prime number.
  • Marks: The marks should be a positive decimal number with exactly two digits after the decimal point.

Implement a method that should function as a filter for student records, throwing exceptions whenever an invalid record is encountered.

Print the count of exceptions that would be thrown while validating the entire set of student records.

Note

The array RecArray is a collection of strings only separated by a space.

A unique roll number means that no 2 students can have the same roll number.

For unique roll numbers, only check the valid student records only.

The uppercase character in the name may not be followed by a lowercase character.

In case of the same roll number, all students after the first student will throw an exception.

Function description

In the provided code snippet, implement the provided UniqueArray(...) method to print the count of exceptions that would be thrown while validating the entire set of student records. You can write your code in the space below the phrase {@literal "WRITE YOUR LOGIC HERE"}

There will be multiple test cases running, so the input and output should match exactly as provided.

The base Output variable result is set to a default value -404 which can be modified.

Additionally, you can add or remove these output variables.

Input format

The first line contains the array RecArray, where each element is in the format <name>:<roll_no>:<marks>.

Sample input

A:3:10.22 Abc_Def:13:12:11 Abc_def:13:11.1 -- denotes RecArray

Constraints

1 < = RecArray.length < = 10000

0 < <name>.length < 100

0 < = <roll no> < = 10000

0.00 < = <marks> < = 100000.00

Output format

The output contains an integer denoting the count of exceptions thrown while validating the entire set of student records.

Sample output

1

Explanation

For 3rd student record

Name: Abc_def (Invalid: 'def' should be 'Def' for proper format)

Roll number: 13 (valid prime number but previously used)

Marks: 11.1 (Invalid format: should have two decimal places)

So, there is only 1 exception.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment