Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #39159

Re: Subtraction question. Yes, apparently I can't subtract

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Subtraction question. Yes, apparently I can't subtract
Date 2019-09-26 11:51 -0400
Organization A noiseless patient Spider
Message-ID <qmimpp$1lb$1@dont-email.me> (permalink)
References <9884f2a0-2655-4ca4-bec6-06501e96129c@googlegroups.com>

Show all headers | View raw


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.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

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

csiph-web