Jeff
Jeff

Reputation: 149

lucky sevens, sum of 3 consecutive numbers in array == 7

Write a function lucky_sevens(numbers), which takes in an array of integers and returns true if any three consecutive elements sum to 7.

Why isn't this producing an output of True? The last 3 values sum = 7.

 def lucky_sevens(numbers):
      x, y = 0, 3
      sum_of_numbers = sum(numbers[x:y])
      while (sum_of_numbers != 7) and (y < len(numbers)):
        x = x + 1
        y = y + 1
      if sum_of_numbers == 7:
        return True
      else:
        return False

    print(lucky_sevens([1,2,3,4,5,1,1]))

Upvotes: 1

Views: 2972

Answers (7)

Vinay Emmaadii
Vinay Emmaadii

Reputation: 145

def f(l):
    if len(l) < 3:
        return False
    for i in range(len(l)):
        if i+3 <= len(l):
            if sum(l[i:i+3]) == 7:
                return True
            continue
        else:
            return False

Upvotes: 0

Joe Ferndz
Joe Ferndz

Reputation: 8508

There are some very interesting responses here. Thought I will share my work as well.

def lucky_seven(num_list):
    if len(num_list) < 3: return False
    return any([True if sum(num_list[i:i+3]) == 7 else False for i in range(len(num_list)-2)])

print ('lucky seven for [1,2,3] is          :',lucky_seven([1,2,3]))
print ('lucky seven for [3,4] is            :',lucky_seven([3,4]))
print ('lucky seven for [3,4,0] is          :',lucky_seven([3,4,0]))
print ('lucky seven for [1,2,3,4,5,1,1] is  :',lucky_seven([1,2,3,4,5,1,1]))
print ('lucky seven for [1,2,3,4,-2,5,1] is :',lucky_seven([1,2,3,4,-2,5,1]))

The output of this will be:

lucky seven for [1,2,3] is          : False
lucky seven for [3,4] is            : False
lucky seven for [3,4,0] is          : True
lucky seven for [1,2,3,4,5,1,1] is  : True
lucky seven for [1,2,3,4,-2,5,1] is : True

Upvotes: 0

james pace
james pace

Reputation: 1

I just used this:

 def lucky_sevens(numbers):
        x = 0
        y = False
        for i in numbers:
            if i == 7:
                x += 7
                if x == 21:
                    y = True
            else:
                x = 0
        return y

Upvotes: 0

Satish Kale
Satish Kale

Reputation: 11

This code returns the consecutive elements in the Array whose sum is 7 and index of initial element.

  function lucky_seven(arr){
    let i=0;
    let lastIndex = 0;
    if(arr.length < 3){
      return false;
    }
    while(i <= lastIndex){
      let sum = 0;
      lastIndex = i + 3;
      let subArr = arr.slice(i,lastIndex);
      if(subArr.length === 3) {
        sum = subArr.reduce((acc, cur) => acc + cur);
        if(sum === 7){
          return {
            subArr: subArr,
            index: i
          };
        }
        i++;
      } else{
        return false;
     }
    }
  }
lucky_seven([3,2,1,4,2])

Upvotes: 0

Jason
Jason

Reputation: 2304

How about something as simple as

    def lucky_sevens(numbers):

        for x in range(len(numbers) - 2):

            if sum(numbers[x:x+3]) == 7:

                return True

        return False

Or with your original code, just cleaned up a little bit.

def lucky_sevens(numbers):

    if len(numbers) < 3:
        return False

    x, y = 0, 3

    sum_of_numbers = sum(numbers[x: y])

    while sum_of_numbers != 7 and y < len(numbers):
        x += 1
        y += 1
        sum_of_numbers = sum(numbers[x: y])

    if sum_of_numbers == 7:
        return True
    return False

Your error came in your while loop. As you were looping, sum_of_numbers was staying constant. Instead, you have to update it for every new x and y within the while loop.

Also some repetitive stuff like else: return False, can be simplified to return False, as it can only get to that line if sum_of_numbers == 7 is False.

Finally x = x + 1 can be written in the more common shorthand x += 1, the same going with y = y + 1.

Upvotes: 1

TheF1rstPancake
TheF1rstPancake

Reputation: 2378

This ought to do the trick:

def lucky_sevens(numbers):
    if len(numbers) < 3:
        return False
    return 7 in [sum(numbers[i:i+3]) for i in range(0, len(numbers)-2)]

print(lucky_sevens([1,2,3,4,5,1,1]))
# True

The list comprehension will move through your list 3 numbers at a time and compute the sum of each set of three integers. If 7 is in that list, then there are 3 consecutive numbers that sum to 7. Otherwise, there isn't.

The one caveat is that doing this sort of list comprehension requires that the list have more than 3 elements in it. That's why the if statement is there.

If you wanted to use your original code though, you just have to make a few adjustments. Your logic was all there, just needs a little cleaning.

def lucky_sevens(numbers):
  x, y = 0, 3
  sum_of_numbers = sum(numbers[x:y])
  while (sum_of_numbers != 7) and (y < len(numbers)):
    x = x + 1
    y = y + 1
    sum_of_numbers = sum(numbers[x:y])
  if sum_of_numbers == 7:
    return True
  else:
    return False

You simply needed to redo the sum within your while loop. That way, sum_of_numbers updates with each loop and each new selection of indices.

Upvotes: 0

The problem that when the function first gets called the sum_of_numbers variable gets assigned the value of the sum of the first 3 values in the list, and never gets updated with the new x,y values, you'd probably want to create a callback function to achieve that behavior.

As it stands, you'll need to move the sum statement into the while loop so the sum gets updated with the new x,y values:

def lucky_sevens(numbers):
    result = False
    x, y = 0, 3

    while (y <= len(numbers)):
        if sum(numbers[x:y]) == 7:
            result = True
            break
        x += 1
        y += 1
    return result

print(lucky_sevens([1,2,3,4,5,1,1]))

Upvotes: 2

Related Questions