Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

seriesUp challenge

Posted on 2016-08-14
7
Medium Priority
?
221 Views
Last Modified: 2016-08-21
Hi,

I am working on below challenge

http://codingbat.com/prob/p104090

My psuedo code of the logical approach is
1. create array of given number
2. loop though each element to set each value with given pattern(not sure how to get other previous patterns?)
3. return the modified array




I wrote my code as below
public int[] seriesUp(int n) {
  int[] nums=new int[n];
  for(int i=0;i<nums.length;i++){
    nums[i]=i;
  }
  return nums;
}

Open in new window






I am not passing all tests



How to improve my design, approach, code? please advise
0
Comment
Question by:gudii9
  • 4
  • 3
7 Comments
 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 2000 total points
ID: 41756180
Which tests are failing? You just need to create a second loop to fill the array. Your first step is not correct (array size too short).
0
 
LVL 7

Author Comment

by:gudii9
ID: 41756463
Expected      Run            
seriesUp(3) → [1, 1, 2, 1, 2, 3]      [0, 1, 2]      X      
seriesUp(4) → [1, 1, 2, 1, 2, 3, 1, 2, 3, 4]      [0, 1, 2, 3]      X      
seriesUp(2) → [1, 1, 2]      [0, 1]      X      
seriesUp(1) → [1]      [0]      X      
seriesUp(0) → []      []      OK      
seriesUp(6) → [1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6]      [0, 1, 2, 3, 4, 5]      X      
Your progress graph for this problem


i am failing above tests
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 41756555
You have to create a second loop.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Author Comment

by:gudii9
ID: 41760202
why we need second loop?
0
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 41760801
Some of your test case fail because you start with 0 - look at the problem description!

A second loop would be needed (in my pseudo code) for this:

1
1 2
1 2 3
1 2 3 4

one loop for the whole series, one loop for the 'subset' of a serie
0
 
LVL 7

Author Comment

by:gudii9
ID: 41764751
public int[] seriesUp(int n) {
  int x=0;
  int[] nums=new int[(n*(n+1))/2];
  for(int i=1;i<nums.length;i++){
    for(int j=1;i<nums.length;j++){
    nums[x]=j;
  }
  }
  return nums;
}

Open in new window


i wrote as aboe and giving below errors. please advise
Expected      Run            
seriesUp(3) → [1, 1, 2, 1, 2, 3]      Timed out      X      
seriesUp(4) → [1, 1, 2, 1, 2, 3, 1, 2, 3, 4]      Timed out      X      
seriesUp(2) → [1, 1, 2]      Timed out      X      
seriesUp(1) → [1]      Timed out      X      
seriesUp(0) → []      Timed out      X      
seriesUp(6) → [1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6]      Timed out      X      
Your progress graph for this problem
0
 
LVL 7

Author Comment

by:gudii9
ID: 41764757
public int[] seriesUp(int n) {
  int x=0;
  int[] nums=new int[(n*(n+1))/2];
  for(int i=1;i<=n;i++){
    for(int j=1;j<=i;j++){
    nums[x++]=j;
  }
  }
  return nums;
}

Open in new window

Expected      Run            
seriesUp(3) → [1, 1, 2, 1, 2, 3]      [1, 1, 2, 1, 2, 3]      OK      
seriesUp(4) → [1, 1, 2, 1, 2, 3, 1, 2, 3, 4]      [1, 1, 2, 1, 2, 3, 1, 2, 3, 4]      OK      
seriesUp(2) → [1, 1, 2]      [1, 1, 2]      OK      
seriesUp(1) → [1]      [1]      OK      
seriesUp(0) → []      []      OK      
seriesUp(6) → [1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6]      [1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6]      OK      

above passed all tests. any improvement, refinements to it?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
Loops Section Overview
Suggested Courses

773 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