Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #39155 > unrolled thread
| Started by | cdalten@gmail.com |
|---|---|
| First post | 2019-09-26 06:54 -0700 |
| Last post | 2019-09-26 11:51 -0400 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.java.programmer
Subtraction question. Yes, apparently I can't subtract cdalten@gmail.com - 2019-09-26 06:54 -0700
Re: Subtraction question. Yes, apparently I can't subtract chad altenburg <cdalten@gmail.com> - 2019-09-26 06:59 -0700
Re: Subtraction question. Yes, apparently I can't subtract chad altenburg <cdalten@gmail.com> - 2019-09-26 07:05 -0700
Re: Subtraction question. Yes, apparently I can't subtract Eric Douglas <e.d.programmer@gmail.com> - 2019-09-26 07:57 -0700
Re: Subtraction question. Yes, apparently I can't subtract nobody@example.org (Scott) - 2019-09-26 16:38 +0000
Re: Subtraction question. Yes, apparently I can't subtract Eric Sosman <esosman@comcast-dot-net.invalid> - 2019-09-26 11:51 -0400
| From | cdalten@gmail.com |
|---|---|
| Date | 2019-09-26 06:54 -0700 |
| Subject | Subtraction question. Yes, apparently I can't subtract |
| Message-ID | <9884f2a0-2655-4ca4-bec6-06501e96129c@googlegroups.com> |
Given the following
"Given an array of ints, return true if the array contains two 7's next to each other, or there are two 7's separated by one element, such as with {7, 1, 7}."
I came up with the following...
public boolean has77(int[] nums) {
for (int i = 0; i < nums.length - 2; i++) {
if (nums[i] == 7 && nums[i+1] == 7) return true; //
if (nums[i+1] == 7 && nums[i+2] == 7) return true; //
if (nums[i] == 7 && nums[i+2] == 7) return true; //
}
return false;
}
The question revolves around the following case...
has77([1, 7, 7])
How does the whatever know to check [1,7,7] when 2 is subtracted from the length?
Wouldn't the loop or the computer just see something like [1] when it traverses the list? if so, then how can it do something like...
if (nums[i] == 7 && nums[i+1] == 7) return true;
[toc] | [next] | [standalone]
| From | chad altenburg <cdalten@gmail.com> |
|---|---|
| Date | 2019-09-26 06:59 -0700 |
| Message-ID | <92486ba8-6210-4a0a-a64d-0e45a0903968@googlegroups.com> |
| In reply to | #39155 |
On Thursday, September 26, 2019 at 6:54:59 AM UTC-7, chad altenburg wrote:
> Given the following
>
> "Given an array of ints, return true if the array contains two 7's next to each other, or there are two 7's separated by one element, such as with {7, 1, 7}."
>
> I came up with the following...
>
> public boolean has77(int[] nums) {
> for (int i = 0; i < nums.length - 2; i++) {
> if (nums[i] == 7 && nums[i+1] == 7) return true; //
> if (nums[i+1] == 7 && nums[i+2] == 7) return true; //
> if (nums[i] == 7 && nums[i+2] == 7) return true; //
> }
>
> return false;
> }
>
>
> The question revolves around the following case...
>
> has77([1, 7, 7])
>
> How does the whatever know to check [1,7,7] when 2 is subtracted from the length?
>
> Wouldn't the loop or the computer just see something like [1] when it traverses the list? if so, then how can it do something like...
>
> if (nums[i] == 7 && nums[i+1] == 7) return true;
Actually, more to the point, how can it do something like...
if (nums[i+1] == 7 && nums[i+2] == 7) return true;
Because when I comment out this line, it returns false (which is incorrect).
[toc] | [prev] | [next] | [standalone]
| From | chad altenburg <cdalten@gmail.com> |
|---|---|
| Date | 2019-09-26 07:05 -0700 |
| Message-ID | <4089c2fe-4018-4829-8a76-63250ba0a23f@googlegroups.com> |
| In reply to | #39156 |
On Thursday, September 26, 2019 at 6:59:33 AM UTC-7, chad altenburg wrote:
> On Thursday, September 26, 2019 at 6:54:59 AM UTC-7, chad altenburg wrote:
> > Given the following
> >
> > "Given an array of ints, return true if the array contains two 7's next to each other, or there are two 7's separated by one element, such as with {7, 1, 7}."
> >
> > I came up with the following...
> >
> > public boolean has77(int[] nums) {
> > for (int i = 0; i < nums.length - 2; i++) {
> > if (nums[i] == 7 && nums[i+1] == 7) return true; //
> > if (nums[i+1] == 7 && nums[i+2] == 7) return true; //
> > if (nums[i] == 7 && nums[i+2] == 7) return true; //
> > }
> >
> > return false;
> > }
> >
> >
> > The question revolves around the following case...
> >
> > has77([1, 7, 7])
> >
> > How does the whatever know to check [1,7,7] when 2 is subtracted from the length?
> >
> > Wouldn't the loop or the computer just see something like [1] when it traverses the list? if so, then how can it do something like...
> >
> > if (nums[i] == 7 && nums[i+1] == 7) return true;
>
> Actually, more to the point, how can it do something like...
>
> if (nums[i+1] == 7 && nums[i+2] == 7) return true;
>
> Because when I comment out this line, it returns false (which is incorrect).
Wait, this is cool. I found that I could google subtraction!
https://en.wikipedia.org/wiki/Subtraction
[toc] | [prev] | [next] | [standalone]
| From | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| Date | 2019-09-26 07:57 -0700 |
| Message-ID | <013d1571-59e0-4ff4-9119-cc54c936db7d@googlegroups.com> |
| In reply to | #39155 |
On Thursday, September 26, 2019 at 9:54:59 AM UTC-4, chad altenburg wrote:
> Given the following
>
> "Given an array of ints, return true if the array contains two 7's next to each other, or there are two 7's separated by one element, such as with {7, 1, 7}."
>
> I came up with the following...
>
> public boolean has77(int[] nums) {
> for (int i = 0; i < nums.length - 2; i++) {
> if (nums[i] == 7 && nums[i+1] == 7) return true; //
> if (nums[i+1] == 7 && nums[i+2] == 7) return true; //
> if (nums[i] == 7 && nums[i+2] == 7) return true; //
> }
>
> return false;
> }
>
>
> The question revolves around the following case...
>
> has77([1, 7, 7])
>
> How does the whatever know to check [1,7,7] when 2 is subtracted from the length?
>
> Wouldn't the loop or the computer just see something like [1] when it traverses the list? if so, then how can it do something like...
>
> if (nums[i] == 7 && nums[i+1] == 7) return true;
1. I'm not sure what this has to do with 'subtraction'.
2. Your code is wrong.
3. Is this some sort of homework problem?
[toc] | [prev] | [next] | [standalone]
| From | nobody@example.org (Scott) |
|---|---|
| Date | 2019-09-26 16:38 +0000 |
| Message-ID | <5d8ce89e.1024112218@core> |
| In reply to | #39158 |
On Thu, 26 Sep 2019 07:57:56 -0700 (PDT), Eric Douglas <e.d.programmer@gmail.com> wrote: >On Thursday, September 26, 2019 at 9:54:59 AM UTC-4, chad altenburg wrote: >> if (nums[i] == 7 && nums[i+1] == 7) return true; > >1. I'm not sure what this has to do with 'subtraction'. >2. Your code is wrong. >3. Is this some sort of homework problem? It reminds me of the kinds of problem they have on on CodingBat.
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2019-09-26 11:51 -0400 |
| Message-ID | <qmimpp$1lb$1@dont-email.me> |
| In reply to | #39155 |
On 9/26/2019 9:54 AM, cdalten@gmail.com wrote:
> Given the following
>
> "Given an array of ints, return true if the array contains two 7's next to each other, or there are two 7's separated by one element, such as with {7, 1, 7}."
>
> I came up with the following...
>
> public boolean has77(int[] nums) {
> for (int i = 0; i < nums.length - 2; i++) {
> if (nums[i] == 7 && nums[i+1] == 7) return true; //
> if (nums[i+1] == 7 && nums[i+2] == 7) return true; //
> if (nums[i] == 7 && nums[i+2] == 7) return true; //
> }
>
> return false;
> }
This should *almost* work, but not quite. Try
has77(new int[] {7, 7}) and see if you can uncover
the problem.
(Aside: There's more than one way to skin a cat, and some
of the other ways may prove less cumbersome. Let's dress up
the scenario a little, and see if you can invent a different
approach. So, imagine people walking up to you one by one,
each announcing what day of the month he was born on. Because
you're a big fan of astrology you believe being born on the
seventh of Anywhen is important, and if you encounter two
sevenses in a row or separated by only one non-seven you will
jump for joy. But each person just walks up to you, announces,
and walks away; you get no opportunity to check his birth day a
second time. How might *you* decide when to jump? Can you get
the computer to use a similar method?)
> The question revolves around the following case...
>
> has77([1, 7, 7])
>
> How does the whatever know to check [1,7,7] when 2 is subtracted from the length?
>
> Wouldn't the loop or the computer just see something like [1] when it traverses the list? if so, then how can it do something like...
>
> if (nums[i] == 7 && nums[i+1] == 7) return true;
The loop will run for each `i' value from 0 through 0 and no
further; that much you've seen. But when `i' is 0, what is `i+1'?
And what is `i+2'? (Extra credit: What is `i+3', if you were to
compute it, and what trouble might `nums[i+3]' encounter?)
--
esosman@comcast-dot-net.invalid
Four hundred eighty-two days to go.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web