Wednesday, October 11, 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>th+++at actually contains the key as the unique code and the list of fruits that correlates to that unique key.

[Sample Input]

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 OE1234, 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 OE1234 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.

Example:

In this cryptic fruit game, you're given two lists. The first list has unique codes, each representing the position of a letter in the English alphabet. The second list contains fruits of various lengths. The objective is to pair each unique code with fruits whose length matches the position of the letter in the alphabet.

For example, if a unique code corresponds to 'E' at the 5th position, it should be paired with fruits that have a length of 5, such as 'Apple' and 'Orange'. The result is a map where the keys are the unique codes, and the values are lists of matching fruits. If there are no matching fruits, it's represented as "[No Fruit]" in the output.

The program generates this map in the specified format and displays it as shown in the example output, showing the key-value pairs.

 Code Explaination:

  1. This program enables users to play the "Cryptic Fruit Game," processing unique codes to find matching fruits based on alphabet letter positions. It demonstrates key Java programming aspects, including user input, data processing, and output generation.
  2. Input Collection:
      • Read the number of unique codes and fruits.
      • Collect unique codes and fruit names from the user.
  3. Alphabet Generation:
      • Generate the English alphabet (A to Z) and store it in the letters ArrayList
  4. Processing Unique Codes:

      • Process each unique code by calling the processWord method.
      • In the processWord method, check each character in the unique code.
      • If a character is an alphabet letter and exists in the letters ArrayList, add it to the processed word.
      • Result is a processed string representing the unique code based on matching alphabet letters.

  5. Matching Fruits:
      • Compare the length of the processed unique code with the indices of the English alphabet.
      • If the lengths match, collect fruits with corresponding lengths from the user's input.
  6. Result Map:
      • Construct a result map where each unique code is paired with matching fruits.
      • Use this map for generating the final output.
  7. Output Display:
      • Display the result for each unique code:
      • If matching fruits are found, list them.
      • If no matching fruits are found, display "[No Fruit]."
GitHub Repo link: http://surl.li/mpkjs 

Source Code(Java):

import java.util.*;
import java.util.stream.Collectors;

public class Question06_Venkata

 {
    static ArrayList<String> letters = new ArrayList<>();

    public static Map<String, List<String>> playGame(List<String> uniqueCodes, List<String> fruits) {
        Map<String, List<String>> resultMap = new HashMap<>();

        // Iterate through the English alphabet
        for (char letter = 'A'; letter <= 'Z'; letter++) {
            String alphabet = String.valueOf(letter);
            letters.add(alphabet);
        }

        // Iterate through each character in the word
        for (String code : uniqueCodes) {
            String uniqueChar = processWord(code);
            int index = letters.indexOf(uniqueChar);

            List<String> matchingFruits = fruits.stream().filter(fruit -> fruit.length() == index).collect(Collectors.toList());
            resultMap.put(code, matchingFruits);
        }

        return resultMap;
    }

    public static String processWord(String word) {
        StringBuilder processedWord = new StringBuilder();

        for (int i = 0; i < word.length(); i++) {
            char character = word.charAt(i);

            // Check if the character is an alphabet letter
            if (Character.isAlphabetic(character)) {
                String characterStr = String.valueOf(character).toUpperCase();

                if (letters.contains(characterStr)) {
                    // If the character exists in the 'letters' ArrayList, add it to the processed word
                    processedWord.append(characterStr);
                }
            }
        }
        return processedWord.toString();
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the number of unique codes and fruits: " + "\n");
        int numValues = scanner.nextInt();
        scanner.nextLine();

        List<String> uniqueCodes = new ArrayList<>();
        System.out.println("Enter unique codes:" + "\n");
        for (int i = 0; i < numValues; i++) {
            String input = scanner.nextLine();
            uniqueCodes.add(input);
        }

        List<String> fruits = new ArrayList<>();
        System.out.println("Enter fruit names:" + "\n");
        for (int i = 0; i < numValues; i++) {
            String input = scanner.nextLine();
            fruits.add(input);
        }

        Map<String, List<String>> resultMap = playGame(uniqueCodes, fruits);

        for (String code : uniqueCodes) {
            List<String> matchingFruits = resultMap.get(code);
            System.out.print(code + ": ");
            if (matchingFruits.isEmpty()) {
                System.out.println("[No Fruit]");
            } else {
                System.out.println(String.join(", ", matchingFruits));
            }
        }
        scanner.close();
    }
}


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...