Solved

topping2 challenge

Posted on 2016-09-29
13
89 Views
Last Modified: 2016-11-15
Hi,

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


Given a map of food keys and their topping values, modify and return the map as follows: if the key "ice cream" has a value, set that as the value for the key "yogurt" also. If the key "spinach" has a value, change that value to "nuts".

topping2({"ice cream": "cherry"}) → {"yogurt": "cherry", "ice cream": "cherry"}
topping2({"spinach": "dirt", "ice cream": "cherry"}) → {"yogurt": "cherry", "spinach": "nuts", "ice cream": "cherry"}
topping2({"yogurt": "salt"}) → {"yogurt": "salt"}

i have not understood above description. pleASE advise
0
Comment
Question by:gudii9
  • 8
  • 5
13 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 41823223
Rule 1. if the key "ice cream" has a value, (=the condition)
set that as the value for the key "yogurt" also. (=the action to perform)

Rule 2. If the key "spinach" has a value, (= condition)
change that value to "nuts". (=the action to perform)

Tell me, what exactly do you not understand about that?

Rule 1 changes
{"ice cream": "cherry"}
into
{"yogurt": "cherry", "ice cream": "cherry"}
What do you not understand?

Both rules change
{"spinach": "dirt", "ice cream": "cherry"}
into
{"yogurt": "cherry", "spinach": "nuts", "ice cream": "cherry"}
What do you not understand?

For the example
{"yogurt": "salt"}
the condition of rule 1 is NOT fullfilled and the condition of rule 2 is NOT fullfilled, hence
it stays unchanged.
What do you not understand?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41823483
let me re read and think
0
 
LVL 7

Author Comment

by:gudii9
ID: 41847359
topping2({"ice cream": "cherry"}) → {"yogurt": "cherry", "ice cream": "cherry"}
i understand rule 1 now
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 7

Author Comment

by:gudii9
ID: 41847364
Rule 2. If the key "spinach" has a value, (= condition)
change that value to "nuts". (=the action to perform)
based on above i thought
{"spinach": "dirt", "ice cream": "cherry"}
into
{"yogurt": "cherry", "spinach": "nuts", "ice cream": "cherry"}
instead of above it should be as below

{"spinach": "dirt", "ice cream": "cherry"}
into
{"nuts" "dirt"}
0
 
LVL 37

Expert Comment

by:zzynx
ID: 41847780
What's your exact question?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41881009
public Map<String, String> topping2(Map<String, String> map) {
//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 below tests. please advise

Expected      Run            
topping2({"ice cream": "cherry"}) → {"yogurt": "cherry", "ice cream": "cherry"}      {"bread": "butter", "ice cream": "cherry"} missing: "yogurt": "cherry"      X      
topping2({"spinach": "dirt", "ice cream": "cherry"}) → {"yogurt": "cherry", "spinach": "nuts", "ice cream": "cherry"}      {"bread": "butter", "spinach": "dirt", "ice cream": "cherry"} missing: "yogurt": "cherry"      X      
topping2({"yogurt": "salt"}) → {"yogurt": "salt"}      {"yogurt": "salt", "bread": "butter", "ice cream": "cherry"}      X      
topping2({"yogurt": "salt", "bread": "butter"}) → {"yogurt": "salt", "bread": "butter"}      {"yogurt": "salt", "bread": "butter", "ice cream": "cherry"}      X      
topping2({}) → {}      {"bread": "butter", "ice cream": "cherry"}      X      
topping2({"salad": "oil", "ice cream": "air"}) → {"salad": "oil", "yogurt": "air", "ice cream": "air"}      {"salad": "oil", "bread": "butter", "ice cream": "cherry"} missing: "yogurt": "air"      X
0
 
LVL 37

Expert Comment

by:zzynx
ID: 41881746
So, your code - without the commented lines - is this:
public Map<String, String> topping2(Map<String, String> map) {
    map.put("bread", "butter");
    map.put("ice cream", "cherry");
    return map;
}

Open in new window


So, in all cases your result is always {"bread": "butter", "ice cream": "cherry"}
In that case, I think you shouldn't be surprised that some tests fail...

Use what you learned already in other topping challenges.
You're certainly able to write that yourself.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41887007
public Map<String, String> topping2(Map<String, String> map) {
//public Map < String, String > topping1(Map < String, String > map) {
   // map.put("spinach", "nuts");
    if (map.containsKey("spinach")) {
        map.put("spinach", "nuts");

   }
      if (map.containsKey("yogurt")) {
        map.put("yogurt", map.get("ice cream"));

   }
    return map;



}

Open in new window


above fails below. please advise

Expected      Run            
topping2({"ice cream": "cherry"}) → {"yogurt": "cherry", "ice cream": "cherry"}      {"ice cream": "cherry"} missing: "yogurt": "cherry"      X      
topping2({"spinach": "dirt", "ice cream": "cherry"}) → {"yogurt": "cherry", "spinach": "nuts", "ice cream": "cherry"}      {"spinach": "nuts", "ice cream": "cherry"} missing: "yogurt": "cherry"      X      
topping2({"yogurt": "salt"}) → {"yogurt": "salt"}      {"yogurt": null}      X      
topping2({"yogurt": "salt", "bread": "butter"}) → {"yogurt": "salt", "bread": "butter"}      {"yogurt": null, "bread": "butter"}      X      
topping2({}) → {}      {}      OK      
topping2({"salad": "oil", "ice cream": "air"}) → {"salad": "oil", "yogurt": "air", "ice cream": "air"}      {"salad": "oil", "ice cream": "air"} missing: "yogurt": "air"      X      
Your progress graph for this problem

0
 
LVL 7

Author Comment

by:gudii9
ID: 41887010
Rule 1 changes
{"ice cream": "cherry"}
into
{"yogurt": "cherry", "ice cream": "cherry"}
What do you not understand?

Open in new window


so we are additionally adding not updating existing pair?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41887017
public Map<String, String> topping2(Map<String, String> map) {
//public Map < String, String > topping1(Map < String, String > map) {
   // map.put("spinach", "nuts");
    if (map.containsValue("spinach")) {
        map.put("spinach", "nuts");
   }
      if (map.containsKey("ice cream")) {
        map.put("yogurt", map.get("ice cream"));
       // map.put("ice cream", map.get("ice cream"));

   }
    return map;
}

Open in new window


above failing one test case please advise

public Map<String, String> topping2(Map<String, String> map) {
//public Map < String, String > topping1(Map < String, String > map) {
   // map.put("spinach", "nuts");
    if (map.containsValue("spinach")) {
        map.put("spinach", "nuts");
   }
      if (map.containsKey("ice cream")) {
        map.put("yogurt", map.get("ice cream"));
       // map.put("ice cream", map.get("ice cream"));

   }
    return map;
}


0
 
LVL 37

Accepted Solution

by:
zzynx earned 500 total points
ID: 41887443
Rule 1 is correctly implemented.
Rule 2 is not.

Why do you implement the condition
>> If the key "spinach" has a value,
like
>> if (map.containsValue("spinach")) {

containsKey() is not the same as containsValue()!!!
0
 
LVL 7

Author Comment

by:gudii9
ID: 41888157
public Map<String, String> topping2(Map<String, String> map) {
//public Map < String, String > topping1(Map < String, String > map) {
   // map.put("spinach", "nuts");
    if (map.containsKey("spinach")) {
        map.put("spinach", "nuts");
   }
      if (map.containsKey("ice cream")) {
        map.put("yogurt", map.get("ice cream"));
       // map.put("ice cream", map.get("ice cream"));

   }
    return map;
}

Open in new window


above pass all
0
 
LVL 37

Expert Comment

by:zzynx
ID: 41888258
Good!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Plain Text Editor for iPad 6 75
egit plugin on eclipse 8 62
xampp tool 12 48
some keys in my laptop is not working. any suggestion 6 25
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn how to implement Singleton Design Pattern in Java.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

813 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

16 Experts available now in Live!

Get 1:1 Help Now