Solved

wordlen challenge

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

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

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

911 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

21 Experts available now in Live!

Get 1:1 Help Now