Skip to main content

Featured

Write a Python Program to find palindrome of a given string

If you reverse the word then you get the same word that word is palindrome. def   palindrome ( str ):     pointer =  str       rev_str =  reversed (pointer)       if   list (pointer) ==  list (rev_str):         print ( "It is palindrome" )       else :         print ( "It is not palindrome" )  palindrome( 'level' ) palindrome( 'madam' ) palindrome( 'life' ) palindrome( 'refer' ) Output:  It is palindrome It is palindrome It is not palindrome It is palindrome

Search in python

 Search is used to find a element is present  in a given sequence of data or not. In python there are two types of search

1) Linear search

2) Binary search

Linear search:

Linear search is one of the most old method used for searching.linear search is used for unsorted elements. We learn it to understand the searching process done in the program. Linear search compares the search element with all the elements present in the sequence which leads to more time.

 It starts from the first and search till the last element till the data is find sometimes the data may not be present in the sequence which leads to waste of process it is one of the disadvantage in linear search.

Best time complexity: 0[1]

Average time complexity: 0[n]

Worst time complexity: 0[n]

  • 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.

Example:

def linearsearch(arrx):
   for i in range(len(arr)):
      if arr[i] == x:
         return i
   return -1
arr = [2,5,7,4,6,8,3,9,1]
x = 6
print("element found at ",(linearsearch(arr,x)))

output :

element found at 4

Binary search:

Binary search is faster than linear search. Binary search is the simplest and easiest process. It is used only in sorted elements. The running time of binary search is log2 N and searching time N/2. 

In binary search it first separate the sequence of data into two parts then it will check the number is present in part one or two the process will continue until the element is found due to this the process it doesn't take much time . The compiler don't want to check all the elements.

Best time complexity: 0[1]

Average time complexity: 0[log n]

Worst time complexity: 0[log n]

Lets see deeply about the algorithm and working process of binary search in next upcoming post!!!!

Hope you understand the program and try yourself for more clear view feel free to comment your thoughts!!

Book for python:  https://amzn.to/3pCgIqy

ebook: https://amzn.to/3pHioiO



Comments

Popular Posts