Question

In: Computer Science

Assume someone has a secret 4-tuple (w, x, y, z) where each number can be from...

Assume someone has a secret 4-tuple (w, x, y, z) where each number can be from 1 to 1000 inclusive. Your code is supposed to find what these integers are by using a loop and comparing the number (part of Brute Force). One approach is to write four nested for-loops to check for the number. This is O(n 4 ). Your task is to determine a random 4-tuple. Is your solution better than O(n^4 )?

1 import random as rn

2

3 x = [rn.randint(1,1000), rn.randint(1,1000), rn.randint(1,1000), rn.←- randint(1,1000)]

4

5 def find_tuple(x):

6 pass

Solutions

Expert Solution

  • Below is the detailed implementation of the above problem in PYTHON with code and output shown.
  • For better understanding please read the comments mentioned in the code.
  • CODE:

#import random module to generate random numbers
import random as rn
#generate a secret tuple
x = [rn.randint(1,1000), rn.randint(1,1000), rn.randint(1,1000), rn.randint(1,1000)]
#print the secret tuple so that we can check what we found afterwards
print("The secret tuple:",x)

#function which takes secret tuple as input and return the integers in tuple after finding them
def find_tuple(x):
#answer list to store integers
ans=[]
  
#for finding the first number in tuple x
#iterate from 1 to 1000 and check if number equals the first number in tuple x
for i in range(1,1001):
#if current number equals first number then add it to list and break the loop
if x[0]==i:
ans.append(i)
break
  
#for finding the second number in tuple x
#iterate from 1 to 1000 and check if number equals the second number in tuple x
for i in range(1,1001):
#if current number equals second number then add it to list and break the loop
if x[1]==i:
ans.append(i)
break
  
#for finding the third number in tuple x
#iterate from 1 to 1000 and check if number equals the third number in tuple x
for i in range(1,1001):
#if current number equals third number then add it to list and break the loop
if x[2]==i:
ans.append(i)
break
  
#for finding the fourth number in tuple x
#iterate from 1 to 1000 and check if number equals the fourth number in tuple x
for i in range(1,1001):
#if current number equals fourth number then add it to list and break the loop
if x[3]==i:
ans.append(i)
break
  
#return found tuple
return ans

#call find_tuple and print what we found
print("We found:",find_tuple(x))

  • OUTPUT:
The secret tuple: [934, 9, 321, 28]
We found: [934, 9, 321, 28]
  • The complexity of the above code is better than O(n^4 ) as we use 4 for-loops to find each of the numbers in the secret tuple and each loop as complexity O(n) so overall we have complexity O(4*n) which is O(n) only. So we found the secret tuple in linear time.
  • Below are the screenshot attached for the code and output for better clarity and understanding.

CODE and OUTPUT

So if you still have any doubt regarding this solution please feel free to ask it in the comment section below and if it is helpful then please upvote this solution, THANK YOU.


Related Solutions

If x = 5, y = 6, z = 4, and w = 3.5, evaluate each...
If x = 5, y = 6, z = 4, and w = 3.5, evaluate each of the following statements, if possible. If it is not possible, state the reason. ((++y) * z) % x (x % y) % (--z) a)     Static_cast<int> ((--y) + x / z) Static_cast<int> (w + Static_cast<double> (x / z + w))
Please explain this prolog code line by line. union([X|Y],Z,W) :- member(X,Z), union(Y,Z,W). union([X|Y],Z,[X|W]) :- \+ member(X,Z),...
Please explain this prolog code line by line. union([X|Y],Z,W) :- member(X,Z), union(Y,Z,W). union([X|Y],Z,[X|W]) :- \+ member(X,Z), union(Y,Z,W). union([],Z,Z).
Assume the following preference order for four goods, w, x, y, and z, of a total...
Assume the following preference order for four goods, w, x, y, and z, of a total of 26 people: # of people 9 6 2 4 5 W X Y Y Z Z Y X Z X X Z Z X Y Y W W W W You want to extract the will of the group and employ different rules. What is the group's preference order when you employ: A) Plurality Vote B) Condorcet Vote C) Borda Count D) "Vote...
Assume that the variables w, x, y, and z are stored in memory locations 16, 32,...
Assume that the variables w, x, y, and z are stored in memory locations 16, 32, 48, and 128 respectively. Translate the following algorithmic operations into MIPS language equivalents: [Assume $s1 is the base register, and $s1 contains the address of memory location 0]                                                                                                             if ((x – y) ³ w) then       set x to y else: set x to z endif
In python this structure can be represented by a set of tuples, where each tuple has...
In python this structure can be represented by a set of tuples, where each tuple has two elements. The following two lines would build the set given above and the print it. >>> L = [(’dog’,’white’), (’cat’,’black’),(’mouse’,’black’)] >>> f = set(L) >>> print(f) {(’cat’, ’black’), (’dog’, ’white’), (’mouse’, ’black’)} In the example, first we store the tuples into a list, and then we create a set with those tuples. There are obviously countless other ways to initialize f and get...
The temperature at a point (x,y,z) is given by T(x,y,z)=200e^(-x^2-y^2/4-z^2/9) , where T is measured in...
The temperature at a point (x,y,z) is given by T(x,y,z)=200e^(-x^2-y^2/4-z^2/9) , where T is measured in degrees Celsius and x,y, and z in meters. just try to keep track of what needs to be a unit vector. a) Find the rate of change of the temperature at the point (1, 1, -1) in the direction toward the point (-5, -4, -3). b) In which direction (unit vector) does the temperature increase the fastest at (1, 1, -1)? c) What is...
The following logic function is given as a sum of minterms F(W,X,Y,Z) = ∑W,X,Y,Z(7,8,10,11,13) + D(5,...
The following logic function is given as a sum of minterms F(W,X,Y,Z) = ∑W,X,Y,Z(7,8,10,11,13) + D(5, 9, 15). (25 points) a) Draw the K-Map and find the minimal sum-of-products expression for this function. b) Draw the circuit implementing this expression c) Give all input pair or pairs where transition between them would create a timing hazard d) Draw the timing diagram showing the glitch corresponding to the pair or one of the pairs. Assume ALL gate delays are equal e)...
a.Consider a hypothetical gas of four distinguishable particles, labeled W, X, Y and Z. Assume that...
a.Consider a hypothetical gas of four distinguishable particles, labeled W, X, Y and Z. Assume that they are distributed between only two translational quantum states of energies 2E and 4E. How many different configurations of this gas are there? b.Consider a hypothetical gas of three distinguishable particles, labeled A, B and C. Each of the particles has only three quantum states of energies 5E, 10E and 15E. How many different configurations of this gas have a total energy of 30E?...
Assume that X, Y, and Z are independent random variables and that each of the random...
Assume that X, Y, and Z are independent random variables and that each of the random variables have a mean of 1. Further, assume σX = 1, σY = 2, and σZ = 3. Find the mean and standard deviation of the following random variables: a. U = X + Y + Z b. R = (X + Y + Z)/3 c. T = 2·X + 5·Y d. What is the correlation between X and Y? e. What is the...
Let S be the cone z = 4 − ( x^2 + y^2)^(1/2) where z ≥...
Let S be the cone z = 4 − ( x^2 + y^2)^(1/2) where z ≥ 0, oriented with downward pointing unit normal vectors. The image below contains S. The grey plane is the xy-plane. Let F~ (x, y, z) = <− z , x − y , x + y >. Use Stokes’ Theorem to evaluate Z Z S ∇ × F~ · dS~.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT