?
Solved

seriesUp challenge

Posted on 2016-08-14
7
Medium Priority
?
213 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
[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
  • 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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

718 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