Solved

wordsWithoutList  challenge

Posted on 2016-08-29
24
75 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
  • 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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 31

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 31

Expert Comment

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Lithium-ion batteries area cornerstone of today's portable electronic devices, and even though they are relied upon heavily, their chemistry and origin are not of common knowledge. This article is about a device on which every smartphone, laptop, an…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now