HomeCHAPTER CLASS 12 Revision Tour - 2 Sumita Arora Practical Class 12 Computer science Solution 3 Comments Facebook Twitter Revision Tour - 2 Sumita Arora Practical Class 12 Computer science SolutionP.I.P :- 2.1Q1. What is a string slice? If a = "blueberry" evaluates the following:(a) a[2:3](b) a[2:](c) a[:3](d) a[:](e) a[-1:-3](f) a[:-1](g) a[1:1]Q2. Define what we mean by traversal of a string. Write a program that inputs a word (as a string, of course) and prints back the word without the vowels, like this:Q3. Change above program to surround the vowels with parentheses instead of not showing them.Q4. Read the following pieces of code. Determine whether they will be executed successfully or not. If yes, show the result that would be printed. If not, explain why not.(a)myTuple1 = (1,2,3)myTuple1.append(4)print(myTuple1)(b)myTuple2 = (1,2,3)myTuple3 = (4)print(myTuple2+myTuple3)(c)myList = [0,3,4,1]myList.remove(3)print(mylist)Q5. Beside each expression, write "LEGAL" if the expression is legal python code, or "ERROR" if the expression would cause an error. Assume that myList, myTuple, and myString are a list, a tuple, and a string, respectively, and all contain at least one element.1. myList = myList + [4]2. myList.append(4)3. del myList[0]4. myTuple myTuple + (4,)5. myTuple.append(4)6. del myTuple[0]7. myString = myString + "4"8. myString.append(4)9. del myString[0]Q6. Consider the following code: what is the value of a at the end?a = [1, 2, 3]a[2] = 0a[a[2]] = 5a[1:2] = []Q7. Write a program that accepts a list and removes the value at index 0 from the list. The program must actually modify the list passed in, and not just create a second list with the first item removed. You may assume the list you are given, will have at least one element.Q8. Assume each of the following lines is entered in the IDLE shell one at a time. Circle any of the lines that produces an error. If no errors are found then write what is printed (from a and b).a = [[[1, 2, 3, 4, 5], [6,7,8]],9]a[-1] = 200b = a[:]b[0][0][3] = 17print(a)print(b)Q9. Write a program that input two tuple and create a third that contain all elements of the first followed of the second.Q10. Arrays in python are implemented through lists. Write a program that sorts an array of integers in ascending order.Q11. Write a program that sorts an array of integer in ascending order. What if, you need to sort element of a tuple? Is it possible? How can you make it happen?Q12. Write a program that reverses a 2d array of integers, implemented as nested list, in the order of 0th element of every inner list.P.I.P :- 2.2Q1. Read the following pieces of code. Determine whether they will be executed successfully not. If yes, show the result that would be printed. If not, explain why not.(a)myDictionary1 = { a:1, b:2, c:3}myDictionary1[d] = 4print (myDictionary1)(b)myDictionary2 = { 10: 1, 20: 2, 30: 3}print (myDictionary2[1])Q2. What would be the output of following code?mydict = {"cat":12, "dog": 6, "elephant":23}mydict["mouse"] = mydict["cat"] + mydict["dog"]print (mydict["mouse"])Q3. What would be the output of following code?mydict = {"cat":12, "dog": 6, "elephant":23,"bear":20}answer = mydict.get("cat")//mydict.get("dog")print(answer)Q4. What would be the output of following code?mydict = {"cat":12, "dog": 6, "elephant":23, "bear":20}print("dog" in mydict)Q5. What would be the output of following code?mydict = {"cat":12, "dog":6, "elephant":23, "bear":20}print (23 in mydict)Q6. What would be the output of following code?total = 0mydict = {"cat":12, "dog": 6, "elephant":23,"bear":20}for akey in mydict: if len(akey) > 3: total = total + mydict[akey]print(total)Q7. Write code to create a python dictionary. add two entries to the dictionary: associate the key ‘name’ with the value ‘jivin’, and associate the key ‘phone’ with “0000-0000”Add two more name and their phones in the dictionary after getting input from user. then ask the user a name and print its corresponding phone.Q8. Write code to create a python dictionary add three name and their phone in the dictionary after getting input from user. the names should act as keys and phones as their values. then ask the user a name and print its corresponding phone.Q9. Given a dictionary with 5 student names and their marks. write a program that prints the dictionary contents in the ascending order of marks.Q10. Carefully read the following description and try to identify the sorting technique it is talking about.(a) In this, every element is compared with its next adjacent element and if the two elements are not in proper order, the two elements are swapped in place. If there are n elements in total, then n comparisons will place the largest element at the last position. Next time, beginning with first two elements and continuing with next elements, n-1 comparisons will place the 2nd largest element at the 2nd last position.(b) In this, we divide the list/array in two parts: (a) sorted part (which has no element in the beginning), (b) unsorted part (which has all the elements in the beginning). Every iteration is performed with an aim of putting the first element of unsorted part at the correct position in sorted part. Each such placement adds one element to sorted part and removes one element from unsorted part.Q11. (a) Carefully go through following code of Bubble Sort technique. It is creating some problem during execution. Figure out the problem and correct it. Try writing efficient solution of the problem.Hint. Efficient solution is error free and has less number of operation taking place:#arr is the list being sortedn = len(arr)for i in range(n): for j in range(n): if(arr[j] > arr[j+1]) temp = arr[1] arr[j] = arr[j+1] arr[j+1] = tempfor i in range(n): print (arr[i], end = "")(b) Find out the problem in following code of Insertion sort technique. State the problem and suggest solution for it.:#arr is the list being sortedn = len(arr)for i in range(n): key = arr[i] j = 1 - 1 while j >= 0 and key < arr[j] : arr[j+1] = arr[j] j = j - 1 arr[j+1] = keyfor i in range (n): print(arr[i])
Yoyoyoyoo
ReplyDeleteYes 🥰
Deleteamazing explanation like harry potter
ReplyDeleteEXPILLIARMUS
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )