Monday, October 9, 2023

EM-Tirupati Codeathon Series #01

[Question]

Algorithms/Data Structures — [Problem Solving]

There is a Specific Need for Changes in a List of Usernames. In a given List of Usernames — For Each Username — If the Username can be Modified and Moved Ahead in a Dictionary. The Allowed Modification is that Alphabets can change Positions in the Given Username.

Example

usernames[] = {“Aab”, “Cat”}

“Aab” cannot be changed to another unique string matching the above rule — Hence, It can Never Find a Place Ahead in the Dictionary. Hence, Output will be “NO”. “Cat” can be Changed to “Act”, “Atc”, “Tca”, “Tac”, “Cta” and Definitely “Act” will Find a Place Before “Cat” in the Dictionary. Hence, Output will be “YES”.

[Function Description]

Complete the function possible Changes in the Editor Below.

Possible Changes has the following parameters:

String usernames[n]: An Array of User Names.

Returns String[n]: An Array with “YES” or “NO” Based on Feasibility

(Actual Question Says String Array, But Signature is List of Strings)

Constraints

• [No Special Constraints Exist, But Cannot Recall Exactly]

[Sample Input]

“The First Line Contains an Integer, n, the Number of Elements in Usernames.”,

“Each Line of the n Subsequent Lines (where 0 < i < n) contains a String usernames[i].”

[Sample Case 0 — Sample Input For Custom Testing]

8

Aab

Cat

Pqrs

Buba

Bapg

Sungi

Lapg

Acba

[Sample Output] (Each Should Be on a Separate Line)

NO YES NO YES YES YES YES YES

Code Explaination:

  1. UsernameValidation is a utility class that encapsulates methods related to username validation and result display.

  2. The possibleChanges method takes a username as input and checks if it can be modified. It compares each character with the characters that follow it and returns true if any character is followed by a smaller character, indicating that the username can be modified.
  3. The displayResults method takes an array of boolean results and displays them as "YES" if the result is true or "NO" if it's false. 
  4. The Question01_Venkata class is the main class that contains the main method and serves as the entry point for the program.
  5. An instance of the UsernameValidation class called validate is created to utilize its validation and display methods. 
  6. Two Scanner objects, scanner and scanner1, are created to read user input.
  7. The user is prompted to input the number of usernames using scanner.nextInt().
  8. Another prompt is displayed, instructing the user to enter string values (usernames).
  9. An array called usernames is created to store the entered usernames.
  10. A boolean array named results is created to store the validation results for each username.
  11. The possibleChanges method from the validate object is called to validate the username and store the result in the results array.
  12. After all usernames are validated, the displayResults method from the validate object is called to display the results.
  13. Results are shown on the console as "YES" if the validation result is true or "NO" if it's false in an Array.
GitHub Repo link:  http://surl.li/mpjte

Source code (Java) :

import java.util.Scanner;

// This class is for username validation and result display

class UsernameValidation {

    

    // Method to check if the username can be modified

    public static Boolean possibleChanges(String username) {

        for (int i = 0; i < username.length(); i++) {

            char currentChar = username.charAt(i);

            for (int j = i + 1; j < username.length(); j++) {

                char nextChar = username.charAt(j);

                if (nextChar < currentChar) {

                    return true;

                }

            }

        }

        return false;

    }

    // Method to display the results

    public static void displayResults(boolean[] results) {

        System.out.println("Output:");

        for (boolean result : results) {

            System.out.println(result ? "YES" : "NO");

        }

    }

}


public class Question01_Venkata {

    public static void main(String[] args) {

        // Creating an instance of UsernameValidation

        UsernameValidation validate = new UsernameValidation();

        Scanner scanner = new Scanner(System.in);

        System.out.println("Please enter the Size:");

        int size = scanner.nextInt();

        Scanner scanner1 = new Scanner(System.in);

        System.out.println("Please enter the Usernames:");


        // Create an array to store usernames

        String[] usernames = new String[size];

        for (int i = 0; i < size; i++) {

            // Read each username, convert it to lowercase, and store it in the array

            usernames[i] = scanner1.next().toLowerCase();

        }

        // Create an array to store the results

        boolean[] results = new boolean[size];

        // Loop through each username and validate it using the UsernameValidation object

        for (int i = 0; i < size; i++) {

            results[i] = validate.possibleChanges(usernames[i]);

        }

        // Display the results using the displayResults method

        validate.displayResults(results);

    }

}


Thank you

Venkata kishore T(Intern)

Shield Warriors,

Data Shield Team,

Enterprise Minds.


No comments:

Post a Comment

EM-Tirupati Codeathon Series #08

[Question] ROBOTIC CRICKET MATCH you should write a program that simulate an automatic cricket match between India and Sri Lanka. The fo...