Question

In: Computer Science

5. [2 marks] Can we do better than this? Recall that the human player is at...

5. [2 marks] Can we do better than this? Recall that the human player is at some fixed location (px, py). Your task is to work out how you would sort the array A so that those enemy AIs that need to be marked can be identified in log(N) time.Specifically, complete the following comparison function that would be used while sorting the array A.   Here, (x1, y1) and (x2, y2) are two points from the array A.   The function should return true if it considers the first point to be less than or equal to the second, and should return false otherwise. Your function can use the player’s coordinate (px, py) as global variables, i.e. you are allowed to refer to px and py in your function。 Function LessOrEqualTo((x1, y1),(x2, y2))

Solutions

Expert Solution

'''

Python version : 2.7

'''

import math

# function that returns true if (x1,y1) is less than or equal to (x2,y2) with respect to player’s coordinate (px, py)

# player’s coordinate (px,py) are global variables which are accessible by this function

def LessOrEqualTo((x1, y1),(x2, y2)):

               # calculate the distance of (x1,y1) from (px,py)

               dist1 = math.sqrt((x1-px)**2 + (y1-py)**2)

               # calculate the distance of (x2,y2) from (px,py)

               dist2 = math.sqrt((x2-px)**2 + (y2-py)**2)

              

               # if dist1 is less than or equal to dist2 return True else return False

               if(dist1 <= dist2):

                              return True

               else:

                              return False

              

#end of function              

Code Screenshot:


Related Solutions

A researcher in the field of women’s studies wondered if women recall emotional events better than...
A researcher in the field of women’s studies wondered if women recall emotional events better than men. She decides to gather some data on the matter. An experiment is conducted in which 10 mean and 10 women were randomly selected and shown 20 highly emotional photographs and then asked to recall them 1 week after the showing. The following recall data are obtained. Scores are indicated as percent correctly recalled. Using α =0.05 1tail answer the questions below. Men Women...
Q: As an accounting professional, what can you do better than machines or artificial intelligence? If...
Q: As an accounting professional, what can you do better than machines or artificial intelligence? If accounting and auditing standards are 100% rules-based without any judgment, would there be more or less accounting jobs for us?
1.researcher in the field of women’s studies wondered if women recall emotional events better than men....
1.researcher in the field of women’s studies wondered if women recall emotional events better than men. She decides to gather some data on the matter. An experiment is conducted in which eight randomly selected men and eight randomly selected women are shown 20 highly emotional photographs and then asked to recall them 1 week after the showing. The sample mean for men was 16 (photos recalled), and the sample standard deviation was 1.73. For the women, the sample mean was...
"We are certainly in a better position in terms of diversity management than we were five...
"We are certainly in a better position in terms of diversity management than we were five years ago," said Patricia Collins-Jones, CEO of BetterFit, Inc. "We certainly have a more diverse employee population, and we have taken steps to involve people from different backgrounds in our business operations. However, I expect us to do more. We must be an organization where diversity not only exists, but is valued. We must Multiple Choice encourage minority group members to adopt the norms...
Can pollution ever make us better off?how do we know ?should we aim to eliminate all...
Can pollution ever make us better off?how do we know ?should we aim to eliminate all pollution? If not, what should our goal be? Explain your answer.
Contingency Theory offers a better way of managing change than either the Classical or Human Relations...
Contingency Theory offers a better way of managing change than either the Classical or Human Relations approaches. Q1- Identify and discuss the different perspectives on managing change offered by: - Contingency Theory: - The Classical approach: - The Human Relations approach: Q2- Evaluate the benefits offered by Contingency Theory’s ability to adapt to situational factors ?
Q2. Contingency Theory offers a better way of managing change than either the Classical or Human...
Q2. Contingency Theory offers a better way of managing change than either the Classical or Human Relations approaches. Identify and discuss the different perspectives on managing change offered by Contingency Theory (Ch.3, 10), the Classical approach (Ch.2) and the Human Relations approach (Ch.3). Evaluate the benefits offered by Contingency Theory’s ability to adapt to situational factors.
Code the game of Rock, Paper, Scissors between a human player and the computer. You can...
Code the game of Rock, Paper, Scissors between a human player and the computer. You can check out the game on Wikipedia if you are not familiar with it. Create a 4 option menu with the human player choices, plus the option of exiting the game. Randomly determine the computer’s choice (although a great deal of AI research has gone in to determining the best computer move). • Loop the game until the human player exits. • Count the number...
Why do we design airports for some capacity less than the highest capacity we can ever...
Why do we design airports for some capacity less than the highest capacity we can ever expect?
1) We say that electrons in the 3d orbital are "better shielded" than those in the...
1) We say that electrons in the 3d orbital are "better shielded" than those in the 3p orbital. a) what does this mean? b) are the electrons in the 3d orbitals better shielded by electrons in the 2p orbital or int he 3p orbital? c) Based on fundamental physics principles not slater's rule, is it possible for electrons in the 4s orbital to partially shield an electron in the 3p orbital?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT