Solved

word0 challenge

Posted on 2016-09-29
3
78 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 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video teaches viewers about errors in exception handling.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

896 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

12 Experts available now in Live!

Get 1:1 Help Now