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

asked on

topping1 challenge

Hi,

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


Map-1 > topping1
prev  |  next  |  chance
Given a map of food keys and topping values, modify and return the map as follows: if the key "ice cream" is present, set its value to "cherry". In all cases, set the key "bread" to have the value "butter".

topping1({"ice cream": "peanuts"}) → {"bread": "butter", "ice cream": "cherry"}
topping1({}) → {"bread": "butter"}
topping1({"pancake": "syrup"}) → {"bread": "butter", "pancake": "syrup"}

i have not understood the above description. please advise.

how
topping1({"ice cream": "peanuts"}) became→ {"bread": "butter", "ice cream": "cherry"}

and

topping1({"pancake": "syrup"}) →became  {"bread": "butter", "pancake": "syrup"}
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany 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
SOLUTION
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> topping1(Map<String, String> map) {
  
  map.put("ice cream", "cherry");
  map.put("bread", "butter");
  return map;
}


/*Yup, should I repeat myself ;)

The description should read

topping1({"ice cream":"whatever", "peanuts":"{whatever}") → 
{"ice cream": "cherry", "peanuts":"whatever", "bread":"butter") 
topping1({}) → {"bread":"butter"}
topping1({"pancake": "syrup"}) → {{"bread": "butter","pancake": "syrup"}

"In all cases, set" means in this case add. Pretty poor describtion.
Best Solution
Assisted Solution

LVL 37
zzynx
Expert Comment 2016-09-30 at 02:52:21ID: 41823215
In all cases, set the key "bread" to have the value "butter".
That means, whether the key "bread" exists or not. If it doesn't exist, add it.*/

Open in new window


failing few tests. please advise
Expected      Run            
topping1({"ice cream": "peanuts"}) → {"bread": "butter", "ice cream": "cherry"}      {"bread": "butter", "ice cream": "cherry"}      OK      
topping1({}) → {"bread": "butter"}      {"bread": "butter", "ice cream": "cherry"}      X      
topping1({"pancake": "syrup"}) → {"bread": "butter", "pancake": "syrup"}      {"bread": "butter", "ice cream": "cherry", "pancake": "syrup"}      X      
topping1({"bread": "dirt", "ice cream": "strawberries"}) → {"bread": "butter", "ice cream": "cherry"}      {"bread": "butter", "ice cream": "cherry"}      OK      
topping1({"salad": "oil", "bread": "jam", "ice cream": "strawberries"}) → {"salad": "oil", "bread": "butter", "ice cream": "cherry"}      {"salad": "oil", "bread": "butter", "ice cream": "cherry"}      OK      
other tests
X      
Avatar of gudii9

ASKER

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 passed all tests. any improvements or alternate approaches?
No. It's perfect as it is.
Avatar of gudii9

ASKER

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 some as no ice cream in first place
What's your question?