Solved

seriesUp challenge

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

"Disruption" is the most feared word for C-level executives these days. They agonize over their industry being disturbed by another player - most likely by startups.
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…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
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…

630 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