Question

In: Computer Science

Question 1 (25pts): Consider the string “data and program analytics”. Write a program to perform the...

Question 1 (25pts):

Consider the string “data and program analytics”. Write a program to perform the following actions

  • Find the index of the character ‘p’ in the given string
  • Print the character which is present at index number seven
  • Find the length of the String
  • Split the string at every occurrence of a whitespace
  • Replace the word “data” in the original string with “information” using a standard string manipulation function. Your output after the manipulation should be “information and program analytics”

Question 2 (15 pts):

List1 = [3, 4, 5, 20, 5]

  • Find the index of the second 5
  • Find the last element of this list

Question 3 (15 pts):

Set1 = {1, 2, 3, 4}, Set2={4, 5, 6}

  • Print all elements that appear in both sets
  • Print those elements that appear in either set
  • Print all elements that appear in Set1 but not Set2

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

  • Reverse words in the string "one apple a day keeps the doctors away" and print the new string
  • Sort the words in the string "one apple a day keeps the doctors away" alphabetically and then print the new string.

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.

Solutions

Expert Solution

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.


Related Solutions

Write a program to perform the following two tasks: 1. The program will accept a string...
Write a program to perform the following two tasks: 1. The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop and smell the rose". Display the result string. 2. Then...
Write a program to perform the following two tasks: 1. The program will accept a string...
Write a program to perform the following two tasks: 1. The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop and smell the rose". Display the result string. 2. Then...
Question 1: Write a program to receive a string from the user and compares the first...
Question 1: Write a program to receive a string from the user and compares the first and second half of the word and prints “First and Second Half Same” or “First and Second Half Different”. If the length of the word is odd, ignore the middle letter. For example: Run 1: Enter a word: abcdabc First and Second Half Same Run 2: Enter a word: abcde First and Second Half Different Hint: use the Math.floor() and length() to get the...
A C program that will perform the following: Input the string: Abc_3_deF Expected Output: The string...
A C program that will perform the following: Input the string: Abc_3_deF Expected Output: The string you entered is: aBC_Three_DEf An output for just this specific input will be fine. If you want to provide a program for all outputs, it would help with my understanding of how the program works overall as well but it is not needed. Thanks!
write a C++ program that : 1. Perform a rot13 substitution 2. Perform a caesar encryption...
write a C++ program that : 1. Perform a rot13 substitution 2. Perform a caesar encryption given a dictionary 3. Perform a caesar decryption given a dictionary 4. Create a random caesar cipher dictionary If user prints: -r : Perform rot13 substitution -g : generate a random caesar cipher dictionary. -e: Encrypt using the caesar cipher -d : Decrypt using the caesar cipher The format for the caesar cipher dictionary is a file with 26 pairs of letters, one per...
Python Program 1: Write a program that takes user input in the form of a string...
Python Program 1: Write a program that takes user input in the form of a string Break up the string into a list of characters Store the characters in a dictionary The key of the dictionary is the character The value of the dictionary is the count of times the letter appears Hint: remember to initialize each key before using Output of program is the count of each character/letter Sort the output by key (which is the character) Python Program...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
You have to perform the following tasks: 1- Write the name of best string matching algorithm...
You have to perform the following tasks: 1- Write the name of best string matching algorithm in terms of time and space complexity. 2- Implement it in (c or c++) 3- Dry run it on following strings: - String A=aaabaaaabbaaabcaaabd - String B=aaabd Deliverable in PDF file: Code + Dry run + Code execution results using the above given strings.
Write a C program that will read a character string and then encrypt the string based...
Write a C program that will read a character string and then encrypt the string based on one of the 3 different encryption methods. The type of encryption is to be selected by the user. Encryption method 1: Swapping by position. Characters in the array are swapped with the opposite characters based on their position in the string. Example: Input string – apple. Encrypted string – elppa Method: The first character ‘a’ and the last character ‘e’ – swap their...
Write a program that prompts the user to input a string. The program then uses the...
Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning of your program and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT