Solved

wordsWithoutList  challenge

Posted on 2016-08-29
24
173 Views
Last Modified: 2016-08-30
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
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
  • 13
  • 7
  • 2
  • +1
24 Comments
 
LVL 14

Expert Comment

by:CPColin
ID: 41775529
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
 
LVL 7

Author Comment

by:gudii9
ID: 41775689
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
 
LVL 7

Author Comment

by:gudii9
ID: 41775700
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Accepted Solution

by:
rrz earned 250 total points
ID: 41775720
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
 
LVL 14

Expert Comment

by:CPColin
ID: 41775723
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
 
LVL 7

Author Comment

by:gudii9
ID: 41776309
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
 
LVL 7

Author Comment

by:gudii9
ID: 41776327
how to add each array element to list?
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41776361
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
 
LVL 7

Author Comment

by:gudii9
ID: 41776385
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
 
LVL 14

Expert Comment

by:CPColin
ID: 41776397
Take a closer look at where you're starting your loop.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41776410
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
 
LVL 14

Assisted Solution

by:CPColin
CPColin earned 250 total points
ID: 41776430
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
 
LVL 7

Author Comment

by:gudii9
ID: 41776437
do codingbat allow generics?
0
 
LVL 7

Author Comment

by:gudii9
ID: 41776441
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
 
LVL 7

Author Comment

by:gudii9
ID: 41776442
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
 
LVL 14

Expert Comment

by:CPColin
ID: 41776468
Streams are a bit much to get into here, I think.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41776479
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
 
LVL 14

Expert Comment

by:CPColin
ID: 41776577
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
 
LVL 27

Expert Comment

by:rrz
ID: 41776694
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
 
LVL 7

Author Comment

by:gudii9
ID: 41776726
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
 
LVL 7

Author Comment

by:gudii9
ID: 41776732
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
 
LVL 32

Expert Comment

by:awking00
ID: 41776742
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
 
LVL 7

Author Comment

by:gudii9
ID: 41776749
sure. same as ID: 41776694 right?
0
 
LVL 32

Expert Comment

by:awking00
ID: 41776760
Yes, exactly. Sorry, rrz, I didn't see that post before.
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Article by: Nicole
This is a research brief on the potential colonization of humans on Mars.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Simple Linear Regression

726 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