Solved

seriesUp challenge

Posted on 2016-08-14
7
105 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 37

Accepted Solution

by:
Gerwin Jansen earned 500 total points
Comment Utility
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
Comment Utility
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 37

Expert Comment

by:Gerwin Jansen
Comment Utility
You have to create a second loop.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 7

Author Comment

by:gudii9
Comment Utility
why we need second loop?
0
 
LVL 37

Expert Comment

by:Gerwin Jansen
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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.
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.

763 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

5 Experts available now in Live!

Get 1:1 Help Now