Uploaded by shopaloka shopaloka

Google Foobar Challenge

advertisement
GOOGLE FOOBAR CHALLENGE
========================
CHALLENGE 1
=============
Re-ID ===== There's some unrest in the minion ranks: minions with ID numbers like "1", "42", and other "good" numbers have been lording it over the poor minions who are stuck with more boring IDs. To quell the unrest, Commander Lambda has tasked you with reassigning everyone new random IDs based on a Completely Foolproof Scheme. Commander Lambda has concatenated the prime numbers in a single long string: "2357111317192329...". Now every minion must draw a number from a hat. That number is the starting index in that string of primes, and the minion's new ID number will be the next five digits in the string. So if a minion draws "3", their ID number will be "71113". Help the Commander assign these IDs by writing a function solution(n) which takes in the starting index n of Lambda's string of all primes, and returns the next five digits in the string. Commander Lambda has a lot of minions, so the value of n will always be between 0 and 10000. Languages ========= To provide a Java solution, edit Solution.java To provide a Python solution, edit solution.py Test cases ========== Your code should pass the following test cases. Note that it may also be run against hidden test cases not shown here. -- Java cases -- Input: Solution.solution(0) Output: 23571 Input: Solution.solution(3) Output: 71113 -- Python cases -- Input: solution.solution(0) Output: 23571 Input: solution.solution(3) Output: 71113 Use verify [file] to test your solution and see how it does. When you are finished editing your code, use submit [file] to submit your answer. If your solution passes the test cases, it will be removed from your home folder.
========
ANSWER
========
def is_prime(num):
if num < 2:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True
def solution(n):
primes = ""
num = 2
while len(primes) < n + 5:
if is_prime(num):
primes += str(num)
num += 1
return primes[n : n + 5]
The problem requires us to generate minion IDs based on a sequence of prime numbers. We need to return the next five digits in the sequence, starting from a given index.
Here's a step-by-step breakdown of the solution:
Helper Function: is_prime
This function takes a number num as input and checks if it is prime.
It iterates from 2 to the square root of num and checks if num is divisible by any number in that range.
If it finds a divisor, it returns False, indicating that the number is not prime. Otherwise, it returns True.
Main Function: solution
This function takes an index n as input and generates the minion ID based on the prime number sequence.
It initializes an empty string variable called primes to store the prime numbers.
It initializes a variable num to 2 and starts iterating through the numbers.
In each iteration, it checks if num is prime using the is_prime helper function.
If num is prime, it appends it to the primes string until the length of primes is at least n + 5.
Once we have a sufficient number of prime digits in primes, we return the substring of length 5 starting from index n.
The solution generates prime numbers dynamically until it has enough digits to fulfill the request for the given index. It then extracts the desired substring from the generated prime numbers and returns it as the minion ID.
============
CHALLENGE 2
============
Numbers Station Coded Messages ============================== When you went undercover in Commander Lambda's organization, you set up a coded messaging system with Bunny Headquarters to allow them to send you important mission updates. Now that you're here and promoted to Henchman, you need to make sure you can receive those messages -- but since you need to sneak them past Commander Lambda's spies, it won't be easy! Bunny HQ has secretly taken control of two of the galaxy's more obscure numbers stations, and will use them to broadcast lists of numbers. They've given you a numerical key, and their messages will be encrypted within the first sequence of numbers that adds up to that key within any given list of numbers. Given a non-empty list of positive integers l and a target positive integer t, write a function solution(l, t) which verifies if there is at least one consecutive sequence of positive integers within the list l (i.e. a contiguous sub-list) that can be summed up to the given target positive integer t (the key) and returns the lexicographically smallest list containing the smallest start and end indexes where this sequence can be found, or returns the array [-1, -1] in the case that there is no such sequence (to throw off Lambda's spies, not all number broadcasts will contain a coded message). For example, given the broadcast list l as [4, 3, 5, 7, 8] and the key t as 12, the function solution(l, t) would return the list [0, 2] because the list l contains the sub-list [4, 3, 5] starting at index 0 and ending at index 2, for which 4 + 3 + 5 = 12, even though there is a shorter sequence that happens later in the list (5 + 7). On the other hand, given the list l as [1, 2, 3, 4] and the key t as 15, the function solution(l, t) would return [-1, -1] because there is no sub-list of list l that can be summed up to the given target value t = 15. To help you identify the coded broadcasts, Bunny HQ has agreed to the following standards: - Each list l will contain at least 1 element but never more than 100. - Each element of l will be between 1 and 100. - t will be a positive integer, not exceeding 250. - The first element of the list l has index 0. - For the list returned by solution(l, t), the start index must be equal or smaller than the end index. Remember, to throw off Lambda's spies, Bunny HQ might include more than one contiguous sublist of a number broadcast that can be summed up to the key. You know that the message will always be hidden in the first sublist that sums up to the key, so solution(l, t) should only return that sublist. Languages ========= To provide a Python solution, edit solution.py To provide a Java solution, edit Solution.java Test cases ========== Your code should pass the following test cases. Note that it may also be run against hidden test cases not shown here. -- Python cases -- Input: solution.solution([1, 2, 3, 4], 15) Output: -1,-1 Input: solution.solution([4, 3, 10, 2, 8], 12) Output: 2,3 -- Java cases -- Input: Solution.solution({1, 2, 3, 4}, 15) Output: -1,-1 Input: Solution.solution({4, 3, 10, 2, 8}, 12) Output: 2,3
=========
ANSWER
=========
Python code
def solution(l, t):
n = len(l)
start = 0
end = 0
total = l[0]
if total == t:
return [0, 0]
while end < n:
if total < t:
end += 1
if end < n:
total += l[end]
elif total > t:
total -= l[start]
start += 1
else:
return [start, end]
return [-1, -1]
we are using a sliding window approach to find the sub-list that sums up to the target t. Here's an explanation of the key steps:
1. We initialize the start and end pointers to the first element of the list, and the total variable to the value of the first element.
2. We check if the initial total is equal to the target t. If so, we return the indexes [0, 0] since the sub-list consists of only the first element.
3. We enter a while loop that continues until the end pointer reaches the end of the list.
4. Inside the loop, we have three cases to handle:
If the current total is less than the target t, we expand the sub-list by moving the end pointer to the next element and add its value to the total.
If the current total is greater than the target t, we need to shrink the sub-list. To do this, we move the start pointer to the next element and subtract its value from the total.
If the current total is equal to the target t, we have found a valid sub-list that sums up to the target. We return the indexes [start, end] as the solution.
If we complete the loop without finding a valid sub-list, we return [-1, -1] to indicate that no such sub-list exists.
By using the sliding window technique, we are able to efficiently traverse the list and adjust the sub-list based on the current sum. This approach has a time complexity of O(n), where n is the length of the list, as we iterate through the list only once.
Download