Solved

topping1 challenge

Posted on 2016-09-29
7
47 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
  • 3
  • 3
7 Comments
 
LVL 32

Accepted Solution

by:
Stefan Hoffmann earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 7

Author Comment

by:gudii9
Comment Utility
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
Comment Utility
No. It's perfect as it is.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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
Comment Utility
What's your question?
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now