Solved

bigHeights  challenge

Posted on 2016-08-30
13
78 Views
Last Modified: 2016-09-09
Hi,


i am working one below challenge
http://codingbat.com/prob/p197710

i was not clear on below description




(A variation on the sumHeights problem.) We have an array of heights, representing the altitude along a walking trail. Given start/end indexes into the array, return the number of "big" steps for a walk starting at the start index and ending at the end index. We'll say that step is big if it is 5 or more up or down. The start end end index will both be valid indexes into the array with start <= end.
how below results are 1,0,1 each? please advise
bigHeights([5, 3, 6, 7, 2], 2, 4) → 1
bigHeights([5, 3, 6, 7, 2], 0, 1) → 0
bigHeights([5, 3, 6, 7, 2], 0, 4) → 1
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
  • 6
  • 4
  • 3
13 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41777530
> return the number of "big" steps for a walk starting at the start index and ending at the end index.
if the absolute height differences is >=5, add up 1 to the result

7 - 2 = 5 which matches, being in the "array indexes 2-4"
on 2nd example, array 0-1 does not have such a big step, hence 0
the results could be higher than 1, that's why there is no boolean
0
 
LVL 7

Author Comment

by:gudii9
ID: 41780976
Expected      Run            
bigHeights([5, 3, 6, 7, 2], 2, 4) → 1      0      X      
bigHeights([5, 3, 6, 7, 2], 0, 1) → 0      0      OK      
bigHeights([5, 3, 6, 7, 2], 0, 4) → 1      0      X      
bigHeights([5, 3, 6, 7, 3], 0, 4) → 0      0      OK      
bigHeights([5, 3, 6, 7, 2], 1, 1) → 0      0      OK      
bigHeights([5, 13, 6, 7, 2], 1, 2) → 1      0      X      
bigHeights([5, 13, 6, 7, 2], 0, 2) → 2      0      X      
bigHeights([5, 13, 6, 7, 2], 1, 4) → 2      0      X      
bigHeights([5, 13, 6, 7, 2], 0, 4) → 3      0      X      
bigHeights([5, 13, 6, 7, 2], 0, 3) → 2      0      X      
bigHeights([1, 2, 3, 4, 5, 4, 3, 2, 10], 0, 3) → 0      0      OK      
bigHeights([1, 2, 3, 4, 5, 4, 3, 2, 10], 4, 8) → 1      0      X      
bigHeights([1, 2, 3, 14, 5, 4, 3, 2, 10], 0, 3) → 1      0      X      
bigHeights([1, 2, 3, 14, 5, 4, 3, 2, 10], 7, 8) → 1      0      X      
bigHeights([1, 2, 3, 14, 5, 4, 3, 2, 10], 3, 8) → 2      0      X      
bigHeights([1, 2, 3, 14, 5, 4, 3, 2, 10], 2, 8) → 3      0      X      
other tests
X

not clear still yet.

bigHeights([5, 3, 6, 7, 2], 2, 4) → 1//7 - 2 = 5 which matches, being in the "array indexes 2-4"why we took 7-2 are not we supposed to take 6-1??
bigHeights([5, 3, 6, 7, 2], 0, 1) → 0//on 2nd example, array 0-1 does not have such a big step, hence 0//why no big step ??is it because 3-5 is only 2 which is not 5 hence not big step?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41781137
because the steps to consider are from each array item to the NEXT one, not the boundaries

bigHeights([5, 3, 6, 7, 2], 2, 4)
you check 6 vs 7 and 7 vs 2

bigHeights([5, 13, 6, 7, 2], 0, 2)
you check 5 vs 13 and 13 vs 6
0
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 
LVL 7

Author Comment

by:gudii9
ID: 41781798
are from each array item to the NEXT one,
where it mentioned in challenge about NEXT neighbor element?
bigHeights([5, 3, 6, 7, 2], 0, 1) → 0
how above is 0
5-3 is 2 which is not big(as less than 5) so 0. i do not see challenge mentioned about 9 anywhereif big (ie greater than 5 ) then return 1?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41783680
> We'll say that step is big if it is 5 or more up or down

that is the sentence telling when to consider a step being "big"
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 41783684
please see the picture + explanation given on sumHeights example, that should clarify
0
 
LVL 7

Author Comment

by:gudii9
ID: 41788972
public int bigHeights(int[] heights, int start, int end) {
 int diff=0;
  int sum=0;
  int absDiff=0;
  int result=0;
  for(int i=start; i<end; i++){
    
    diff=heights[i+1]-heights[i];
    absDiff=Math.abs(diff);
    sum=sum+absDiff;
    if(sum>=5){
      result=1;
    }else if(sum<5){
      result=0;
    }
  }
  return result;
}

Open in new window


