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

I am struggling to write a python script that calculates the average length ,max length shortest...
I am struggling to write a python script that calculates the average length ,max length shortest length and overall average length of of a snake and ladder game. I use python 3 The length is the number the dice is rolled until the max is reached We need a python script that calculates the number of moves it will take for a single player to reach 100. We have to simulate a snake and ladder game using python language. So...
write a python script for rock scissors paper game
write a python script for rock scissors paper game
write a python script to calculate 401k with compounding interest
write a python script to calculate 401k with compounding interest
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 the following Python script: Problem Statement We want to know how many different people have...
Write the following Python script: Problem Statement We want to know how many different people have eaten at a restaurant this past week. The parameter meals has strings in the format "name:restaurant" for a period of time. Sometimes a person eats at the same restaurant often. Return the number of different people who have eaten at the eating establishment specified by parameter restaurant. For example, "John Doe:Moes" shows that John Doe ate one meal at Moes. Write function howMany that...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT