Solved

topping3 challenge

Posted on 2016-09-29
14
68 Views
Last Modified: 2016-11-15
Hi,

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
0
Comment
Question by:gudii9
  • 6
  • 5
  • 3
14 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 250 total points
ID: 41823224
You have to apply two rules:
Rule 1: if the key "potato" has a value, set that as the value for the key "fries".
Rule 2: If the key "salad" has a value, set that as the value for the key "spinach".
What do you not understand about that?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41823496
public Map < String, String > topping3(Map < String, String > map) {
    // map.put("bread", "butter");
    if (map.containsKey("potato")) {
        map.put("potato", "fries");

    }
    if (map.containsKey("salad")) {
        map.put("salad", "spinach");

    }
    return map;
}

Open in new window


above failse some tests.
Expected      Run            
topping3({"potato": "ketchup"}) → {"fries": "ketchup", "potato": "ketchup"}      {"potato": "fries"} missing: "fries": "ketchup"      X      
topping3({"potato": "butter"}) → {"fries": "butter", "potato": "butter"}      {"potato": "fries"} missing: "fries": "butter"      X      
topping3({"salad": "oil", "potato": "ketchup"}) → {"salad": "oil", "fries": "ketchup", "spinach": "oil", "potato": "ketchup"}      {"salad": "spinach", "potato": "fries"} missing: "fries": "ketchup""spinach": "oil"      X      
topping3({"salad": "oil", "potato": "ketchup", "toast": "butter"}) → {"salad": "oil", "fries": "ketchup", "spinach": "oil", "potato": "ketchup", "toast": "butter"}      {"salad": "spinach", "potato": "fries", "toast": "butter"} missing: "fries": "ketchup""spinach": "oil"      X      
topping3({}) → {}      {}      OK      
topping3({"salad": "pepper", "fries": "salt"}) → {"salad": "pepper", "fries": "salt", "spinach": "pepper"}      {"salad": "spinach", "fries": "salt"} missing: "spinach": "pepper"      X      
other tests
OK      
Your progress graph for this problem

please advise
0
 
LVL 37

Expert Comment

by:zzynx
ID: 41823580
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
if (map.containsKey("potato")) {
        map.put("potato", "fries");   //// <<<<<<<<<<<<<<<<< the key is "potato" in this line, not "fries"
}

Open in new window


and no, the solution is not simply switch "potato" and "fries"...
0
 
LVL 7

Author Comment

by:gudii9
ID: 41823593
ok
0
 
LVL 32

Assisted Solution

by:awking00
awking00 earned 250 total points
ID: 41823683
>>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".<<

Both rules require finding a value for a given key (maybe the get function) and setting the value for a given key (maybe the put function).
0
 
LVL 32

Expert Comment

by:awking00
ID: 41844067
>>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.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41847369
let me try
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Author Comment

by:gudii9
ID: 41881013
still need to try this
0
 
LVL 37

Expert Comment

by:zzynx
ID: 41881748
You don't have to tell us. Just do it.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41887023
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;
}

Open in new window


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

Open in new window

0
 
LVL 37

Expert Comment

by:zzynx
ID: 41887447
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.
0
 
LVL 32

Expert Comment

by:awking00
ID: 41888003
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.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41888135
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;
}

Open in new window


got it. above passed all tests.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 41888259
All right!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:

929 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

21 Experts available now in Live!

Get 1:1 Help Now