We help IT Professionals succeed at work.

topping3 challenge

Medium Priority
232 Views
Last Modified: 2017-07-27
Hi,

How below solution is different from

public Map<String, String> topping3(Map<String, String> map) {
 // Map<String, String> map=null;
  if(map.get("potato")!=null){
    map.put("fries",map.get("potato"));
  }
    if(map.get("salad")!=null){
    map.put("spinatch",map.get("salad"));
  }
  else
  map=map;
  return map;
  
}

Open in new window


below solution

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



please advise.

First one does not pass all tests from
http://codingbat.com/prob/p128461
Expected      Run            
topping3({"potato": "ketchup"}) → {"potato": "ketchup", "fries": "ketchup"}      {"potato": "ketchup", "fries": "ketchup"}      OK      
topping3({"potato": "butter"}) → {"potato": "butter", "fries": "butter"}      {"potato": "butter", "fries": "butter"}      OK      
topping3({"salad": "oil", "potato": "ketchup"}) → {"spinach": "oil", "salad": "oil", "potato": "ketchup", "fries": "ketchup"}      {"salad": "oil", "spinatch": "oil", "potato": "ketchup", "fries": "ketchup"} missing: "spinach": "oil"      X      
topping3({"toast": "butter", "salad": "oil", "potato": "ketchup"}) → {"toast": "butter", "spinach": "oil", "salad": "oil", "potato": "ketchup", "fries": "ketchup"}      {"toast": "butter", "salad": "oil", "spinatch": "oil", "potato": "ketchup", "fries": "ketchup"} missing: "spinach": "oil"      X      
topping3({}) → {}      {}      OK      
topping3({"salad": "pepper", "fries": "salt"}) → {"spinach": "pepper", "salad": "pepper", "fries": "salt"}      {"salad": "pepper", "spinatch": "pepper", "fries": "salt"} missing: "spinach": "pepper"      X      
other tests
OK
Comment
Watch Question

student
CERTIFIED EXPERT
Commented:
You misspelled spinach. You should delete this question.

Author

Commented:
public Map<String, String> topping3(Map<String, String> map) {
  if(map.get("potato")!=null){
    map.put("fries",map.get("potato"));
  }
    if(map.get("salad")!=null){
    map.put("spinach",map.get("salad"));
  }
  else
  map=map;
  return map;
}

Open in new window


above worked all cases
rrzstudent
CERTIFIED EXPERT

Commented:
map=map;

Open in new window

That doesn't do anything.

Author

Commented:
agree better to skip else loop.

if i skip sometimes i get compilation errors saying you should put else

when i should put else when i can skip else?
rrzstudent
CERTIFIED EXPERT

Commented:
if i skip sometimes i get compilation errors saying you should put else
I think that happens when the compiler doesn't know what to return for a particular logical  result.  For example, if we used the following in some method;
if(this){ doSomething(); return thing}  
then we would need to add  
else return another-thing.
because the compiler needs to return something no matter what happens within the method.

Author

Commented:
For example, if we used the following in some method;
if(this){ doSomething(); return thing}  
then we would need to add  
else return another-thing.
because the compiler needs to return something no matter what happens within the method.

not clear on this example. can yo please elaborate may with reference to below code


public Map<String, String> topping2(Map<String, String> map) {
    if(map.get("ice cream")!=null){
    map.put("yogurt", map.get("ice cream"));
  }
  if(map.get("spinach")!=null){
    map.put("spinach", "nuts");
  }
  return map;
}

Open in new window

Author

Commented:
or with reference to below code

public Map<String, String> topping3(Map<String, String> map) {
  if(map.get("potato")!=null){
    map.put("fries",map.get("potato"));
  }
    if(map.get("salad")!=null){
    map.put("spinach",map.get("salad"));
  }
 // else
 // map=map;
  return map;
}

Open in new window

rrzstudent
CERTIFIED EXPERT

Commented:
public Map<String, String> topping2(Map<String, String> map) {
if(map.get("ice cream")!=null || map.get("spinach")!=null){
    if(map.get("ice cream")!=null){
    map.put("yogurt", map.get("ice cream"));
  }
  if(map.get("spinach")!=null){
    map.put("spinach", "nuts");
  }
  return map;
} else {return map;}
}

Open in new window

Thus if the outer if block evaluated to false then the else block would be necessary to provide a return object.

Author

Commented:
public Map<String, String> topping2(Map<String, String> map) {
//public Map < String, String > topping1(Map < String, String > map) {
 
      if (map.containsKey("ice cream")) {
        map.put("yogurt", map.get("ice cream"));
       // map.put("ice cream", map.get("ice cream"));

   }

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

    return map;
}

Open in new window


based on challenge description

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".

I wonder how you chose map.containsKey("spinach") approach rather than  map.get(" approach as challenge talks about the value of a particular key?

public Map<String, String> topping2(Map<String, String> map) {
    if(map.get("ice cream")!=null){
    map.put("yogurt", map.get("ice cream"));
  }
  if(map.get("spinach")!=null){
    map.put("spinach", "nuts");
  }
  return map;
}

Open in new window




what happens in below case

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


if the map do not contain value for key named "ice cream"?

Author

Commented:
Thus if the outer if block evaluated to false then the else block would be necessary to provide a return object.

that make sense otherwise method do not have anything to return

Author

Commented:
topping2({"ice cream": " "})

what hapeens as above if "ice cream" key has  value which is " " or "" or null

please advise
rrzstudent
CERTIFIED EXPERT

Commented:
I wonder how you chose map.containsKey("spinach") approach rather than  map.get(" approach as challenge talks about the value of a particular key?
  The challenge says
if the key "potato" has a value
 That is another way of stating "if the map contains the key potato". Please consider the following demonstration.
import java.util.*;
public class T {
    public static void main(String arg[]){
		Map<String,String> map = new HashMap<String,String>();
		map.put("potato", "ketchup");
		map.put("potato2", "");
		map.put("potato3", null);
		//map.put("potato4", );
		System.out.println(map);
    }
}

Open in new window

The output is
{potato=ketchup, potato3=null, potato2=}

Open in new window

The keys potato, potato2, and potato3 can added to map. Just because we can't imagine an empty String or null as food toppings, doesn't mean that they aren't values. But, if we try to add potato4 which doesn't have a value, then we get
T.java:8: error: illegal start of expression
                map.put("potato4", );
                                   ^
1 error

Open in new window

It would seem that every key in a Map has to have a value.  
if the map do not contain value for key named "ice cream"?
If there is no value, then there is no key named ice cream.  
what hapeens as above if "ice cream" key has  value which is " " or "" or null
then that value is used for the value of the key yogurt.