[Tutor] Arbitrary-argument set function

bob gailer bgailer at gmail.com
Wed Oct 2 14:05:55 CEST 2013


On 10/1/2013 6:43 PM, Spyros Charonis wrote:
>
> Dear Pythoners,
>
>
> I am trying to extract from a set of about 20 sequences, the 
> characters which are unique to each sequence. For simplicity, imagine 
> I have only 3 "sequences" (words in this example) such as:
>
>
> s1='spam'; s2='scam', s3='slam'
>
>
> I would like the character that is unique to each sequence, i.e. I 
> need my function to return the list [ 'p', 'c', ',l' ]. This function 
> I am using is as follows:
>
>
> def uniq(*args):
>
>     """ FIND UNIQUE ELEMENTS OF AN ARBITRARY NUMBER OF SEQUENCES"""
>
>     unique = []
>
>     for i in args[0]:
>
>         if:
>
>            unique.append(i)
>
>     return unique
>
>
> and is returning the list [ 's', 'p', 'a', 'm' ]. Any help much 
> appreciated,
>
Problems with the above.
1 - where is the call to the function? (we can only guess what you fed it.)
2 - for i in args[0]: tests each character in only the first argument
3 - i not in args[1:] will always be True. Why?
4 - "characters which are unique to each sequence" is hard to translate. 
Did you mean
"characters which appear in exactly one sequence"?

BTW this sounds like homework; we are willing to give some assistance 
with HW but we need you to put more effort into your design and testing.

I suggest you either run the program in some tool that will show you 
clearly what happens at each step or "desk check" it - pretend you are 
the computer and go step by step, making sure you know what each line 
does. No guessing. You always have the interactive prompt to test 
things, and the documentation to explain things.

-- 
Bob Gailer
919-636-4239
Chapel Hill NC



More information about the Tutor mailing list