map challenge

gudii9
gudii9 used Ask the Experts™
on
hi,

http://codingbat.com/prob/p136950

for above challenge
Map-1 > mapAB4
prev  |  next  |  chance

Modify and return the given map as follows: if the keys "a" and "b" have values that have different lengths, then set "c" to have the longer value. If the values exist and have the same length, change them both to the empty string in the map.

mapAB4({"a": "aaa", "b": "bb", "c": "cake"}) → {"a": "aaa", "b": "bb", "c": "aaa"}
mapAB4({"a": "aa", "b": "bbb", "c": "cake"}) → {"a": "aa", "b": "bbb", "c": "bbb"}
mapAB4({"a": "aa", "b": "bbb"}) → {"a": "aa", "b": "bbb", "c": "bbb"}

what are input parameters for the method.

what is the return for it?


"a": "aaa", "b": "bb", "c": "cake"//is these are bunch of string values pretending to be key and value for a map??

please advise on how to understand and approach this challenge
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Scott FellDeveloper & EE Moderator
Fellow 2018
Most Valuable Expert 2013

Commented:
gudii9 - You have multiple server side languages selected and this could also be a client side (javascript) function as well.  Can you help the Experts narrow the scope of your query?  Which specific language are you looking to solve this for?

Author

Commented:
java

Commented:
it should return a modified map with a new value for key "C".

Modify and return the given map as follows: if the keys "a" and "b" have values that have different lengths, then set "c" to have the longer value. If the values exist and have the same length, change them both to the empty string in the map.

Example 1: mapAB4({"a": "aaa", "b": "bb", "c": "cake"})
Value of A  (aaa) has a longer string than the value of B (bb), so replace the "aaa" as value of C.  Result should be {"a": "aaa", "b": "bb", "c": "aaa"}

Example 2: mapAB4({"a": "aa", "b": "bbb", "c": "cake"})
Value of B (bbb) has a longer string than the value of A (aa), so replace the "bbb" as value of C.  Result should be {"a": "aa", "b": "bbb", "c": "bbb"}

Example 3: mapAB4({"a": "aa", "b": "bbb"})
Value of B (bbb) has a longer string than the value of A (aa) and C is not present, so add C with the "bbb" as value.  Result should be {"a": "aa", "b": "bbb", "c": "bbb"}

Hope the logic is clear now. Its just to check the length of values of key A and key B, then set the value of C. Simple.
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
If the values exist and have the same length, change them both to the empty string in the map.

what is the meanin of above?

Author

Commented:
mapAB4({"a": "aaa"}) → {"a": "aaa"}      null      X      
mapAB4({"b": "bbb"}) → {"b": "bbb"}      null      X      
mapAB4({"a": "aaa", "b": "bbb", "c": "cake"}) → {"a": "", "b": "", "c": "cake"}

not clear on above too?

Author

Commented:
mapAB4({"a": "a", "b": "", "c": "cake"}) → {"a": "a", "b": "", "c": "a"}      null      X      
mapAB4({"c": "cat", "d": "dog"}) → {"c": "cat", "d": "dog"}
not clear on above also?
Senior Engineer
Commented:
Hi.

If you're not sure of a design, a great way to approach it is to write a test. The test will help you to determine what you need as inputs to a method and what it should return so you can evaluate whether it meets the acceptance criteria. In this case, your problem statement is a pretty good clue as to the starting point: "Modify and return the given map". So our test class might start off like this:

public class MapChallengeTest {
	private MapChallenge sut = new MapChallenge();

	@Test
	public void given_valueForKeyAIsLongerThanValueForKeyB_thenReplaceCwithIt() {
		String inputA = "aaa";
		String inputB = "bb";

		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		input.put(C, "cake");
		
		Map<String, String> output = sut.evaluate(input);

		Assert.assertTrue("Output map is not the input map", output == input);
		Assert.assertEquals("Value for key A should be unmodified", inputA, output.get(A));
		Assert.assertEquals("Value for key B should be unmodified", inputB, output.get(B));
		Assert.assertEquals("Value for key C should be same as key B", input.get(A), output.get(C));
	}
}

Open in new window


the implementation has a single method evaluate which takes a Map<String, String> and returns a Map<String, String>; The assertions verify that the input object and output object are the same (since the requirements say "modify" the map), the values for keys "a" and "b" have not changed but the value for key "c" is now the same as the value for key "a" because it is longer than the value for key "b". I've added these as messages in the Asserts because I like my tests to be clear about why they failed. We can keep doing this, writing a test and a changing the implementation for each scenario until we arrive at something like the attached.

We were able to implement all the scenarios with our initial method signature of Map evaluate(Map). One could argue that the implementation would be more "generic" and "reusable" if we also passed the 3 keys instead of assuming "a", "b" and "c" in the method so the signature could be Map evaluate(Map, String, String, String). While technically true, that could also be characterized as gold plating because this was not strictly a requirement we were given. But I wouldn't frown on it very hard. There are other scenarios that could be tested, though the problem statement doesn't specify how to handle these and they technically don't change the method signature so perhaps aren't important to answering your question -- what are the input parameters for the method and what does it return:
  • the input Map is null -- return null or throw an exception?
  • key A is not in the map or is null -- is null equivalent to a string of length 0? continue or throw exception?
  • key B is not in the map or is null -- same problem as for key A
MapChallengeTest.java
MapChallenge.java

Author

