Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL For Data Science Data Cleaning Wrangling And Analytics With Relational Databases

Authors: Antonio Badia

1st Edition

3030575918, 978-3030575915

More Books

Students also viewed these Databases questions

Question

Briefly describe the attribution theory of charismatic leadership.

Answered: 1 week ago