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