Solved

wordsFront challenge

Posted on 2016-08-29
8
83 Views
Last Modified: 2016-08-29
Hi,

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

Psedo code:
1. create new array of given n size
2. loop through given array
3. fill the new array with given array
4. return new array
I wrote my code as below

public String[] wordsFront(String[] words, int n) {
 int count=0;
 String[] arr=new String[n];
for(int i=0;i<n;i++){
  arr[i]=words[i];
  
 
}
return arr;
}

Open in new window




I am passing all tests

How to improve/modify my design, code and any other alternate approaches. please advise
0
Comment
Question by:gudii9
  • 3
  • 3
  • 2
8 Comments
 
LVL 14

Assisted Solution

by:CPColin
CPColin earned 250 total points
ID: 41775511
Your line 2 there does nothing. The only way you could really change anything here is by using System.arrayCopy(), which performs essentially the same operation as the loop you've written. Looks like that website doesn't let you do that, though.
0
 
LVL 16

Accepted Solution

by:
krakatoa earned 250 total points
ID: 41775513
Yes, you could make it a bit shorter . . .

public String[] wordsFront(String[] words, int n) {
  return Arrays.copyOfRange(words,  0,  n);
}

Open in new window

0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41775516
using System.arrayCopy(),

I have said before that Codingbat does not permit System calls ! How could it? Where do you suppose that  output would go ??
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41775522
Well arrayCopy() doesn't do output, so there's no technical reason to disallow that specific method. Interesting that they allowed Arrays, without a package name; they must be importing stuff from java.util behind the scenes.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 16

Expert Comment

by:krakatoa
ID: 41775541
It's the SYSTEM API call that's wrong!!!
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41775545
I'm not sure what you're on about. I know the site disallows use of members of the System class, seeing as I mentioned that in my first comment.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41775684
package com.solution;



	import java.lang.*;

	public class SystemDemo {

	   public static void main(String[] args) {

	   int arr1[] = { 0, 1, 2, 3, 4, 5 };
	   int arr2[] = { 5, 10, 20, 30, 40, 50 };
	    
	   // copies an array from the specified source array
	   System.arraycopy(arr1, 0, arr2, 0, 2);
	   System.out.print("array2 = ");
	   System.out.print(arr2[0] + " ");
	   System.out.print(arr2[1] + " ");
	   System.out.print(arr2[2] + " ");
	   System.out.print(arr2[3] + " ");
	   System.out.print(arr2[4] + " ");
	   }
	}

Open in new window

array2 = 0 1 20 30 40
something like above instead of 2 give n but coding bat wont allow with System class methods but allows Array class methods which is bit strange
0
 
LVL 7

Author Comment

by:gudii9
ID: 41775688
package com.solution;

import java.lang.*;
import java.util.Arrays;

public class SystemDemo2 {

	public static void main(String[] args) {
		String words[] = { "a", "b", "c", "d" };
		System.out.println("vslue is--->" + Arrays.toString(wordsFront(words, 1)));		

	}

	public static String[] wordsFront(String[] words, int n) {
		String[] newwords = new String[n];
		// copies an array from the specified source array
		System.arraycopy(words, 0, newwords, 0, 1);
		// System.out.print("array2 = ");
		System.out.print(newwords[0] + " ");
		// System.out.print(newwords[1] + " ");
		// System.out.print(arr2[2] + " ");
		// System.out.print(arr2[3] + " ");
		// System.out.print(arr2[4] + " ");
		// return System.arraycopy(words, 0,newwords,0, n);
		return newwords;

	}

}

Open in new window



eclipse it is working fine

a vslue is--->[a]
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Prime numbers are natural numbers greater than 1 that have only two divisors (the number itself and 1). By “divisible” we mean dividend % divisor = 0 (% indicates MODULAR. It gives the reminder of a division operation). We’ll follow multiple approac…
This article will show, step by step, how to integrate R code into a R Sweave document
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

929 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

18 Experts available now in Live!

Get 1:1 Help Now