Question

In: Computer Science

C++, Java, Python. Assume we have two sequences of values S1 containing 1, 5, 3, 6,...

C++, Java, Python. Assume we have two sequences of values S1 containing 1, 5, 3, 6, 7, 8 while S2 containing 2, 5, 6, 9, 7. We’d store these two sequences as sets and performance set intersection and set difference. Write C++, Java, Python codes to do that respectively. Compare and contrast the readability and writability.

Solutions

Expert Solution

CODE IN PYTHON:

# IF YOU WANT TO TAKE SET INPUT FROM USER:

set1=set([int(value) for value in input().split()])

set2=set([int(value) for value in input().split()])

#INTERSECTION

# methods for the intersection between two lists or set

#method 1: (intersection means a value is present in both sets)

lst1=[1,5,3,6,7,8]
lst2=[2,5,6,9,7]
lst3=[value for value in lst1 if value in lst2]   # lst3 hold the intersection of lst1 and lst2
print("intersection of two list lst1 and lst2:")
print(lst3) #simply print insection list:

#output:

the intersection of two list lst1 and lst2:
[5, 6, 7]

#method 2:
set1=set([1,5,3,6,7,8]) #convert list in to set because set never have repeated values but list can hold repeated values.
set2=set([2,5,6,9,7])
set3=set1.intersection(set2)   
print("intersection of two set set1 and set2:")
print(set3)

#output:

intersection of two set set1 and set2:
{5, 6, 7}

#DIFFERENCE


#methods for difference between two list or set

# method 1:(difference means a value is present in one set but not present in another set )
lst1=[1,5,3,6,7,8]
lst2=[2,5,6,9,7]
lst3=[value for value in lst1 if value not in lst2]
print("difference of two list lst1 and lst2:")
print(lst3)

#output:

the difference of two list lst1 and lst2:
[1, 3, 8]

# method 2:
set1=set([1,5,3,6,7,8])
set2=set([2,5,6,9,7])
set3=set1.difference(set2)
print("difference of two set of lst1 and lst2:")
print(set3)

#output:
difference of two set of lst1 and lst2:
{8, 1, 3}

... I think I have to explain everything very clearly. you can check this code by running.


Related Solutions

Assume that you have a fair 6 sided die with values {1, 2, 3, 4, 5,...
Assume that you have a fair 6 sided die with values {1, 2, 3, 4, 5, 6} and a fair 12 sided die with values {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}. A discrete random variable is generated by rolling the two dice and adding the numerical results together. (a) Create a probability mass function that captures the probability of all possible values of this random variable. You may use R or draw the pmf...
For any two real sequences {an} and {bn}, prove that Rudin’s Ex. 5 We assume that...
For any two real sequences {an} and {bn}, prove that Rudin’s Ex. 5 We assume that the right hand side is defined, that is, not of the form ∞ − ∞ or −∞ + ∞. lim sup (an + bn) ≤ lim sup an + lim sup bn. Proof If lim sup an = ∞ or lim sup bn = ∞, there is nothing to prove
Python. 5) What will the code below do? (Assume that we have a dataset df with...
Python. 5) What will the code below do? (Assume that we have a dataset df with these two columns named Occupation' and 'Age') df.groupby('Occupation')['Age'].mean() a) It will return the average age per occupation b) It will return an error c) It will return the total age per occupation d) None of the options 6) df.describe() will return basic descriptive statistics only for numerical variables True/False ? 7) Pandas dataframes can be converted into numpy arrays Truse/False ?
make a C / C++, Java, or Python program with two processes, a producer and a...
make a C / C++, Java, or Python program with two processes, a producer and a consumer. The producer and consumer will share an integer array with a length of 5 which is a circular buffer. The producer and consumer will also share one or two (your choice) variables to coordinate the placing and removal of items from the circular buffer. The producer process consists of a loop that writes the loop count (a value from 0 to 99) into...
5. (20%) Suppose we have an array int a [8] = {1, 2, 3, 5, 6};...
5. (20%) Suppose we have an array int a [8] = {1, 2, 3, 5, 6}; and we also have a linked list L of 5 entries 1 -> 2 -> 3 -> 5 -> 6, where 1 is the first in the linked list L, followed by 2 etc. We want to put a new item 4 between 3 and 5 in the array a and in the linked list L (a) Explain in plain English how you do...
Assume that we have 3 Stocks A, B & C. A B C Expected return 8%...
Assume that we have 3 Stocks A, B & C. A B C Expected return 8% 12% 6% Variance 0.012 0.028 0.009 If the covariance between stock A & B return is equal to 0.009159, stock A & C return is equal to -0.005146 and stock B & C return 0.01572 Required Calculate the Standard Deviation for stock A, B & C. which stock is risky and why? Calculate the correlation between every two stocks. What is the indication of...
We would like to align two DNA sequences: (v) C G A T A C T,...
We would like to align two DNA sequences: (v) C G A T A C T, and (w) G A T T C G T : i) s(i, j) = 1.5 if vi = wj (matches); ii) s(i, j) = -1.0 if vi != wj (mismatches); iii) d = 0.25 (indels: insertions or deletions). What would be the maximum alignment score? Explain how you get the result.
Java Programming Assignment 6-1 We have covered the “Methods” this week. Apply the concepts that we...
Java Programming Assignment 6-1 We have covered the “Methods” this week. Apply the concepts that we have learnt in class and come up with a “problem scenario” for which you can create a “solution”. Utilize any/all of the examples from the book and class that we discussed. You program should be interactive and you should give a detailed statement about what the “description of the program – purpose and how to use it”. You should also use a “good bye”...
Which of these DNA sequences is palindromic when double-stranded? A) 5-ATGGTA-3 B) 5-GGGCCC-3 C) 5-ATCCAT-3 D)...
Which of these DNA sequences is palindromic when double-stranded? A) 5-ATGGTA-3 B) 5-GGGCCC-3 C) 5-ATCCAT-3 D) 5-ATGATG-3
2. Translate the following C/Java code to MIPS assembly code. Assume that the values of a,...
2. Translate the following C/Java code to MIPS assembly code. Assume that the values of a, i, and j are in registers $s0, $t0, and $t1, respectively. Assume that register $s2 holds the base address of the array A (add comments to your MIPS code). j = 0; for(i=0 ; i<a ; i++) A[i]=i+j++;
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT