Question: import math import random import sys class coordinates(): #generating random target coordinates def __init__(self): self.r1=random.randint(1,int(sys.maxsize)) self.r2 =random.randint(1,int(sys.maxsize)) self.hit=0 # this method returns a distance to

import math

import random

import sys

class coordinates():

#generating random target coordinates def __init__(self): self.r1=random.randint(1,int(sys.maxsize))

self.r2 =random.randint(1,int(sys.maxsize))

self.hit=0

# this method returns a distance to the generated target from the shot coordinates

def distance(self,x,y):

self.hit_x = x

self.hit_y = y

self.hit +=1

self.distance_to_target_coordinates=math.sqrt((self.hit_x-self.r1)**2+(self.hit_y-self.r2)**2)

print ("hit is %s in (%s,%s) missed %s" % (self.hit,x,y,self.distance_to_target_coordinates))

return self.distance_to_target_coordinates

#check if coordinates hit target,if hit target return

def target(self):

self.x=0

self.y =0

while self.distance(self.x, self.y) > self.distance(self.x+1, self.y):

self.x += 1

while self.distance(self.x, self.y) > self.distance(self.x, self.y+1):

self.y += 1

return self.x,self.y

t = coordinates()

print("coordinates: %s,%s" % t.target())

########################################

The question is to rewrite the target() function with the most optimal algorithm to make a few "hits" as possible to hit the target. In your function, you need to use the t.distance() method, but every time you call the method it counts your"hits". Your function should use the minimum number of "hits" to hit the target.

EXAMPLE:Correct solution hits the target with 5 shots or less for coordinates:(2,3)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!