Hey again! So this is a picture of the code that I did for the intersecting sets. I’m not allowed to use any built in functions, like append, and also not allowed to use the in operator as well, but obviously did in my solution.
It needs to work with letters or numbers or anything with a set and the sets have to be of any length, etc.
Would there be a way to solve this with slicing or recursion?
You’re seriously not allowed to use append? That’s so strange. I’m stumped, to be honest. Sorry.
it’s cool! thanks for taking the time to look! do you think you could show me how to do it without using the in operator, but using append and whatever else?
Not very elegant, but it works.
len() a built-in function?
You were on the right track with slicing and recursion, I think. Here’s how I’d do it:
# Returns True if haystack contains needle, or False otherwise. def contains(haystack, needle): return (haystack and (haystack == needle or contains(haystack[1:], needle))) # Prints all the elements that s1 and s2 share. def printIntersections(s1, s2): if (s1): if contains(s2, s1): print "Element is in both sets:", s1 printIntersections(s1[1:], s2) # Same as above, but also warns of empty sets. def intersections(s1, s2): if not s1: print "Set 1 has no values." if not s2: print "Set 2 has no values." printIntersections(s1, s2)
This doesn’t use any built-in functions and it doesn’t use the