Commented:
public Map<String, String> mapAB4(Map<String, String> map) {
   if( map.get("a")!=null && map.get("b")!=null && map.get("a").length()> map.get("b").length() ){
    map.put("c",map.get("a"));
    
  }
    if( map.get("a")!=null && map.get("b")!=null && map.get("b").length()> map.get("a").length() ){
    map.put("c",map.get("b"));
    
  }
   if( map.get("a")!=null && map.get("b")!=null && map.get("a").equals(map.get("b")) ){
    map.put("a","");
    map.put("b","");
  }
  
  if( map.get("a")!=null && map.get("b")!=null && map.get("a").equals(map.get("b")) ){
    map.put("a","");
    map.put("b","");
  }
  return map;
}

Open in new window


Expected      Run            
mapAB4({"a": "aaa", "b": "bb", "c": "cake"}) → {"a": "aaa", "b": "bb", "c": "aaa"}      {"a": "aaa", "b": "bb", "c": "aaa"}      OK      
mapAB4({"a": "aa", "b": "bbb", "c": "cake"}) → {"a": "aa", "b": "bbb", "c": "bbb"}      {"a": "aa", "b": "bbb", "c": "bbb"}      OK      
mapAB4({"a": "aa", "b": "bbb"}) → {"a": "aa", "b": "bbb", "c": "bbb"}      {"a": "aa", "b": "bbb", "c": "bbb"}      OK      
mapAB4({"a": "aaa"}) → {"a": "aaa"}      {"a": "aaa"}      OK      
mapAB4({"b": "bbb"}) → {"b": "bbb"}      {"b": "bbb"}      OK      
mapAB4({"a": "aaa", "b": "bbb", "c": "cake"}) → {"a": "", "b": "", "c": "cake"}      {"a": "aaa", "b": "bbb", "c": "cake"}      X      
mapAB4({"a": "a", "b": "b", "c": "cake"}) → {"a": "", "b": "", "c": "cake"}      {"a": "a", "b": "b", "c": "cake"}      X      
mapAB4({"a": "", "b": "b", "c": "cake"}) → {"a": "", "b": "b", "c": "b"}      {"a": "", "b": "b", "c": "b"}      OK      
mapAB4({"a": "a", "b": "", "c": "cake"}) → {"a": "a", "b": "", "c": "a"}      {"a": "a", "b": "", "c": "a"}      OK      
mapAB4({"c": "cat", "d": "dog"}) → {"c": "cat", "d": "dog"}      {"c": "cat", "d": "dog"}      OK      
mapAB4({"ccc": "ccc"}) → {"ccc": "ccc"}      {"ccc": "ccc"}      OK      
mapAB4({"c": "a", "d": "b"}) → {"c": "a", "d": "b"}      {"c": "a", "d": "b"}      OK      
mapAB4({}) → {}      {}      OK      
mapAB4({"a": "", "z": "z"}) → {"a": "", "z": "z"}      {"a": "", "z": "z"}      OK      
mapAB4({"b": "", "z": "z"}) → {"b": "", "z": "z"}      {"b": "", "z": "z"}      OK      
other tests
OK      
'
i tried as above and failing few tests. please advise

Author

Commented:
package app;

import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;

public class MapChallengeTest {
	private static final String C = "c";
	private static final String B = "b";
	private static final String A = "a";
	private MapChallenge sut = new MapChallenge();

	@Test
	public void given_valueForKeyAIsLongerThanValueForKeyB_thenReplaceCwithIt() {
		String inputA = "aaa";
		String inputB = "bb";

		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		input.put(C, "cake");
		
		Map<String, String> output = sut.evaluate(input);

		Assert.assertTrue("Output map is not the input map", output == input);
		Assert.assertEquals("Value for key A should be unmodified", inputA, output.get(A));
		Assert.assertEquals("Value for key B should be unmodified", inputB, output.get(B));
		Assert.assertEquals("Value for key C should be same as key B", input.get(A), output.get(C));
	}
	
	@Test
	public void given_valueforKeyBisLongerThanValueForKeyA_thenReplaceKeyCwithIt() {
		String inputA = "aa";
		String inputB = "bbb";
		
		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		input.put(C, "cake");
		
		Map<String, String> output = sut.evaluate(input);

		Assert.assertTrue("Output map is not the input map", output == input);
		Assert.assertEquals("Value for key A should be unmodified", inputA, output.get(A));
		Assert.assertEquals("Value for key B should be unmodified", inputB, output.get(B));
		Assert.assertEquals("Value for key C should be same as key B", input.get(B), output.get(C));
	}
	
	@Test
	public void given_valueforKeyBisLonger_thenCreateKeyCWithIt() {
		String inputA = "aa";
		String inputB = "bbb";

		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		
		Map<String, String> output = sut.evaluate(input);

		Assert.assertTrue("Output map is not the input map", output == input);
		Assert.assertEquals("Value for key A should be unmodified", inputA, output.get(A));
		Assert.assertEquals("Value for key B should be unmodified", inputB, output.get(B));
		Assert.assertEquals("Value for key C should be same as key A", input.get(B), output.get(C));
	}
	
	@Test
	public void given_valuesforBothKeysExistAndAreSameLength_thenReplaceBothWithEmptyString() {
		String inputA = "aa";
		String inputB = "bb";
		
		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		
		Map<String, String> output = sut.evaluate(input);

		Assert.assertTrue("Output map is not the input map", output == input);
		Assert.assertEquals("Value for key A should be empty string", "", output.get(A));
		Assert.assertEquals("Value for key B should be empty string", "", output.get(B));
		Assert.assertFalse("Value for key C should not exist", output.containsKey(C));
	}
}

Open in new window


when i run the test case you sent i am getting compilation errors at line 13, 25, 43, 60 etc
please advise

Author

