## Google FooBar – Level 2 – Please Pass the Coded Messages – Python

I’ve recently been learning Python and was invited to the Google Foobar challenge. Google Foobar is an invitation only event that appears if Google detects that you’ve been searching for Python or Java related topics. You can accept the even and complete 5 levels which if you complete you will be able to fill out your personal information for a chance at an interview at Google. My skills aren’t the best but I had some trouble figuring out some of the levels so I decided to publish my submissions in hopefully helping someone out.

### Objective

To complete the Google Foobar Level 2 challenge, Please Pass the Coded Messages

### Instructions

When I first wrote the answer for this puzzle it worked on my computer but Google kept giving an error code around for:

1 | num= ''.join(str(x) for x in subset) |

I couldn’t figure out another solution to the puzzle. I ended up going a different route. Both my solutions are below but only the second will work. If anyone knows how to get the first solution to work please let me know.

### The Challenge

You need to pass a message to the bunny prisoners, but to avoid detection, the code you agreed to use is… obscure, to say the least. The bunnies are given food on standard-issue prison plates that are stamped with the numbers 0-9 for easier sorting, and you need to combine sets of plates to create the numbers in the code. The signal that a number is part of the code is that it is divisible by 3. You can do smaller numbers like 15 and 45 easily, but bigger numbers like 144 and 414 are a little trickier. Write a program to help yourself quickly create large numbers for use in the code, given a limited number of plates to work with.

You have L, a list containing some digits (0 to 9). Write a function answer(L) which finds the largest number that can be made from some or all of these digits and is divisible by 3. If it is not possible to make such a number, return 0 as the answer. L will contain anywhere from 1 to 9 digits. The same digit may appear multiple times in the list, but each element in the list may only be used once.

### Test cases

1 2 3 4 5 6 7 8 9 | Inputs: (int list) l = [3, 1, 4, 1] Output: (int) 4311 Inputs: (int list) l = [3, 1, 4, 1, 5, 9] Output: (int) 94311 |

### Solution 1

This was my solution that works on my setup but doesn’t work when submitting to Google:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | from itertools import permutations def answer(l): highest_num=0 for i in range(1, len(l)+1): for subset in permutations(l, i): if len(subset) >= 1: num= ''.join(str(x) for x in subset) num=int(num) mod_check=num%3 if mod_check == 0: if num > highest_num: highest_num = num return highest_num |

### Solution 2

This solution will work when submitting your code to Google:

1 2 3 4 5 6 7 8 | from itertools import combinations def answer(l): l.sort(reverse = True) for i in reversed(range(1, len(l) + 1)): for tup in combinations(l, i): if sum(tup) % 3 == 0: return int(''.join(map(str, tup))) return 0 |