Solved

topping1 challenge

Posted on 2016-09-29
7
151 Views
Last Modified: 2016-11-09
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"}
0
Comment
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
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 34

Accepted Solution

by:
ste5an earned 250 total points
ID: 41823200
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.
0
 
LVL 37

Assisted Solution

by:zzynx
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

by:gudii9
ID: 41823467
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      
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 7

Author Comment

by:gudii9
ID: 41823472
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?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 41823571
No. It's perfect as it is.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41847353
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
0
 
LVL 37

Expert Comment

by:zzynx
ID: 41847777
What's your question?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question