Commented:
if (cVal != null)
			if (input.containsKey("c")) {
				input.replace("c", cVal);
			} else { 
				input.put("c", cVal);
			}

Open in new window


what is meaning of above code

how input.get("key") different from .containsKey("key") method. please advise
Commented:
how input.get("key") different from .containsKey("key") method. please advise

This seems irrelevent question to the original question. However, please have a look on the API doc for both.

1. https://docs.oracle.com/javase/7/docs/api/java/util/Map.html#get(java.lang.Object)
2. https://docs.oracle.com/javase/7/docs/api/java/util/Map.html#containsKey(java.lang.Object)

Author

Commented:
package app;

import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;

public class MapChallengeTest {
	private static final String C = "c";
	private static final String B = "b";
	private static final String A = "a";
	private MapChallenge sut = new MapChallenge();

	@Test
	public void given_valueForKeyAIsLongerThanValueForKeyB_thenReplaceCwithIt() {
		String inputA = "aaa";
		String inputB = "bb";

		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		input.put(C, "cake");
		
		Map<String, String> output = sut.evaluate(input);

		Assert.assertTrue("Output map is not the input map", output == input);
		Assert.assertEquals("Value for key A should be unmodified", inputA, output.get(A));
		Assert.assertEquals("Value for key B should be unmodified", inputB, output.get(B));
		Assert.assertEquals("Value for key C should be same as key B", input.get(A), output.get(C));
	}
	
	@Test
	public void given_valueforKeyBisLongerThanValueForKeyA_thenReplaceKeyCwithIt() {
		String inputA = "aa";
		String inputB = "bbb";
		
		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		input.put(C, "cake");
		
		Map<String, String> output = sut.evaluate(input);

		Assert.assertTrue("Output map is not the input map", output == input);
		Assert.assertEquals("Value for key A should be unmodified", inputA, output.get(A));
		Assert.assertEquals("Value for key B should be unmodified", inputB, output.get(B));
		Assert.assertEquals("Value for key C should be same as key B", input.get(B), output.get(C));
	}
	
	@Test
	public void given_valueforKeyBisLonger_thenCreateKeyCWithIt() {
		String inputA = "aa";
		String inputB = "bbb";

		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		
		Map<String, String> output = sut.evaluate(input);

		Assert.assertTrue("Output map is not the input map", output == input);
		Assert.assertEquals("Value for key A should be unmodified", inputA, output.get(A));
		Assert.assertEquals("Value for key B should be unmodified", inputB, output.get(B));
		Assert.assertEquals("Value for key C should be same as key A", input.get(B), output.get(C));
	}
	
	@Test
	public void given_valuesforBothKeysExistAndAreSameLength_thenReplaceBothWithEmptyString() {
		String inputA = "aa";
		String inputB = "bb";
		
		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		
		Map<String, String> output = sut.evaluate(input);

		Assert.assertTrue("Output map is not the input map", output == input);
		Assert.assertEquals("Value for key A should be empty string", "", output.get(A));
		Assert.assertEquals("Value for key B should be empty string", "", output.get(B));
		Assert.assertFalse("Value for key C should not exist", output.containsKey(C));
	}
}

Open in new window

package app;

import java.util.Map;

public class MapChallenge {

	public Map<String, String> evaluate(Map<String, String> input) {
		String aVal = input.get("a");
		String bVal = input.get("b");
		String cVal = null;
		if (aVal.length() > bVal.length()) {
			cVal = aVal;
		} else if (bVal.length() > aVal.length()) {
			cVal = bVal;
		} else {
			input.replace("a", "");
			input.replace("b", "");
		}
		
		if (cVal != null)
			if (input.containsKey("c")) {
				input.replace("c", cVal);
			} else { 
				input.put("c", cVal);
			}
		return input;
		
		
	}

}

Open in new window

looks like import of MapChallenge missed earlier. now i imported and passes all tests

Author

Commented:
if( map.get("a")!=null && map.get("b")!=null && map.get("a").equals(map.get("b")) ){
    map.put("a","");
    map.put("b","");
  }
  

Open in new window


above passes all tests

Author

Commented:
package snippet;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

public class MapChallengeTest {
	private MapChallenge sut = new MapChallenge();

	@Test
	public void given_valueForKeyAIsLongerThanValueForKeyB_thenReplaceCwithIt() {
		String inputA = "aaa";
		String inputB = "bb";

		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		input.put(C, "cake");
		
		Map<String, String> output = sut.evaluate(input);

		Assert.assertTrue("Output map is not the input map", output == input);
		Assert.assertEquals("Value for key A should be unmodified", inputA, output.get(A));
		Assert.assertEquals("Value for key B should be unmodified", inputB, output.get(B));
		Assert.assertEquals("Value for key C should be same as key B", input.get(A), output.get(C));
	}
}

Open in new window

line 9 gives below error.

Multiple markers at this line
      - MapChallenge cannot be resolved
       to a type
      - MapChallenge cannot be resolved
       to a type


can you please advise what i have write for MapChallenge?

Author

Commented:
i just saw attachments and that passed all tests

so write a test to understand design to meet acceptance criteria. interesting to know this and learn using junit. probably i should write unit tests for all the challenges to iron out this concept in my mind
junitSuccess.png

Author

Commented:
     if (cVal != null)
                  if (input.containsKey("c")) {
                        input.replace("c", cVal);
                  } else {
                        input.put("c", cVal);
                  }

above not given in challenge description right? please advise

package snippet;

import java.util.Map;

public class MapChallenge {

