human
Minimum number of towers required such that every house is in the range of at least one tower | Function to count the number of tower.
first we sort the house numbers.
for count number of towers.
for iterate all houses.
count number of towers.
find find the middle location.
traverse till middle location.
this is point to middle house where we insert the tower.
now find the last location.
traverse till last house of the range.
return the number of tower.
Driver code.
given elements.
print number of towers"
How can the above be solved in Python?
assistant
#Here is code to minimum number of towers required such that every house is in the range of at least one tower and the other steps required.
 def number_of_tower ( house , r , n ) :
	house . sort ( )
	numOfTower = 0
	i = 0
	while ( i < n ) :
		numOfTower += 1
		loc = house [ i ] + r
		while ( i < n and house [ i ] <= loc ) :
			i += 1
		i -= 1
		loc = house [ i ] + r
		while ( i < n and house [ i ] <= loc ) :
			i += 1
	return numOfTower
if __name__ == " _ _ main _ _ " :
	house = [ 7 , 2 , 4 , 6 , 5 , 9 , 12 , 11 ]
	r = 2
	n = len ( house )
	print ( number_of_tower ( house , r , n ) )