Question

In: Computer Science

Python 3: Part B needed Write a function "closest_pair" to determine the closest pair in Theta(N^2)...

Python 3: Part B needed

Write a function "closest_pair" to determine the closest pair in Theta(N^2) time, return that pair as a tuple. The closest pair is defined as the pair of numbers whose difference is the minimum of all pairs in the list. For example:

Given the list [ 3,6,4,10,23] the closest pair would be (3,4) since the difference is just 1.

Given the list [10, 45, 55, 42, 20], the closest pair would be (42,45) since the different is just 3

Given the list [4,10,6,7,10,5], the closest pair would be (10,10) since the distance is zero.

B) Write a function "fast_closest_pair" to determine the closest pair (same as in part A), except this time you should do it in better than Theta (N^2).

Solutions

Expert Solution

Hello, I am putting the code for Both A as well as B. Where I put the comment also for your better understanding where first program is taking O(n*n) time complexity and second will take O(n*log n) time complexity. which is better than first one. Although you have any doubt, feel free to comment down and if you like my answer, don't forget to give me upvote.

Thank you, Enjoy your answer.
# Answer : A)

mylist = [4,6,10,7,10,5];

tuplee = (mylist[0], mylist[1])
minimum = abs(mylist[0] - mylist[1])


# this loop will take O(n)
for i in range(0, len(mylist)-1):
        # below loop also will take O(n) times
        # As It is using nested loop 
        # So here complexity will be multiplied.
        # so complexity = O(n)* O(n) = O(n^2)
        for j in range(i+1, len(mylist)):
                a = mylist[i]
                b = mylist[j]
                if(minimum > abs(a-b)):
                        minimum = abs(a-b)
                        tuplee = (a, b)
print("using n*n time complexity: ",tuplee)

Now Second Program:


mylist = [4,6,10,7,10,5];


# this will take O(n*logn)
mylist = sorted(mylist)

tuplee = (mylist[0], mylist[1])
minimum = abs(mylist[0] - mylist[1])

tuplee = (mylist[0], mylist[1])

# this will take O(n), but it is not using nested loops
# so, here Complexity will added.
# so complexity = O(n*log n) + O(n) = O(n*log n)
# which means this is better than first one
for i in range(1,len(mylist)):
        a = mylist[i-1]
        b = mylist[i]

        if(minimum > abs(a-b)):
                minimum = abs(a-b)
                tuplee = (a, b)
print("using n*log n time complexity: ",tuplee)

Hope You understood this answer.


Related Solutions

PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns...
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns the number of 1's in the binary representation of n. Use the fact that this is equal to the number of 1's in the representation of n//2 (integer division) plus 1 if n is odd. >>>numOnes(0) 0 >>>numOnes(1) 1 >>>numOnes(14) 3
Prove that 1+ cos theta + cos 2theta + .... cos ntheta = 1/2 + (sin(n+1/2)theta)/2sin(theta/2)
Prove that 1+ cos theta + cos 2theta + .... cos ntheta = 1/2 + (sin(n+1/2)theta)/2sin(theta/2)
Please use Python 3 3). Write a function that writes a series of random numbers to...
Please use Python 3 3). Write a function that writes a series of random numbers to a text file named ‘random_number.txt’. Each random number should be in the range of 1 through 500. The function should let the user specify how many random numbers the file will hold. Then write another function that reads the random numbers from the ‘random_number.txt’ file, displays the numbers, and then displays the total of the numbers and the number of random numbers read from...
Use Java for the following; Part 1 n!= n * (n –1)* (n–2)* ...* 3 *...
Use Java for the following; Part 1 n!= n * (n –1)* (n–2)* ...* 3 * 2 * 1 For example, 5! = 5 * 4 * 3 * 2 * 1 = 120 Write a function called factorial that takes as input an integer. Your function should verify that the input is positive (i.e. it is greater than 0). Then, it should compute the value of the factorial using a for loop and return the value. In main, display...
Python 3 Write the definition of a function that take one number, that represents a temperature...
Python 3 Write the definition of a function that take one number, that represents a temperature in Fahrenheit and prints the equivalent temperature in degrees Celsius. Write the definition of another function that takes one number, that represents speed in miles/hour and prints the equivalent speed in meters/second. Write the definition of a function named main. It takes no input, hence empty parenthesis, and does the following: - prints Enter 1 to convert Fahrenheit temperature to Celsius - prints on...
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer...
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer value n and returns an approximation of e as (1 + 1/n)^n I am not even sure what the question is asking me to do but I think it is asking me to code that function. Does anyone know how to do this?
#Python program using loop instead of numpy or pandas. Write a function to enter n student...
#Python program using loop instead of numpy or pandas. Write a function to enter n student scores from 0 to 100 to represent student score, Count how many A (100 - 90), B(89-80), C(79-70), D(69-60), F(<60) We will use numpy array OR pandas later for this problem, for now use loop Invoke the function with 10 student scores, show the result as follow: Test Enter 10 scores Enter a student score: 99 Enter a student score: 88 Enter a student...
Write a function that takes three integers, n, a and b and a filename and writes...
Write a function that takes three integers, n, a and b and a filename and writes to the file a list with n random integers between a and b. And then write a function that can read the files as generated above and return the values. language: python
Find the order of growth for the following function ((n^3) − (60n^2) − 5)(nlog(n) + 3^n...
Find the order of growth for the following function ((n^3) − (60n^2) − 5)(nlog(n) + 3^n )
For Python: In this assignment you are asked to write a Python program to determine the...
For Python: In this assignment you are asked to write a Python program to determine the Academic Standing of a studentbased on their CGPA. The program should do the following: Prompt the user to enter his name. Prompt the user to enter his major. Prompt the user to enter grades for 3 subjects (A, B, C, D, F). Calculate the CGPA of the student. To calculate CGPA use the formula: CGPA = (quality points * credit hours) / credit hours...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT