0% found this document useful (0 votes)
19 views8 pages

Lab 09........

The document discusses searching and sorting algorithms in Python. It provides examples of linear search and merging sorted lists. It asks the reader to write functions to perform linear search on a list, check if two lists have a common member, and merge two sorted lists. Sample code and outputs are provided for linear search, common member checking, and merged sorting. Errors in example code are also identified and corrected.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views8 pages

Lab 09........

The document discusses searching and sorting algorithms in Python. It provides examples of linear search and merging sorted lists. It asks the reader to write functions to perform linear search on a list, check if two lists have a common member, and merge two sorted lists. Sample code and outputs are provided for linear search, common member checking, and merged sorting. Errors in example code are also identified and corrected.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Programming Fundamentals (CS-116L) M SAAD KHAN

LAB # 09

SEARCHING & SORTING

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.

Correct Source Code:

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.

What will be the output of the following programs:


1. Code
strs = ['aa', 'BB', 'zz', 'CC']
Programming Fundamentals (CS-116L) M SAAD KHAN

print (sorted(strs))
print (sorted(strs, reverse=True))

Output:

2. Code
test_list = [1, 4, 5, 8, 10]
print ("Original list : " , test_list)

# check sorted list

if(test_list == sorted(test_list)):
print ("Yes, List is sorted.")
else :
print ("No, List is not sorted.")

Output:

C. Write Python programs for the following:

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:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy