In: Computer Science
How would you do the following using Python code:
For the second exercise determine the union, intersection and difference of the square and cube of integers ranging from 1 to 100. Sets are clearly the way to go here. You can use Math functions to generate the sets for the square and cube for the Integers. The following functionality should be available for the user via a simple interface:
1. Display the Square and Cube for Integers ranging from 1 to 100
2. Search the sets for a specific Integer and display the Square and Cube values
3. Display the Union of Square and Cube sets
4. Display the Intersection of Square and Cube sets
5. Display the Difference of Square and Cube sets
6. Exit the program If an Integer is not found an appropriate message should be displayed The program should continue to allow selections until the program is exited.
Hints: 1. Use Sets and their associated union(), interaction() and difference() methods 2. Use functions as often as possible 3. Use comments to document your code
(Note: Does not specify which integer search option should be used for searching.)
menuStr = '''1. Display the Square and Cube for Integers ranging from 1 to 100 2. Search the sets for a specific Integer and display the Square and Cube values 3. Display the Union of Square and Cube sets 4. Display the Intersection of Square and Cube sets 5. Display the Difference of Square and Cube sets 6. Exit the program Enter your choice: ''' squares = set([i*i for i in range(1, 101)]) cubes = set([i*i*i for i in range(1, 101)]) option = int(input(menuStr)) while option != 6: if option == 1: print('squares: ', squares) print('cubes: ', cubes) print() elif option == 2: x = int(input('Enter integer to search: ')) if x in squares: print(x, 'is a square value.') else: print(x, 'is not a square value.') if x in cubes: print(x, 'is a cube value.') else: print(x, 'is not a cube value.') elif option == 3: print(squares.union(cubes)) elif option == 4: print(squares.intersection(cubes)) elif option == 5: print(squares.difference(cubes)) elif option == 6: break else: print('Invalid option') print() option = int(input(menuStr))
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.