• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to skip one element in an array?  RSS feed

 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

How to skip one element in an array and continue looping? For example, this question:

"Return the sum of the numbers in the array, returning 0 for an empty array. Except the number "13" is very unlucky, so it does not count and numbers that come immediately after a 13 also do not count".


sum13({1, 2, 13, 2, 1, 13}) → correct answer 4, but my answer is 3

sum13({13, 1, 2, 13, 2, 1, 13}) → correct answer 3, my answer is 0




Whether I use 'break' or 'continue', it does not work. How do I skip the number that comes after 13 and continue looping and adding to the sum?

All suggestions are appreciated. Thank you.
[ November 03, 2008: Message edited by: LS chin ]
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't post that sort of thing when I am around because I am one of those people who believe you should only use break in a switch block. It ought to give the correct result with "continue," however.

Try putting the if (nums[i] != 13) test around the line where you add. That is simpler and easier to read.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should also check out the previous element (as stated in the problem description):

Please note that i > 0 check; without it the code fails if the first element is not 13.

Also, you shouldn't use break since that will abort the entire loop; any number afterwards is discarded.
 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,
Thank you! The code works.

Originally posted by Rob Prime:
You should also check out the previous element (as stated in the problem description):

Please note that i > 0 check; without it the code fails if the first element is not 13.

Also, you shouldn't use break since that will abort the entire loop; any number afterwards is discarded.


I knew it had something to do with 'nums[i-1]' or 'nums[i+1] but I didn't know where to put it. :roll: Now, I see it~.


Thank you.
 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,

Thank you! You are right. Got to use 'continue' instead of 'break', and combined with the new test conditions, the code is now perfect.

Originally posted by Campbell Ritchie:
You shouldn't post that sort of thing when I am around because I am one of those people who believe you should only use break in a switch block. It ought to give the correct result with "continue," however.




Thank you!
[ November 03, 2008: Message edited by: LS chin ]
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome. Let's see if it will work in old-fashioned "structured programming."
 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,

I tried your code and... nope, it doesn't work. Try putting your code in this link here and you'll see the answers to some of the arrays are correct and some aren't.

Originally posted by Campbell Ritchie:
You're welcome. Let's see if it will work in old-fashioned "structured programming."


Enjoy the puzzles.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because Campbell hasn't used DeMorgan's law properly

The negation of

is

In this case, the <= can be replaced with == since i cannot be negative.

In Campbell's code, if the first element was 13 it would never be ignored.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never realised it was a Javabat.

It is the test; it won't recognise anything when i == 0. It works if you use de Morgan's rule.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I didn't realise Rob was (as usual) about to post 2 minutes before I did.
 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob, Campbell,

Thank you. DeMorgan's law, eh? It's pretty challenging.

Btw, can you please explain the test condition again? The question says "Return the sum of the numbers in the array, except the number "13" and the 'number that come immediately' after a 13" (i.e. only one number that comes immediately after 13 is not counted, and the rest are counted, right?).

So, why do we use nums[i-1] instead of nums[i+1]?

Originally posted by Rob Prime:
You should also check out the previous element (as stated in the problem description):


Sorry for the trouble.

Thanks again.


Edit: Wait, I think I got it!

if nums[i] == 13

nums[i - 1] == 13 // ----> is the array element that comes AFTER a 13, right?
[ November 04, 2008: Message edited by: LS chin ]
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by LS chin:
Thank you. DeMorgan's law, eh? It's pretty challenging.

It's not that hard, and comes in two parts.

Part 1:
!(A && B) is the same as !A || !B

Part 2:
!(A || B) is the same as !A && !B

In this case B is a bit more complex, and has DeMorgan applied again.


Edit: Wait, I think I got it!

if nums[i] == 13

nums[i - 1] == 13 // ----> is the array element that comes AFTER a 13, right?

i is the index of the element after 13 yes, so you are checking if its previous element is 13 yes. With num[i + 1] == 13 you would be checking if the next element is 13, which you should use if you should skip the number before 13.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was taught de Morgan's law was "break the line and change the sign" which rhymes nicely but only works when you can draw _____ above the symbols.
In this version of Boolean logic, an overscore means "not" a + means "or" and a . means "and."


and



In both cases vice versa applies.

[edit]Add code tags[/edit]
[ November 04, 2008: Message edited by: Campbell Ritchie ]
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's exactly what I said, but with images
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is the same law. One person will remember it one way, another the other way.
 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Rob, Campbell,

Thank you both very much!
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public int sum13(int[] nums) { 1 2 13 2 5
int sum = 0;
for (int i=0; i<nums.length; i++) {
if (nums[i] == 13) {
i++;
continue;
} else {
sum = sum + nums[i];
}
}
return sum;
}
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
re: James Tharakan

Nice, reads much easier than the other solution, I think. Also, you dont need the continue, as follows:


[ November 05, 2008: Message edited by: Tom Johnson ]
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is a nice solution, Tom Johnson.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sure is.

I just don't like changing i within the loop as well, but that stems from my education. I've learned to write loops which you can prove are correct, and incrementing i within the loop would make proving correctness a lot harder.
 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I do agree its a little unusual to increment inside the loop as well, perhaps confusing. However I think


is rather confusing as well, until you have a good look at it. As with most of these kind of things, its a personal choice as to which you prefer, rather than which is "better" I reckon.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neither is better, they both do the job perfectly and both have its quirks.
 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that's what I was implying....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!