bluepulse5577
BAN USERGood job shawn! Here's a working python solution:
def find_aggregated_number(A):
for i in range(1,int(len(A)/2)):
for j in range(i+1,int(len(A)*2/3)+1):
if(check_aggregate(i,j,A)):
return True
return False
def check_aggregate(i,j,A):
result = A[0:i] + A[i:j]
first = int(A[0:i])
second = int(A[i:j])
while(len(A) > len(result)):
third = first + second
result = result + str(third)
first = second
second = third
return (len(result)==len(A)) and (result == A)
for A in ["112358","122436","1299111210","112112224"]:
if (find_aggregated_number(A)):
print("{} is an aggregated number".format(A))
else:
print("{} is not an aggregated number".format(A))
Nice logic! Here is the working python code using quicksort:
A = [2,3,1,4,5,6,7,8]
B = [2,1,3,8,7,6,5,4]
def find_if_equal(X,Y):
if ((len(X) != len(Y)) or (X[0]!=Y[0])):
return False
else:
if(len(X) == 1): return True
Amin = [x for x in X[1:] if x<=X[0]]
Amax = [x for x in X[1:] if x>X[0]]
Bmin = [y for y in Y[1:] if y<=Y[0]]
Bmax = [y for y in Y[1:] if y>Y[0]]
if(Amin):
if(Bmin.count(Amin[0]) == 0): return False
if(len(Bmin)>1 and Amin[0]!= Bmin[0]):
i = Bmin.index(Amin[0])
Bmin[0],Bmin[i] = Bmin[i],Bmin[0]
if (not find_if_equal(Amin,Bmin)): return False
if(Amax):
if(Bmax.count(Amax[0]) == 0): return False
if((len(Bmax)>1) and (Amax[0]!=Bmax[0])):
i = Bmax.index(Amax[0])
Bmax[0],Bmax[i] = Bmax[i],Bmax[0]
if (not find_if_equal(Amax,Bmax)): return False
return True
if (find_if_equal(A,B)):
print("Two arrays A:{} and B:{} will make identical Binary Search Trees".format(A,B))
else:
print("Two arrays A:{} and B:{} will not make identical BST's".format(A,B))
Initially i walk through all the nodes and store the coordinates of guards and rooms. I then calculate the absolute difference of the coordinates from room to guards to get the result.
Here is a working python code:
in_matrix = [[0,0,0],['B','G','G'],['B',0,0]]
room_pointers = []
guard_pointers = []
out_matrix = in_matrix
"define a class to store the coordinates of rooms and guards"
class coordinates:
def __init__(self,x,y):
self.x = x
self.y = y
def calculate_steps():
for i in range(len(in_matrix)):
for j in range(len(in_matrix[0])):
if(in_matrix[i][j]==0):
room_pointers.append(coordinates(i,j))
elif(in_matrix[i][j]=='G'):
guard_pointers.append(coordinates(i,j))
for i in range(len(in_matrix)):
for j in range(len(in_matrix[0])):
if(in_matrix[i][j] == 0):
room = room_pointers[0]
sum = 5
for guard in guard_pointers:
temp = abs(guard.x-room.x)+abs(guard.y-room.y)
if(temp < sum): sum = temp
out_matrix[i][j] = sum
del room_pointers[0]
print("The steps from a room to nearest Guard room is: ")
print(out_matrix)
calculate_steps()
Working python solution:
class Node:
def __init__(self,val):
self.l_child = None
self.r_child = None
self.val = val
def binary_insert(root,node):
if root is None:
root = node
elif(root.val > node.val):
if(root.l_child == None):
root.l_child = node
else:
binary_insert(root.l_child,node)
else:
if(root.r_child == None):
root.r_child = node
else:
binary_insert(root.r_child,node)
r = Node(8)
binary_insert(r,Node(3))
binary_insert(r,Node(1))
binary_insert(r,Node(6))
binary_insert(r,Node(4))
binary_insert(r,Node(7))
binary_insert(r,Node(10))
binary_insert(r,Node(14))
binary_insert(r,Node(13))
arr1 = []
arr2 = []
def add_to_arr1(node):
if node is None:
return
else:
arr1.append(node)
add_to_arr1(node.l_child)
def add_to_arr2(node):
if node is None:
return
else:
arr2.append(node)
add_to_arr2(node.r_child)
def find_sum(r,x):
if r is None:
return
add_to_arr1(r.l_child)
add_to_arr2(r)
while(arr1 and arr2):
if(arr1[-1].val + arr2[-1].val > x):
if(arr2[-1].l_child is None):
arr2.pop()
else:
a=arr2.pop()
arr2.append(a.l_child)
elif(arr1[-1].val + arr2[-1].val < x):
if(arr1[-1].r_child is None):
arr1.pop()
else:
a=arr1.pop()
arr1.append(a.r_child)
else:
print("result found. {} and {} sum up to {}".format(arr1[-1].val,arr2[-1].val,x))
return [arr1[-1],arr2[-1]]
break
result = find_sum(r,11)
if result is None:
print("Search not successful")
Just use xor to reduce the complexity of toggle operation.
#include <stdbool.h>
#include <stdio.h>
const int n = 10;
int onoffstatus[10] = {0};
bool isOn(int i)
{
if(i < n){
if (onoffstatus[i]==1) return true;
else return false;
} else
return false;
}
void toggle(int start, int end)
{
if(start < 0 || end >= 10) return;
int i;
for(i=start;i<end+1;i++){
onoffstatus[i] = onoffstatus[i] ^ 1;
}
}
void main()
{
bool a = isOn(6);
printf("%d \n",a);
toggle(2,7);
a = isOn(6);
printf("%d \n",a);
toggle(2,7);
a = isOn(6);
printf("%d \n",a);
}
Repgradyjvierra, Animator at Alliance Global Servies
Je suis Grady de Phoenix USA. Je travaille en tant que technicien de laboratoire clinique dans la société Samuels Men ...
RepMelissaJoy, Associate at Bocada
Hi, I am Melissa, a purchasing agent from Texas. Kind of a good Samaritan, terrible athlete, but extremely blessed in ...
Here is a working python solution using Shawn's logic:
- bluepulse5577 September 21, 2015