September 07, 2020

Chef's in paris cooking with ingredients: How to solve below complex problem using java

below is the problem :-

Chef's ingredients:-


1.The chef receives exactly 1 ingredient per day from the market.The
ingredients never repeat.

2. Every ingredient belongs to 1 of the 3 categories namely FIBER,FAT & CARB.

3.Every ingredient has a unique ingredient id.

4.The ingredient id always starts with the category name (ex:FIBERBroccoli,FATCheese,CARBRice)

Chef's Dishes

1. All of the chef's dishes have a constant number of ingredients.(this will be your program's input)

2. All the ingredients used will be fully used in a Dish. The chef doesn' use some part/quntity of an ingredient.

3.All of the chef's dishes mush have at least 60% of the ingredients from a single category.(i.e. if the chef cooks using
4 ingredients,then at leaast 3 FAT ingredients OR at least 3 FIBER ingredients OR at least 3 CARB ingredients are needed)


Chef's Cooking style:-

1. If the chef has multiple options of ingredients for the dish,then he takes the oldest possible ones to cook
in the order of their arrival.

2.After the chef prepares a dish,the ingredients used can Not be reused as theyhave been already used.

3.The chef prepares a maximum of 1 dish per day.

4.if the Chef does not have enough ingredients to cook the dish with above mentioned rules,then he does not
cook on that day.

Given the input array of ingredient id that the chef receives every day (i.e. array index is the day number)
write a program to print when does the chef cook a dish and when he does not.


Input:-

Line 1: The total number of days for the scope of the problem(1<=input<=20)

Line 2: The exact number of ingredients that chef uses to cook(1<=input<=20)

Line 3: The space separated ingredient ids.(6<=length(ingredientid)<=20)

Output:- Print the ":" separated used ingredient ids in order of their arrival if the chef cooks on that day
and print "-" if the chef doesn't cook anything on that day.Print the output as  single string.

Example input 1:

5
3
FATOil FATCheese FATEgg FIBERSpinach CARBRice FIBERBeans

Example INPUT 2:

6
3
FATOil:FATCheese FATEgg FIBERSpinach CARBRice FIBERBeans

EXAMPLE OUTPUT 2:

--FATOil:FATCheese:FATEgg--FIBERSpinach:CARBRice:FIBERBeans

EXAMPLE INPUT 3:

12
4
FATOil FIBERSpinach CARBRice FATCheese FIBERBeans FATEgg
FIBERBroccoil CARBPotato CARBCorn FATOlive FIBERCarrot




package com.cfed;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

public class Solution {

public static void main(String args[]) {

String[] ingre = { "FATOil", "FIBERSpinach", "CARBRice", " FATCheese", "FIBERBeans" };
Map<Integer, String> ingred = new HashMap<Integer, String>();
ingred.put(1, "FATOil");
ingred.put(2, "FIBERSpinach");
ingred.put(3, "CARBRice");
ingred.put(4, "FATCheese");
ingred.put(5, "FIBERBeans");
ingred.put(6, "FATEgg");
ingred.put(7, "FIBERBrocoli");
ingred.put(8, "CARBPotato");
ingred.put(9, "CARBCorn");
ingred.put(10, "FATOlive");
ingred.put(11, "FIBERCarrot");
ingred.put(12, "CARBBeetroot");

int days = 6;
findChefCookingDays(days, ingred);

}

List<String> categories = List.of("FAT", "FIBER", "CARB");
static int fatCount = 0;
static int fiberCount = 0;
static int CarbCount = 0;

static List<String> fatList = new ArrayList<>();
static List<String> fiberList = new ArrayList<>();
static List<String> CarbList = new ArrayList<>();

private static void findChefCookingDays(int days, Map<Integer, String> ingred) {

for (Entry<Integer, String> entry : ingred.entrySet()) {

if (entry.getValue().contains("FAT")) {
fatList.add(entry.getValue());
fatCount++;
} else if (entry.getValue().contains("FIBER")) {
fiberList.add(entry.getValue());
fiberCount++;
} else if (entry.getValue().contains("CARB")) {
CarbList.add(entry.getValue());
CarbCount++;
}

int isCook = tryToCook(ingred);
System.out.print(isCook);

}

/*
* for (String ing : ingred) { tryToCook(); if (ing.contains("FAT")) {
* fatCount++; } else if (ing.contains("FIBER")) { fiberCount++; } else if
* (ing.contains("CARB")) { CarbCount++; }
* }
*/

}

private static int tryToCook(Map<Integer, String> ingred) {

if (fatList.size() > 2 || (fatList.size() >= 2 && (fiberList.size() > 0 || CarbList.size() > 0))) {
if (CarbList.size() > 0) {
CarbList.remove(0);
}else if (fiberList.size() > 0) {
fiberList.remove(0);
} else if (fatList.size() > 2) {
fatList.remove(0);
}
fatList.remove(0);
fatList.remove(0);

return 1;

} else if (fiberList.size() >2 ||( fiberList.size() >= 2 && (fatList.size() > 0 || CarbList.size() > 0))) {

if (fatList.size() > 0) {
fatList.remove(0);
} else if (CarbList.size() > 0) {
CarbList.remove(0);
} else if (fiberList.size() > 2) {
fiberList.remove(0);
}
fiberList.remove(0);
fiberList.remove(0);

return 1;

} else if (CarbList.size() > 2 || (CarbList.size() >= 2 && (fatList.size() > 0 || fiberList.size() > 0))) {

if (fatList.size() > 0) {
fatList.remove(0);
} else if (fiberList.size() > 0) {
fiberList.remove(0);
} else if (CarbList.size() > 2) {
CarbList.remove(0);
}
CarbList.remove(0);
CarbList.remove(0);
return 1;
} else {

return 0;
}

}

}

No comments:

Post a Comment

Your feedback may help others !!!

Facebook comments