Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
57 views

Practice 12 - Python Array

The document contains code to find if a target element is present in an input list using linear search. It takes the list and target element as input, iterates through the list and returns the index if found, else returns -1.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Practice 12 - Python Array

The document contains code to find if a target element is present in an input list using linear search. It takes the list and target element as input, iterates through the list and returns the index if found, else returns -1.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Write a program to find the given element is present in the input list or not.

def linearSearch(list1, target):

n = len(list1)

for i in range(n) :

# If the target is in the ith element, return True

if list1[i] == target:

return i

return -1

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

list1 = list1.split()

list1 = [int(x) for x in list1]

target = int(input('Enter the number to be search: '))

index = linearSearch(list1, target)

if index < 0:

print('Element {} was not found.'. format(target))

else:

print('Element {} was found at index {}.'.format(target, index))


Write a program to find the numbers which are not multiple of 5 from the user list. Print the numbers in a single
line separated by a space as shown in the sample test cases:

n = input("Enter integers in the list separated by a space: ")

list1 = n.split()

for i in range(0,len(list1)):

list1[i] = int(list1[i])

list2 = []

for i in list1:

if (i%5 != 0):

list2.append(i)

else:

continue

if(list2 == []):

print("All the integers of the list are multiples of 5")

else:

print("Integers which are not multiples of 5 are:",end=" ")

for i in list2:

print(i,end=" ")
Given a list 'A' of N numbers (integers), you have to write a program which prints the sum of the elements of
list 'A' with the corresponding elements of the reverse of the list 'A'.

n = int(input("Enter the number of integers in list:"))

x = input("Enter integers in the list:")

l1 = x.split()

for i in range(len(l1)):

l1[i] = int(l1[i])

l2 = l1[::-1]

for i in range(len(l2)):

l2[i] = int(l2[i])

i=0

l3 = []

while(i<len(l1)):

l3.append(l1[i]+l2[i])

i+=1

print("Resultant list:",l3)
By using list comprehension, write a program to print the input list after removing an integer 'n' from it.

#Initializing/Creating a list

li = list(input("Enter integers in the list: ").split(","))

#Converting all the elements of the list into integers

for i in range(len(li)):

li[i] = int(li[i])

#Asking the user to enter the element to be removed

n = int(input("Enter the element to be removed from the list: "))

#Creating a new list using list comprehension syntax

lst = [x for x in li if x!=n]

print(lst)
Write a program to print the list after removing all even numbers

lst = input("Enter elements separated by a comma:").split(",")

result = [int(i) for i in lst]

print('List is:',result)

result = [x for x in result if x%2!=0]

print('List after removing all even numbers:',result)


Create a list with the user-given inputs. Write a program to print EQUAL if first and last elements of a list are
same, otherwise print NOT EQUAL.

Sample Input and Output 1:


data: Oliver,John,George,Oliver
equal
Sample Input and Output 2:
data: James,Charlie,Chaplin
not equal

data = input("data: ")

list1 = data.split(",")

if list1[0] == list1[-1]:

print("equal")

else:

print("not equal")
Create two lists with the user-given elements. Write a program to check whether the first or last elements of two
lists are the same or not. If yes print True, otherwise print False as shown in the examples.

Sample Input and Output 1:


data1: Python,Java,Perl,Swift
data2: Django,Flask,Swift
True

Sample Input and Output 2:


data1: 10,20,30,40
data2: 50,60,30,40,20
False

data1 = input("data1: ")

list1 = data1.split(",")

data2 = input("data2: ")

list2 = data2.split(",")

if list1[0] == list2[0] or list1[-1] == list2[-1]:

print("True")

else:

print("False")
By using list comprehension, write a program to print the square of each odd number in the user list.
Constraints:

  User list should not contain 0.

lst = list(input("Enter integers in the list: ").split(","))

for i in range(len(lst)):

lst[i] = int(lst[i])

lst_1 = [i**2 for i in lst if(i%2!=0)]

if(lst_1 == []):

print("There are no odd numbers in the list!")

else:

print(*lst_1,sep = ",")
Write a Python program that defines a matrix and prints matrices.

A matrix is a rectangular table of elements. The following is a 2x4 matrix, meaning there are 2 rows, 4 columns:

Matrix Representation:
5 4 7 11
3 3 8 17
In Python and other programming languages, a matrix is often represented with a list of lists. The sample matrix
above could be created with:

matrix = [ [5, 4, 7, 11], [3, 3, 8, 17] ] So matrix[0][0] is 5, matrix[0][1] is 4, matrix[1,0] is 3 and so on.

Sample Input and Output:


Number of rows, m = 2
Number of columns, n = 2
Entry in row: 1 column: 1
Entry in row: 1 column: 2
Entry in row: 2 column: 1
Entry in row: 2 column: 2
[[1, 2], [3, 4]]

m = int(input('Number of rows, m = '))

n = int(input('Number of columns, n = '))

matrix = []

# initialize the number of rows

for i in range(0,m):

matrix += [0]

# initialize the matrix with zeros

for i in range (0,m):

matrix[i] = [0]*n

# Get input

for i in range (0,m):

for j in range (0,n):

print ('Entry in row:', i+1,'column:', j+1)

matrix[i][j] = int(input())

print (matrix)
Create an integer List with user-given input. Write a program to print True if the first or last element of the List
is 3, otherwise print False

Sample Input and Output 1:


data: 12,52,63,96,85,3
True

Sample Input and Output 2:


data: 11,22,33,44,55,66,3,77
False

data = input("data: ")

list1 = data.split(",")

for i in range(0, len(list1)):

list1[i] = int(list1[i])

if (list1[0] == 3) or (list1[-1] == 3):

print("True")

else:

print("False")

You might also like