Solved

wordlen challenge

Posted on 2016-09-29
3
79 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
[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
3 Comments
 
LVL 27

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wild fly 8 startup error 2 37
How to Have Macro Files Automatically "Edit" 5 45
"lblTime is not declared" 3 28
Oracle programming for starter 14 27
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…
A short article about problems I had with the new location API and permissions in Marshmallow
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.

740 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