Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using Python .. The intersection (n) of two sets (s1, s2) is the set of all elem

ID: 3931074 • Letter: U

Question

Using Python ..

The intersection (n) of two sets (s1, s2) is the set of all elements that are in s1 and are also in s2. Write a function (intersect) that takes two lists as input (you can assume they have no duplicate elements), and returns the intersection of those two sets (as a list) without using the in operator or any built-in functions, except for range() and len(). Write some code to test your function, as well. if elem in set2: # do something Sample Output for Part 1: >>> intersect ([1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25], [1, 4, 9, 16, 25]) [1, 9, 25]

Explanation / Answer

Python CODE

# Function definition is here
def intersect( set1,set2 ):
   newset = [] # creating a new set to store the interset elements
   for i in set1: # selecting the elements of first list and iterating
       for j in set2: # selecting the elements of second list
           if i == j: # comparing the elements of each list
              newset = newset+[i] # adding the elements to the new set
   print newset # printing the intersection
   return

# Function call
intersect( [1,3,5,7,9,11,13,15,17,19,21,23,25], [1,4,9,16,25] )

OUTPUT

$ python 15884665.py
[1, 9, 25]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote