gudii9
asked on
topping3 challenge
Hi,
I am working on below challenge.
http://codingbat.com/prob/p128461
i have not understood above description please advise
I am working on below challenge.
http://codingbat.com/prob/p128461
Given a map of food keys and topping values, modify and return the map as follows: if the key "potato" has a value, set that as the value for the key "fries". If the key "salad" has a value, set that as the value for the key "spinach".
topping3({"potato": "ketchup"}) → {"fries": "ketchup", "potato": "ketchup"}
topping3({"potato": "butter"}) → {"fries": "butter", "potato": "butter"}
topping3({"salad": "oil", "potato": "ketchup"}) → {"salad": "oil", "fries": "ketchup", "spinach": "oil", "potato": "ketchup"}
i have not understood above description please advise
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You mess up "key" with "value"
>> Rule 1: if the key "potato" has a value, set that as the value for the key "fries".
and you write
and no, the solution is not simply switch "potato" and "fries"...
>> Rule 1: if the key "potato" has a value, set that as the value for the key "fries".
and you write
if (map.containsKey("potato")) {
map.put("potato", "fries"); //// <<<<<<<<<<<<<<<<< the key is "potato" in this line, not "fries"
}
and no, the solution is not simply switch "potato" and "fries"...
ASKER
ok
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>if the key "potato" has a value, set that as the value for the key "fries"<<
Define the meaning of relevant parts of the above rule -
if the key "potato" exists, the word "if" pretty much indicates a condition to be met, and if it' met then it will always have a value so get that value (perhaps map.get("potato")).
set that as the value for the key "fries", in other words put that as the value for the key "fries" (perhaps map.put("fries", the value you got for the "potato" key Hope this helps.
Define the meaning of relevant parts of the above rule -
if the key "potato" exists, the word "if" pretty much indicates a condition to be met, and if it' met then it will always have a value so get that value (perhaps map.get("potato")).
set that as the value for the key "fries", in other words put that as the value for the key "fries" (perhaps map.put("fries", the value you got for the "potato" key Hope this helps.
ASKER
let me try
ASKER
still need to try this
You don't have to tell us. Just do it.
ASKER
public Map < String, String > topping3(Map < String, String > map) {
// map.put("bread", "butter");
if (map.containsKey("potato")) {
map.put("fries", map.get("potato"));
}
if (map.containsKey("salad")) {
map.put("salad", map.get("spinach"));
}
return map;
}
above fails below. please advise
Expected Run
topping3({"potato": "ketchup"}) → {"fries": "ketchup", "potato": "ketchup"} {"fries": "ketchup", "potato": "ketchup"} OK
topping3({"potato": "butter"}) → {"fries": "butter", "potato": "butter"} {"fries": "butter", "potato": "butter"} OK
topping3({"salad": "oil", "potato": "ketchup"}) → {"salad": "oil", "fries": "ketchup", "spinach": "oil", "potato": "ketchup"} {"salad": null, "fries": "ketchup", "potato": "ketchup"} missing: "spinach": "oil" X
topping3({"salad": "oil", "potato": "ketchup", "toast": "butter"}) → {"salad": "oil", "fries": "ketchup", "spinach": "oil", "potato": "ketchup", "toast": "butter"} {"salad": null, "fries": "ketchup", "potato": "ketchup", "toast": "butter"} missing: "spinach": "oil" X
topping3({}) → {} {} OK
topping3({"salad": "pepper", "fries": "salt"}) → {"salad": "pepper", "fries": "salt", "spinach": "pepper"} {"salad": null, "fries": "salt"} missing: "spinach": "pepper" X
other tests
OK
Rule 1 is correctly implemented.
Rule 2 is not.
I don't understand why you implement rule 2 differently than rule 1 while they are in fact exactly the same. They only differ in the key values.
Rule 2 is not.
I don't understand why you implement rule 2 differently than rule 1 while they are in fact exactly the same. They only differ in the key values.
Look at your if statement that was implemented correctly. Whatever the map contains is used as the parameter for the map.get function. But that's not what the second if statement does.
ASKER
public Map < String, String > topping3(Map < String, String > map) {
// map.put("bread", "butter");
if (map.containsKey("potato")) {
map.put("fries", map.get("potato"));
}
if (map.containsKey("salad")) {
map.put("spinach", map.get("salad"));
}
return map;
}
got it. above passed all tests.
All right!
ASKER
Open in new window
above failse some tests.
please advise