Question

Suppose we have an array that contains tuples. These tuple contains three positive numbers. Implement an...

Suppose we have an array that contains tuples. These tuple contains three positive numbers.

Implement an algorithm that counts how many distinct tuple that an array has(contains same number in same order).

ex) [(1, 2, 1), (2, 2, 2), (3, 8, 3), (1, 2, 1), (3, 4, 3)] gives 4.

1 The algorithm should be implemented in Python3.

2 The function must have average-case runtime of O(n). You can assume Simple Uniform Random Hashing.

3 Python built-in dictionary cannot be used.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the following python function to find the distinct elements in the array.

Algorithm:

Step 1: Iterate the given array by one element at a time

Step 2: Have temporary empty array and populate the array with the unique tuples in the temporary array

Step 3: Return the count of elements in temporary array.

Program:

ArrayOfTuples=[(1, 2, 1), (2, 2, 2), (3, 8, 3), (1, 2, 1), (3, 4, 3)]

def countDistinctElementinArray(array):
finalArray=[]
count = 0
#traverse the array, if tuple not found in findArray list , append to findArray list
for i in array:
if i not in finalArray:
#increment the count value for every unique tuple in the list
count = count + 1
finalArray.append(i)
return count

print("The distinct value in the array of tuples is: ", end="")
print(countDistinctElementinArray(ArrayOfTuples))
  

Sample Output:

The distinct value in the array of tuples is: 4

Screen Shot:

Add a comment
Know the answer?
Add Answer to:
Suppose we have an array that contains tuples. These tuple contains three positive numbers. Implement an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A triangle is expressed by tuples of three positive numbers, which are the length of sides...

    A triangle is expressed by tuples of three positive numbers, which are the length of sides of the triangle. Two triangles a1 and a2 are similar if a2 can be generated by rotating and reflecting triangle a1. For example, t1 = (7, 10, 13) is similar to t2 = (7, 13, 10) since a2 can be obtained by rotating and reflecting t1. (7, 10, 13) rotate −−−→ (10, 13, 7) reflect −−−−→ (7, 13, 10) On the other hand, t1...

  • 1) We have returned to our ingredients dictionary. We want to display the ingredients in alphabetical...

    1) We have returned to our ingredients dictionary. We want to display the ingredients in alphabetical order. Dictionaries cannot be sorted, so we need to use the items method to convert the dictionary into a list of tuples. Then we can sort that new list of tuples, then display the ingredients. Use items and list to convert ingredients into a list of tuples. Sort the new list, called ingred_list Write a for loop which goes through each tuple in ingred_list....

  • Version:0.9 StartHTML:0000000105 EndHTML:0000008229 StartFragment:0000000141 EndFragment:0000008189 In Python, a tuple is a collection similar to a list...

    Version:0.9 StartHTML:0000000105 EndHTML:0000008229 StartFragment:0000000141 EndFragment:0000008189 In Python, a tuple is a collection similar to a list in that it is an ordered collection of items. An important difference, however, is that a tuple is immutable – once created, a tuple cannot be changed. Also, tuples are denoted using parentheses instead of square brackets. As with lists, elements of a tuple are accessed using indexing notation. For example, given the tuple subjects = (’physics’, ’chemistry’, ’biology’), we could ac cess the...

  • Let A be an array of N distinct positive numbers such that at most sqrt(N) of...

    Let A be an array of N distinct positive numbers such that at most sqrt(N) of them is larger than N. Ex: if N=100, there are at most 10 elements in A that are greater than 100. 1. Split A into A1 and A2 such that A1 contains the elements greater than N and A2 contains the remaining elements. Do this in O(N) 2. Sort A1 in O(N) 3. Sort A2 in O(N) 4. Sort A in O(N) I don't...

  • Problem 3 (20 points): An array A of size (n) contains floating-point items. 1. Implement a...

    Problem 3 (20 points): An array A of size (n) contains floating-point items. 1. Implement a Divide & Conquer algorithm to return the number of items with values less than a given value (x). (5 points) 2. Test your algorithm by generating an array A of size n = 1024 random floating-point numbers with each number R between 0 and 1, i.e. 0.0 <R< 1.0. Run the algorithm to find the percentage of the numbers in the array with values...

  • Suppose you have a sorted array of positive and negative integers and would like to determine...

    Suppose you have a sorted array of positive and negative integers and would like to determine if there exist some value of x such that both x and -x are in the array. Consider the following three algorithms: Algorithm #1: For each element in the array, do a sequential search to see if its negative is also in the array. Algorithm #2:For each element in the array, do a binary search to see if its negative is also in the...

  • Suppose you have an array S indexed from 1 to n which contains n numbers, not...

    Suppose you have an array S indexed from 1 to n which contains n numbers, not in any particular order, and you wish to count how many times a given number x occurs in S. Consider the recursive algorithm below for this which finds the number of occurrences of x in the index range i...j in S. Of course, solving the problem would involve an initial call to the algorithm for the range 1.n: int CountOccur (int i,j) { int...

  • Stacks and Java 1. Using Java design and implement a stack on an array. Implement the...

    Stacks and Java 1. Using Java design and implement a stack on an array. Implement the following operations: push, pop, top, size, isEmpty. Make sure that your program checks whether the stack is full in the push operation, and whether the stack is empty in the pop operation. None of the built-in classes/methods/functions of Java can be used and must be user implemented. Practical application 1: Arithmetic operations. (a) Design an algorithm that takes a string, which represents an arithmetic...

  • In this problem you will implement an algorithm for computing all the square roots of a congruenc...

    In this problem you will implement an algorithm for computing all the square roots of a congruence class in ℤ/nℤ, given a complete factorization of n into its distinct prime factor powers (assuming all the prime factors are in 3 + 4ℤ). a) Implement a Python function sqrtsPrime(a, p) that takes two arguments: an integer a and a prime number p. You may assume that a and p are coprime. If p is not in 3 + 4ℤ or a...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT