Solved

wordsFront challenge

Posted on 2016-08-29
8
89 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
tomcat not starting 6 45
servlet example 17 32
How to execute a Python program and gather return output in Java 2 30
CSV file parsing thru Java 13 31
The purpose of this article is to demonstrate how we can use conditional statements using Python.
When there is a disconnect between the intentions of their creator and the recipient, when algorithms go awry, they can have disastrous consequences.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
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.

809 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