Solved

seriesUp challenge

Posted on 2016-08-14
7
192 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 500 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
Technology Partners: 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!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb example issues 3 52
Identify two keyboard codes and how to enter them 9 69
hashmap order 17 40
American Express @Work site and Java 4 64
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.

756 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