# PRIP 9.1 Sumita Arora Solutions | Class 12 Computer Science

Here are PRIP 9.1 Sumita Arora Solutions for class 12 Computer Science. To view Sumita Arora solutions for all chapters, visit here.

Q.1: List Operations. Supposing that the command `LList = ['Cats', 'rule', '!', '?', '!', '?']` has been executed, complete the following tables as usual, using Python interactive mode to verify whether you were right. Remember: list slicing produces a new list object.

 S. No. Commands Expected Output Were you Right? 1. LList.remove(‘!’)print(LList) [‘Cats’, ‘rule’, ‘?’, ‘!’, ‘?’] Yes 2. LList.remove(‘C’) ValueError: C not in list Yes 3. print(LList) ? Yes 4. print(LList) IndexError: list index out of range Yes 5. LList.insert(1, ‘may’)print(LList) [‘Cats’, ‘may’, ‘rule’, ‘?’, ‘!’, ‘?’] Yes 6. print(LList.index(‘?’)) 3 Yes 7. print(LList.index(‘?!’)) ValueError: ‘?!’ is not in list Yes 8. copy1 = LListcopy2 = LList[:]LList = ‘Dogs’print(copy1) [‘Dogs’, ‘may’, ‘rule’, ‘?’, ‘!’, ‘?’] Yes 9. print(copy2) [‘Cats’, ‘may’, ‘rule’, ‘?’, ‘!’, ‘?’] Yes 10. Copy2.append(‘And also drool.’)             print(copy2) [‘Cats’, ‘may’, ‘rule’, ‘?’, ‘!’, ‘?’, ‘And also drool.’] Yes 11. print(len(copy2)) 7 Yes

Q.2: Consider the following sorted array:
`data = [1, 4, 6,7,9,10,14] `

(a) If using linear sequential search, how many comparisons (i.e., how many elements in the array do we need to examine) are needed to determine that 9 is in the array?
total comparisons 5
we need to compare every element till we reach 9 and we can stop execution when we compare 9

(b) If using linear sequential search, how many comparisons (i.e., how many elements in the array do we need to examine) are needed to determine that 11 is not in the array?
total comparisons 7
As 11 is not present in array execution stops when we reach end of array. i.e. we compare every element in array

Q.3: Fill in the blanks in the following code segment to implement sequential search of the given array. The method should return a boolean value indicating whether or not the parameter value was found in the array.
`def seqSearch( s) :    a = [9,12,14,3,25]     for i in ____:        if ____:            return ____    return ____`

`Code:def seqSearch(s):    a = [9,12,14,3,25]     for i in a:       # traverse through list a        if s == i:    # check if i is equal to s             return True  # if yes return True    return False         # else return falseprint(seqSearch(3))print(seqSearch(11))Output:TrueFalse`
Q.4: Write a function to insert an element in a sorted list only if it does not already exist in the list.

`Code:def insert_if_not_present(num,arr):    if num not in arr:          # check if num present in arr         arr.append(num)         # if not append num to list        return "number added"   # and return success msg    return "number already present"  # else return already present msg#__main__        a = [1, 2, 3, 4, 7, 8, 9]     # sorted arraynumber = int(input("Enter number you want to enter in list:")) # take input of numprint(insert_if_not_present(number,a))            #function callprint(a)                                      # print modified listOutput:Enter number you want to enter in list:9number already present[1, 2, 3, 4, 7, 8, 9]`
Q.5: Write a function to insert an element in a sorted list only if it already exists in the list. (i.e., only duplicate entries can be inserted)

`Code:def insert_if_not_present(num,arr):    if num in arr:          # check if num present in arr         arr.append(num)         # if not append num to list        return "number present and added"   # and return success msg    return "number not present"  # else return already present msg#__main__        a = [1, 2, 3, 4, 7, 8, 9]     # sorted arraynumber = int(input("Enter number you want to enter in list:")) # take input of numprint(insert_if_not_present(number,a))            #function callprint(a)                                      # print modified listOutput:Enter number you want to enter in list:10number not present[1, 2, 3, 4, 7, 8, 9]Enter number you want to enter in list:1number present and added[1, 2, 3, 4, 7, 8, 9, 1]`
Q.6: Write a function to delete an element from a sorted list.

`Code:def remove_element(num,arr):       if num in arr:             # check if number present in array        arr.remove(num)        # if yes remove it        return "number removed" # return success msg    return "number not present" # else return failure msg a = [1, 2, 3, 4, 7, 8, 9]     # sorted arraynumber = int(input("Enter number you want to enter in list:")) # take input of numprint(remove_element(number,a))            #function callprint(a)                                      # print modified listOutput:Enter number you want to enter in list:4number removed[1, 2, 3, 7, 8, 9]Enter number you want to enter in list:10number not present[1, 2, 3, 4, 7, 8, 9]`
Q.7: Modify the function deleting an element from a sorted list so that only duplicate entries can be removed. An element which is unique in the list cannot be deleted, e.g., from the array given below only 10, 35, 43 can be deleted as these have multiple entries ; other element cannot be deleted from it
`[5, 10, 10, 12, 20, 35, 35, 35, 40, 42, 43, 43, 50, 70]`

`Code:def remove_element(num,arr):     if arr.count(num) > 1:     # check count of number is greater than 1        arr.remove(num)        # if yes remove it        return "number has more than one copy hence removed" # return success msg    return "number not removed" # else return failure msg a = [5, 10, 10, 12, 20, 35, 35, 35, 40, 42, 43, 43, 50, 70]    # sorted arraynumber = int(input("Enter number you want to enter in list:")) # take input of numprint(remove_element(number,a))            #function callprint(a)                                      # print modified listOutput:Enter number you want to enter in list:10number has more than one copy hence removed[5, 10, 12, 20, 35, 35, 35, 40, 42, 43, 43, 50, 70]Enter number you want to enter in list:12number not removed[5, 10, 10, 12, 20, 35, 35, 35, 40, 42, 43, 43, 50, 70]`
Q.8: A is a two dimensional list created through following code:
`rows = int(input("Rows:")) cols= int(input("Columns:")) A = [(*cols) for row in range(rows)]A= 42`
Now consider following codes and determine what will be the output?

 Consider this What output do you anticipate? Were you right?(run to confirm) B = [row if row != 0 else row*2 for row in A] print (B) [[42, 0, 0, 0, 0],  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] yes rows = len(A)cols = len(A) print (“rows =”, rows) print (“cols =”, cols) rows = 5cols = 5 yes
9. Create a two dimensional List in Python that stores runs scored by a batsmen in five overs. The list looks somewhat like :
`Runs – [ [0, 6, 4, 1, 0, 0], [3, 0, 2, 0, 0, 0 ], [0, 0, 4, 4, 0, 1], [0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0]]`

(a) Write a function that returns the over in which the batsman scored the highest runs.

`Code:def Max_runs_over(runs):    over_runs =[]    for each in runs:             # for each list in runs        over_runs.append(sum(each))  # append total score in new list    max_score  = max(over_runs)      # get maximum number from new list i.e. max score    return over_runs.index(max_score)+1    # find index of max score in list and add 1 to it         Runs = [ [0, 6, 4, 1, 0, 0],          [3, 0, 2, 0, 0, 0],         [0, 0, 4, 4, 0, 1],         [0, 0, 0, 1, 0, 0],         [0, 0, 0, 0, 0, 0]]print("Maximum runs in over: {} ".format(Max_runs_over(Runs)))Output:Maximum runs in over: 1`

(b) Write a function that returns the over(s) in which the batsman scored the minimum runs.

`Code:def Min_runs_over(runs):    over_runs =[]    for each in runs:             # for each list in runs        over_runs.append(sum(each))  # append min score in new list    min_score  = min(over_runs)      # get Minimum number from new list i.e. min score    return over_runs.index(min_score)+1    # find index of min score in list and add 1 to it         Runs = [ [0, 6, 4, 1, 0, 0],          [3, 0, 2, 0, 0, 0],         [0, 0, 4, 4, 0, 1],         [0, 0, 0, 1, 0, 0],         [0, 0, 0, 0, 0, 0]]print("Minimum runs in over: {} ".format(Min_runs_over(Runs)))Output:Minimum runs in over: 5`

(c) Write a function that returns the total runs scored by the batsman.

`Code:def total_score(runs):    score = 0    for each in runs:        score += sum(each)    return scoreRuns = [ [0, 6, 4, 1, 0, 0],          [3, 0, 2, 0, 0, 0],         [0, 0, 4, 4, 0, 1],         [0, 0, 0, 1, 0, 0],         [0, 0, 0, 0, 0, 0]]print("Total score by batsman: {} ".format(total_score(Runs)))Output:Total score by batsman: 26`

Clear Doubts with Computer Tutor
In case you’re facing problems in understanding concepts, writing programs, solving questions, want to learn fun facts | tips | tricks or absolutely anything around computer science, feel free to join CTs learner-teacher community: students.computertutor.in

Subscribe
Notify of 