Page 1 of 1

Warmup: Finding common elements in two lists This problem will give you practice with yet another aspect of using lists.

Posted: Thu Jun 02, 2022 8:14 am
by answerhappygod
Warmup: Finding common elements in two lists
This problem will give you practice with yet another aspect of
using lists. Besides providing a little programming warm-up before
you dive right into the main part of the assignment, the function
you write here may actually be something that you use in some form
in the second part of this assignment as well. You should write
your code for this problem in the file common_elements.py.
There are many situations in which we may be interested in
identifying the common elements of two lists. For example, say we
have a list of all the students who have completed all their Ways
course requirements at Stanford and another list of all the
students who have completed the requirements for their major(s) at
Stanford. By taking the elements (students) who appear in both
lists, we could produce a list of students who have met all their
course requirements at Stanford. If were then to take that
resulting list (of students who have met all their course
requirements) and a list of all undergrads at Stanford who
completed at least 180 units and took the common elements from
those two lists, we could produce a new list of all the students
who were eligible to graduate. Thus, producing a list of the common
elements from two other lists not only has practical applications,
but can be applied repeatedly to produce even more specific
results.
You task is to write the following function:
The function takes in two lists and should return a new list
which contains only those elements which appear
in both list1 and list2. The
original two lists passed into the function should not be
changed.
For example, if your function were called as follows:
it should return the list:
If your function is called with two lists that have no
overlapping elements, such as:
it should return the empty list:
Also, if your function is called with two lists that have
multiple of the same shared element, such as:
it should return a result that includes only one of the elements
that appeared multiple times, as shown in the result below:
Doctests are provided for you to test your function. Feel free
to write additional doctests. Also, feel free to write any
additional functions that may help you solve this problem. A main
function is also provided, which calls your function with some
sample test cases and prints the results.