Question: I need help writing a python code that returns the number of unique elements in a tuple without using the append function or set function.
I need help writing a python code that returns the number of unique elements in a tuple without using the append function or set function. Our assignment recommended that we make a second tuple containing each element only once and having it return the length for the second tuple, but when I try that Im accidentally excluding the values that appear more than once. I need to include the number of unique values without double counting them. I'll attach a picture of my solution trying to create a second tuple it's not working because it's only counting values that appear once but I need to know the number of elements that appear I just should only count them once The other picture was a suggested solution from someone here, but it doesn't work either because it only counts values that appear once.
I need a solution that counts every element, then I need to remove duplicates. But without using append or set. I can use ifelifelse or tryexcept or the count function from an introcs module the one I used in my answer Im very limited in tools I can use because this is a very basic python class.
Thank you for your time!
Implement
uniques
Read the specification of the function
uniques in the module
Implement this function according to
its specification. As a hint, we suggest
you take the following approach:
make a new tuple that contains each
element of the original tuple, but
only contains it once. Then return the
length of this new tuple.
When you have implemented the
function, test your answer with the
test script. You should now pass all
tests.
Check the Function
You may run this test multiple times.
LAST RUN on :: PM
The call uniques
returns not Implement uniques
Read the specification of the function uniques in the module
funcs. Implement this function according to its specification.
As a hint, we suggest you take the following approach: make a
new tuple that contains each element of the original tuple, but
only contains it once. Then return the length of this new tuple.
When you have implemented the function, test your answer
with the test script. You should now pass all tests.
Check the Function
You may run this test multiple times.
LAST RUN on :: PM
The call uniques returns not
def uniquestup:
Returns the number of unique elements in the tuple.
Examplse:
uniques returns
uniquesaa returns
uniques returns
Parameter tup: the tuple to copy
Precondition: tup is a tuple
result
for in tup:
if introcs.counttuptupx:
result result
sectup
for in tup:
if introcs.counttuptupx:
sectup sectup
return lenresultlensectup
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
