Question

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 be
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:

#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:

inpuu 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: {2, 3,

main.py temp/setdata.txt 1 Set A : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29} Set B : {@, 1, 2, 3, 34, 5, 8, 13, 21} Intersection o

Add a comment
Know the answer?
Add Answer to:
71% I MTNSA-Take Car... LTE 13:10 <Siya Prac_Manual_2020.pdf 3.2 Write code to calculate 2 lists: lista:...
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
  • 3.2 Write code to calculate 2 lists: listA: all prime numbers between 1 and 30. listB:...

    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 the results to a file called "setdata.txt" in...

  • See the image below and write the code using C++, without using variables, only recursion. 3....

    See the image below and write the code using C++, without using variables, only recursion. 3. 5 a [DO NOT USE ANY VARIABLES Write a C++ function that takes two integer parameters (a and b) and prints a list of all the integers between a and b-1 (inclusive). one (2, 12) should print 2 34567 8 9 10 11. one (2, 13) should print 2 34567 8 9 10 11 12 b. [D NOT USE ANY VARIABLES Write a C++...

  • write a Matlab program ( solve the three questions). please use array and create your own...

    write a Matlab program ( solve the three questions). please use array and create your own function to check primality Question 1 : [10 points) Write a MATLAB program that will store all the first 1000 prime numbers in an array variable named arr_of_primes. Please note that a prime number is a positive integer that is bigger than or equal to 2 which is divisible only by 1 and itself. Examples of the first 8 primes are: 2, 3, 5,...

  • In Python Question 3 (13 points): Purpose: To practice your ability to modify lists and compute...

    In Python Question 3 (13 points): Purpose: To practice your ability to modify lists and compute with lists of lists Degree of Difficulty: Moderate For this question, you are given some population estimates, by age group, from Statistics Canada for some provinces. Starter File a5q3 starter.py is a file that contains a list of lists, to which the variable Pop-data refers, which represents 2020 population numbers. The first item in Pop_data is a list whose first item is the string...

  • Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

    Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...

  • python 3 question Project Description Electronic gradebooks are used by instructors to store grades on individual assignments and to calculate students’ overall grades. Perhaps your instructor uses gr...

    python 3 question Project Description Electronic gradebooks are used by instructors to store grades on individual assignments and to calculate students’ overall grades. Perhaps your instructor uses gradebook software to keep track of your grades. Some instructors like to calculate grades based on what is sometimes called a “total points” system. This is the simplest way to calculate grades. A student’s grade is the sum of the points earned on all assignments divided by the sum of the points available...

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

  • i need help to understand those two question from discussion in this artical please. 3. Take...

    i need help to understand those two question from discussion in this artical please. 3. Take a few minutes to re-read the first four paragraphs of the Discussion section. Explain in your own words the "metabolic suppression hypothesis" proposed by the researchers: what is the hypothesis, and why might it be plausible? WRITE YOUR RESPONSE HERE PLEASE (don't forget the in-text citation, as well as quotation marks and a page number if you are quoting in your answer) This study...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

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