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.)
Thanks for the question. Below is the code you will be needing Let me know if you have any doubts or if you need anything to change. Thank You !! ================================================================================================ def printOptions(): print('[1] Display the Square and Cube for Integers ranging from 1 to 100') print('[2] Search the sets for a specific Integer') print('[3] Display Union of Square and Cube Sets' ) print('[4] Display Intersection of Square and Cube Sets') print('[5] Display the Difference') print('[6] Exit') choice=input('Enter your choice: ') return choice def display(): print('{0:<10}{1:^10}{2:^10}'.format('Number','Square','Cube')) for i in range(1,101): print('{0:<10}{1:^10}{2:^10}'.format(i,i**2,i**3)) def search(): number=int(input('Enter your number: ')) if 1<=number and number<=100: print('Square: {} Cube: {}'.format(number**2,number**3)) else: print('Number not found.') def union(square_set, cube_set): union_set=square_set.union(cube_set) linebreak=1 for num in union_set: print('{0:>7}'.format(num),end=' ') if linebreak%20==0:print() linebreak+=1 print() def intersection(square_set, cube_set): union_set = square_set.intersection(cube_set) linebreak = 1 for num in union_set: print('{0:>7}'.format(num), end=' ') if linebreak % 20 == 0: print() linebreak += 1 print() def difference(square_set, cube_set): first_difference=square_set.difference(cube_set) print('Numbers in Square Set but not in Cube Set are:') linebreak = 1 for num in first_difference: print('{0:>7}'.format(num), end=' ') if linebreak % 20 == 0: print() linebreak += 1 print() second_difference=cube_set.difference(square_set) print('Numbers in Cube Set but not in Square Set are:') linebreak = 1 for num in second_difference: print('{0:>7}'.format(num), end=' ') if linebreak % 20 == 0: print() linebreak += 1 print() def main(): integer_set=set() square_set = set() cube_set=set() for i in range(1,101): integer_set.add(i) square_set.add(i**2) cube_set.add(i**3) while True: choice=printOptions() if choice=='1':display() elif choice=='2':search() elif choice=='3':union(square_set,cube_set) elif choice=='4':intersection(square_set,cube_set) elif choice=='5':difference(square_set,cube_set) elif choice=='6':break else:print('Invalid selection. Please choice the correct option') main()