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.

Answer:

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[4])

?

Yes

4.

print(LList[5])

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 = LList
copy2 = LList[:]
LList[0] = ‘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?
Answer: 
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?
Answer:
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 ____

Answer:

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 false
print(seqSearch(3))
print(seqSearch(11))


Output:
True
False
Q.4: Write a function to insert an element in a sorted list only if it does not already exist in the list.

Answer:

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 array
number = int(input("Enter number you want to enter in list:")) # take input of num
print(insert_if_not_present(number,a))            #function call
print(a)                                      # print modified list


Output:
Enter number you want to enter in list:9
number 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)

Answer:

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 array
number = int(input("Enter number you want to enter in list:")) # take input of num
print(insert_if_not_present(number,a))            #function call
print(a)                                      # print modified list


Output:
Enter number you want to enter in list:10
number not present
[1, 2, 3, 4, 7, 8, 9]

Enter number you want to enter in list:1
number present and added
[1, 2, 3, 4, 7, 8, 9, 1]
Q.6: Write a function to delete an element from a sorted list.

Answer:

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 array
number = int(input("Enter number you want to enter in list:")) # take input of num
print(remove_element(number,a))            #function call
print(a)                                      # print modified list


Output:
Enter number you want to enter in list:4
number removed
[1, 2, 3, 7, 8, 9]

Enter number you want to enter in list:10
number 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]

Answer:

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 array
number = int(input("Enter number you want to enter in list:")) # take input of num
print(remove_element(number,a))            #function call
print(a)                                      # print modified list


Output:
Enter number you want to enter in list:10
number 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:12
number 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 = [([0]*cols) for row in range(rows)]
A[0][0]= 42

Now consider following codes and determine what will be the output?

Answer:

Consider this

What output do you anticipate?

Were you right?
(run to confirm)

B = [row if row[0] != 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[0]) 
print (“rows =”, rows) 
print (“cols =”, cols)

rows = 5
cols = 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.
Answer:

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.
Answer:

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.
Answer:

Code:
def total_score(runs):
    score = 0
    for each in runs:
        score += sum(each)
    return score

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("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
guest
0 Comments
Inline Feedbacks
View all comments

You cannot copy content of this page