Please help with part B This is part A def sumTwoarray sizea
Please help with part B.
This is part A.
def sumTwo(array, size_array, sum):
binmap = [0]*CONST_MAX
for i in range(0,size_array):
tmp = sum-array[i]
if (tmp>=0 and binmap[tmp]==1):
print \"sum of two numbers is\", array[i], \"and\", tmp
binmap[array[i]]=1
a = [10,11,2,3,15,5,8,19,-2,23,25,18,45]
T = 28
sumTwo(a, len(a), T)
Solution
implementation of part (a) seems to be incorrect, as in for loop, tmp can very well be -ve, and also we need to use hash table, as specified in part a.
I am returning array with numbers that make the sum, when solution is found and empty array when not, thus checking length of array can tell us whether we got the solution or not
code:
def sumTwo(array, sum):
hashTable = {};
for i in range( len( array ) ):
tmp = sum-array[i]
if hashTable.has_key(tmp ):
return [ array[i], tmp ];
hashTable[ array[i] ] = True;
return [];
def sumThree( array, T ):
for i in range( len( array) - 1 ):
#will look in the remaining array on the right of i
result= sumTwo( array[i+1:] , T - array[i] );
if len(result) != 0:
result.append( array[i] );
return result;
return [];
a = sumThree( [10,11,2,3,15,5,8,19,-2,23,25,18,45], 3 );
print a;
![Please help with part B. This is part A. def sumTwo(array, size_array, sum): binmap = [0]*CONST_MAX for i in range(0,size_array): tmp = sum-array[i] if (tmp> Please help with part B. This is part A. def sumTwo(array, size_array, sum): binmap = [0]*CONST_MAX for i in range(0,size_array): tmp = sum-array[i] if (tmp>](/WebImages/16/please-help-with-part-b-this-is-part-a-def-sumtwoarray-sizea-1029536-1761533409-0.webp)