Question

In: Computer Science

The intersection (∩) of two sets (s1, s2) is the set of all elements that are...

The intersection (∩) 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.

Note: Do not use the in operator for this assignment. The keyword in used in for loops (e.g. for x in list) is allowed, as that is not the same as using the in operator. The following code, which uses the in operator, would not be permitted, since it makes this question too easy:

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]

Solutions

Expert Solution

def intersect(list1,list2):
list3=[]
for i in list1:#check for each number in 1st list
k=0
while k<len(list2) and list2[k]!=i:#go through list2 untill we find element i or end is reached
k+=1
if k!=len(list2):#if we find an element in both lists
m=0
while m<len(list3) and list3[m]!=i:#check if the element is not already in intersection list
m+=1
if m==len(list3):#add to list if it is not already in
list3.append(i)
return list3


Related Solutions

If there are two energy states, S1 and S2 respectively such that S2>S1 then there exists...
If there are two energy states, S1 and S2 respectively such that S2>S1 then there exists some probability for an atom in S2 to decay to S1. What actually causes the atom to decay to the lower energy state? is it the fact that the lower state is more probable for the atom to be in as given by the Boltzmann Factor? so since it is more probable, it has more microstates and entropy causes it to decay? Please help...
Let S1 and S2 be any two equivalence relations on some set A, where A ≠...
Let S1 and S2 be any two equivalence relations on some set A, where A ≠ ∅. Recall that S1 and S2 are each a subset of A×A. Prove or disprove (all three): The relation S defined by S=S1∪S2 is (a) reflexive (b) symmetric (c) transitive
Given the two stacks S1 and S2 each with a set of numbers, write an algorithm...
Given the two stacks S1 and S2 each with a set of numbers, write an algorithm to check if the two stacks are identical (have the same information). Use only the Push and Pop operations. The original stacks should be kept. Briefly explain the time complexity of your algorithm.
Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting...
Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in both s1 and s2. => union(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in either s1 or s2. =>difference(String s1, String s2): takes two strings and returns the string consisting of all letters that appear only in s1.
4. Consider a two-player game with strategy sets S1 ={α1,...,αm} and S2 = {β1,...,βn}. (a) Suppose...
4. Consider a two-player game with strategy sets S1 ={α1,...,αm} and S2 = {β1,...,βn}. (a) Suppose that (αi,βj) is a Nash equilibrium. Is it possible that a strategy αj strictly dominates αi? (b) Assume again that (αi,βj) is a Nash equilibrium. Is it possible that another strategy αj weakly dominates αi?
Two sets are separated if the intersection of the closure of one of the sets with...
Two sets are separated if the intersection of the closure of one of the sets with the other is empty. a) Prove that two closed and disjoint sets of some metric space are separate. b) Prove that two open and disjoint sets of some metric space are separate.
C++ Given two finite sets, list all elements in the Cartesian product of these two sets.
C++ Given two finite sets, list all elements in the Cartesian product of these two sets.
if s1 and s2 are two simple functions then prove that the max and minimum of...
if s1 and s2 are two simple functions then prove that the max and minimum of then are also simple function.
Suppose you have two strains of mice, S1 and S2. Strain S2 is genetically modified to...
Suppose you have two strains of mice, S1 and S2. Strain S2 is genetically modified to metabolize a pharmacon P supposedly faster than S1. You conducted an experiment in a sample set of each strain, in which the pharmacon was injected and its concentration in blood was measured every 15min for 2h. Of course, age, gender, and weight was recorded for each animal. You want to statistically demonstrate that the metabolic rate of N is higher in S2 than S1....
Program in C: The strncpy(s1,s2,n) function copies exactly n characters from s2 to s1, truncating s2...
Program in C: The strncpy(s1,s2,n) function copies exactly n characters from s2 to s1, truncating s2 or padding it with extra null characters as necessary. The target string may not be null-terminated if the length of s2 is n or more. The function returns s1. Write your own version of this function. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT