wordsWithoutList challenge

Hi,

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

Psedo code:
1. create new array of given difference of size
2. loop through given array
3. fill the new array with given array starting from above difference till end
4. return new array as list
I wrote my code as below

public List wordsWithoutList(String[] words, int len) {
 int count=0;
 int origlen=words.length;
 int newLen=origlen-len;
 String[] arr=new String[newLen];
 for(int i=len-1;i<newLen;i++){
  arr[i]=words[i];
  
 
}
return Arrays.asList(arr);
}

Open in new window




I am not passing all tests
Expected      Run            
wordsWithoutList(["a", "bb", "b", "ccc"], 1) → ["bb", "ccc"]      ["a", "bb", "b"]      X      
wordsWithoutList(["a", "bb", "b", "ccc"], 3) → ["a", "bb", "b"]      [null]      X      
wordsWithoutList(["a", "bb", "b", "ccc"], 4) → ["a", "bb", "b", "ccc"]      []      X      
wordsWithoutList(["xx", "yyy", "x", "yy", "z"], 1) → ["xx", "yyy", "yy"]      ["xx", "yyy", "x", "yy"]      X      
wordsWithoutList(["xx", "yyy", "x", "yy", "z"], 2) → ["yyy", "x", "z"]      [null, "yyy", "x"]      X      

How to improve/modify my design, code and any other alternate approaches. please advise
LVL 7
gudii9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CPColinSenior Java ArchitectCommented:
Don't bother with an array right now; the method returns a List, so you might as well build the list directly, instead of building an array and converting it to a list at the end.
0
gudii9Author Commented:
Don't bother with an array right now; the method returns a List, so you might as well build the list directly, instead of building an array and converting it to a list at the end.

i wonder why above array approach do not work. As i am working lot with arrays now and not started with collection challenges yet on coding bat
0
gudii9Author Commented:
public List wordsWithoutList(String[] words, int len) {
 int count=0;
 int origlen=words.length;
 int newLen=origlen-len;
 String[] arr=new String[newLen];
 List a1 = new ArrayList();
     
for(int i=len-1;i<newLen;i++){
 // arr[i]=words[i];
   a1.add("words[i]");
     // a1.add("Mahnaz");
     // a1.add("Ayan");
  
 
}
return a1;
}

Open in new window


above gives below errors

Expected	Run		
wordsWithoutList(["a", "bb", "b", "ccc"], 1) → ["bb", "ccc"]	["words[i]", "words[i]", "words[i]"]	X	
wordsWithoutList(["a", "bb", "b", "ccc"], 3) → ["a", "bb", "b"]	[]	X	
wordsWithoutList(["a", "bb", "b", "ccc"], 4) → ["a", "bb", "b", "ccc"]	[]	X	
wordsWithoutList(["xx", "yyy", "x", "yy", "z"], 1) → ["xx", "yyy", "yy"]	["words[i]", "words[i]", "words[i]", "words[i]"]	X	
wordsWithoutList(["xx", "yyy", "x", "yy", "z"], 2) → ["yyy", "x", "z"]	["words[i]", "words[i]"]	X	

Open in new window


please advise
0
Discover the Answer to Productive IT

Discover app within WatchGuard's Wi-Fi Cloud helps you optimize W-Fi user experience with the most complete set of visibility, troubleshooting, and network health features. Quickly pinpointing network problems will lead to more happy users and most importantly, productive IT.

rrzCommented:
I think you misunderstood the challenge.
Psedo code:
 1. create new array of given difference of size
That is not what you want to do. You need to return the Strings whose length is not equal to len(the second input parameter). Your code will determine which elements of the array pass the test (word.length() != len). Therefore the numbers of elements is a variable.  Lists have a variable length(by using the add method). It is easier to work with Lists(instead of arrays) for this challenge.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CPColinSenior Java ArchitectCommented:
I agree with rrz. Start over with your pseudocode and try again. What you have on line 10 is definitely not going to work; you're adding the string "words[i]" to your list over and over.
0
gudii9Author Commented:
i see i was surely misunderstood challenge



Given an array of strings, return a new List (e.g. an ArrayList) where all the strings of the given length are omitted. See wordsWithout() below which is more difficult because it uses arrays.

wordsWithoutList(["a", "bb", "b", "ccc"], 1) → ["bb", "ccc"]//i ws ommitting first element which is wrong
wordsWithoutList(["a", "bb", "b", "ccc"], 3) → ["a", "bb", "b"]
wordsWithoutList(["a", "bb", "b", "ccc"], 4) → ["a", "bb", "b", "ccc"]
0
gudii9Author Commented:
how to add each array element to list?
0
CPColinSenior Java ArchitectCommented:
I think we shouldn't tell you that, since that's most of the challenge right there. You are already using List.add(), but you're passing a string literal to it. What else can you pass to it?
0
gudii9Author Commented:
i see your point. i can add array element directly rather than string literal
public List wordsWithoutList(String[] words, int len) {
 List a1 = new ArrayList();
for(int i=len-1;i<words.length;i++){
   if(words[i].length()!=len){
   a1.add(words[i]);
   }
}
return a1;
}

Open in new window


