Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

word0 challenge

Posted on 2016-09-29
3
Medium Priority
?
161 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Make the most of your online learning experience.
Six Sigma Control Plans
Introduction to Processes

715 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