Lab 09........
Lab 09........
LAB # 09
OBJECTIVE
Searching and sorting data in the form of list.
EXERCISE
A. Point out the errors, if any, and paste the output also in the following Python
programs.
1. Code
'apple' is in ['orange', 'apple', 'grape']
Errors:
1-We have to use an “if” statement to search an element inside the list.
Corrected Output:
2. Code
def countX(lst, x):
return lst.count(x)
Errors:
1-We have to provide a list to count the occurrence of “x” in the list.
print (sorted(strs))
print (sorted(strs, reverse=True))
Output:
2. Code
test_list = [1, 4, 5, 8, 10]
print ("Original list : " , test_list)
if(test_list == sorted(test_list)):
print ("Yes, List is sorted.")
else :
print ("No, List is not sorted.")
Output:
1. Write a program that take function which implements linear search. It should take a
list and an element as a parameter, and return the position of the element in the list. The
algorithm consists of iterating over a list and returning the index of the first occurrence
of an item once it is found. If the element is not in the list, the function should return
„not found‟ message.
Source Code:
Programming Fundamentals (CS-116L) M SAAD KHAN
Output:
2. Write a program that create function that takes two lists and returns True if they have
at least one common member. Call the function with atleast two data set for searching.
Source Code:
Programming Fundamentals (CS-116L) M SAAD KHAN
Output:
3. Write a program that create function that merges two sorted lists and call two list
with random numbers.
Source Code:
Programming Fundamentals (CS-116L) M SAAD KHAN
r
Output:
Practice Code
Code 1:
Programming Fundamentals (CS-116L) M SAAD KHAN
Source Code:
Output:
Code 2:
Source Code:
Programming Fundamentals (CS-116L) M SAAD KHAN
Output:
Code 3:
Source Code:
Programming Fundamentals (CS-116L) M SAAD KHAN
Output:
Code 4:
Source Code:
Output: