Solved

wordlen challenge

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 38
Change the background and font colors in Notepad++ 5 83
ejb on wildfly 5 20
How to parse the string and split the sub directory names in asp.net 7 41
This is an explanation of a simple data model to help parse a JSON feed
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

776 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