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

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


Related Solutions

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