Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

topping2 challenge

Hi,

I am working on below challenge.
http://codingbat.com/prob/p196458


Given a map of food keys and their topping values, modify and return the map as follows: if the key "ice cream" has a value, set that as the value for the key "yogurt" also. If the key "spinach" has a value, change that value to "nuts".

topping2({"ice cream": "cherry"}) → {"yogurt": "cherry", "ice cream": "cherry"}
topping2({"spinach": "dirt", "ice cream": "cherry"}) → {"yogurt": "cherry", "spinach": "nuts", "ice cream": "cherry"}
topping2({"yogurt": "salt"}) → {"yogurt": "salt"}

i have not understood above description. pleASE advise
Avatar of zzynx
zzynx
Flag of Belgium image

Rule 1. if the key "ice cream" has a value, (=the condition)
set that as the value for the key "yogurt" also. (=the action to perform)

Rule 2. If the key "spinach" has a value, (= condition)
change that value to "nuts". (=the action to perform)

Tell me, what exactly do you not understand about that?

Rule 1 changes
{"ice cream": "cherry"}
into
{"yogurt": "cherry", "ice cream": "cherry"}
What do you not understand?

Both rules change
{"spinach": "dirt", "ice cream": "cherry"}
into
{"yogurt": "cherry", "spinach": "nuts", "ice cream": "cherry"}
What do you not understand?

For the example
{"yogurt": "salt"}
the condition of rule 1 is NOT fullfilled and the condition of rule 2 is NOT fullfilled, hence
it stays unchanged.
What do you not understand?
Avatar of gudii9

ASKER

let me re read and think
Avatar of gudii9

ASKER

topping2({"ice cream": "cherry"}) → {"yogurt": "cherry", "ice cream": "cherry"}
i understand rule 1 now
Avatar of gudii9

ASKER

Rule 2. If the key "spinach" has a value, (= condition)
change that value to "nuts". (=the action to perform)
based on above i thought
{"spinach": "dirt", "ice cream": "cherry"}
into
{"yogurt": "cherry", "spinach": "nuts", "ice cream": "cherry"}
instead of above it should be as below

{"spinach": "dirt", "ice cream": "cherry"}
into
{"nuts" "dirt"}
What's your exact question?
Avatar of gudii9

ASKER

public Map<String, String> topping2(Map<String, String> map) {
//public Map < String, String > topping1(Map < String, String > map) {
    map.put("bread", "butter");
  //  if (map.containsKey("ice cream")) {
        map.put("ice cream", "cherry");

  //  }
    return map;



}

Open in new window


above fails below tests. please advise

Expected      Run            
topping2({"ice cream": "cherry"}) → {"yogurt": "cherry", "ice cream": "cherry"}      {"bread": "butter", "ice cream": "cherry"} missing: "yogurt": "cherry"      X      
topping2({"spinach": "dirt", "ice cream": "cherry"}) → {"yogurt": "cherry", "spinach": "nuts", "ice cream": "cherry"}      {"bread": "butter", "spinach": "dirt", "ice cream": "cherry"} missing: "yogurt": "cherry"      X      
topping2({"yogurt": "salt"}) → {"yogurt": "salt"}      {"yogurt": "salt", "bread": "butter", "ice cream": "cherry"}      X      
topping2({"yogurt": "salt", "bread": "butter"}) → {"yogurt": "salt", "bread": "butter"}      {"yogurt": "salt", "bread": "butter", "ice cream": "cherry"}      X      
topping2({}) → {}      {"bread": "butter", "ice cream": "cherry"}      X      
topping2({"salad": "oil", "ice cream": "air"}) → {"salad": "oil", "yogurt": "air", "ice cream": "air"}      {"salad": "oil", "bread": "butter", "ice cream": "cherry"} missing: "yogurt": "air"      X
So, your code - without the commented lines - is this:
public Map<String, String> topping2(Map<String, String> map) {
    map.put("bread", "butter");
    map.put("ice cream", "cherry");
    return map;
}

Open in new window


So, in all cases your result is always {"bread": "butter", "ice cream": "cherry"}
In that case, I think you shouldn't be surprised that some tests fail...

Use what you learned already in other topping challenges.
You're certainly able to write that yourself.
Avatar of gudii9

ASKER

public Map<String, String> topping2(Map<String, String> map) {
//public Map < String, String > topping1(Map < String, String > map) {
   // map.put("spinach", "nuts");
    if (map.containsKey("spinach")) {
        map.put("spinach", "nuts");

   }
      if (map.containsKey("yogurt")) {
        map.put("yogurt", map.get("ice cream"));

   }
    return map;



}

Open in new window


above fails below. please advise

Expected      Run            
topping2({"ice cream": "cherry"}) → {"yogurt": "cherry", "ice cream": "cherry"}      {"ice cream": "cherry"} missing: "yogurt": "cherry"      X      
topping2({"spinach": "dirt", "ice cream": "cherry"}) → {"yogurt": "cherry", "spinach": "nuts", "ice cream": "cherry"}      {"spinach": "nuts", "ice cream": "cherry"} missing: "yogurt": "cherry"      X      
topping2({"yogurt": "salt"}) → {"yogurt": "salt"}      {"yogurt": null}      X      
topping2({"yogurt": "salt", "bread": "butter"}) → {"yogurt": "salt", "bread": "butter"}      {"yogurt": null, "bread": "butter"}      X      
topping2({}) → {}      {}      OK      
topping2({"salad": "oil", "ice cream": "air"}) → {"salad": "oil", "yogurt": "air", "ice cream": "air"}      {"salad": "oil", "ice cream": "air"} missing: "yogurt": "air"      X      
Your progress graph for this problem

Avatar of gudii9

ASKER

Rule 1 changes
{"ice cream": "cherry"}
into
{"yogurt": "cherry", "ice cream": "cherry"}
What do you not understand?

Open in new window


so we are additionally adding not updating existing pair?
Avatar of gudii9

ASKER

public Map<String, String> topping2(Map<String, String> map) {
//public Map < String, String > topping1(Map < String, String > map) {
   // map.put("spinach", "nuts");
    if (map.containsValue("spinach")) {
        map.put("spinach", "nuts");
   }
      if (map.containsKey("ice cream")) {
        map.put("yogurt", map.get("ice cream"));
       // map.put("ice cream", map.get("ice cream"));

   }
    return map;
}

Open in new window


above failing one test case please advise

public Map<String, String> topping2(Map<String, String> map) {
//public Map < String, String > topping1(Map < String, String > map) {
   // map.put("spinach", "nuts");
    if (map.containsValue("spinach")) {
        map.put("spinach", "nuts");
   }
      if (map.containsKey("ice cream")) {
        map.put("yogurt", map.get("ice cream"));
       // map.put("ice cream", map.get("ice cream"));

   }
    return map;
}


ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

public Map<String, String> topping2(Map<String, String> map) {
//public Map < String, String > topping1(Map < String, String > map) {
   // map.put("spinach", "nuts");
    if (map.containsKey("spinach")) {
        map.put("spinach", "nuts");
   }
      if (map.containsKey("ice cream")) {
        map.put("yogurt", map.get("ice cream"));
       // map.put("ice cream", map.get("ice cream"));

   }
    return map;
}

Open in new window


above pass all
Good!