Avatar of gudii9
gudii9Flag for United States of America

asked on 

mapShare challenge

Hi,

 I am working on below challenge

http://codingbat.com/prob/p148813


Map-1 > mapShare
prev  |  next  |  chance


Modify and return the given map as follows: if the key "a" has a value, set the key "b" to have that same value. In all cases remove the key "c", leaving the rest of the map unchanged.

mapShare({"b": "bbb", "c": "ccc", "a": "aaa"}) → {"b": "aaa", "a": "aaa"}
mapShare({"b": "xyz", "c": "ccc"}) → {"b": "xyz"}
mapShare({"d": "hi", "c": "meh", "a": "aaa"}) → {"d": "hi", "b": "aaa", "a": "aaa"}


does the  'a' key corresponding value remains same in output map as in below case "aaa"?

mapShare({"b": "bbb", "c": "ccc", "a": "aaa"}) → {"b": "aaa", "a": "aaa"}

please advise
Java EEJavaProgrammingProgramming Languages-Other

Avatar of undefined
Last Comment
zzynx
Avatar of zzynx
zzynx
Flag of Belgium image

>> does the  'a' key corresponding value remains same in output map as in below case "aaa"?
Yes.

This is really a so basic challenge that you should be able to solve this on your own.
ASKER CERTIFIED SOLUTION
Avatar of awking00
awking00
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

public Map<String, String> mapShare(Map<String, String> map) {
  
  String val=map.get("a");
  String backup=val;
  map.put("b",backup);
 // map.put("a","");
 map.remove("c");
  
  return map;

}

Open in new window

Expected      Run            
mapShare({"b": "bbb", "c": "ccc", "a": "aaa"}) → {"b": "aaa", "a": "aaa"}      {"b": "aaa", "a": "aaa"}      OK      
mapShare({"b": "xyz", "c": "ccc"}) → {"b": "xyz"}      {"b": null}      X      
mapShare({"d": "hi", "c": "meh", "a": "aaa"}) → {"d": "hi", "b": "aaa", "a": "aaa"}      {"d": "hi", "b": "aaa", "a": "aaa"}      OK      
mapShare({"b": "1234", "c": "yo", "a": "xyz", "z": "zzz"}) → {"b": "xyz", "a": "xyz", "z": "zzz"}      {"b": "xyz", "a": "xyz", "z": "zzz"}      OK      
mapShare({"d": "ddd", "e": "everything", "b": "1234", "c": "yo", "a": "xyz"}) → {"d": "ddd", "e": "everything", "b": "xyz", "a": "xyz"}      {"d": "ddd", "e": "everything", "b": "xyz", "a": "xyz"}      OK      
other tests
X      
Correct for more than half the tests

Your progress graph for this problem

above fails one test.
please advise
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

public Map<String, String> mapShare(Map<String, String> map) {
  if(map.containsKey("a")){
  String val=map.get("a");
  String backup=val;
  map.put("b",backup);
 // map.put("a","");
 map.remove("c");
  }
  else{
     map.remove("c");
  }
  return map;

}

Open in new window


above passes all tests. any improvements or alternate approaches?
Avatar of awking00
awking00
Flag of United States of America image

You might consolidate some of the steps -
map.remove"c";  ==> in all cases
if (map.containsKey("a") {
  map.put("b",map.get("a")); ==>no need for intermediate variable
  map.put("a","");
}
Avatar of awking00
awking00
Flag of United States of America image

Typo - map.remove("c"); ==> needs parentheses
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

public Map<String, String> mapShare(Map<String, String> map) {
  if(map.containsKey("a")){
 // String val=map.get("a");
 // String backup=val;
  map.put("b",map.get("a"));
 // map.put("a","");
 map.remove("c");
  }
 // else{
 //    map.remove("c");
 // }
  return map;

}

Open in new window


above failse one test if i try to remove c in all cases

Expected      Run            
mapShare({"b": "bbb", "c": "ccc", "a": "aaa"}) → {"b": "aaa", "a": "aaa"}      {"b": "aaa", "a": "aaa"}      OK      
mapShare({"b": "xyz", "c": "ccc"}) → {"b": "xyz"}      {"b": "xyz", "c": "ccc"}      X      
mapShare({"d": "hi", "c": "meh", "a": "aaa"}) → {"d": "hi", "b": "aaa", "a": "aaa"}      {"d": "hi", "b": "aaa", "a": "aaa"}      OK      
mapShare({"b": "1234", "c": "yo", "a": "xyz", "z": "zzz"}) → {"b": "xyz", "a": "xyz", "z": "zzz"}      {"b": "xyz", "a": "xyz", "z": "zzz"}      OK      
mapShare({"d": "ddd", "e": "everything", "b": "1234", "c": "yo", "a": "xyz"}) → {"d": "ddd", "e": "everything", "b": "xyz", "a": "xyz"}      {"d": "ddd", "e": "everything", "b": "xyz", "a": "xyz"}      OK      
other tests
OK      


please advise
SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of awking00
awking00
Flag of United States of America image

Without comments, your code is -
if (map.containsKey("a")) {
    map.put("b",map.get("a"));
    map.remove("c");
}
return map;

You need to remove "c" whether "a" exists or not -
    map.remove("c"); ==> note - if "c" doesn't exist, nothing will happen, if it does, the record is removed from the map
if (map.containsKey("a")) {
    map.put("b",map.get("a"));
    map.put("a",""); ==> this is still needed
}
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

public Map < String, String > mapShare(Map < String, String > map) {
    if (map.containsKey("a")) {
        String val = map.get("a");
        String backup = val;
        map.put("b", backup);
        // map.put("a","");
        map.remove("c");
    } else {
        map.remove("c");
    }
    return map;

}

Open in new window


i see it is in if loop
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

public Map < String, String > mapShare(Map < String, String > map) {
    map.remove("c");
    if (map.containsKey("a")) {
        // String val=map.get("a");
        // String backup=val;
        map.put("b", map.get("a"));
        // map.put("a","");
        //map.remove("c");
    }
    // else{
    //    map.remove("c");
    // }
    return map;

}

Open in new window


above passes all tests
Avatar of gudii9
gudii9
Flag of United States of America image

ASKER

http://www.tutorialspoint.com/online_java_formatter.htm
above is good formatter tool i found
Avatar of zzynx
zzynx
Flag of Belgium image

>> above is good formatter tool i found
All right. Try to use it :)
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo