codeparrot/xlcost-text-to-code huggingface dataset:
Check if a string can be obtained by rotating another string d places | Function to reverse an array from left index to right index ( both inclusive ) ; Function that returns true if str1 can be made equal to str2 by rotating either d places to the left or to the right ; Left Rotation string will contain the string rotated Anti - Clockwise Right Rotation string will contain the string rotated Clockwise ; Copying the str1 string to left rotation string and right rotation string ; Rotating the string d positions to the left ; Rotating the string d positions to the right ; Compairing the rotated strings ; If cannot be made equal with left rotation ; If cannot be made equal with right rotation ; If both or any one of the rotations of str1 were equal to str2 ; Driver code ; d is the rotating factor ; In case length of str1 < d
def ReverseArray ( arr , left , right ) : NEW_LINE INDENT while ( left < right ) : NEW_LINE INDENT temp = arr [ left ] ; NEW_LINE arr [ left ] = arr [ right ] ; NEW_LINE arr [ right ] = temp ; NEW_LINE left += 1 ; NEW_LINE right -= 1 ; NEW_LINE DEDENT DEDENT def RotateAndCheck ( str1 , str2 , d ) : NEW_LINE INDENT if ( len ( str1 ) != len ( str2 ) ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT left_rot_str1 = [ ] ; right_rot_str1 = [ ] ; NEW_LINE left_flag = True ; right_flag = True ; NEW_LINE str1_size = len ( str1 ) ; NEW_LINE for i in range ( str1_size ) : NEW_LINE INDENT left_rot_str1 . append ( str1 [ i ] ) ; NEW_LINE right_rot_str1 . append ( str1 [ i ] ) ; NEW_LINE DEDENT ReverseArray ( left_rot_str1 , 0 , d - 1 ) ; NEW_LINE ReverseArray ( left_rot_str1 , d , str1_size - 1 ) ; NEW_LINE ReverseArray ( left_rot_str1 , 0 , str1_size - 1 ) ; NEW_LINE ReverseArray ( right_rot_str1 , 0 , str1_size - d - 1 ) ; NEW_LINE ReverseArray ( right_rot_str1 , str1_size - d , str1_size - 1 ) ; NEW_LINE ReverseArray ( right_rot_str1 , 0 , str1_size - 1 ) ; NEW_LINE for i in range ( str1_size ) : NEW_LINE INDENT if ( left_rot_str1 [ i ] != str2 [ i ] ) : NEW_LINE INDENT left_flag = False ; NEW_LINE DEDENT if ( right_rot_str1 [ i ] != str2 [ i ] ) : NEW_LINE INDENT right_flag = False ; NEW_LINE DEDENT DEDENT if ( left_flag or right_flag ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str1 = list ( " abcdefg " ) ; NEW_LINE str2 = list ( " cdefgab " ) ; NEW_LINE d = 2 ; NEW_LINE d = d % len ( str1 ) ; NEW_LINE if ( RotateAndCheck ( str1 , str2 , d ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
Count rotations of N which are Odd and Even | Function to count of all rotations which are odd and even ; Driver code
def countOddRotations ( n ) : NEW_LINE INDENT odd_count = 0 ; even_count = 0 NEW_LINE while n != 0 : NEW_LINE INDENT digit = n % 10 NEW_LINE if digit % 2 == 0 : NEW_LINE INDENT odd_count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT even_count += 1 NEW_LINE DEDENT n = n // 10 NEW_LINE DEDENT print ( " Odd ▁ = " , odd_count ) NEW_LINE print ( " Even ▁ = " , even_count ) NEW_LINE DEDENT n = 1234 NEW_LINE countOddRotations ( n ) NEW_LINE
Generate all rotations of a number | function to return the count of digit of n ; function to print the left shift numbers ; formula to calculate left shift from previous number ; Update the original number ; Driver code
def numberofDigits ( n ) : NEW_LINE INDENT cnt = 0 NEW_LINE while n > 0 : NEW_LINE INDENT cnt += 1 NEW_LINE n //= 10 NEW_LINE DEDENT return cnt NEW_LINE DEDENT def cal ( num ) : NEW_LINE INDENT digit = numberofDigits ( num ) NEW_LINE powTen = pow ( 10 , digit - 1 ) NEW_LINE for i in range ( digit - 1 ) : NEW_LINE INDENT firstDigit = num // powTen NEW_LINE left = ( num * 10 + firstDigit - ( firstDigit * powTen * 10 ) ) NEW_LINE print ( left , end = " ▁ " ) NEW_LINE num = left NEW_LINE DEDENT DEDENT num = 1445 NEW_LINE cal ( num ) NEW_LINE
Generating Lyndon words of length n | Python implementation of the above approach ; To store the indices of the characters ; Loop till w is not empty ; Incrementing the last character ; Repeating w to get a n - length string ; Removing the last character as long it is equal to the largest character in S
n = 2 NEW_LINE S = [ '0' , '1' , '2' ] NEW_LINE k = len ( S ) NEW_LINE S . sort ( ) NEW_LINE w = [ - 1 ] NEW_LINE while w : NEW_LINE INDENT w [ - 1 ] += 1 NEW_LINE m = len ( w ) NEW_LINE if m == n : NEW_LINE INDENT print ( ' ' . join ( S [ i ] for i in w ) ) NEW_LINE DEDENT while len ( w ) < n : NEW_LINE INDENT w . append ( w [ - m ] ) NEW_LINE DEDENT while w and w [ - 1 ] == k - 1 : NEW_LINE INDENT w . pop ( ) NEW_LINE DEDENT DEDENT
Check whether all the rotations of a given number is greater than or equal to the given number or not | Python3 implementation of the approach ; Splitting the number at index i and adding to the front ; Checking if the value is greater than or equal to the given value ; Driver code
def CheckKCycles ( n , s ) : NEW_LINE INDENT ff = True NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT x = int ( s [ i : ] + s [ 0 : i ] ) NEW_LINE if ( x >= int ( s ) ) : NEW_LINE INDENT continue NEW_LINE DEDENT ff = False NEW_LINE break NEW_LINE DEDENT if ( ff ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT n = 3 NEW_LINE s = "123" NEW_LINE CheckKCycles ( n , s ) NEW_LINE
Rotate the sub | Python3 implementation of the above approach ; Definition of node of linkedlist ; This function take head pointer of list , start and end points of sublist that is to be rotated and the number k and rotate the sublist to right by k places . ; If k is greater than size of sublist then we will take its modulo with size of sublist ; If k is zero or k is equal to size or k is a multiple of size of sublist then list remains intact ; m - th node ; This loop will traverse all node till end node of sublist . Current traversed node ; Count of traversed nodes ; Previous of m - th node ; We will save ( m - 1 ) th node and later make it point to ( n - k + 1 ) th node ; That is how we bring ( n - k + 1 ) th node to front of sublist . ; This keeps rest part of list intact . ; Function for creating and linking new nodes ; Driver code
import math NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def rotateSubList ( A , m , n , k ) : NEW_LINE INDENT size = n - m + 1 NEW_LINE if ( k > size ) : NEW_LINE INDENT k = k % size NEW_LINE DEDENT if ( k == 0 or k == size ) : NEW_LINE INDENT head = A NEW_LINE while ( head != None ) : NEW_LINE INDENT print ( head . data ) NEW_LINE head = head . next NEW_LINE DEDENT return NEW_LINE DEDENT link = None NEW_LINE if ( m == 1 ) : NEW_LINE INDENT link = A NEW_LINE DEDENT c = A NEW_LINE count = 0 NEW_LINE end = None NEW_LINE pre = None NEW_LINE while ( c != None ) : NEW_LINE INDENT count = count + 1 NEW_LINE if ( count == m - 1 ) : NEW_LINE INDENT pre = c NEW_LINE link = c . next NEW_LINE DEDENT if ( count == n - k ) : NEW_LINE INDENT if ( m == 1 ) : NEW_LINE INDENT end = c NEW_LINE A = c . next NEW_LINE DEDENT else : NEW_LINE INDENT end = c NEW_LINE pre . next = c . next NEW_LINE DEDENT DEDENT if ( count == n ) : NEW_LINE INDENT d = c . next NEW_LINE c . next = link NEW_LINE end . next = d NEW_LINE head = A NEW_LINE while ( head != None ) : NEW_LINE INDENT print ( head . data , end = " ▁ " ) NEW_LINE head = head . next NEW_LINE DEDENT return NEW_LINE DEDENT c = c . next NEW_LINE DEDENT DEDENT def push ( head , val ) : NEW_LINE INDENT new_node = Node ( val ) NEW_LINE new_node . data = val NEW_LINE new_node . next = head NEW_LINE head = new_node NEW_LINE return head NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = push ( head , 70 ) NEW_LINE head = push ( head , 60 ) NEW_LINE head = push ( head , 50 ) NEW_LINE head = push ( head , 40 ) NEW_LINE head = push ( head , 30 ) NEW_LINE head = push ( head , 20 ) NEW_LINE head = push ( head , 10 ) NEW_LINE tmp = head NEW_LINE print ( " Given ▁ List : ▁ " , end = " " ) NEW_LINE while ( tmp != None ) : NEW_LINE INDENT print ( tmp . data , end = " ▁ " ) NEW_LINE tmp = tmp . next NEW_LINE DEDENT print ( ) NEW_LINE m = 3 NEW_LINE n = 6 NEW_LINE k = 2 NEW_LINE print ( " After ▁ rotation ▁ of ▁ sublist : ▁ " , end = " " ) NEW_LINE rotateSubList ( head , m , n , k ) NEW_LINE DEDENT
Generating numbers that are divisor of their right | Python program to Generating numbers that are divisor of their right - rotations ; Function to check if N is a divisor of its right - rotation ; Function to generate m - digit numbers which are divisor of their right - rotation ; Driver code
from math import log10 NEW_LINE def rightRotationDivisor ( N ) : NEW_LINE INDENT lastDigit = N % 10 NEW_LINE rightRotation = ( lastDigit * 10 ** int ( log10 ( N ) ) + N // 10 ) NEW_LINE return rightRotation % N == 0 NEW_LINE DEDENT def generateNumbers ( m ) : NEW_LINE INDENT for i in range ( 10 ** ( m - 1 ) , 10 ** m ) : NEW_LINE INDENT if rightRotationDivisor ( i ) : NEW_LINE INDENT print ( i ) NEW_LINE DEDENT DEDENT DEDENT m = 3 NEW_LINE generateNumbers ( m ) NEW_LINE
Generating numbers that are divisor of their right | Python program to Generating numbers that are divisor of their right - rotations ; Function to generate m - digit numbers which are divisor of their right - rotation ; Driver code
from math import log10 NEW_LINE def generateNumbers ( m ) : NEW_LINE INDENT numbers = [ ] NEW_LINE for y in range ( 1 , 10 ) : NEW_LINE INDENT k_max = ( ( 10 ** ( m - 2 ) * ( 10 * y + 1 ) ) // ( 10 ** ( m - 1 ) + y ) ) NEW_LINE for k in range ( 1 , k_max + 1 ) : NEW_LINE INDENT x = ( ( y * ( 10 ** ( m - 1 ) - k ) ) // ( 10 * k - 1 ) ) NEW_LINE if ( ( y * ( 10 ** ( m - 1 ) - k ) ) % ( 10 * k - 1 ) == 0 ) : NEW_LINE INDENT numbers . append ( 10 * x + y ) NEW_LINE DEDENT DEDENT DEDENT for n in sorted ( numbers ) : NEW_LINE INDENT print ( n ) NEW_LINE DEDENT DEDENT m = 3 NEW_LINE generateNumbers ( m ) NEW_LINE
Check if an array is sorted and rotated | Python3 program to check if an array is sorted and rotated clockwise ; Function to check if an array is sorted and rotated clockwise ; Find the minimum element and it 's index ; Check if all elements before minIndex are in increasing order ; Check if all elements after minIndex are in increasing order ; Check if last element of the array is smaller than the element just before the element at minIndex starting element of the array for arrays like [ 3 , 4 , 6 , 1 , 2 , 5 ] - not sorted circular array ; Driver code ; Function Call
import sys NEW_LINE def checkIfSortRotated ( arr , n ) : NEW_LINE INDENT minEle = sys . maxsize NEW_LINE maxEle = - sys . maxsize - 1 NEW_LINE minIndex = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] < minEle : NEW_LINE INDENT minEle = arr [ i ] NEW_LINE minIndex = i NEW_LINE DEDENT DEDENT flag1 = 1 NEW_LINE for i in range ( 1 , minIndex ) : NEW_LINE INDENT if arr [ i ] < arr [ i - 1 ] : NEW_LINE INDENT flag1 = 0 NEW_LINE break NEW_LINE DEDENT DEDENT flag2 = 2 NEW_LINE for i in range ( minIndex + 1 , n ) : NEW_LINE INDENT if arr [ i ] < arr [ i - 1 ] : NEW_LINE INDENT flag2 = 0 NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag1 and flag2 and arr [ n - 1 ] < arr [ 0 ] ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT arr = [ 3 , 4 , 5 , 1 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE checkIfSortRotated ( arr , n ) NEW_LINE
Rotate a matrix by 90 degree in clockwise direction without using any extra space | Python3 implementation of above approach ; Function to rotate the matrix 90 degree clockwise ; printing the matrix on the basis of observations made on indices . ; Driver code
N = 4 NEW_LINE def rotate90Clockwise ( arr ) : NEW_LINE INDENT global N NEW_LINE for j in range ( N ) : NEW_LINE INDENT for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT print ( arr [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT arr = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] ] NEW_LINE rotate90Clockwise ( arr ) ; NEW_LINE
Elements that occurred only once in the array | Function to find the elements that appeared only once in the array ; Sort the array ; Check for first element ; Check for all the elements if it is different its adjacent elements ; Check for the last element ; Driver code
def occurredOnce ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE if arr [ 0 ] != arr [ 1 ] : NEW_LINE INDENT print ( arr [ 0 ] , end = " ▁ " ) NEW_LINE DEDENT for i in range ( 1 , n - 1 ) : NEW_LINE INDENT if ( arr [ i ] != arr [ i + 1 ] and arr [ i ] != arr [ i - 1 ] ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if arr [ n - 2 ] != arr [ n - 1 ] : NEW_LINE INDENT print ( arr [ n - 1 ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 7 , 7 , 8 , 8 , 9 , 1 , 1 , 4 , 2 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE occurredOnce ( arr , n ) NEW_LINE DEDENT
Elements that occurred only once in the array | Python3 implementation to find elements that appeared only once ; Function to find the elements that appeared only once in the array ; Store all the elements in the map with their occurrence ; Traverse the map and print all the elements with occurrence 1 ; Driver code
import math as mt NEW_LINE def occurredOnce ( arr , n ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] in mp . keys ( ) : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT for it in mp : NEW_LINE INDENT if mp [ it ] == 1 : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 7 , 7 , 8 , 8 , 9 , 1 , 1 , 4 , 2 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE occurredOnce ( arr , n ) NEW_LINE
Elements that occurred only once in the array | Function to find the elements that appeared only once in the array ; Check if the first and last element is equal If yes , remove those elements ; Start traversing the remaining elements ; Check if current element is equal to the element at immediate previous index If yes , check the same for next element ; Else print the current element ; Check for the last element ; Driver code
def occurredOnce ( arr , n ) : NEW_LINE INDENT i = 1 NEW_LINE len = n NEW_LINE if arr [ 0 ] == arr [ len - 1 ] : NEW_LINE INDENT i = 2 NEW_LINE len -= 1 NEW_LINE DEDENT while i < n : NEW_LINE INDENT if arr [ i ] == arr [ i - 1 ] : NEW_LINE INDENT i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT print ( arr [ i - 1 ] , end = " ▁ " ) NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if ( arr [ n - 1 ] != arr [ 0 ] and arr [ n - 1 ] != arr [ n - 2 ] ) : NEW_LINE INDENT print ( arr [ n - 1 ] ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 7 , 7 , 8 , 8 , 9 , 1 , 1 , 4 , 2 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE occurredOnce ( arr , n ) NEW_LINE DEDENT
Split the array and add the first part to the end | Set 2 | Function to reverse arr [ ] from index start to end ; Function to print an array ; Function to left rotate arr [ ] of size n by k ; Driver Code ; Function calling
def rvereseArray ( arr , start , end ) : NEW_LINE INDENT while start < end : NEW_LINE INDENT temp = arr [ start ] NEW_LINE arr [ start ] = arr [ end ] NEW_LINE arr [ end ] = temp NEW_LINE start += 1 NEW_LINE end -= 1 NEW_LINE DEDENT DEDENT def printArray ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def splitArr ( arr , k , n ) : NEW_LINE INDENT rvereseArray ( arr , 0 , n - 1 ) NEW_LINE rvereseArray ( arr , 0 , n - k - 1 ) NEW_LINE rvereseArray ( arr , n - k , n - 1 ) NEW_LINE DEDENT arr = [ 12 , 10 , 5 , 6 , 52 , 36 ] NEW_LINE n = len ( arr ) NEW_LINE k = 2 NEW_LINE splitArr ( arr , k , n ) NEW_LINE printArray ( arr , n ) NEW_LINE
Check if strings are rotations of each other or not | Set 2 | Python program to check if two strings are rotations of each other ; create lps [ ] that will hold the longest prefix suffix values for pattern ; length of the previous longest prefix suffix ; lps [ 0 ] is always 0 ; the loop calculates lps [ i ] for i = 1 to n - 1 ; Match from that rotating point ; Driver code
def isRotation ( a : str , b : str ) -> bool : NEW_LINE INDENT n = len ( a ) NEW_LINE m = len ( b ) NEW_LINE if ( n != m ) : NEW_LINE INDENT return False NEW_LINE DEDENT lps = [ 0 for _ in range ( n ) ] NEW_LINE length = 0 NEW_LINE i = 1 NEW_LINE lps [ 0 ] = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( a [ i ] == b [ length ] ) : NEW_LINE INDENT length += 1 NEW_LINE lps [ i ] = length NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( length == 0 ) : NEW_LINE INDENT lps [ i ] = 0 NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT length = lps [ length - 1 ] NEW_LINE DEDENT DEDENT DEDENT i = 0 NEW_LINE for k in range ( lps [ n - 1 ] , m ) : NEW_LINE INDENT if ( b [ k ] != a [ i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = " ABACD " NEW_LINE s2 = " CDABA " NEW_LINE print ( "1" if isRotation ( s1 , s2 ) else "0" ) NEW_LINE DEDENT
Count rotations divisible by 8 | function to count of all rotations divisible by 8 ; For single digit number ; For two - digit numbers ( considering all pairs ) ; first pair ; second pair ; considering all three - digit sequences ; Considering the number formed by the last digit and the first two digits ; Considering the number formed by the last two digits and the first digit ; required count of rotations ; Driver Code
def countRotationsDivBy8 ( n ) : NEW_LINE INDENT l = len ( n ) NEW_LINE count = 0 NEW_LINE if ( l == 1 ) : NEW_LINE INDENT oneDigit = int ( n [ 0 ] ) NEW_LINE if ( oneDigit % 8 == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT if ( l == 2 ) : NEW_LINE INDENT first = int ( n [ 0 ] ) * 10 + int ( n [ 1 ] ) NEW_LINE second = int ( n [ 1 ] ) * 10 + int ( n [ 0 ] ) NEW_LINE if ( first % 8 == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT if ( second % 8 == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT threeDigit = 0 NEW_LINE for i in range ( 0 , ( l - 2 ) ) : NEW_LINE INDENT threeDigit = ( int ( n [ i ] ) * 100 + int ( n [ i + 1 ] ) * 10 + int ( n [ i + 2 ] ) ) NEW_LINE if ( threeDigit % 8 == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT threeDigit = ( int ( n [ l - 1 ] ) * 100 + int ( n [ 0 ] ) * 10 + int ( n [ 1 ] ) ) NEW_LINE if ( threeDigit % 8 == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT threeDigit = ( int ( n [ l - 2 ] ) * 100 + int ( n [ l - 1 ] ) * 10 + int ( n [ 0 ] ) ) NEW_LINE if ( threeDigit % 8 == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = "43262488612" NEW_LINE print ( " Rotations : " , countRotationsDivBy8 ( n ) ) NEW_LINE DEDENT
Minimum move to end operations to make all strings equal | Python 3 program to make all strings same using move to end operations . ; Returns minimum number of moves to end operations to make all strings same . ; Consider s [ i ] as target string and count rotations required to make all other strings same as str [ i ] . ; find function returns the index where we found arr [ i ] which is actually count of move - to - front operations . ; If any two strings are not rotations of each other , we can 't make them same. ; Driver Code
import sys NEW_LINE def minimunMoves ( arr , n ) : NEW_LINE INDENT ans = sys . maxsize NEW_LINE for i in range ( n ) : NEW_LINE INDENT curr_count = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT tmp = arr [ j ] + arr [ j ] NEW_LINE index = tmp . find ( arr [ i ] ) NEW_LINE if ( index == len ( arr [ i ] ) ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT curr_count += index NEW_LINE DEDENT ans = min ( curr_count , ans ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ " xzzwo " , " zwoxz " , " zzwox " , " xzzwo " ] NEW_LINE n = len ( arr ) NEW_LINE print ( minimunMoves ( arr , n ) ) NEW_LINE DEDENT
Count rotations in sorted and rotated linked list | Linked list node ; Function that count number of rotation in singly linked list . ; Declare count variable and assign it 1. ; Declare a min variable and assign to data of head node . ; Check that while head not equal to None . ; If min value is greater then head -> data then it breaks the while loop and return the value of count . ; head assign the next value of head . ; Function to push element in linked list . ; Allocate dynamic memory for newNode . ; Assign the data into newNode . ; newNode -> next assign the address of head node . ; newNode become the headNode . ; Display linked list . ; Driver code ; Create a node and initialize with None ; push ( ) insert node in linked list . 15 -> 18 -> 5 -> 8 -> 11 -> 12 ; Function call countRotation ( )
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def countRotation ( head ) : NEW_LINE INDENT count = 0 NEW_LINE min = head . data NEW_LINE while ( head != None ) : NEW_LINE INDENT if ( min > head . data ) : NEW_LINE INDENT break NEW_LINE DEDENT count += 1 NEW_LINE head = head . next NEW_LINE DEDENT return count NEW_LINE DEDENT def push ( head , data ) : NEW_LINE INDENT newNode = Node ( data ) NEW_LINE newNode . data = data NEW_LINE newNode . next = ( head ) NEW_LINE ( head ) = newNode NEW_LINE return head NEW_LINE DEDENT def printList ( node ) : NEW_LINE INDENT while ( node != None ) : NEW_LINE INDENT print ( node . data , end = ' ▁ ' ) NEW_LINE node = node . next NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = push ( head , 12 ) NEW_LINE head = push ( head , 11 ) NEW_LINE head = push ( head , 8 ) NEW_LINE head = push ( head , 5 ) NEW_LINE head = push ( head , 18 ) NEW_LINE head = push ( head , 15 ) NEW_LINE printList ( head ) ; NEW_LINE print ( ) NEW_LINE print ( " Linked ▁ list ▁ rotated ▁ elements : ▁ " , end = ' ' ) NEW_LINE print ( countRotation ( head ) ) NEW_LINE DEDENT