Solved

# topping1 challenge

Posted on 2016-09-29
134 Views
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({"pancake": "syrup"}) → {"bread": "butter", "pancake": "syrup"}

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

and

topping1({"pancake": "syrup"}) →became  {"bread": "butter", "pancake": "syrup"}
0
Question by:gudii9
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 3

LVL 33

Accepted Solution

ste5an earned 250 total points
ID: 41823200
Yup, should I repeat myself ;)

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

"In all cases, set" means in this case add. Pretty poor describtion.
0

LVL 37

Assisted Solution

zzynx earned 250 total points
ID: 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.
0

LVL 7

Author Comment

ID: 41823467
``````public Map<String, String> topping1(Map<String, String> map) {

map.put("ice cream", "cherry");
return map;
}

/*Yup, should I repeat myself ;)

topping1({"ice cream":"whatever", "peanuts":"{whatever}") →
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.*/
``````

Expected      Run
topping1({"ice cream": "peanuts"}) → {"bread": "butter", "ice cream": "cherry"}      {"bread": "butter", "ice cream": "cherry"}      OK
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
other tests
X
0

LVL 7

Author Comment

ID: 41823472
``````public Map < String, String > topping1(Map < String, String > map) {
if (map.containsKey("ice cream")) {
map.put("ice cream", "cherry");

}
return map;
}
``````
above passed all tests. any improvements or alternate approaches?
0

LVL 37

Expert Comment

ID: 41823571
No. It's perfect as it is.
0

LVL 7

Author Comment

ID: 41847353
``````public Map < String, String > topping1(Map < String, String > map) {
//  if (map.containsKey("ice cream")) {
map.put("ice cream", "cherry");

//  }
return map;
}
``````

above fails some as no ice cream in first place
0

LVL 37

Expert Comment

ID: 41847777
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Simple Linear Regression