wordsWithoutList  challenge

gudii9
gudii9 used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
CPColinSenior Java Architect

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.

Author

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

Author

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
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
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.
CPColinSenior Java Architect

Commented:
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.

Author

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"]

Author

Commented:
how to add each array element to list?
CPColinSenior Java Architect

Commented:
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?

Author

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      
CPColinSenior Java Architect

Commented:
Take a closer look at where you're starting your loop.

Author

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
CPColinSenior Java Architect
Commented:
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.

Author

Commented:
do codingbat allow generics?

Author

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

Author

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
CPColinSenior Java Architect

Commented:
Streams are a bit much to get into here, I think.

Author

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?
CPColinSenior Java Architect

Commented:
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

Commented:
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

Author

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]

Author

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
awking00Information Technology Specialist

Commented:
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]);
}

Author

Commented:
sure. same as ID: 41776694 right?
awking00Information Technology Specialist

Commented:
Yes, exactly. Sorry, rrz, I didn't see that post before.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial