Question

In: Computer Science

How do I write a script for this in python in REPL or atom, NOT python...

How do I write a script for this in python in REPL or atom, NOT python shell

Consider the following simple “community” in Python . . .

triangle = [
["top", [0, 1]], ["bottom-left", [0, 0]], ["bottom-right", [2, 0]],

]

This is the calling of function. >>> nearestneighbor([0, 0.6], triangle, myeuclidean) 'top'

The new point is (0, 0.6) and the distance function is Euclidean. Now let’s confirm this result . . .

  • >>> myeuclidean([0, 0.6], [0, 1])

  • 0.4

  • >>> myeuclidean([0, 0.6], [0, 0])

  • 0.6

  • >>> myeuclidean([0, 0.6], [2, 0])

  • 2.08806130178211

Solutions

Expert Solution

Code Without Snippet:

# Implementation of myeuclidean function
def myeuclidean(point,point_):

   # Calculation Euclidean Distance
   distance = ((point[0]-point_[0])**2) + ((point[1]-point_[1])**2)
   distance = distance**(1/2)

   return distance

# Implementation of Nearest Neighbours
def nearestneighbor(point,triangle,myeuclidean):
   distances=[]

   # Calling myeuclidean for each point of triangle
   for i in range(0,len(triangle)):
       distances.append(myeuclidean(point,triangle[i][1]))

   # Finding out the minimum distance point index
   minimum_distance_position = distances.index(min(distances))

   return triangle[minimum_distance_position][0]


# Main Function
if __name__=="__main__":
   # Traingle Variable
   triangle = [ ["top", [0, 1]], ["bottom-left", [0, 0]], ["bottom-right", [2, 0]] ]

   # Function Call
   print(nearestneighbor([0, 0.6], triangle, myeuclidean))

Code With Snippet:

Input and Output Snippet:

Note:

  • The complete code was saved in a file named check.py using Atom Editor.
  • Please copy the complete code before running the script.
  • Be aware of the indentation of the code.
  • Please feel free to make changes, if required.
  • Use print(distance) in myeuclidean function to see the computed distances for your reference, if required.
  • Try around with other examples too.

Thank you!!!


Related Solutions

write a python script for rock scissors paper game
write a python script for rock scissors paper game
Write the following Python script: Imagine you live in a world without modules in Python! No...
Write the following Python script: Imagine you live in a world without modules in Python! No numpy! No scipy! Write a Python script that defines a function called mat_mult() that takes two lists of lists as parameters and, when possible, returns a list of lists representing the matrix product of the two inputs. Your function should make sure the lists are of the appropriate size first - if not, your program should print “Invalid sizes” and return None. Note: it...
Write the following Python script: Imagine you live in a world without modules in Python! No...
Write the following Python script: Imagine you live in a world without modules in Python! No numpy! No scipy! Write a Python script that defines a function called mat_mult() that takes two lists of lists as parameters and, when possible, returns a list of lists representing the matrix product of the two inputs. Your function should make sure the lists are of the appropriate size first - if not, your program should print “Invalid sizes” and return None. Note: it...
Write a Python script that takes an input image and output's the name of the dominant...
Write a Python script that takes an input image and output's the name of the dominant color in that image(i.e. red, green, blue).  
PYTHON: Write a script that imports the functions in the module below and uses all of...
PYTHON: Write a script that imports the functions in the module below and uses all of the functions. import math def _main():     print("#" * 28, "\n", "Testing...1, 2, 3...testing!\n", "#" * 28, "\n", sep="")     f = -200     print("{:.2f} F is {:.2f} C".format(f, f2c(f)))     f = 125     print("{:.2f} F is {:.2f} C".format(f, f2c(f)))     c = 0     print("{:.2f} C is {:.2f} F".format(c, c2f(c)))     c = -200     print("{:.2f} C is {:.2f} F".format(c, c2f(c))) def f2c(temp):     #Converts Fahrenheit tempature to Celcius     if temp <...
In this assignment, you will write a Python script to increase the population of each city...
In this assignment, you will write a Python script to increase the population of each city in the world_x database by 10% (rounded). First, add a new column to the "world_x" city table using the following MySQL command: ALTER TABLE `world_x`.`city` ADD COLUMN `Population` DOUBLE NULL DEFAULT NULL AFTER `Info`; The existing population data are stored as JSON datatype in the city table in a field named Info. You learned about JSON data types in Module 2. To obtain the...
Write a Flask app in which a Python script prompts the user to enter a string...
Write a Flask app in which a Python script prompts the user to enter a string and then that string, as entered, gets displayed on an otherwise blank HTML file, called output.html. Show the Python/Flask code and the html code of the output.html file, one below the other as part of your answer.
The school bookstore wants you to write a Python script to calculate the point of sale...
The school bookstore wants you to write a Python script to calculate the point of sale (total cost) of their new 25$ gift cards. They are also running a special, if a customer buys a gift card they can buy all books for 5$ dollars each. The gift card cost is $25.00 plus $5.00 per book. In addition, there is a sales tax which should be applied to the subtotal and it is 8% (multiply the subtotal by 0.08.) Requirements:...
The school bookstore wants you to write a Python script to calculate the point of sale...
The school bookstore wants you to write a Python script to calculate the point of sale (total cost) of their new 25$ gift cards. They are also running a special, if a customer buys a gift card they can buy all books for 5$ dollars each. The gift card cost is $25.00 plus $5.00 per book. In addition, there is a sales tax which should be applied to the subtotal and it is 8% (multiply the subtotal by 0.08.) Requirements:...
The school bookstore wants you to write a Python script to calculate the point of sale...
The school bookstore wants you to write a Python script to calculate the point of sale (total cost) of their new 25$ gift cards. They are also running a special, if a customer buys a gift card they can buy all books for 5$ dollars each. The gift card cost is $25.00 plus $5.00 per book. In addition, there is a sales tax which should be applied to the subtotal and it is 8% (multiply the subtotal by 0.08.) Requirements:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT