Question

In: Computer Science

How would you do the following using Python code: For the second exercise determine the union,...

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.)

Solutions

Expert Solution

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.


Related Solutions

For the second exercise determine the union, intersection and difference of the square and cube of...
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...
4.9 Branching Practice - Python Exercise #Write the code for the following # Exercise 1 #...
4.9 Branching Practice - Python Exercise #Write the code for the following # Exercise 1 # Ask user for a number using an input statement (no prompt) & assign the input to a variable # Convert the user entry to an integer and assign it to a variable # Condition: user entry is greater than zero # Action: display the following word on screen: positive # Exercise 2 # Ask user for a number using an input statement (no prompt)...
Do the following code by using python: ( Thank!) You roll two six-sided dice, each with...
Do the following code by using python: ( Thank!) You roll two six-sided dice, each with faces containing one, two, three, four, five and six spots, respectively. When the dice come to rest, the sum of the spots on the two upward faces is calculated. • If the sum is 7 or 11 on the first roll, you win. • If the sum is 2, 3 or 12 on the first roll (called “Mygame”), you lose (i.e., the “house” wins)....
Please using python to do the following code: You roll two six-sided dice, each with faces...
Please using python to do the following code: You roll two six-sided dice, each with faces containing one, two, three, four, five and six spots, respectively. When the dice come to rest, the sum of the spots on the two upward faces is calculated. • If the sum is 7 or 11 on the first roll, you win. • If the sum is 2, 3 or 12 on the first roll (called “Mygame”), you lose (i.e., the “house” wins). •...
What would the Python code be to create the following working program using turtle: -Make 4...
What would the Python code be to create the following working program using turtle: -Make 4 turtles that are YELLOW, and 4 turtles that are GREEN. Create these turtles using one or more lists. -Make the yellow turtles start at a different randomized location somewhere on the left side of the screen within the range of (-100,100) and (0, -100) -Make the green turtles start at a different randomized location somewhere on the right side of the screen within the...
4.10 Branching Practice 2 - Python Exercise #Write the code for the following #Exercise 1 #Ask...
4.10 Branching Practice 2 - Python Exercise #Write the code for the following #Exercise 1 #Ask the user to input a name using an input statement (no prompt) and assign it to a variable #Condition: user input is equal to 'Joseph' #Action: display the user input on screen #Exercise 2 #Ask the user to input his/her first name using an input statement (no prompt) and assign it to a variable #Condition: user input is equal to 'Joseph' #Action: display the...
How do you write a VHDL code for this The SecondGenerator block generates the second count...
How do you write a VHDL code for this The SecondGenerator block generates the second count i.e; it increments an internal variable once every second from 0 to 59 and then rolls back to 0. That (std_logic_vector) variable can be assigned to a (std_logic_vector) signal second[5..0] which comes out of the block. Note that this is a 6-bit std_logic_vector value because counts up to 59 can be accommodated in 6 binary digits (26 = 64). As most clocks do not...
how do you code a psuedo-random probability(%) in python? please provide an example.
how do you code a psuedo-random probability(%) in python? please provide an example.
Code in python the grocery receipt the following steps using the knowledge you have. declare a...
Code in python the grocery receipt the following steps using the knowledge you have. declare a string variable called card_number. Assign a value of “xxx8974” declare a string variable called date and assign a string value of “9\7\2020” You need to use an escape sequence for the slashes in this string. See Mosh’s video 2.4-Escape Sequence if needed. You do not need to use a date class variable as we have not learned that data type yet. Use a simple...
what is the solution of this code? Also how would I run it in python to...
what is the solution of this code? Also how would I run it in python to check? q = Queue() q.enqueue(10) q.enqueue(12) q.enqueue(15) for i in range(len(q)): → if len(q) % 2 == 0: → → q.enqueue(q.dequeue()) → else: → → q.dequeue() print(q)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT