In: Computer Science
Question 1 (25pts):
Consider the string “data and program analytics”. Write a program to perform the following actions
Question 2 (15 pts):
List1 = [3, 4, 5, 20, 5]
Question 3 (15 pts):
Set1 = {1, 2, 3, 4}, Set2={4, 5, 6}
Question 4 (15 pts):
L = [('',), (), ('apple', 1), (), ("Paul", "Merage', 2020, "MSBA212"), ("d")]
Write a Python program to remove an empty tuple(s) from a list of tuples.
Expected outcome is:
[('',), ('apple', 1), ('Paul', 'Merage', 2020, 'MSBA212'), 'd']
Question 5 (15 pts):
Question 6 (15 pts):
Two given lists [1,2,4,8,5,10] and [2,4,5,8,12,15], write a program to make a list whose elements are intersection of the above given lists.
Question 1)
s="data and program analytics"
print("The index of character p is :",s.index('p'))
print("The character at index 7 is :",s[7])
print("The length of the string is :",len(s))
l=s.split(" ")
print("The string is split to list of words at occurence of white
space:",l)
s=s.replace("data","information")
print("The string after replacement is :",s)
OUTPUT:
Question 2)
List1 = [3, 4, 5, 20, 5]
print("The index of second 5 is :",end="")
c=0
for i in range(len(List1)):
if List1[i]==5:
c+=1
if c==2:#if it is second 5 then print the index i
print(i)
print("Tha last element of the list is :",List1[-1])#print the last
element
OUTPUT:
Question 3)
Set1 = {1, 2, 3, 4}
Set2={4, 5, 6}
print("The elements that appear in both the sets are
:",Set1.intersection(Set2))
print("The elements that appear in either of the sets
are:",Set1.union(Set2))
print("The elements that appear in set1 but not set2 are:
",Set1-Set2)
OUTPUT:
Question 4)
L = [('',), (), ('apple', 1), (), ("Paul", "Merage", 2020,
"MSBA212"), ("d")]
i=0
while i<len(L):
if len(L[i])==0:#if tuple is empty then pop it
L.pop(i)
i+=1
print(L)
OUTPUT:
Question 5)
s="one apple a day keeps the doctors away"
l=s.split(" ")
newstring=[]
for i in range(len(l)-1,-1,-1):
newstring.append(l[i])#store the words in reverse order
print(' '.join(newstring))#print the string
l.sort()
print("The sorted string is: ",end="")
print(' '.join(l))#print the string after sorting
OUTPUT:
Question 6)
res=[]
l1=[1,2,4,8,5,10]
l2=[2,4,5,8,12,15]
for i in l1:
if i in l2:#if element is in both lists then add it to res
list
res.append(i)
print("The intersection of above two lists is :",res)
OUTPUT:
Note:
Please let me know in case of any help needed in the comments section.
Please see that as per the guidelines, only one question can be solved.
In case of multiple choice type questions, upto 4 questions can be solved.
Plese upvote my answer. Thank you.