Monday, October 9, 2023

Codeathon06_Haritha


[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

OE1234

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

package src; import java.util.*; public class Codeathon06_Haritha { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number of values for list1 and list2: "); int numValues = sc.nextInt(); sc.nextLine(); List<String> list1 = new ArrayList<>(); System.out.println("Enter list1 elements:"); // To enter list1 values for (int i = 0; i < numValues; i++) { String input = sc.nextLine(); list1.add(input); } List<String> list2 = new ArrayList<>(); System.out.println("Enter list2 elements:"); // To Enter List two Values for (int i = 0; i < numValues; i++) { String input = sc.nextLine(); list2.add(input); } Map<String, List<String>> resultMap = new HashMap<>(); for (int i = 0; i < list1.size(); i++) { String str = list1.get(i); List<String> equalF = sameFuritsSize(str, list2); resultMap.put(str, equalF); } for (int i = 0; i < list1.size(); i++) { String str = list1.get(i); List<String> findFruits = resultMap.get(str); System.out.print(str + ": "); if (findFruits.isEmpty()) { System.out.println("[no fruit]"); } else { System.out.println(String.join(", ", findFruits)); } } } private static List<String> sameFuritsSize(String s1, List<String> fruitList) {

// Function to find fruits of the same size char word = fruits(s1); List<String> findFruits = new ArrayList<>(); for (int i = 0; i < fruitList.size(); i++) { // To find matching fruits String s = fruitList.get(i); if (s.length() == word - 'A' + 1) { findFruits.add(s); } } return findFruits; } private static char fruits(String code) { for (char ch : code.toCharArray()) { if (Character.isLetter(ch)) { return Character.toUpperCase(ch); } } return ' '; } } [Explanation of Solution] 1. The program takes user input for the number of values for list1 and list2. 2. Two lists, list1 and list2, are initialized to store user-entered elements. 3. A HashMap named resultMap is created to associate elements from list1 with matching fruits from list2. 4. The code iterates through list1 and calls the sameFuritsSize() method to find fruits in list2 that have the same size as the current element from list1. 5. The results are stored in resultMap, where each element from list1 is associated with a list of matching fruits from list2. 6. The code then iterates through list1 again to display the results: 7. For each element in list1, it retrieves the corresponding list of matching fruits from resultMap. It prints the element from list1 followed by a colon and then either the list of matching fruits or "[no fruit]" if no matches were found. Two helper methods are used: 8. sameFuritsSize(String s1, List<String> fruitList): Finds fruits in fruitList that have the same size as the input string s1. fruits(String code): Extracts the first letter (alphabetical character) from the input string code and converts it to uppercase.


Haritha .P(Intern),
Guard Ninjas,
Data Shield Team,
Enterprise Minds.   


0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home