i am failing some tests. please advise
Expected      Run            
bigHeights([5, 3, 6, 7, 2], 2, 4) → 1      1      OK      
bigHeights([5, 3, 6, 7, 2], 0, 1) → 0      0      OK      
bigHeights([5, 3, 6, 7, 2], 0, 4) → 1      1      OK      
bigHeights([5, 3, 6, 7, 3], 0, 4) → 0      1      X      
bigHeights([5, 3, 6, 7, 2], 1, 1) → 0      0      OK      
bigHeights([5, 13, 6, 7, 2], 1, 2) → 1      1      OK      
bigHeights([5, 13, 6, 7, 2], 0, 2) → 2      1      X      
bigHeights([5, 13, 6, 7, 2], 1, 4) → 2      1      X      
bigHeights([5, 13, 6, 7, 2], 0, 4) → 3      1      X      
bigHeights([5, 13, 6, 7, 2], 0, 3) → 2      1      X      
bigHeights([1, 2, 3, 4, 5, 4, 3, 2, 10], 0, 3) → 0      0      OK      
bigHeights([1, 2, 3, 4, 5, 4, 3, 2, 10], 4, 8) → 1      1      OK      
bigHeights([1, 2, 3, 14, 5, 4, 3, 2, 10], 0, 3) → 1      1      OK      
bigHeights([1, 2, 3, 14, 5, 4, 3, 2, 10], 7, 8) → 1      1      OK      
bigHeights([1, 2, 3, 14, 5, 4, 3, 2, 10], 3, 8) → 2      1      X      
bigHeights([1, 2, 3, 14, 5, 4, 3, 2, 10], 2, 8) → 3      1      X      
other tests
X      


bigHeights([5, 3, 6, 7, 3], 0, 4) → 0      1      X

how above is 0. i thought it should output 1 since
5-------------->3--------------------->6------------------------>7----------------------->3
    diff 2                diff3                         diff 1                     difference 4


total sum of difference is 2+3+1+4=10 which is >=5 so output should be 1 right
if totalsum of diference less than 5 then 0 right?
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 250 total points
ID: 41789038
total sum of difference is 2+3+1+4=10 which is >=5 so output should be 1 right
You are not suppose to add differences up.  
The challenge stated  
return the number of "big" steps
and as Guy Hengel posted
if the absolute height differences is >=5, add up 1 to the result

You are not accumulating the steps. Your returned result is only 1 at the most because you have
    if(sum>=5){ result=1;}

Open in new window

also the following code
sum=sum+absDiff;

Open in new window

is not necessary. You need to return the number of big steps. You don't need to add up the differences.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41790622
You need to return the number of big steps.

5-------------->3--------------------->6------------------------>7----------------------->3
    diff 2                diff3                         diff 1                     difference 4

out of all above differences check if any one of them greater than equal to 5 if yes return 1??

as above differences 2, 3,1, 4 none of them greater than 5 so return 0??

please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 41790623
public int bigHeights(int[] heights, int start, int end) {
 int diff=0;
  int sum=0;
  int absDiff=0;
  int result=0;
  for(int i=start; i<end; i++){
    
    diff=heights[i+1]-heights[i];
   // absDiff=Math.abs(diff);
   // sum=sum+absDiff;
    if(diff>=5){
      result=1;
    }else if(diff<5){
      result=0;
    }
  }
  return result;
}

Open in new window

like above?
i am failing some tests. please advise
Expected      Run            
bigHeights([5, 3, 6, 7, 2], 2, 4) → 1      0      X      
bigHeights([5, 3, 6, 7, 2], 0, 1) → 0      0      OK      
bigHeights([5, 3, 6, 7, 2], 0, 4) → 1      0      X      
bigHeights([5, 3, 6, 7, 3], 0, 4) → 0      0      OK      
bigHeights([5, 3, 6, 7, 2], 1, 1) → 0      0      OK      
bigHeights([5, 13, 6, 7, 2], 1, 2) → 1      0      X      
bigHeights([5, 13, 6, 7, 2], 0, 2) → 2      0      X      
bigHeights([5, 13, 6, 7, 2], 1, 4) → 2      0      X      
bigHeights([5, 13, 6, 7, 2], 0, 4) → 3      0      X      
bigHeights([5, 13, 6, 7, 2], 0, 3) → 2      0      X      
bigHeights([1, 2, 3, 4, 5, 4, 3, 2, 10], 0, 3) → 0      0      OK      
bigHeights([1, 2, 3, 4, 5, 4, 3, 2, 10], 4, 8) → 1      1      OK      
bigHeights([1, 2, 3, 14, 5, 4, 3, 2, 10], 0, 3) → 1      1      OK      
bigHeights([1, 2, 3, 14, 5, 4, 3, 2, 10], 7, 8) → 1      1      OK      
bigHeights([1, 2, 3, 14, 5, 4, 3, 2, 10], 3, 8) → 2      1      X      
bigHeights([1, 2, 3, 14, 5, 4, 3, 2, 10], 2, 8) → 3      1      X      
other tests
X
0
 
LVL 27

Expert Comment

by:rrz
ID: 41790694
    if(diff>=5){
      result=1;

Open in new window

That keeps the result at 1 at the most. You are not summing the number of big steps. Also, you are just finding the big up steps. What about the big down steps?
 
else if(diff<5){
      result=0;
    }

Open in new window

That should be removed.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41791349
public int bigHeights(int[] heights, int start, int end) {
 int diff=0;
  int sum=0;
  int absDiff=0;
  int result=0;
  for(int i=start; i<end; i++){
    
    diff=Math.abs(heights[i+1]-heights[i]);
   // absDiff=Math.abs(diff);
   // sum=sum+absDiff;
    if(diff>=5){
      result=result+1;
    }
  }
  return result;
}

Open in new window

something like above? it passes all tests. please advise on improvements, alternate approaches?
0
 
LVL 27

Expert Comment

by:rrz
ID: 41791389
something like above?
Yes,  but if you cleaned it up, then it would be more readable.  
public int bigHeights(int[] heights, int start, int end) {
  int diff = 0;
  int result = 0;
  for(int i = start; i < end; i++){
    diff = Math.abs(heights[i+1] - heights[i]);
    if(diff >= 5){
      result++;
    }
  }
  return result;
}

Open in new window

0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

751 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