Skip to main content

Posts

Showing posts from June 11, 2021

Program for array rotation

  Program for array rotation Difficulty Level :   Easy Last Updated :   16 Apr, 2021   Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements.  # Python3 program to rotate an array by # d elements # Function to left rotate arr[] of size n by d*/ def leftRotate(arr, d, n):      for i in range (d):          leftRotatebyOne(arr, n)   # Function to left Rotate arr[] of size n by 1*/ def leftRotatebyOne(arr, n):      temp = arr[ 0 ]      for i in range (n - 1 ):          arr[i] = arr[i + 1 ]      arr[n - 1 ] = temp              # utility function to print an array */ def printArray(arr, size):      for i in range (size):          print ( "% d" % arr[i], end = " " )       # Driver program to test above functions */ arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] leftRotate(arr, 2 , 7 ) printArray(arr, 7 )   # This code is contributed by Shreyanshi Arun Output :    3 4 5 6 7 1 2

Tower Of Hanoi

  Tower Of Hanoi Introduction to Tower of Hanoi The Tower of Hanoi is a mathematical puzzle also known as  Lucas’ Tower  . It is a game which consists of 3 rods/pegs/stack and  n  number of disks. These disks are of different sizes. These disks are arranged in an ascending order on a given peg. The objective of the game is to move the entire lot of disks of size  n  to another peg, following certain simple contraints which are: Only a single disk can be moved from one rod to another rod at a given time. Each move involves selecting the top-most disk from the source peg and moving it to the top of another peg. In other words we can say, only the topmost disk of a peg can be moved to the other. No larger disk can be placed on a smaller disk. Understanding the concept Each move consists of a shift of one disk from one peg to another. In the tower of hanoi problem we are given 3 pegs, namely : The source peg:  The rod from which the disk is moved to another peg is known as the source peg T

Python Program for Linear Search

  Python Program for Linear Search Difficulty Level :   Medium Last Updated :  11 JUNE, 2021 Problem:  Given an array arr[] of n elements, write a function to search a given element x in arr[]. Examples : Input : arr[] = {10, 20, 80, 30, 60, 50,110, 100, 130, 170} x = 110; Output : 6 Element x is present at index 6 Input : arr[] = {10, 20, 80, 30, 60, 50, 110, 100, 130, 170} x = 175; Output : -1 Element x is not present in arr[]. A simple approach is to do  linear search , i.e Start from the leftmost element of arr[] and one by one compare x with each element of arr[] If x matches with an element, return the index. If x doesn’t match with any of elements, return -1. Example: # Searching an element in a list/array in python # can be simply done using \'in\' operator # Example: # if x in arr: #   print arr.index(x)     # If you want to implement Linear Search in python     # Linearly search x in arr[] # If x is present then return its location # else ret