      public Map<String, String> evaluate(Map<String, String> input) {
            String aVal = input.get("a");
            String bVal = input.get("b");
            String cVal = null;
            if (aVal.length() > bVal.length()) {
                  cVal = aVal;
            } else if (bVal.length() > aVal.length()) {
                  cVal = bVal;
            } else {
                  input.replace("a", "");
                  input.replace("b", "");
            }
            
            if (cVal != null)
                  if (input.containsKey("c")) {
                        input.replace("c", cVal);
                  } else {
                        input.put("c", cVal);
                  }
            return input;
            
            
      }

}



other class
package snippet;

import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;

public class MapChallengeTest {
	private static final String C = "c";
	private static final String B = "b";
	private static final String A = "a";
	private MapChallenge sut = new MapChallenge();

	@Test
	public void given_valueForKeyAIsLongerThanValueForKeyB_thenReplaceCwithIt() {
		String inputA = "aaa";
		String inputB = "bb";

		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		input.put(C, "cake");
		
		Map<String, String> output = sut.evaluate(input);

		Assert.assertTrue("Output map is not the input map", output == input);
		Assert.assertEquals("Value for key A should be unmodified", inputA, output.get(A));
		Assert.assertEquals("Value for key B should be unmodified", inputB, output.get(B));
		Assert.assertEquals("Value for key C should be same as key B", input.get(A), output.get(C));
	}
	
	@Test
	public void given_valueforKeyBisLongerThanValueForKeyA_thenReplaceKeyCwithIt() {
		String inputA = "aa";
		String inputB = "bbb";
		
		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		input.put(C, "cake");
		
		Map<String, String> output = sut.evaluate(input);

		Assert.assertTrue("Output map is not the input map", output == input);
		Assert.assertEquals("Value for key A should be unmodified", inputA, output.get(A));
		Assert.assertEquals("Value for key B should be unmodified", inputB, output.get(B));
		Assert.assertEquals("Value for key C should be same as key B", input.get(B), output.get(C));
	}
	
	@Test
	public void given_valueforKeyBisLonger_thenCreateKeyCWithIt() {
		String inputA = "aa";
		String inputB = "bbb";

		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		
		Map<String, String> output = sut.evaluate(input);

		Assert.assertTrue("Output map is not the input map", output == input);
		Assert.assertEquals("Value for key A should be unmodified", inputA, output.get(A));
		Assert.assertEquals("Value for key B should be unmodified", inputB, output.get(B));
		Assert.assertEquals("Value for key C should be same as key A", input.get(B), output.get(C));
	}
	
	@Test
	public void given_valuesforBothKeysExistAndAreSameLength_thenReplaceBothWithEmptyString() {
		String inputA = "aa";
		String inputB = "bb";
		
		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		
		Map<String, String> output = sut.evaluate(input);

		Assert.assertTrue("Output map is not the input map", output == input);
		Assert.assertEquals("Value for key A should be empty string", "", output.get(A));
		Assert.assertEquals("Value for key B should be empty string", "", output.get(B));
		Assert.assertFalse("Value for key C should not exist", output.containsKey(C));
	}
}

Open in new window

ozo
Most Valuable Expert 2014
Top Expert 2015
Commented:
public Map<String, String> mapAB4(Map<String, String> map) {
  if( map.get("a")==null || map.get("b") == null ){ return map; }
  if( map.get("a").length()> map.get("b").length() ){
    map.put("c",map.get("a"));
  }else if( map.get("b").length()> map.get("a").length() ){
    map.put("c",map.get("b"));
  }else{
    map.put("a","");
    map.put("b","");
  }
  return map;
}

Author

Commented:
public Map<String, String> mapAB4(Map<String, String> map) {
   if( map.get("a")!=null && map.get("b")!=null && map.get("a").length()> map.get("b").length() ){
    map.put("c",map.get("a"));
    
  }
    if( map.get("a")!=null && map.get("b")!=null && map.get("b").length()> map.get("a").length() ){
    map.put("c",map.get("b"));
    
  }
   if( map.get("a")!=null && map.get("b")!=null && map.get("a").equals(map.get("b")) ){
    map.put("a","");
    map.put("b","");
  }
  
  
  if( map.get("a")!=null && map.get("b")!=null && map.get("a").length()==map.get("b").length() ){
    map.put("a","");
    map.put("b","");
  }
  return map;
}

Open in new window


