Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import math import random class coordinates(): #generating random target coordinates def __init__(self): self.r1=random.randint(1,infinity) self.r2 =random.randint(1,infinity) self.hit=0 # this method returns a distance to the generated
import math import random class coordinates(): #generating random target coordinates def __init__(self): self.r1=random.randint(1,infinity) self.r2 =random.randint(1,infinity) 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 target coordinates are positive integers randomly selected
>The target() function uses an algorithm to get the target coordinates.
> rewrite the target() function with the most optimal algorithm to make as few hits as possible to hit the target.
> The function should return the correct target coordinates once the target is shot.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started