Friday, 6 October 2023

EM-Tirupati Codeathon Series #06

[Question]
CRYPTIC FRUIT GAME
you are given 2 list of values. The first list contains a unique identifier that needs to be matches the second list that has a set of fruits. Each unique identifier has exactly one letter of the English alphabet. The position of that letter in the English alphabet correlates to the length of the fruit give in the second list. The output of the program will be of the format Map<String, List<String>that actually contains the key as the unique code and the list of fruits that correlates to that unique key.

[Sample Input]
First line contains number of identifiers in both the lists and next lines contain input as follows:
7
List 1
0E1234
0823F
1200J
600K
456700I
A001
8432X
 
List 2
Apple
Orange
Banana
Grape
Watermelon
Pomegranate
Jackfruit
 
[Sample Output]
OE1234: Apple, Grape
0823F: Orange, Banana
1200J: Watermelon
600K: Pomegranate
456700I: Jackfruit
8432X: [No Fruit]
A001: [No Fruit]
 
Explanation of the Output
From the Sample Input, If we take 0E1234, E is the letter of the english alphabet that is on the 5th position in the English alphabet. Now, the fruits that are of length 5 in the second list are > 'Apple', 'Orange'. Hence the output will have the Key as 0E1234 and the corresponding value will be 'Apple', 'Orange. You have to store the output as Map<String, List<String>> and also print the output in the format shown above. If there are no fruits matching, for example in A001, the position of A in english alphabet is 1 and there are no fruits with length 1 in the second list, so you have to print [No Fruit] against it. Please adhere exactly to the output format as given above.
 
Areas: Java Collections, Core Java, Logic, Problem Solving, String Handling

[Explanation of Solution]
The program first reads the input, storing the values from both lists into list1 and list2, respectively. Then, it iterates through list1 and, for each unique identifier, finds the matching fruits length from list2 based on the alphabets respective value (A -1, B-2, C-3,….,Z-26 respectively). The matching fruits are stored in a map called resultMap, with the unique identifiers as keys and the matching fruits as values. The program then prints the results.
 
Github Repo for Code:  https://tinyurl.com/5yhtwf8h
 
[Solution (Java Code)]
/*
 * Core Java Problem Solving - Codeathon.
 * A.R. Kishore Kumar ~ (c) 2023 ~ -- ~ Tirupati, Andhra Pradesh, India ~
 *
 */
package codeathon;
import java.util.*;
public class Codeathon06_Kishore {
    public static void main(String[] args) {
        List<String> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
 
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();   // Number of Elements in each List
 
        System.out.println("List 1");
        for (int i = 0; i < n; i++) {    // Storing first list of values
            list1.add(sc.next());
        }
 
        System.out.println("List 2");
        for (int i = 0; i < n; i++) {    // Storing second list of values
            list2.add(sc.next());
        }
 
        // storing matching fruits to resultMap and displaying final output
        Map<String, List<String>> resultMap = new HashMap<>();
        for (String text : list1) {
            List<String> matchingFruits = Matching.findMatchingFruits(text, list2);
            resultMap.put(text, matchingFruits);
        }
        for (String list1String : list1) {
            List<String> matchingFruits = resultMap.get(list1String);
 
            System.out.print(list1String + ": ");
            if (matchingFruits.isEmpty()) {
                System.out.println("[No Fruit]");
            } else {
                System.out.println(String.join(", ", matchingFruits));
            }
        }
    }
}
class Matching
{
    // method for finding matched fruits
    public static List<String> findMatchingFruits(String numberString, List<String> list2) {
        char alphabet = findAlphabetCharacter(numberString);
        List<String> matchingFruits = new ArrayList<>();
        for (String fruit : list2)
        {
            if (fruit.length() == alphabet - 'A' + 1)
            {
                matchingFruits.add(fruit);
            }
        }
        return matchingFruits;
    }
 
    // Method to know no of letters Based on Alphabet
    private static char findAlphabetCharacter(String text) {
        for (char c : text.toCharArray())
        {
            if (Character.isLetter(c)) {
                return Character.toUpperCase(c);
            }
        }
        return ' ';
    }
}

No comments:

Post a Comment