Solved

wordsFront challenge

Posted on 2016-08-29
8
65 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

708 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

15 Experts available now in Live!

Get 1:1 Help Now