Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

word0 challenge

Posted on 2016-09-29
3
Medium Priority
?
185 Views
Last Modified: 2016-10-04
Hi,

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

Given an array of strings, return a Map<String, Integer> containing a key for every different string in the array, always with the value 0. For example the string "hello" makes the pair "hello":0. We'll do more complicated counting later, but for this problem the value is simply 0.

word0(["a", "b", "a", "b"]) β†’ {"b": 0, "a": 0}
word0(["a", "b", "a", "c", "b"]) β†’ {"b": 0, "c": 0, "a": 0}
word0(["c", "b", "a"]) β†’ {"b": 0, "c": 0, "a": 0}

not clean on above description . please advise
0
Comment
Question by:gudii9
  • 2
3 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 41822756
Map<String, Integer>
For every unique key (i.e., a string), there is an integer value. In this problem, the integer value is always zero. If the unique key appears more than once in an array, then the map can only have one node for that unique key.

(["a", "b", "a", "c", "b"]) -- this array has three unique keys, "a", "b", "c"
The map therefore can only have three nodes, one for each unique key.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41827031
public Map<String, Integer> word0(String[] strings) {
  int len=strings.length;
   Map<String, Integer> map = new HashMap<String, Integer>();
 for(int i=0;i<len;i++){
   
 map.put(strings[i],0);
 }
  return map;
}

Open in new window

above passes all the tests. any improvements or alternate approaches?
0
 
LVL 32

Accepted Solution

by:
phoffric earned 2000 total points
ID: 41827313
At work, when code is not indented, we do not even review it. Indentation is that important. In fact, if you learn Python, if  you don't get the indentation exactly right, then you can get very subtle errors. (I think that is overkill in enforcing good coding standards, but now Python is gaining in popularity, so keep that in mind.)

Since you only have a few lines, I was willing to review it. Try to indent in the future. Others have asked you to do that. Don't forget, you can hit the "Preview" button to see how your post with code looks.

Your code is fine, but the code from their Show Solution button is even better:
public Map<String, Integer> word0(String[] strings) {
   Map<String, Integer> map = new HashMap();
   for (String s:strings) {  // no need to compute len as you did since compiler knows the array length
      map.put(s, 0);
   }
   return map;
}

Open in new window


The for-statement says, assign s, a String, to each element in the strings array, one at a time, and use that s in the body of the for-loop. It is just as you did, but much cleaner looking since their solution only has three variables: strings, map and s. It is not just cleaner, but also less error prone, since you have to manipulate you counter variable, i.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Six Sigma Control Plans
Introduction to Processes
Suggested Courses

926 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