0% found this document useful (0 votes)
54 views12 pages

Python Lab Ex 4 Ex 5

The document discusses two Python programs to perform linear search on a list. The first program searches for an element and returns its index if found, else returns -1. The second program takes user input for the list and search element, calls a linear_search function, and prints whether the element was found or not along with its index if found.

Uploaded by

pavansvgp2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views12 pages

Python Lab Ex 4 Ex 5

The document discusses two Python programs to perform linear search on a list. The first program searches for an element and returns its index if found, else returns -1. The second program takes user input for the list and search element, calls a linear_search function, and prints whether the element was found or not along with its index if found.

Uploaded by

pavansvgp2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

EX.

NO:4 PYTHON PROGRAM TO FIND THE MAXIMUM AND MINIMUM


ELEMENT IN A LIST

#1
my_list = []

#2
count = int(input("How many numbers you want to add : "))

#3
for i in range(1,count+1):
my_list.append(int(input("Enter number {} : ".format(i))))

#4
print("Input Numbers : ")
print(my_list)

#5
min = my_list[0]
max = my_list[0]

#6
for no in my_list:
if no < min : min = no
elif no > max :
max = no

#7
print("Minimum number : {}, Maximum number : {}".format(min,max))
1. Create one empty list mylist. We are using one _empty square bracket to
create the empty list. This is a list without any items.
2. Get the total number of elements the user is going to enter and save it
in the count variable. This is required because we will ask the user to
enter each number one by one. If the value of count is 4, the user will
have to enter four numbers to add to the list.
3. Using a_ for loop, get the numbers and append it to the list _mylist. For
appending a number, _append _method is used. This method takes the
value we are adding as the parameter. For reading the value, we are using
the _input _method. This method will read the value from the user. The
return value of this method is of _string _type. Wrapping it as int()_ will
convert the entered value to an integer.
4. Print the list to the user.
5. Create two variables to hold the minimum and maximum number. We
are assigning the first element of the list to both of these variables first.
We will update these variables on the next step. On this step, we are
assuming that the minimum and maximum value of the list is equal to
the first element of the list. We will compare this value with all other
elements of the list one by one and update them if required.
6. Run one for the loop on the list again. For each number, check if it is less
than the minimum number. If yes, assign the minimum value holding
variable to this number. Similarly, update the maximum value if the
number is more than the current maximum.
7. After the list is completed reading, print out both maximum and
minimum numbers
PYTHON PROGRAM TO FIND LARGEST OF N NUMBERS USING MAX FUNCTION
NOTE: THIS PROGRAM SHOULD BE RUN THROUGH ONLINE COMPILER

 Take input number for the length of the list using python
input() function.
 Initialize an empty list lst = [].
 Read each number in your python program using a for loop.
 In the for loop append each number to the list.
 Use built-in python function max() to find the largest
element in a list.
 End of the program print the largest number from list.

# Python program to find largest


# number in a list

# creating empty list


list1 = []

# asking number of elements to put in list


num = int(input("Enter number of elements in list: "))

# iterating till num to append elements in list


for i in range(1, num + 1):
ele = int(input("Enter elements: "))
list1.append(ele)

# print maximum element


print("Largest element is:", max(list1))
EX.NO.5: PYTHON PROGRAM ON LINEAR SEARCH
Algorithm
 Start from the leftmost element of given arr[] and one by one compare element x
with each element of arr[]
 If x matches with any of the element, return the index value.
 If x doesn’t match with any of elements in arr[] , return -1 or element not found.
Now let’s see the visual representation of the given approach −

def linearsearch(arr, x):

for i in range(len(arr)):

if arr[i] == x:

return i

return -1

arr = ['t','u','t','o','r','i','a','l']

x = 'a'

print("element found at index "+str(linearsearch(arr,x)))


SECOND METHOD: PYTHON PROGRAM ON LINEAR SEARCH
EXPLANATION
1. The user is prompted to enter a list of numbers.
2. The user is then asked to enter a key to search for.
3. The list and key is passed to linear_search.
4. If the return value is -1, the key is not found and a message is displayed,
otherwise the index of the found item is displayed.
NOTE:
 THIS PROGRAM USES SPLIT() METHOD
 First, raw_input().split() reads a line which is then split into its whitespace-
separated components. So a line like 1 3 2 5 7 3 becomes the list ['1', '3', '2',
'5', '7', '3']
 This list is used in a generator expression int(x) for x in list_above. This
expression evaluates to a generator which transforms the elements of the
list into their int() representations.
def linear_search(alist, key):
"""Return index of key in alist. Return -1 if key not present."""
for i in range(len(alist)):
if alist[i] == key:
return i
return -1

alist = input('Enter the list of numbers: ')


alist = alist.split()
alist = [int(x) for x in alist]
key = int(input('The number to search for: '))

index = linear_search(alist, key)


if index < 0:
print('{} was not found.'.format(key))
else:
print('{} was found at index {}.'.format(key, index))

You might also like