i am failing 2 tests where length is greater?
Expected      Run            
wordsWithoutList(["a", "bb", "b", "ccc"], 1) → ["bb", "ccc"]      ["bb", "ccc"]      OK      
wordsWithoutList(["a", "bb", "b", "ccc"], 3) → ["a", "bb", "b"]      ["b"]      X      
wordsWithoutList(["a", "bb", "b", "ccc"], 4) → ["a", "bb", "b", "ccc"]      ["ccc"]      X      
wordsWithoutList(["xx", "yyy", "x", "yy", "z"], 1) → ["xx", "yyy", "yy"]      ["xx", "yyy", "yy"]      OK      
wordsWithoutList(["xx", "yyy", "x", "yy", "z"], 2) → ["yyy", "x", "z"]      ["yyy", "x", "z"]      OK      
0
CPColinSenior Java ArchitectCommented:
Take a closer look at where you're starting your loop.
0
gudii9Author Commented:
public List wordsWithoutList(String[] words, int len) {
 List a1 = new ArrayList();
for(int i=0;i<words.length;i++){
   if(words[i].length()!=len){
   a1.add(words[i]);
   }
}
return a1;
}

Open in new window


oops my mistake while modifying old code. now passing all tests. any improvement or alaternate approaches for this?
Expected      Run            
wordsWithoutList(["a", "bb", "b", "ccc"], 1) → ["bb", "ccc"]      ["bb", "ccc"]      OK      
wordsWithoutList(["a", "bb", "b", "ccc"], 3) → ["a", "bb", "b"]      ["a", "bb", "b"]      OK      
wordsWithoutList(["a", "bb", "b", "ccc"], 4) → ["a", "bb", "b", "ccc"]      ["a", "bb", "b", "ccc"]      OK      
wordsWithoutList(["xx", "yyy", "x", "yy", "z"], 1) → ["xx", "yyy", "yy"]      ["xx", "yyy", "yy"]      OK      
wordsWithoutList(["xx", "yyy", "x", "yy", "z"], 2) → ["yyy", "x", "z"]      ["yyy", "x", "z"]      OK      

All Correct
0
CPColinSenior Java ArchitectCommented:
That's about the best you can do, as far as the challenge is concerned. In modern Java, you would probably want to use generics and return a List<String>, instead of a raw list. In Java 8, you might use a stream, in place of the loop. I think their site doesn't allow Java 8 features, though.
0
gudii9Author Commented:
do codingbat allow generics?
0
gudii9Author Commented:
public List wordsWithoutList(String[] words, int len) {
 List<String> a1 = new ArrayList<String>();
for(int i=0;i<words.length;i++){
   if(words[i].length()!=len){
   a1.add(words[i]);
   }
}
return a1;
}

Open in new window


you mean like above? it passed all tests
0
gudii9Author Commented:
In Java 8, you might use a stream, in place of the loop. I think their site doesn't allow Java 8 features, though

how to use this? i can try in eclipse
0
CPColinSenior Java ArchitectCommented:
Streams are a bit much to get into here, I think.
0
gudii9Author Commented:
import java.util.ArrayList;
import java.util.List;

public class WordsWithoutList {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String[] ar = { "a", "bb", "b", "ccc" };
		System.out.println("wordsWithoutList value-->" + wordsWithoutList(ar, 1));
	}

	public static List wordsWithoutList(String[] words, int len) {
		List<String> a1 = new ArrayList<String>();
		for (int i = 0; i < words.length; i++) {
			if (words[i].length() != len) {
				a1.add(words[i]);
			}
		}
		return a1;
	}

}

Open in new window

wordsWithoutList value-->[bb, ccc]

i wrote in eclipse with generics which works fine. How to write using Streams just to get fundamental idea?
0
CPColinSenior Java ArchitectCommented:
You could use something like this:

	public static List<String> wordsWithoutList(String[] words, int len) {
                return Arrays.stream(words) // Make a stream that produces the elements of the array.
                        .filter(word -> word.length() != len) // Keep only the words with length different than len.
                        .collect(Collectors.toList()); // Collect the resulting elements in a List.
	}

Open in new window

0
rrzCommented:
You could use the enhanced for-loop to increase readability.  
public List wordsWithoutList(String[] words, int len){
    List<String> list = new ArrayList();
    for(String word: words){
		if(word.length() != len)list.add(word);
    }
    return list;
}

Open in new window

0
gudii9Author Commented:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class WordsWithoutList {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String[] ar = { "a", "bb", "b", "ccc" };
		System.out.println("wordsWithoutList value-->" + wordsWithoutList(ar, 1));
	}

	//public static List wordsWithoutList(String[] words, int len) {
	/*
		List<String> a1 = new ArrayList<String>();
		for (int i = 0; i < words.length; i++) {
			if (words[i].length() != len) {
				a1.add(words[i]);
			}
		}
		return a1;*/
		//}
	
	
	public static List<String> wordsWithoutList(String[] words, int len) {
        return Arrays.stream(words) // Make a stream that produces the elements of the array.
                .filter(word -> word.length() != len) // Keep only the words with length different than len.
                .collect(Collectors.toList()); // Collect the resulting elements in a List.
}

}

Open in new window


above works and passes test as below
wordsWithoutList value-->[bb, ccc]
0
gudii9Author Commented:
public List wordsWithoutList(String[] words, int len) {
 List<String> a1 = new ArrayList<String>();
for(int i=0;i<words.length;i++){
   if(words[i].length()!=len){
   a1.add(words[i]);
   }
}
return a1;
}

Open in new window


above also passes all tests and looks more simple
0
awking00Information Technology SpecialistCommented:
Just because I like the look you can still loop through the array without incrementing the index (no points please) -
for (word : words) {
      if (words.length() != len) {
      a1.add(word]);
}
0
gudii9Author Commented:
sure. same as ID: 41776694 right?
0
awking00Information Technology SpecialistCommented:
Yes, exactly. Sorry, rrz, I didn't see that post before.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.