Question

3.2 Write code to calculate 2 lists: listA: all prime numbers between 1 and 30. listB: first 10 numbers in the Fibonacci seri
3.3 Add code to the script in 3.2 to allow your code to save both sets and the results to a file called setdata.txt in a fo
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

python file name: main.py

#importing math library for sqrt() function
#importing os for file management
import math
import os

#function to check a number is prime or not
def isPrime(n):
    flag = True
    i = 2
    while i<=math.sqrt(n):
        if n % i == 0:
            flag = False
            break
        i = i + 1
    return flag

#function to write the results to the file
def writing_file():
    try:
        os.chdir(path) #changing Directory from current Directory to temp directory
    except OSError:
        print("Directory changed %s failed" %path)
    else:
        print("Directory changed %s successfully" %path)
        file1 = open("setdata.txt","w") #creating a text file setdata.txt
        #writing all the information to the file
        file1.write("Set A : ")
        file1.write(str(setA)) #to write you have to convert the set to string using set function
        file1.write("\n")
        file1.write("Set B : ")
        file1.write(str(setB))
        file1.write("\n")
        file1.write("Intersection of setA and setB :")
        file1.write(str(res1))
        file1.write("\n")
        file1.write("Difference of setA and setB : ")
        file1.write(str(res2))
        file1.write("\n")
        file1.write("Difference of setB and setA : ")
        file1.write(str(res3))
        print("Successfully write all the result to the text file")
    

#creating empty lists to store prime numbers and fibonacci numbers 
listA = []
listB = []

for i in range(2,30):
    if isPrime(i):
        listA.append(i) #appending each number to the list when it is a prime number
   
a = 0
b = 1
sums = 0
count = 1
while count<= 10:
    listB.append(sums) #appending fibonacci numbers to the list
    count += 1
    a = b
    b = sums
    sums = a + b
    
setA = set(listA) #converting list to set by using set() function
setB = set(listB)

res1 = setA.intersection(setB) #intersection function is used to store intersection of two sets
res2 = setA.difference(setB) #difference function is used to store the difference of two sets 
res3 = setB.difference(setA)
print("setA :",setA)
print("setB :",setB)
print("Intersection of setA and setB: ",res1)
print("Difference of setA and setB: ",res2)
print("Difference of setB and setA: ",res3)
c_path = os.getcwd() #storing current path to the c_path variable
path = os.path.join(c_path,'temp') #appending temp folder name to current path 
try:
    os.mkdir(path) #creating temp folder
except OSError:
    print("Creation of the directory %s failed" % path)
    print("The directory may exist!!!")
    writing_file() #calling writing_file() function to write in the file
else:
    print("Successfully created the directory %s" %path)
    writing_file() #calling writing_file() function to write in the file
    

output:

setA : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29} setB : {0, 1, 2, 3, 34, 5, 8, 13, 21} Intersection of setA and setB: {13, 2, 3, 5

This PC > Desktop python > temp Name Date modified Type Size setdata 31-07-2020 18:59 Text Document 1 KB setdata - Notepad Fi

Add a comment
Know the answer?
Add Answer to:
3.2 Write code to calculate 2 lists: listA: all prime numbers between 1 and 30. listB:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 71% I MTNSA-Take Car... LTE 13:10 <Siya Prac_Manual_2020.pdf 3.2 Write code to calculate 2 lists: lista:...

    71% I MTNSA-Take Car... LTE 13:10 <Siya Prac_Manual_2020.pdf 3.2 Write code to calculate 2 lists: lista: all prime numbers between 1 and 30. listB: first 10 numbers in the Fibonacci series. Convert these lists to sets: set and setB. Your script must then calculate and display the intersection and the difference between these sets. Answers: Intersection: Difference: 2 Write out the complete script. 3.3 Add code to the script in 3.2 to allow your code to save both sets and...

  • Write a program that finds and output all of the prime numbers between 2 to 2000...

    Write a program that finds and output all of the prime numbers between 2 to 2000 to the display screen. You are to declare any required array in main and pass them to your function sieve that will process the array.C++

  • Add JavaScript code in the “find_primeV2.js” to allow users to enter a number, and then based...

    Add JavaScript code in the “find_primeV2.js” to allow users to enter a number, and then based on the number of user enters, to find out how many prime numbers there are up to and including the user inputted number and then display them on the web page. The following are the detailed steps to complete this assignment: Step 1. [30 points] In “find_primeV2.js”, complete isPrime() function by (1) Adding one parameter in function header. That parameter is used to accept...

  • All information about the question (Task 1 and task 2) are down below Programming Assignments Task...

    All information about the question (Task 1 and task 2) are down below Programming Assignments Task 1 – Page 39. in Programming A Comprehensive Introduction Update your program from Assignment 2, Task #2 Allow the user to input their weight for the earth weight to moon weight conversion problem. Add an ifstatement that prompts the user if she inputs 0 or a negative number for her earth weight. #13. Mars’ gravity is about 17 percent less that of the earth’s....

  • Java BlackJack Game: Help with 1-4 using the provided code below Source code for Project3.java: import...

    Java BlackJack Game: Help with 1-4 using the provided code below Source code for Project3.java: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class Project3 extends JFrame implements ActionListener { private static int winxpos = 0, winypos = 0; // place window here private JButton exitButton, hitButton, stayButton, dealButton, newGameButton; private CardList theDeck = null; private JPanel northPanel; private MyPanel centerPanel; private static JFrame myFrame = null; private CardList playerHand = new CardList(0); private CardList dealerHand = new CardList(0);...

  • #include <iostream> #include <iomanip> #include <vector> using namespace std; Part 1. [30 points] In this part,...

    #include <iostream> #include <iomanip> #include <vector> using namespace std; Part 1. [30 points] In this part, your program loads a vending machine serving cold drinks. You start with many foods, some are drinks. Your code loads a vending machine from foods, or, it uses water as a default drink. Create class Drink, make an array of drinks, load it and display it. Part 1 steps: [5 points] Create a class called Drink that contains information about a single drink. Provide...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • Additional code needed: PartA: BurgerOrder class (1 point) Objective: Create a new class that represents an...

    Additional code needed: PartA: BurgerOrder class (1 point) Objective: Create a new class that represents an order at a fast-food burger joint. This class will be used in Part B, when we work with a list of orders. As vou work through this part and Part B, draw a UML diagram of each class in using the UML drawing tool 1) Create a new Lab5TestProject project in Netbeans, right-click on the lab5testproject package and select New>Java Class 2) Call your...

  • This is the sequence 1,3,6,10,15 the pattern is addin 1 more than last time but what is the name for this pattern These are called the triangular numbers The sequence is 1 3=1+2 6=1+2+3 10=1+2+3+4 15=1+2+3+4+5 You can also observe this pattern

    This is the sequence 1,3,6,10,15 the pattern is addin 1 more than last time but what is the name for this patternThese are called the triangular numbers The sequence is 1 3=1+2 6=1+2+3 10=1+2+3+4 15=1+2+3+4+5 You can also observe this pattern x _________ x xx __________ x xx xxx __________ x xx xxx xxxx to see why they're called triangular numbers. I think the Pythagoreans (around 700 B.C.E.) were the ones who gave them this name. I do know the...

  • please answer all pre-lab questions 1 through 5. THANK YOU!!! this is the manual to give...

    please answer all pre-lab questions 1 through 5. THANK YOU!!! this is the manual to give you some background. the pre-lab questions.. the pre-lab sheet. Lab Manual Lab 10: String Waves & Resonance Before the lab, read the theory in Sections 1-3 and answer questions on Pre-lab Submit your Pre-lab at the beginning of the lab. During the lab, read Section 4 and follow the procedure to do the experiment. You will record data sets, perform analyses, answer questions, and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT