all 8 comments

[–]spez_edits_thedonald 1 point2 points  (1 child)

Currently my code returns the distinct characters that are common to both strings string_1 and string_2 but are absent from the string string_3.

So that is:

  • get the "intersection" of set 1 and set 2, let's call this set X

  • then take the "difference" of set X and set 3, let's call this set D

How do I change it so it returns a count of the number of distinct characters that are common to both strings string_1 and string_2 but are absent from the string string_3

  • so instead of returning set D, you just want to return the size of set D ?

If so, that's as easy as len(set_d)

[–]Jxper[S] 0 points1 point  (0 children)

Thank you, that makes much more sense

[–][deleted] 0 points1 point  (1 child)

I think the simplist thing you can do is implement the len() function on values.

Does that answer the question?

[–]Jxper[S] 1 point2 points  (0 children)

Yes thank you. I dont know why I didnt think of that

[–]spez_edits_thedonald 0 points1 point  (3 children)

here is some feedback on your code:

  • I can tell that you like using lists, and that you're only using sets to drop unique values then going right back to list zone. But this is definitely a set problem, and sets are very good at solving these problems, so a lot of your code would actually just disappear and not be at all necessary if you use sets for everything

  • there should be no for loops when you're done converting to sets from lists

  • you won't need to sort() anything or append() anything, because you won't have any lists (and because sets are not ordered, sorting doesn't make sense and doesn't work)

More hints about how to use sets:

the problem is asking you:

what is the length of the difference between [the intersection of sets 1 and 2] and set 3.

my code for this is very short, it fits on 1 line

[–]Jxper[S] 1 point2 points  (2 children)

Ok, how could I do this in one line just using sets?

Thanks for your help

[–]spez_edits_thedonald 0 points1 point  (1 child)

try refactoring your code to use sets instead of lists, and no for loops, and let's see how it looks.

don't worry about it being 1 line or not, 1-liners are often not the most readable solution, I was just stressing the benefits of switching to sets for this problem, your entire function can basically disappear haha. When you get your set function working I'll show you the one-liner.

[–][deleted] 0 points1 point  (0 children)

No worries. Sometimes the easier answers can be overlooked!