Question: I need help writing a python code that returns the number of unique elements in a tuple without using the append or set function built
I need help writing a python code that returns the number of unique elements in a tuple without using the append or set function built in to python. Our assignment recommended that we make a second tuple only containing eash element once and return th length of that for our answer, but I don't know how to get the new tuple to only contain things once. I thought I might make a tuple with all the elements and subtract the ones that are duplicates, but that make me realize Im deleting all the doubles instead of keeping one of each element. Here's the coding I have so far. The introcs module is online, Im very limited it what functions I can use beyond ifthen tryexcept and what's in the introcs module.
Thank you for your time! Here is my work so far:
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 x in tup:
if introcs.counttuptupx:
result result x
sectup
for x in tup:
if introcs.counttuptupx:
sectup sectup x
return lenresultlensectup 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
uniques aa returns
uniques returns
Parameter tup: the tuple to copy
Precondition: tup is a tuple
result
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
