1 answer

71% I MTNSA-Take Car... LTE 13:10 <Siya Prac_Manual_2020.pdf 3.2 Write code to calculate 2 lists: lista:...

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
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 the results to a file called "setdata.txt" in a folder called "temp". Write out the added lines of code.

Answers

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

.

Similar Solved Questions

1 answer
Question 12 A person who has two identical alleles for a particular gene is ___ A....
Question 12 A person who has two identical alleles for a particular gene is ___ A. heterozygous B. recessive C. dominant D. homozygous for that gene. > A Moving to another question will save this response. MacBook Pro...
1 answer
Using the big M method to find the maximum value. Maximize subject to P = 3X,...
Using the big M method to find the maximum value. Maximize subject to P = 3X, +5X2 +6X3 2xy + X₂ + 223 572 2X1 + X2 - 2x = 3 X, X2, X3 20 Select the correct choice below and, if necessary, fill in the answer boxes to complete your choice. O A. The maximum value P = at x1 , X2= Xz = B. There is...
1 answer
6. (12 pts) (Polynomial decomposition) Find the values of a, b, c,d such that the following...
6. (12 pts) (Polynomial decomposition) Find the values of a, b, c,d such that the following equation holds for all IER 275 - 93* +11.13 - 11+92 + 18 = (4 – 2) (1 - 3)(ax' +ba? +c+d)...
1 answer
A 1500 kg aircraft going 35 m/s collides with a 2000 kg aircraft that is parked...
A 1500 kg aircraft going 35 m/s collides with a 2000 kg aircraft that is parked and they stick together after the collision and are going 15 m/s after the collision. If they skid for 16 seconds before stopping, how far did they skid? Hint: Are the aircraft moving at a constant velocity after the col...
1 answer
Question Completion Status QUESTION 4 which in Henry Mintzberg's sector organizational model those individuals found in...
Question Completion Status QUESTION 4 which in Henry Mintzberg's sector organizational model those individuals found in the State A ssectores producing or providing products or services to customers or cients directly supervising coordinating controlling and providing resources for erators stand...
1 answer
Carnes Cosmetics Co.'s stock price is $74.81, and it recently paid a $1.75 dividend. This dividend...
Carnes Cosmetics Co.'s stock price is $74.81, and it recently paid a $1.75 dividend. This dividend is expected to grow by 27% for the next 3 years, then grow forever at a constant rate, g; and rs = 13%. At what constant rate is the stock expected to grow after Year 3? Round your answer to two de...
1 answer
The logistic equation represents the idea that populations first increase exponentially but then their growth decreases...
The logistic equation represents the idea that populations first increase exponentially but then their growth decreases as they approach carrying capacity....
1 answer
Write it in C++ please Exercise # 1 : The 10 digits are spelled out respectively...
write it in C++ please Exercise # 1 : The 10 digits are spelled out respectively as follows: "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", and "Nine". Write a program that prompts the user to e...
1 answer
A Moving to another question will save this response. Question 21 Predict the product for the...
A Moving to another question will save this response. Question 21 Predict the product for the following reaction sequence. о H2O* 1. NaOCH2CH3 2. CH3CH2CH2! о о НО НО || III ОН 1 IV A II ООООО В. І с...
1 answer
A Radar transmits a 6µSec pulse observed two targets at the same azimuth and elevation. One...
A Radar transmits a 6µSec pulse observed two targets at the same azimuth and elevation. One target is 610 m behind the other. Since this distance is less than the range resolution, only one pulse will be received. How long will this pulse live? (c=3x108)...
1 answer
How many moles are in 11.0 g of silver metal? 1.19 x 10 moles 1.02 x...
How many moles are in 11.0 g of silver metal? 1.19 x 10 moles 1.02 x 10 moles 9.81 moles Next...
1 answer
4. Roscoe Restanrant is evaluating the potential to modify their service line. They expect to the...
4. Roscoe Restanrant is evaluating the potential to modify their service line. They expect to the cush flows with this project: -1,200 358 358 358 358 358 394 Compute the rate of returo on the SI 200 investment to within .1% Show your work [25 pts lint: Less than 9%....
1 answer
A 56-year-old male anatomy professor at a well-known university in North Hollywood was brought to the...
A 56-year-old male anatomy professor at a well-known university in North Hollywood was brought to the emergency room at the UCLA Ronald Reagan Medical Center. His chief complaint was right flank pain. History of present IlIness: One day prior to admission, the patient developed a change in the color...
1 answer
If someone can answer question 7, I would greatly appreciate it. #6 Finding the Phase Angle...
If someone can answer question 7, I would greatly appreciate it. #6 Finding the Phase Angle in a Series RLC Circuit Suppose a RLC series cir- cuit driven with an AC voltage source has a resistance of 35.6 12, a capacitative reactance of 29.3 2, and an inductive reactance of 43.5 22. What is the imp...
1 answer
QUESTION 3 [10 MARKS] a) Isobutane is used as a feedstock in the petrochemical industry, for...
QUESTION 3 [10 MARKS] a) Isobutane is used as a feedstock in the petrochemical industry, for example in the synthesis of isooctane. When 3.0 g of isobutene is heated to 400 C, it will change into butene. Table 2 shows the data that obtained in one experiment. After 25 minutes the mass of unreacted i...
1 answer
Question 1 4 pts Which of the following transactions will not cause a change in the...
Question 1 4 pts Which of the following transactions will not cause a change in the amount of total assets of a business? O purchased a piece of equipment by paying 1/2 down and agreeing to pay the remainder in 30 days O purchased supplies on account sold inventory costing $20,000 to a customer for ...
1 answer
Listed below are speeds (in mi/h) of cars on the New Jersey Turnpike by race of...
Listed below are speeds (in mi/h) of cars on the New Jersey Turnpike by race of the driver. All cars are going in the same direction, and all of the cars are from New Jersey. The data are from Statlib and the authors are Joseph Kadane and John Lamberth. Find Variance and find the standard deviation....