Skip to main content

Posts

Showing posts from February, 2023

Palindrome Checker Using Python

  Problem:               To find whether a given input is a palindrome or not. We use two methods to find the palindrome, one is using "reverse indexing" and "reverse function". Palindrome:  The term palindrome means, consider a string and reverse it then compare both the                                 string if they are equal then it is a palindrome. Code: def palindrome ( a ):     if a == a [::- 1 ]:         print ( "It is a Palindrome" )     else :         print ( "It is not a Palindrome" ) val = input ( "Enter the value" ) palindrome ( val )

Binary Search Using Functions in Python

Problem:                    To find an element from a python list using "Binary Search" method. This search is quite different from  Linear Search . There we used a loop to traverse and searched for element but here we first sort the list and compare the key value to the mid value of the list if it matches, we found the element or else we split the list into two one half consists of the elements lesser than the mid value and other half consists of the elements greater than the mid value, This process is repeated till we find a match. Code: def binarysearch ( a , k , start , end ):     mid = int (( start + end + 0.1 )/ 2 )     if a [ mid ]== k :         print ( "number found at position " , mid + 1 )     elif a [ mid ] > k :         binarysearch ( a , k , start , mid )     elif a [ mid ] < k :         binarysearch (...

Linear Search Using Functions Python

                                                            Problem:                         We need to search an element from an list. We have variety of methods to search an element from an array but the simple way of search is "Linear Search" method. We just need a loop traversing the array and search for match. We can see  two ways for achieving linear search in this post.  Method 1:                    Here we just directly give the values of array and value to search in default , the code follows. list1 =[ 2 , 3 , 1 , 4 , 5 ] key = 2 for i in range ( len ( list1 )):     if list1 [ i ]== key :         print ( "Number found" )  ...

Greatest of Four Number using Functions in C Programming

                        Hello everyone, this blog is to find the greatest of four numbers and we i am using the functions concept for this program. First we need a function named max_of_four(), and that accepts four number as parameter named a, b, c, d.                                   Our function is a combination of two greatest of two numbers, and it checks first two number and return the greatest then returns the greatest of next two numbers. This function is called form the main function and there   we get all inputs from the user. Code: # include  <stdio.h> int  max_of_four( int  a, int  b, int  c, int  d) {      int  max,max1;     //max of a and b      if (a>b)     { ...