do i need both below lines

   if( map.get("a")!=null && map.get("b")!=null && map.get("a").equals(map.get("b")) ){
    map.put("a","");
    map.put("b","");
  }
 
 
  if( map.get("a")!=null && map.get("b")!=null && map.get("a").length()==map.get("b").length() ){
    map.put("a","");
    m


what is difference between above two lines one checks value same other checks value length same?

Author

Commented:
if( map.get("a")!=null && map.get("b")!=null && map.get("a").equals(map.get("b")) ){
    map.put("a","");
    map.put("b","");
  }


even i remove above i passed all tests
public Map<String, String> mapAB4(Map<String, String> map) {
   if( map.get("a")!=null && map.get("b")!=null && map.get("a").length()> map.get("b").length() ){
    map.put("c",map.get("a"));
   
  }
    if( map.get("a")!=null && map.get("b")!=null && map.get("b").length()> map.get("a").length() ){
    map.put("c",map.get("b"));
   
  }

 
 
  if( map.get("a")!=null && map.get("b")!=null && map.get("a").length()==map.get("b").length() ){
    map.put("a","");
    map.put("b","");
  }
  return map;
}
Expected      Run            
mapAB4({"a": "aaa", "b": "bb", "c": "cake"}) → {"a": "aaa", "b": "bb", "c": "aaa"}      {"a": "aaa", "b": "bb", "c": "aaa"}      OK      
mapAB4({"a": "aa", "b": "bbb", "c": "cake"}) → {"a": "aa", "b": "bbb", "c": "bbb"}      {"a": "aa", "b": "bbb", "c": "bbb"}      OK      
mapAB4({"a": "aa", "b": "bbb"}) → {"a": "aa", "b": "bbb", "c": "bbb"}      {"a": "aa", "b": "bbb", "c": "bbb"}      OK      
mapAB4({"a": "aaa"}) → {"a": "aaa"}      {"a": "aaa"}      OK      
mapAB4({"b": "bbb"}) → {"b": "bbb"}      {"b": "bbb"}      OK      
mapAB4({"a": "aaa", "b": "bbb", "c": "cake"}) → {"a": "", "b": "", "c": "cake"}      {"a": "", "b": "", "c": "cake"}      OK      
mapAB4({"a": "a", "b": "b", "c": "cake"}) → {"a": "", "b": "", "c": "cake"}      {"a": "", "b": "", "c": "cake"}      OK      
mapAB4({"a": "", "b": "b", "c": "cake"}) → {"a": "", "b": "b", "c": "b"}      {"a": "", "b": "b", "c": "b"}      OK      
mapAB4({"a": "a", "b": "", "c": "cake"}) → {"a": "a", "b": "", "c": "a"}      {"a": "a", "b": "", "c": "a"}      OK      
mapAB4({"c": "cat", "d": "dog"}) → {"c": "cat", "d": "dog"}      {"c": "cat", "d": "dog"}      OK      
mapAB4({"ccc": "ccc"}) → {"ccc": "ccc"}      {"ccc": "ccc"}      OK      
mapAB4({"c": "a", "d": "b"}) → {"c": "a", "d": "b"}      {"c": "a", "d": "b"}      OK      
mapAB4({}) → {}      {}      OK      
mapAB4({"a": "", "z": "z"}) → {"a": "", "z": "z"}      {"a": "", "z": "z"}      OK      
mapAB4({"b": "", "z": "z"}) → {"b": "", "z": "z"}      {"b": "", "z": "z"}      OK      
other tests
OK      
so just value lenghts important not actual content

Author

Commented:
package snippet;

import java.util.Map;

public class MapChallenge {

	public Map<String, String> evaluate(Map<String, String> input) {
		String aVal = input.get("a");
		String bVal = input.get("b");
		String cVal = null;
		if (aVal.length() > bVal.length()) {
			cVal = aVal;
		} else if (bVal.length() > aVal.length()) {
			cVal = bVal;
		} else {
			input.replace("a", "");
			input.replace("b", "");
		}
		
		/*if (cVal != null)
			if (input.containsKey("c")) {
				input.replace("c", cVal);
			} else { 
				input.put("c", cVal);
			}*/
		return input;
		
		
	}

}

Open in new window


if i commenr above i am failing below
Assert.assertEquals("Value for key C should be same as key B", input.get(A), output.get(C));//line 30
Assert.assertEquals("Value for key C should be same as key B", input.get(B), output.get(C));//line 48
Assert.assertEquals("Value for key C should be same as key A", input.get(B), output.get(C));//line 65

in first 3 methods? i wonder why they are failing


package snippet;

import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;

public class MapChallengeTest {
	private static final String C = "c";
	private static final String B = "b";
	private static final String A = "a";
	private MapChallenge sut = new MapChallenge();

	@Test
	public void given_valueForKeyAIsLongerThanValueForKeyB_thenReplaceCwithIt() {
		String inputA = "aaa";
		String inputB = "bb";

		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		input.put(C, "cake");
		
		Map<String, String> output = sut.evaluate(input);

		Assert.assertTrue("Output map is not the input map", output == input);
		Assert.assertEquals("Value for key A should be unmodified", inputA, output.get(A));
		Assert.assertEquals("Value for key B should be unmodified", inputB, output.get(B));
		Assert.assertEquals("Value for key C should be same as key B", input.get(A), output.get(C));
	}
	
	@Test
	public void given_valueforKeyBisLongerThanValueForKeyA_thenReplaceKeyCwithIt() {
		String inputA = "aa";
		String inputB = "bbb";
		
		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		input.put(C, "cake");
		
		Map<String, String> output = sut.evaluate(input);

		Assert.assertTrue("Output map is not the input map", output == input);
		Assert.assertEquals("Value for key A should be unmodified", inputA, output.get(A));
		Assert.assertEquals("Value for key B should be unmodified", inputB, output.get(B));
		Assert.assertEquals("Value for key C should be same as key B", input.get(B), output.get(C));
	}
	
	@Test
	public void given_valueforKeyBisLonger_thenCreateKeyCWithIt() {
		String inputA = "aa";
		String inputB = "bbb";

		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		
		Map<String, String> output = sut.evaluate(input);

		Assert.assertTrue("Output map is not the input map", output == input);
		Assert.assertEquals("Value for key A should be unmodified", inputA, output.get(A));
		Assert.assertEquals("Value for key B should be unmodified", inputB, output.get(B));
		Assert.assertEquals("Value for key C should be same as key A", input.get(B), output.get(C));
	}
	
	@Test
	public void given_valuesforBothKeysExistAndAreSameLength_thenReplaceBothWithEmptyString() {
		String inputA = "aa";
		String inputB = "bb";
		
		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		
		Map<String, String> output = sut.evaluate(input);

		Assert.assertTrue("Output map is not the input map", output == input);
		Assert.assertEquals("Value for key A should be empty string", "", output.get(A));
		Assert.assertEquals("Value for key B should be empty string", "", output.get(B));
		Assert.assertFalse("Value for key C should not exist", output.containsKey(C));
	}
}

Open in new window

Author

Commented:
package snippet;

import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;

public class MapChallengeTest {
	private static final String C = "c";
	private static final String B = "b";
	private static final String A = "a";
	private MapChallenge sut = new MapChallenge();

	@Test
	public void given_valueForKeyAIsLongerThanValueForKeyB_thenReplaceCwithIt() {
		String inputA = "aaa";
		String inputB = "bb";

		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		input.put(C, "cake");
		
		Map<String, String> output = sut.evaluate(input);

		Assert.assertTrue("Output map is not the input map", output == input);
		Assert.assertEquals("Value for key A should be unmodified", inputA, output.get(A));
		Assert.assertEquals("Value for key B should be unmodified", inputB, output.get(B));
		Assert.assertEquals("Value for key C should be same as key B", input.get(A), output.get(C));
	}
	
	@Test
	public void given_valueforKeyBisLongerThanValueForKeyA_thenReplaceKeyCwithIt() {
		String inputA = "aa";
		String inputB = "bbb";
		
		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		input.put(C, "cake");
		
		Map<String, String> output = sut.evaluate(input);

		Assert.assertTrue("Output map is not the input map", output == input);
		Assert.assertEquals("Value for key A should be unmodified", inputA, output.get(A));
		Assert.assertEquals("Value for key B should be unmodified", inputB, output.get(B));
		Assert.assertEquals("Value for key C should be same as key B", input.get(B), output.get(C));
	}
	
	@Test
	public void given_valueforKeyBisLonger_thenCreateKeyCWithIt() {
		String inputA = "aa";
		String inputB = "bbb";

		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		
		Map<String, String> output = sut.evaluate(input);

		Assert.assertTrue("Output map is not the input map", output == input);
		Assert.assertEquals("Value for key A should be unmodified", inputA, output.get(A));
		Assert.assertEquals("Value for key B should be unmodified", inputB, output.get(B));
		Assert.assertEquals("Value for key C should be same as key A", input.get(B), output.get(C));
	}
	
	@Test
	public void given_valuesforBothKeysExistAndAreSameLength_thenReplaceBothWithEmptyString() {
		String inputA = "aa";
		String inputB = "bb";
		
		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		
		Map<String, String> output = sut.evaluate(input);

		Assert.assertTrue("Output map is not the input map", output == input);
		Assert.assertEquals("Value for key A should be empty string", "", output.get(A));
		Assert.assertEquals("Value for key B should be empty string", "", output.get(B));
		Assert.assertFalse("Value for key C should not exist", output.containsKey(C));
	}
}

Open in new window


how to decide order of above methods 1, 2, 3, 4 can i shuffle method 4(given_valuesforBothKeysExistAndAreSameLength_thenReplaceBothWithEmptyString)  into place of method 3(given_valueforKeyBisLonger_thenCreateKeyCWithIt)?
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
so just value lenghts important not actual content
Modify and return the given map as follows: if the keys "a" and "b" have values that have different lengths, then set "c" to have the longer value. If the values exist and have the same length, change them both to the empty string in the map.
The only place it talks about any actual contents is when "c" is set to have the longer value

Author

Commented:
@Test
	public void given_valueforKeyBisLongerThanValueForKeyA_thenReplaceKeyCwithIt() {
		String inputA = "aa";
		String inputB = "bbb";
		
		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		input.put(C, "cake");
		
		Map<String, String> output = sut.evaluate(input);

		Assert.assertTrue("Output map is not the input map", output == input);
		Assert.assertEquals("Value for key A should be unmodified", inputA, output.get(A));
		Assert.assertEquals("Value for key B should be unmodified", inputB, output.get(B));
		Assert.assertEquals("Value for key C should be same as key B", input.get(B), output.get(C));
	}
	
	@Test
	public void given_valueforKeyBisLonger_thenCreateKeyCWithIt() {
		String inputA = "aa";
		String inputB = "bbb";

		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		
		Map<String, String> output = sut.evaluate(input);

		Assert.assertTrue("Output map is not the input map", output == input);
		Assert.assertEquals("Value for key A should be unmodified", inputA, output.get(A));
		Assert.assertEquals("Value for key B should be unmodified", inputB, output.get(B));
		Assert.assertEquals("Value for key C should be same as key A", input.get(B), output.get(C));
	}

Open in new window


what is difference between above method 2 and method 3.
to me both looks same except c has cake there as value?Description did not talk about it

Modify and return the given map as follows: if the keys "a" and "b" have values that have different lengths, then set "c" to have the longer value. If the values exist and have the same length, change them both to the empty string in the map.
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
can i shuffle method
Can any of the methods affect the conditions required by any of the other methods?
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
what is difference between above method 2 and method 3.
to me both looks same except c has cake there as value?
mapAB4({"a": "aa", "b": "bbb", "c": "cake"}) → {"a": "aa", "b": "bbb", "c": "bbb"}
mapAB4({"a": "aa", "b": "bbb"}) → {"a": "aa", "b": "bbb", "c": "bbb"}
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
what is difference between above method 2 and method 3.
This is a question. Questions should end with "?"

to me both looks same except c has cake there as value?
This is a statement, not a question.  Statements should end "."

Author

Commented:
mapAB4({"a": "aa", "b": "bbb"}) → {"a": "aa", "b": "bbb", "c": "bbb"}

above is not clear to me.
where in the challenge they metioned about it like if  'c' not there  as key add 'c' as key and add  value to it by taking maximum length out of 'a'  key value and 'b' key value?


Modify and return the given map as follows: if the keys "a" and "b" have values that have different lengths, then set "c" to have the longer value. If the values exist and have the same length, change them both to the empty string in the map.

Author

Commented:
how above is relevant to below commented code?
please advise
package snippet;

import java.util.Map;

public class MapChallenge {

	public Map<String, String> evaluate(Map<String, String> input) {
		String aVal = input.get("a");
		String bVal = input.get("b");
		String cVal = null;
		if (aVal.length() > bVal.length()) {
			cVal = aVal;
		} else if (bVal.length() > aVal.length()) {
			cVal = bVal;
		} else {
			input.replace("a", "");
			input.replace("b", "");
		}
		
		/*if (cVal != null)
			if (input.containsKey("c")) {
				input.replace("c", cVal);
			} else { 
				input.put("c", cVal);
			}*/
		return input;
		
		
	}

}

Open in new window

Author

Commented:
why we are passing cake as input as well in 2nd method
@Test
	public void given_valueforKeyBisLongerThanValueForKeyA_thenReplaceKeyCwithIt() {
		String inputA = "aa";
		String inputB = "bbb";
		
		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		input.put(C, "cake");

Open in new window

Author

Commented:
below challenge only says 2 sentences one if a and b values equal other if not equal

1.
Modify and return the given map as follows: if the keys "a" and "b" have values that have different lengths, then set "c" to have the longer value.

2.
If the values exist and have the same length, change them both to the empty string in the map.
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
if the keys "a" and "b" have values that have different lengths, then set "c" to have the longer value
mapAB4({"a": "aa", "b": "bbb"}) → {"a": "aa", "b": "bbb", "c": "bbb"}
the keys "a" and "b" have values "aa" and "bbb"
"aa" and "bbb" are values that have different lengths
the longer value is "bbb"
set "c" to have the longer value
→ set "c" to have the value "bbb"


mapAB4({"a": "aa", "b": "bbb", "c": "cake"}) → {"a": "aa", "b": "bbb", "c": "bbb"}
the keys "a" and "b" have values "aa" and "bbb"
"aa" and "bbb" are values that have different lengths
the longer value is "bbb"
set "c" to have the longer value
→ set "c" to have the value "bbb"

If the values exist and have the same length, change them both to the empty string in the map.
"aa" and "bbb" exist, but do not have the same length.

Author

Commented:
/*if (cVal != null)
			if (input.containsKey("c")) {
				input.replace("c", cVal);
			} else { 
				input.put("c", cVal);
			}*/

Open in new window


what above code means?
if cVal is not null and input contains c as key then replace key c's value to cVal
else
put new entry with key c and cVal/

where is this one in the code challenge?
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
set "c" to have the longer value
here cVal contains the longer value

Author

Commented:
if (cVal != null)
			if (input.containsKey("c")) {
				input.replace("c", cVal);
			} else { 
				input.put("c", cVal);
			}

Open in new window


above code means as below with brakets?

if (cVal != null){
			if (input.containsKey("c")) {
				input.replace("c", cVal);
			} else { 
				input.put("c", cVal);
			}}

Open in new window

or means below which is kind of meaning less?//i know if flower brackets not there if has influence until very next line right but here how it influences below inner if and else?
if (cVal != null)
			{  if (input.containsKey("c")) {   }
				input.replace("c", cVal);
			} else { 
				input.put("c", cVal);
			}

Open in new window

Author

Commented:
	@Test
	public void given_valueforKeyBisLongerThanValueForKeyA_thenReplaceKeyCwithIt() {
		String inputA = "aa";
		String inputB = "bbb";
		
		Map<String, String> input = new HashMap<String, String>();
		input.put(A, inputA);
		input.put(B, inputB);
		input.put(C, "cake");
		
		Map<String, String> output = sut.evaluate(input);

		Assert.assertTrue("Output map is not the input map", output == input);
		Assert.assertEquals("Value for key A should be unmodified", inputA, output.get(A));
		Assert.assertEquals("Value for key B should be unmodified", inputB, output.get(B));
		Assert.assertEquals("Value for key C should be same as key B", input.get(B), output.get(C));
	}
	

Open in new window


why only cake considered as c value any other value also may come for c right like for example icecream
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
Expected      Run

mapAB4({"c": "cat", "d": "dog"}) → {"c": "cat", "d": "dog"}      {"c": "cat", "d": "dog"}      OK
mapAB4({"c": "a", "d": "b"}) → {"c": "a", "d": "b"}      {"c": "a", "d": "b"}      OK

Author

Commented:
public Map<String, String> mapAB4(Map<String, String> map) {
   if( map.get("a")!=null && map.get("b")!=null && map.get("a").length()> map.get("b").length() ){
    map.put("c",map.get("a"));
    
  }
    if( map.get("a")!=null && map.get("b")!=null && map.get("b").length()> map.get("a").length() ){
    map.put("c",map.get("b"));
    
  }

  
  
  if( map.get("a")!=null && map.get("b")!=null && map.get("a").length()==map.get("b").length() ){
    map.put("a","");
    map.put("b","");
  }
  return map;
}

Open in new window


above 3 conditions enough right not sure if there is need of below commented code or not which is not clear to me in the first place? please advise

package snippet;

import java.util.Map;

public class MapChallenge {

	public Map<String, String> evaluate(Map<String, String> input) {
		String aVal = input.get("a");
		String bVal = input.get("b");
		String cVal = null;
		if (aVal.length() > bVal.length()) {
			cVal = aVal;
		} else if (bVal.length() > aVal.length()) {
			cVal = bVal;
		} else {
			input.replace("a", "");
			input.replace("b", "");
		}
		
		/*if (cVal != null){
			if (input.containsKey("c")) {
				input.replace("c", cVal);
			} else { 
				input.put("c", cVal);
			}}*/
		return input;
		
		
	}

}

Open in new window

ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
if (cVal != null) 

Open in new window

controls the next statement

{
			if (input.containsKey("c")) {
				input.replace("c", cVal);
			} else { 
				input.put("c", cVal);
			}
}

Open in new window

is a statement

                     if (input.containsKey("c")) {
				input.replace("c", cVal);
			} else { 
				input.put("c", cVal);
			}

Open in new window

is a statement

Author

Commented:
Example 1: mapAB4({"a": "aaa", "b": "bb", "c": "cake"})
Value of A  (aaa) has a longer string than the value of B (bb), so replace the "aaa" as value of C.  Result should be {"a": "aaa", "b": "bb", "c": "aaa"}
Example 2: mapAB4({"a": "aa", "b": "bbb", "c": "cake"})
Value of B (bbb) has a longer string than the value of A (aa), so replace the "bbb" as value of C.  Result should be {"a": "aa", "b": "bbb", "c": "bbb"}
Example 3: mapAB4({"a": "aa", "b": "bbb"})
Value of B (bbb) has a longer string than the value of A (aa) and C is not present, so add C with the "bbb" as value.  Result should be {"a": "aa", "b": "bbb", "c": "bbb"}
Modify and return the given map as follows: if the keys "a" and "b" have values that have different lengths, then set "c" to have the longer value. If the values exist and have the same length, change them both to the empty string in the map. above example make it more clear on why we do input.replace in inner if loop and input.put for inner else loop

if (cVal != null){
                  if (input.containsKey("c")) {
                        input.replace("c", cVal);
                  } else {
                        input.put("c", cVal);
                  }}

Author

Commented:
is a statement

i thought statement means only one line not entire if else with 10 lines of code in it. where i can read more about what is statement esp. this kind of weird cases?

Author

Commented:
Example 3: mapAB4({"a": "aa", "b": "bbb"})
Value of B (bbb) has a longer string than the value of A (aa) and C is not present, so add C with the "bbb" as value.  Result should be {"a": "aa", "b": "bbb", "c": "bbb"}

where in challenge above case or scenario discussed?

Modify and return the given map as follows: if the keys "a" and "b" have values that have different lengths, then set "c" to have the longer value. If the values exist and have the same length, change them both to the empty string in the map.
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
set "c" to have the longer value
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:

Author

Commented:
set "c" to have the longer value

even c is preset or absent as key
if c is present just relace
else c not present as key (in input)
put as separate entry with key c and longer value among aVal and bVal

Author

Commented:
Statements

Statements are roughly equivalent to sentences in natural languages. A statement forms a complete unit of execution. The following types of expressions can be made into a statement by terminating the expression with a semicolon (;).

Assignment expressions
Any use of ++ or --
Method invocations
Object creation expressions
Such statements are called expression statements. Here are some examples of expression statements.

// assignment statement
aValue = 8933.234;
// increment statement
aValue++;
// method invocation statement
System.out.println("Hello World!");
// object creation statement
Bicycle myBike = new Bicycle();
In addition to expression statements, there are two other kinds of statements: declaration statements and control flow statements. A declaration statement declares a variable. You've seen many examples of declaration statements already:

// declaration statement
double aValue = 8933.234;
Finally, control flow statements regulate the order in which statements get executed. You'll learn about control flow statements in the next section, Control Flow Statements
they gave as last line and redirected to different page to divert attention of reader?

Author

Commented:
>>Finally, control flow statements regulate the order in which statements get executed. You'll learn about control flow statements in the next section, Control Flow Statements

they are control flow statements which is kind of weird to consider 1000 lines of code as one statement rather they should call block or something else right?

Author

Commented:
then there are 3 different kinds of control statements


The statements inside your source files are generally executed from top to bottom, in the order that they appear. Control flow statements, however, break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code. This section describes the decision-making statements (if-then, if-then-else, switch), the looping statements (for, while, do-while), and the branching statements (break, continue, return) supported by the Java programming language.

Author

Commented:
public Map<String, String> mapAB4(Map<String, String> map) {
  if( map.get("a")==null || map.get("b") == null ){ return map; }
  if( map.get("a").length()> map.get("b").length() ){
    map.put("c",map.get("a"));
  }else if( map.get("b").length()> map.get("a").length() ){
    map.put("c",map.get("b"));
  }else{
    map.put("a","");
    map.put("b","");
  }
  return map;
}

Open in new window


in your above code where you took care of below scenario
	if (cVal != null){
			if (input.containsKey("c")) {
				input.replace("c", cVal);
			} else { 
				input.put("c", cVal);
			}}
		return input;
		

Open in new window


seems you ignored that scenario right?

Author

Commented:
public Map<String, String> mapAB4(Map<String, String> map) {
  if( map.get("a")==null || map.get("b") == null ){ return map; }//both a,b valuesnull return as it is
  if( map.get("a").length()> map.get("b").length() ){
    map.put("c",map.get("a"));////both a value large length than b values put a value to c value
  }else if( map.get("b").length()> map.get("a").length() ){
    map.put("c",map.get("b"));//////both b value large length than a values put b value to c value
  }else{//////both a value length same as b value length(does any other cases come to picture like key a missing and key b is missing ettc here??? then  put a value to null and b value to null.
    map.put("a","");
    map.put("b","");
  }
  return map;
}
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
any other cases come to picture like key a missing and key b is missing ettc here?
no, those cases were caught by if( map.get("a")==null || map.get("b") == null ){ return map; }

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial