Interview Questions

Lucky sevens
A user who was applying to App Academy posted this question on Coderbyte:
Write a function called lucky_sevens which takes an array of integers and returns true if any three consecutive elements sum to 7.
To solve this challenge we'll simply loop through the array starting at the 3rd position, and checking if the number at this index plus the two previous elements sums to 7. We continue doing this as we loop through the entire array. Once we find three elements that sum to 7, we simply return true. If we reach the end of the array without finding elements that sum to 7, we return false. Below is some code in JavaScript and Python with step-by-step comments.

## Code

```function lucky_sevens(arr) {

// if less than 3 elements then this challenge is not possible
if (arr.length < 3) {
return "not possible";
}

// because we know there are at least 3 elements we can
// start the loop at the 3rd element in the array (i=2)
// and check it along with the two previous elements (i-1) and (i-2)
for (var i = 2; i < arr.length; i++) {
if (arr[i] + arr[i-1] + arr[i-2] === 7) {
return true;
}
}

// if loop is finished and no elements summed to 7
return false;

}

lucky_sevens([2, 1, 5, 1, 0]);
```
```def lucky_sevens(arr):

# if less than 3 elements then this challenge is not possible
if len(arr) < 3:
return "not possible"

# because we know there are at least 3 elements we can
# start the loop at the 3rd element in the array (i=2)
# and check it along with the two previous elements (i-1) and (i-2)
for i in range(2, len(arr)):
if (arr[i] + arr[i-1] + arr[i-2] == 7):
return True

# if loop is finished and no elements summed to 7
return False

print lucky_sevens([2, 1, 5, 1, 0])
```
mrdaniel published this on 12/4/15 |
• +
• 7
• -
• don't listen to Anhtaihuman... we're not talking about whether three consecutive integers are 7, we are talking about whether they sum to 7. Your function should have returned true since 1 + 5 + 1 === 7
• +
• 0
• -
• ```def lucky_sevens(arr):
str_arr = [str(num) for num in arr]
return '777' in ''.join(str_arr) or (len(arr) < 3) * 'not possible'
```
The easiest way I know.
• +
• 0
• -
• To display code include the following tags: ```function luckySevens(arr){ for (let i = 0; i< arr.length; i++){ if (arr.slice(i,i+2).reduce((a,c)=>a+c) === 7) return true else if (arr.slice(i,i+3).reduce((a,c)=>a+c) === 7) return true } return false }```
• +
• -1
• -
• To display code include the following tags: ```package main; import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; import java.util.Scanner; import java.util.ArrayList; public class MainCls { public static void main(String[] args){ Scanner input = new Scanner(System.in); String unos = input.nextLine(); String[] strniz = unos.split(","); ArrayList<Integer> nizbr = new ArrayList<Integer>(1); for(String k: strniz){ nizbr.add(Integer.parseInt(k)); } for(int i = 2; i < nizbr.size(); i++){ if(nizbr.get(i)+nizbr.get(i-1)+nizbr.get(i-2)==7){ System.out.println("Luckyyy Seeeeveeens"); break; } } } }```
• +
• -1
• -
• nvm i got it
• +
• -1
• -
• I'm running the same code as mrdaniel and everything but i get false when calling 1,5,1 even with your code. What am i doing wrong? This is on Atom.
• +
• -21
• -
• A simpler way to do this would be to use RegEx. ```function lucky_sevens(arr) { return /777/g.test(arr.join('')); } lucky_sevens([2, 1, 5, 1, 0, 7, 7, 0, 7]);```