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 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 <...
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:...
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...
how to do the tf-idf vectorization in python ? how do we fit the model? i...
how to do the tf-idf vectorization in python ? how do we fit the model? i am trying like this but i am getting confused please explain with complete program vectorizer = Tfidvectorizer() x=vectorizer.fit_transofrm() // what data should be passed here ? do we split it in tokens ?? do we use vocabulary?? do we use s.split() ?? x.toarray() ? == pleae provide complete program with output because i am not understanding
Solve using PYTHON PROGRAMMING 9. Write a script that reads a file “ai_trends.txt”, into a list...
Solve using PYTHON PROGRAMMING 9. Write a script that reads a file “ai_trends.txt”, into a list of words, eliminates from the list of words the words in the file “stopwords_en.txt” and then a. Calculates the average occurrence of the words. Occurrence is the number of times a word is appearing in the text b. Calculates the longest word c. Calculates the average word length. This is based on the unique words: each word counts as one d. Create a bar...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT