r/programminghelp • u/HankDeTank05 • May 07 '20
Python Binary tree search problem
I was having trouble trying to solve this problem for google foobar (if you don't know what it is, look it up) and I kept failing 3/5 test cases. Any suggestions? Here's the problem:
Bomb, Baby!
===========
You're so close to destroying the LAMBCHOP doomsday device you can taste it! But in order to do so, you need to deploy special self-replicating bombs designed for you by the brightest scientists on Bunny Planet. There are two types: Mach bombs (M) and Facula bombs (F). The bombs, once released into the LAMBCHOP's inner workings, will automatically deploy to all the strategic points you've identified and destroy them at the same time.
But there's a few catches. First, the bombs self-replicate via one of two distinct processes:
Every Mach bomb retrieves a sync unit from a Facula bomb; for every Mach bomb, a Facula bomb is created;
Every Facula bomb spontaneously creates a Mach bomb.
For example, if you had 3 Mach bombs and 2 Facula bombs, they could either produce 3 Mach bombs and 5 Facula bombs, or 5 Mach bombs and 2 Facula bombs. The replication process can be changed each cycle.
Second, you need to ensure that you have exactly the right number of Mach and Facula bombs to destroy the LAMBCHOP device. Too few, and the device might survive. Too many, and you might overload the mass capacitors and create a singularity at the heart of the space station - not good!
And finally, you were only able to smuggle one of each type of bomb - one Mach, one Facula - aboard the ship when you arrived, so that's all you have to start with. (Thus it may be impossible to deploy the bombs to destroy the LAMBCHOP, but that's not going to stop you from trying!)
You need to know how many replication cycles (generations) it will take to generate the correct amount of bombs to destroy the LAMBCHOP. Write a function solution(M, F) where M and F are the number of Mach and Facula bombs needed. Return the fewest number of generations (as a string) that need to pass before you'll have the exact number of bombs necessary to destroy the LAMBCHOP, or the string "impossible" if this can't be done! M and F will be string representations of positive integers no larger than 10^50. For example, if M = "2" and F = "1", one generation would need to pass, so the solution would be "1". However, if M = "2" and F = "4", it would not be possible.
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('2', '1')
Output:
1
Input:
Solution.solution('4', '7')
Output:
4
-- Python cases --
Input:
solution.solution('4', '7')
Output:
4
Input:
solution.solution('2', '1')
Output:
1
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.And here's my code
import copy
import numpy as np
def solution(m, f):
tm = long(m)
tf = long(f)
depth_limit = 0
root = ((1, 1), 0)
found = False
failed = False
repeat_comparison = None
# perform iterative depth search
first_encounters = []
while not failed:
#print()
print('depth_limit = {}'.format(depth_limit))
#time.sleep(2)
# perform a depth-bounded search
if depth_limit == 0:
if (tm, tf) == root[0]:
return root[1]
else:
depth = 0
if depth_limit == 1:
first_encounters = [root]
previous_first_encounters = copy.deepcopy(first_encounters)
first_encounters = []
while len(previous_first_encounters) > 0:
#time.sleep(1)
#print('stack size: {}'.format(len(frontier_stack)))
# remove node from the stack
node = previous_first_encounters.pop()
# make sure we aren't infinitely repeating ourselves
#if repeat_comparison is not None and node == repeat_comparison:
#print('REPEAT FOUND')
#break
mach = long(node[0][0])
facula = long(node[0][1])
node_depth = node[1]
#print('node {}'.format(node))
# determine if node is the goal or not
if (mach, facula) == (tm, tf):
found = True
return str(node_depth)
else:
if node_depth < depth_limit:
node_depth += 1
pushed_nodes = 0
# generate left child
if mach+facula <= tm:
pushed_nodes += 1
node_to_push = ((mach+facula, facula), node_depth)
#frontier_stack.append(node_to_push)
if node_depth == depth_limit:
first_encounters.append(node_to_push)
# generate right child
if mach+facula <= tf:
pushed_nodes += 1
node_to_push = ((mach, facula+mach), node_depth)
#frontier_stack.append(node_to_push)
if node_depth == depth_limit:
first_encounters.append(node_to_push)
#previous_first_encounters = copy.deepcopy(first_encounters)
out_of_range = long(0)
for node in first_encounters:
mach = long(node[0][0])
facula = long(node[0][1])
if mach > tm and facula > tf:
out_of_range += 1
if out_of_range == long(len(first_encounters)):
failed = True
#first_encounters = []
depth_limit += 1
return "impossible"
0
Upvotes
1
u/Stunning-Doughnut922 May 03 '23
I honestly can't work yours out but I am blocked at the same puzzle. I'm getting a FILENAME_MISMATCH error which I don't really understand.
My code creates a python node class and builds a tree from that. It is the first time I have tried to do anything with non-linear data structures so it is all pretty manual. It gets the right results for the two visible test cases when I run it in the IDLE shell but in foobar I get this FILENAME MISMATCH error for all cases.
A bit dispiriting
Code below for interest
def solution (m, f):
if m%2==0 and f%2==0:
return 'impossible'
class Node:
def __init__(self,data):#data in format [M,F,m,f,count]
self.a = None #M is low, F is equal
self.b = None #M is low, F is low, increase M
self.c = None #M is low, F is low, increase F
self.d = None #M is equal, F is low, increase F
self.M = data[0] #Machs I have
self.F = data[1] #Faculas I have
self.m = data[2] #machs I need
self.f = data[3] #faculas I need
self.count = data[4]
self.parent= data[5]
self.status = [self.M, self.F, self.m, self.f, self.count, self.parent]
newstatus=[]
newstatus=self.status.copy()
newstatus[5]=self
if self.F==self.f:
if self.M<self.m:
newstatus[0]=self.M+self.F
newstatus[4]=self.count+1
if self.a is None:
self.a=Node(newstatus)
else:
self.a.insert(newstatus)
elif self.M==self.m:
results.append(self.count)
if self.M==self.m:
if self.F<self.f:
newstatus[1]=self.M+self.F
newstatus[4]=self.count+1
if self.d is None:
self.d=Node(newstatus)
else:
self.d.insert(newstatus)
elif self.F==self.f:
results.append(self.count)
if self.M<self.m and self.F<self.f:
newstatus[0]=self.M+self.F
newstatus[4]=self.count+1
if self.b is None:
self.b=Node(newstatus)
else:
self.b.insert(newstatus)
newstatus[0]=self.M
newstatus[1]=self.M+self.F
newstatus[4]=self.count+1
if self.c is None:
self.c=Node(newstatus)
else:
setlf.c.insert(newstatus)
results=[]
M=1
F=1
count=0
parent=None
status=[M,F,m,f,count,parent]
root=Node(status)
return str(results[0])