Created
January 27, 2022 21:42
-
-
Save PythonRulz/6f77890802a8d762f30931bcccbbe8b1 to your computer and use it in GitHub Desktop.
The program takes two lists and finds the intersection of the two lists.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Problem Description | |
The program takes two lists and finds the intersection of the two lists. | |
Problem Solution | |
1. Define a function that accepts two lists and returns the intersection of them. | |
2. Declare two empty lists and initialize them to an empty list. | |
3. Consider a for loop to accept values for the two lists. | |
4. Take the number of elements in the list and store it in a variable. | |
5. Accept the values into the list using another for loop and insert into the list. | |
6. Repeat 4 and 5 for the second list also. | |
7. Find the intersection of the two lists. | |
8. Print the intersection. | |
9. Exit. | |
''' | |
def create_list(size, the_list): | |
for num in range (1, size+1): | |
the_list.append(int(input(f"Enter number {num}: "))) | |
return the_list | |
def find_union(list1, list2): | |
intersect_list = [] | |
for num in list1: | |
if num in list2: | |
intersect_list.append(num) | |
return intersect_list | |
def main(): | |
list1 = [] | |
list2 = [] | |
num_elements = int(input("Enter the number of elements for list 1: ")) | |
list1 = create_list(num_elements, list1) | |
print() | |
num_elements = int(input("Enter the number of elements for list 2: ")) | |
list2 = create_list(num_elements, list2) | |
result = find_union(list1, list2) | |
result.sort() | |
print(f"The intersection of the 2 lists is {result}") | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment