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 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 script or function in Python that approximates the solution to the system ??⃗=?⃗⃗ using...
Write a script or function in Python that approximates the solution to the system ??⃗=?⃗⃗ using the Jacobi Method. The inputs should be an nxn matrix A, an n-dimensional vector?⃗⃗, a starting vector ?⃗0,an error tolerance ?, and a maximum number of iterations N. The outputs should be either an approximate solution to the system??⃗=?⃗⃗ or an error message, along with the number of iterations completed. Additionally, it would be wise to build in functionality that allows you to optionally...
Write the following Python script: Write a function called linsolve() that will be useful in categorizing...
Write the following Python script: Write a function called linsolve() that will be useful in categorizing and solving linear algebra problems. The function will have up to three parameters: • A required 2D array representing the coefficient matrix of a linear algebra equation, • A required 1D or 2D array representing the right-side constants of a linear algebra equations, and • An optional parameter used to determine which condition number to use in determining the condition of the system. The...
•Write a Write a Python script that performs brute force to extract a password protected zip...
•Write a Write a Python script that performs brute force to extract a password protected zip file named sec3.zip. The password is believed to be associated with one of the dictionary words in the 'wordlist.txt file. The password policy enforces that all employees MUST use passwords that include at least one capital and one digit. The files are attached (hint must import zipfile)
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).  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT