Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

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
Avatar of CPColin
CPColin
Flag of United States of America image

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.
Avatar of gudii9

ASKER

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
Avatar of gudii9

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of gudii9

ASKER

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"]
Avatar of gudii9

ASKER

how to add each array element to list?
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?
Avatar of gudii9

ASKER

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      
Take a closer look at where you're starting your loop.
Avatar of gudii9

ASKER

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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

do codingbat allow generics?
Avatar of gudii9

ASKER

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
Avatar of gudii9

ASKER

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
Streams are a bit much to get into here, I think.
Avatar of gudii9

ASKER

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

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

Avatar of gudii9

ASKER

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]
Avatar of gudii9

ASKER

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
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]);
}
Avatar of gudii9

ASKER

sure. same as ID: 41776694 right?
Yes, exactly. Sorry, rrz, I didn't see that post before.