[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>thactually
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
0E1234: 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.
Explanation:
In this cryptic fruit game, two
lists are given. The first list contains unique identifiers, each representing
a letter's position in the English alphabet. The second list contains fruits of
varying lengths. The goal is to match each unique identifier with fruits whose
length corresponds to the letter's position in the alphabet.
For example, if a unique
identifier has 'E' at the 5th position, it should be matched with fruits of
length 5, such as 'Apple' and 'Orange'. The output is a map where the keys are
unique identifiers, 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 creates a map with this format and prints it as shown in the example output, displaying the key-value pairs.
[Explaination of the solution]
In this problem list1 have one character in Unique code and list2 has some fruits name. If fruits name length is equal to the character in English's alphabetic order.Then we need to print list the fruits name with paricular unique code using <map,list<string>>.
Github Repo for Code:
[Solution (Java Code)]
package codeathon;
import java.util.*;
public class Codeathon06_vijay {
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("give list1 values:");
for (int i = 0; i < numValues; i++) {
String input = sc.nextLine();
list1.add(input);
}
List<String> list2 = new ArrayList<>();
System.out.println("give list2 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> fruits = findTheListOfFruits(str, list2);
resultMap.put(str, fruits);
}
for (int i = 0; i < list1.size(); i++) {
String str = list1.get(i);
List<String> findFruits = resultMap.get(str);//here we are getting list of fruits name with key string value
System.out.print(str + ": ");
if (findFruits.isEmpty()) {
System.out.println("[no fruit]");
} else {
System.out.println(String.join(",", findFruits));
}
}
}
private static List<String> findTheListOfFruits(String str, List<String> fruitList) {
char word = findTheLetter(str);
List<String> findFruits = new ArrayList<>();
for (int i = 0; i < fruitList.size(); i++) {
String s = fruitList.get(i);
if (s.length() == word - 'a' + 1) {
findFruits.add(s);
}
}
return findFruits;
}
private static char findTheLetter(String uniqueCode) {
for (char ch : uniqueCode.toCharArray()) {
if (Character.isLetter(ch)) {
return Character.toLowerCase(ch);
}
}
return ' ';
}
}
Thank you,
k.vijay(Intern)
vijay.keradhi@eminds.ai
Enterprise Minds.
No comments:
Post a Comment