Solved

wordsFront challenge

Posted on 2016-08-29
8
104 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
[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
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 78
Java: The Public Class Main 4 46
Java class and jar 3 43
IBM TS2900 (3572) Tape Autoloader Java? 12 61
This article will show, step by step, how to integrate R code into a R Sweave document
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

730 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