Thursday, 5 October 2023

EM-Tirupati Codeathon Series #04

[Question]
Java Advanced - Lambda Expressions [www.techgig.com]
Write the Following Methods that Return a Lambda Expression Performing a Specified Action: Perform Operationis Odd(): The Lambda Expression must return  if a Number is Odd or  If it is Even. Perform Operationis Prime(): The lambda expression must return  if a number is prime or  if it is composite. Perform Operationis Palindrome(): The Lambda Expression must return  if a number is a Palindrome or if it is not.

[Sample Input]
Input is as Show in the Format Below (Deduce Unknowns!)
Input
3
1 3
2 7
3 7777
 
Constraints
NA
 
[Sample Output]
Output is as Show in the Format Below (Deduce Unknowns!)
 
Output
ODD
PRIME
PALINDROME
 
[Explanation of Solution]
The LambdaOperation interface serves as a template for these lambda expressions, each designed to evaluate a different condition. The program takes input pairs of operation types and numbers, processes them according to the lambda expressions, and stores the results in an array. A switch-case structure directs the program to the appropriate lambda expression based on the operation type, and the corresponding result is stored in the output array. Finally, the program prints the results for each input operation type.
 
Github Repo for Code: https://tinyurl.com/nyzv7237

[Solution (Java Code)]
/*
 * Core Java Problem Solving - Codeathon.
 * A.R. Kishore Kumar ~ (c) 2023 ~ -- ~ Tirupati, Andhra Pradesh, India ~
 *
 */
package codeathon;
import java.util.Scanner;
interface LambdaOperation
{
    boolean perform(int x);
}
 
public class Codeathon04_Kishore
{
    public static void main(String args[])
    {
        // Check given number is even or odd
        LambdaOperation isOdd = n -> {
            boolean check = false;
            if (n % 2 != 0)
                check = true;
            return check;
        };
 
        //check given number is prime or composite
        LambdaOperation isPrime = n -> {
            boolean check = true;
            for (int i = 2; i < n - 1; i++) {
                if (n % i == 0) {
                    check = false;
                    break;
                }
            }
            return check;
        };
 
        //check given number is palindrome or not
        LambdaOperation isPalindrome = n -> {
            boolean check = false;
            int p, r=0, k = n;
            while ( n != 0)
            {
                p = n%10;
                r = r*10 + p;
                n = n/10;
            }
            if( k == r)
                return true;
            else
                return false;
        };
 
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
 
        String output[] = new String[3];
 
        for (int i = 0; i < num; i++)
        {
            int ch = scanner.nextInt();
            int no = scanner.nextInt();
            switch (ch)
            {
                case 1:
                    if (isOdd.perform(no))
                        output[i] = "ODD";
                    else
                        output[i] = "EVEN";
                    break;
 
                case 2:
                    if (isPrime.perform(no))
                        output[i] = "PRIME";
                    else
                        output[i] = "COMPOSITE";
                    break;
                case 3:
                    if (isPalindrome.perform(no))
                        output[i] = "PALINDROME";
                    else
                        output[i] = "NOT A PALINDROME";
            }
        }
        for(int i=0;i<3;i++)
            System.out.println(output[i]);
    }
}

No comments:

Post a Comment