Solved

wordlen challenge

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

710 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