Solved

wordlen challenge

Posted on 2016-09-29
3
47 Views
Last Modified: 2016-09-30
Hi,

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

public Map<String, Integer> wordLen(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],strings[i].length());
 }
  return map;
//}

}

Open in new window

i passed all tests. any improvements or alternate approaches? please advise
0
Comment
Question by:gudii9
3 Comments
 
LVL 26

Accepted Solution

by:
dpearson earned 250 total points
ID: 41822702
Nope - what you have is pretty much optimal.

The only thing is I do notice the code your post often seems to have poor indentation and spacing:

e.g:

  int len=strings.length;
   Map<String, Integer> map = new HashMap<String, Integer>();
 for(int i=0;i<len;i++){
   
 map.put(strings[i],strings[i].length());
 }

Open in new window


should be:

  int len=strings.length;
  Map<String, Integer> map = new HashMap<String, Integer>();    // Delete one space at the start
                                              // Added a blank line
  for(int i=0;i<len;i++){
    map.put(strings[i],strings[i].length());  // Indent the code inside the loop
  }                                           // Lines up with the for statement

Open in new window


It doesn't change the behavior of course - but it makes a huge difference in the readability of the code, which is always important to consider.

Doug
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 250 total points
ID: 41823590
About the indentation: I told you already many, many, many oh so many times...

No improvements. You're code is correct and simply does what it should do.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41823595
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

757 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

23 Experts available now in Live!

Get 1:1 Help Now