codeparrot/xlcost-text-to-code huggingface dataset:
Longest subsequence of a number having same left and right rotation | Python3 program to implement the above approach ; Function to find the longest subsequence having equal left and right rotation ; Length of the string ; Iterate for all possible combinations of a two - digit numbers ; Check for alternate occurrence of current combination ; Increment the current value ; Increment the current value ; If alternating sequence is obtained of odd length ; Reduce to even length ; Update answer to store the maximum ; Return the answer ; Driver code
import sys NEW_LINE def findAltSubSeq ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE ans = - sys . maxsize - 1 NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT for j in range ( 10 ) : NEW_LINE INDENT cur , f = 0 , 0 NEW_LINE for k in range ( n ) : NEW_LINE INDENT if ( f == 0 and ord ( s [ k ] ) - ord ( '0' ) == i ) : NEW_LINE INDENT f = 1 NEW_LINE cur += 1 NEW_LINE DEDENT elif ( f == 1 and ord ( s [ k ] ) - ord ( '0' ) == j ) : NEW_LINE INDENT f = 0 NEW_LINE cur += 1 NEW_LINE DEDENT DEDENT if i != j and cur % 2 == 1 : NEW_LINE INDENT cur -= 1 NEW_LINE DEDENT ans = max ( cur , ans ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT s = "100210601" NEW_LINE print ( findAltSubSeq ( s ) ) NEW_LINE
Rotate matrix by 45 degrees | Function to rotate matrix by 45 degree ; Counter Variable ; Iterate [ 0 , m ] ; Iterate [ 0 , n ] ; Diagonal Elements Condition ; Appending the Diagonal Elements ; Printing reversed Diagonal Elements ; Dimensions of Matrix ; Given matrix ; Function Call
def matrix ( n , m , li ) : NEW_LINE INDENT ctr = 0 NEW_LINE while ( ctr < 2 * n - 1 ) : NEW_LINE INDENT print ( " ▁ " * abs ( n - ctr - 1 ) , end = " " ) NEW_LINE lst = [ ] NEW_LINE for i in range ( m ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if i + j == ctr : NEW_LINE INDENT lst . append ( li [ i ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT lst . reverse ( ) NEW_LINE print ( * lst ) NEW_LINE ctr += 1 NEW_LINE DEDENT DEDENT n = 8 NEW_LINE m = n NEW_LINE li = [ [ 4 , 5 , 6 , 9 , 8 , 7 , 1 , 4 ] , [ 1 , 5 , 9 , 7 , 5 , 3 , 1 , 6 ] , [ 7 , 5 , 3 , 1 , 5 , 9 , 8 , 0 ] , [ 6 , 5 , 4 , 7 , 8 , 9 , 3 , 7 ] , [ 3 , 5 , 6 , 4 , 8 , 9 , 2 , 1 ] , [ 3 , 1 , 6 , 4 , 7 , 9 , 5 , 0 ] , [ 8 , 0 , 7 , 2 , 3 , 1 , 0 , 8 ] , [ 7 , 5 , 3 , 1 , 5 , 9 , 8 , 5 ] ] NEW_LINE matrix ( n , m , li ) NEW_LINE
Mth element after K Right Rotations of an Array | Function to return Mth element of array after k right rotations ; The array comes to original state after N rotations ; If K is greater or equal to M ; Mth element after k right rotations is ( N - K ) + ( M - 1 ) th element of the array ; Otherwise ; ( M - K - 1 ) th element of the array ; Return the result ; Driver Code
def getFirstElement ( a , N , K , M ) : NEW_LINE INDENT K %= N NEW_LINE if ( K >= M ) : NEW_LINE INDENT index = ( N - K ) + ( M - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT index = ( M - K - 1 ) NEW_LINE DEDENT result = a [ index ] NEW_LINE return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE N = len ( a ) NEW_LINE K , M = 3 , 2 NEW_LINE print ( getFirstElement ( a , N , K , M ) ) NEW_LINE DEDENT
Find the Mth element of the Array after K left rotations | Function to return Mth element of array after k left rotations ; The array comes to original state after N rotations ; Mth element after k left rotations is ( K + M - 1 ) % N th element of the original array ; Return the result ; Driver Code ; Array initialization ; Size of the array ; Given K rotation and Mth element to be found after K rotation ; Function call
def getFirstElement ( a , N , K , M ) : NEW_LINE INDENT K %= N NEW_LINE index = ( K + M - 1 ) % N NEW_LINE result = a [ index ] NEW_LINE return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 3 , 4 , 5 , 23 ] NEW_LINE N = len ( a ) NEW_LINE K = 2 NEW_LINE M = 1 NEW_LINE print ( getFirstElement ( a , N , K , M ) ) NEW_LINE DEDENT
Rotate all odd numbers right and all even numbers left in an Array of 1 to N | Function to left rotate ; Function to right rotate ; Function to rotate the array ; Driver code
def left_rotate ( arr ) : NEW_LINE INDENT last = arr [ 1 ] ; NEW_LINE for i in range ( 3 , len ( arr ) , 2 ) : NEW_LINE INDENT arr [ i - 2 ] = arr [ i ] NEW_LINE DEDENT arr [ len ( arr ) - 1 ] = last NEW_LINE DEDENT def right_rotate ( arr ) : NEW_LINE INDENT start = arr [ len ( arr ) - 2 ] NEW_LINE for i in range ( len ( arr ) - 4 , - 1 , - 2 ) : NEW_LINE INDENT arr [ i + 2 ] = arr [ i ] NEW_LINE DEDENT arr [ 0 ] = start NEW_LINE DEDENT def rotate ( arr ) : NEW_LINE INDENT left_rotate ( arr ) NEW_LINE right_rotate ( arr ) NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE rotate ( arr ) ; NEW_LINE
Maximize count of corresponding same elements in given permutations using cyclic rotations | Python3 program for the above approach ; Function to maximize the matching pairs between two permutation using left and right rotation ; Left array store distance of element from left side and right array store distance of element from right side ; Map to store index of elements ; idx1 is index of element in first permutation idx2 is index of element in second permutation ; If element if present on same index on both permutations then distance is zero ; Calculate distance from left and right side ; Calculate distance from left and right side ; Maps to store frequencies of elements present in left and right arrays ; Find maximum frequency ; Return the result ; Driver Code ; Given permutations P1 and P2 ; Function Call
from collections import defaultdict NEW_LINE def maximumMatchingPairs ( perm1 , perm2 , n ) : NEW_LINE INDENT left = [ 0 ] * n NEW_LINE right = [ 0 ] * n NEW_LINE mp1 = { } NEW_LINE mp2 = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp1 [ perm1 [ i ] ] = i NEW_LINE DEDENT for j in range ( n ) : NEW_LINE INDENT mp2 [ perm2 [ j ] ] = j NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT idx2 = mp2 [ perm1 [ i ] ] NEW_LINE idx1 = i NEW_LINE if ( idx1 == idx2 ) : NEW_LINE INDENT left [ i ] = 0 NEW_LINE right [ i ] = 0 NEW_LINE DEDENT elif ( idx1 < idx2 ) : NEW_LINE INDENT left [ i ] = ( n - ( idx2 - idx1 ) ) NEW_LINE right [ i ] = ( idx2 - idx1 ) NEW_LINE DEDENT else : NEW_LINE INDENT left [ i ] = ( idx1 - idx2 ) NEW_LINE right [ i ] = ( n - ( idx1 - idx2 ) ) NEW_LINE DEDENT DEDENT freq1 = defaultdict ( int ) NEW_LINE freq2 = defaultdict ( int ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq1 [ left [ i ] ] += 1 NEW_LINE freq2 [ right [ i ] ] += 1 NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans = max ( ans , max ( freq1 [ left [ i ] ] , freq2 [ right [ i ] ] ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT P1 = [ 5 , 4 , 3 , 2 , 1 ] NEW_LINE P2 = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE n = len ( P1 ) NEW_LINE print ( maximumMatchingPairs ( P1 , P2 , n ) ) NEW_LINE DEDENT
Count of rotations required to generate a sorted array | Function to return the count of rotations ; Find the smallest element ; Return its index ; If array is not rotated at all ; Driver Code
def countRotation ( arr , n ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] < arr [ i - 1 ] ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return 0 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr1 = [ 4 , 5 , 1 , 2 , 3 ] NEW_LINE n = len ( arr1 ) NEW_LINE print ( countRotation ( arr1 , n ) ) NEW_LINE DEDENT
Count of rotations required to generate a sorted array | Function to return the count of rotations ; If array is not rotated ; Check if current element is greater than the next element ; The next element is the smallest ; Check if current element is smaller than it 's previous  element ; Current element is the smallest ; Check if current element is greater than lower bound ; The sequence is increasing so far Search for smallest element on the right subarray ; Smallest element lies on the left subarray ; Search for the smallest element on both subarrays ; Driver code
def countRotation ( arr , low , high ) : NEW_LINE INDENT if ( low > high ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT mid = low + ( high - low ) // 2 NEW_LINE if ( mid < high and arr [ mid ] > arr [ mid + 1 ] ) : NEW_LINE INDENT return mid + 1 NEW_LINE DEDENT if ( mid > low and arr [ mid ] < arr [ mid - 1 ] ) : NEW_LINE INDENT return mid NEW_LINE DEDENT if ( arr [ mid ] > arr [ low ] ) : NEW_LINE INDENT return countRotation ( arr , mid + 1 , high ) NEW_LINE DEDENT if ( arr [ mid ] < arr [ high ] ) : NEW_LINE INDENT return countRotation ( arr , low , mid - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT rightIndex = countRotation ( arr , mid + 1 , high ) NEW_LINE leftIndex = countRotation ( arr , low , mid - 1 ) NEW_LINE if ( rightIndex == 0 ) : NEW_LINE INDENT return leftIndex NEW_LINE DEDENT return rightIndex NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 4 , 5 , 1 , 2 , 3 ] NEW_LINE N = len ( arr1 ) NEW_LINE print ( countRotation ( arr1 , 0 , N - 1 ) ) NEW_LINE DEDENT
Range sum queries for anticlockwise rotations of Array by K indices | Function to execute the queries ; Construct a new array of size 2 * N to store prefix sum of every index ; Copy elements to the new array ; Calculate the prefix sum for every index ; Set start pointer as 0 ; Query to perform anticlockwise rotation ; Query to answer range sum ; If pointing to 1 st index ; Display the sum upto start + R ; Subtract sum upto start + L - 1 from sum upto start + R ; Driver code ; Number of query ; Store all the queries
def rotatedSumQuery ( arr , n , query , Q ) : NEW_LINE INDENT prefix = [ 0 ] * ( 2 * n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT prefix [ i ] = arr [ i ] NEW_LINE prefix [ i + n ] = arr [ i ] NEW_LINE DEDENT for i in range ( 1 , 2 * n ) : NEW_LINE INDENT prefix [ i ] += prefix [ i - 1 ] ; NEW_LINE DEDENT start = 0 ; NEW_LINE for q in range ( Q ) : NEW_LINE INDENT if ( query [ q ] [ 0 ] == 1 ) : NEW_LINE INDENT k = query [ q ] [ 1 ] NEW_LINE start = ( start + k ) % n ; NEW_LINE DEDENT elif ( query [ q ] [ 0 ] == 2 ) : NEW_LINE INDENT L = query [ q ] [ 1 ] NEW_LINE R = query [ q ] [ 2 ] NEW_LINE if ( start + L == 0 ) : NEW_LINE INDENT print ( prefix [ start + R ] ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( prefix [ start + R ] - prefix [ start + L - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT DEDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; NEW_LINE Q = 5 NEW_LINE query = [ [ 2 , 1 , 3 ] , [ 1 , 3 ] , [ 2 , 0 , 3 ] , [ 1 , 4 ] , [ 2 , 3 , 5 ] ] NEW_LINE n = len ( arr ) ; NEW_LINE rotatedSumQuery ( arr , n , query , Q ) ; NEW_LINE
Check if a string can be formed from another string by at most X circular clockwise shifts | Function to check that the string s1 can be converted to s2 by clockwise circular shift of all characters of str1 atmost X times ; Check for all characters of the strings whether the difference between their ascii values is less than X or not ; If both characters are the same ; Condition to check if the difference less than 0 then find the circular shift by adding 26 to it ; If difference between their ASCII values exceeds X ; Driver Code ; Function Call
def isConversionPossible ( s1 , s2 , x ) : NEW_LINE INDENT n = len ( s1 ) NEW_LINE s1 = list ( s1 ) NEW_LINE s2 = list ( s2 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT diff = ord ( s2 [ i ] ) - ord ( s1 [ i ] ) NEW_LINE if diff == 0 : NEW_LINE INDENT continue NEW_LINE DEDENT if diff < 0 : NEW_LINE INDENT diff = diff + 26 NEW_LINE DEDENT if diff > x : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = " you " NEW_LINE s2 = " ara " NEW_LINE x = 6 NEW_LINE result = isConversionPossible ( s1 , s2 , x ) NEW_LINE if result : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Count rotations which are divisible by 10 | Function to return the count of all rotations which are divisible by 10. ; Loop to iterate through the number ; If the last digit is 0 , then increment the count ; Driver code
def countRotation ( n ) : NEW_LINE INDENT count = 0 ; NEW_LINE while n > 0 : NEW_LINE INDENT digit = n % 10 NEW_LINE if ( digit % 2 == 0 ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT n = int ( n / 10 ) NEW_LINE DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10203 ; NEW_LINE print ( countRotation ( n ) ) ; NEW_LINE DEDENT
Clockwise rotation of Linked List | Link list node ; A utility function to push a node ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; A utility function to print linked list ; Function that rotates the given linked list clockwise by k and returns the updated head pointer ; If the linked list is empty ; len is used to store length of the linked list tmp will point to the last node after this loop ; If k is greater than the size of the linked list ; Subtract from length to convert it into left rotation ; If no rotation needed then return the head node ; current will either point to kth or None after this loop ; If current is None then k is equal to the count of nodes in the list Don 't change the list in this case ; current points to the kth node ; Change next of last node to previous head ; Change head to ( k + 1 ) th node ; Change next of kth node to None ; Return the updated head pointer ; Driver code ; The constructed linked list is : 1.2 . 3.4 . 5 ; Rotate the linked list ; Print the rotated linked list
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 push ( head_ref , new_data ) : NEW_LINE INDENT new_node = Node ( new_data ) NEW_LINE new_node . data = new_data NEW_LINE new_node . next = ( head_ref ) NEW_LINE ( head_ref ) = new_node NEW_LINE return head_ref 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 print ( " NULL " ) NEW_LINE DEDENT def rightRotate ( head , k ) : NEW_LINE INDENT if ( not head ) : NEW_LINE INDENT return head NEW_LINE DEDENT tmp = head NEW_LINE len = 1 NEW_LINE while ( tmp . next != None ) : NEW_LINE INDENT tmp = tmp . next NEW_LINE len += 1 NEW_LINE DEDENT if ( k > len ) : NEW_LINE INDENT k = k % len NEW_LINE DEDENT k = len - k NEW_LINE if ( k == 0 or k == len ) : NEW_LINE INDENT return head NEW_LINE DEDENT current = head NEW_LINE cnt = 1 NEW_LINE while ( cnt < k and current != None ) : NEW_LINE INDENT current = current . next NEW_LINE cnt += 1 NEW_LINE DEDENT if ( current == None ) : NEW_LINE INDENT return head NEW_LINE DEDENT kthnode = current NEW_LINE tmp . next = head NEW_LINE head = kthnode . next NEW_LINE kthnode . next = None NEW_LINE return head NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = push ( head , 5 ) NEW_LINE head = push ( head , 4 ) NEW_LINE head = push ( head , 3 ) NEW_LINE head = push ( head , 2 ) NEW_LINE head = push ( head , 1 ) NEW_LINE k = 2 NEW_LINE updated_head = rightRotate ( head , k ) NEW_LINE printList ( updated_head ) NEW_LINE DEDENT
Check if it is possible to make array increasing or decreasing by rotating the array | Python3 implementation of the approach ; Function that returns True if the array can be made increasing or decreasing after rotating it in any direction ; If size of the array is less than 3 ; Check if the array is already decreasing ; If the array is already decreasing ; Check if the array is already increasing ; If the array is already increasing ; Find the indices of the minimum and the maximum value ; Check if we can make array increasing ; If the array is increasing upto max index and minimum element is right to maximum ; Check if array increasing again or not ; Check if we can make array decreasing ; If the array is decreasing upto min index and minimum element is left to maximum ; Check if array decreasing again or not ; If it is not possible to make the array increasing or decreasing ; Driver code
import sys NEW_LINE def isPossible ( a , n ) : NEW_LINE INDENT if ( n <= 2 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT flag = 0 ; NEW_LINE for i in range ( n - 2 ) : NEW_LINE INDENT if ( not ( a [ i ] > a [ i + 1 ] and a [ i + 1 ] > a [ i + 2 ] ) ) : NEW_LINE INDENT flag = 1 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT flag = 0 ; NEW_LINE for i in range ( n - 2 ) : NEW_LINE INDENT if ( not ( a [ i ] < a [ i + 1 ] and a [ i + 1 ] < a [ i + 2 ] ) ) : NEW_LINE INDENT flag = 1 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT val1 = sys . maxsize ; mini = - 1 ; NEW_LINE val2 = - sys . maxsize ; maxi = - 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] < val1 ) : NEW_LINE INDENT mini = i ; NEW_LINE val1 = a [ i ] ; NEW_LINE DEDENT if ( a [ i ] > val2 ) : NEW_LINE INDENT maxi = i ; NEW_LINE val2 = a [ i ] ; NEW_LINE DEDENT DEDENT flag = 1 ; NEW_LINE for i in range ( maxi ) : NEW_LINE INDENT if ( a [ i ] > a [ i + 1 ] ) : NEW_LINE INDENT flag = 0 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( flag == 1 and maxi + 1 == mini ) : NEW_LINE INDENT flag = 1 ; NEW_LINE for i in range ( mini , n - 1 ) : NEW_LINE INDENT if ( a [ i ] > a [ i + 1 ] ) : NEW_LINE INDENT flag = 0 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( flag == 1 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT flag = 1 ; NEW_LINE for i in range ( mini ) : NEW_LINE INDENT if ( a [ i ] < a [ i + 1 ] ) : NEW_LINE INDENT flag = 0 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( flag == 1 and maxi - 1 == mini ) : NEW_LINE INDENT flag = 1 ; NEW_LINE for i in range ( maxi , n - 1 ) : NEW_LINE INDENT if ( a [ i ] < a [ i + 1 ] ) : NEW_LINE INDENT flag = 0 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( flag == 1 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT return False ; NEW_LINE DEDENT a = [ 4 , 5 , 6 , 2 , 3 ] ; NEW_LINE n = len ( a ) ; NEW_LINE if ( isPossible ( a , n ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
Count number of rotated strings which have more number of vowels in the first half than second half | Function to return the count of rotated strings which have more number of vowels in the first half than the second half ; Create a new string ; Pre array to store count of all vowels ; Compute the prefix array ; To store the required answer ; Find all rotated strings ; Right and left index of the string ; x1 stores the number of vowels in the rotated string ; Left stores the number of vowels in the first half of rotated string ; Right stores the number of vowels in the second half of rotated string ; If the count of vowels in the first half is greater than the count in the second half ; Return the required answer ; Driver code
def cntRotations ( s , n ) : NEW_LINE INDENT str = s + s ; NEW_LINE pre = [ 0 ] * ( 2 * n ) ; NEW_LINE for i in range ( 2 * n ) : NEW_LINE INDENT if ( i != 0 ) : NEW_LINE INDENT pre [ i ] += pre [ i - 1 ] ; NEW_LINE DEDENT if ( str [ i ] == ' a ' or str [ i ] == ' e ' or str [ i ] == ' i ' or str [ i ] == ' o ' or str [ i ] == ' u ' ) : NEW_LINE INDENT pre [ i ] += 1 ; NEW_LINE DEDENT DEDENT ans = 0 ; NEW_LINE for i in range ( n - 1 , 2 * n - 1 , 1 ) : NEW_LINE INDENT r = i ; l = i - n ; NEW_LINE x1 = pre [ r ] ; NEW_LINE if ( l >= 0 ) : NEW_LINE INDENT x1 -= pre [ l ] ; NEW_LINE DEDENT r = ( int ) ( i - n / 2 ) ; NEW_LINE left = pre [ r ] ; NEW_LINE if ( l >= 0 ) : NEW_LINE INDENT left -= pre [ l ] ; NEW_LINE DEDENT right = x1 - left ; NEW_LINE if ( left > right ) : NEW_LINE INDENT ans += 1 ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT s = " abecidft " ; NEW_LINE n = len ( s ) ; NEW_LINE print ( cntRotations ( s , n ) ) ; NEW_LINE
Count number of rotated strings which have more number of vowels in the first half than second half | Function to return the count of rotated strings which have more number of vowels in the first half than the second half ; Compute the number of vowels in first - half ; Compute the number of vowels in second - half ; Check if first - half has more vowels ; Check for all possible rotations ; Return the answer ; Driver code ; Function call
def cntRotations ( s , n ) : NEW_LINE INDENT lh , rh , ans = 0 , 0 , 0 NEW_LINE for i in range ( n // 2 ) : NEW_LINE INDENT if ( s [ i ] == ' a ' or s [ i ] == ' e ' or s [ i ] == ' i ' or s [ i ] == ' o ' or s [ i ] == ' u ' ) : NEW_LINE INDENT lh += 1 NEW_LINE DEDENT DEDENT for i in range ( n // 2 , n ) : NEW_LINE INDENT if ( s [ i ] == ' a ' or s [ i ] == ' e ' or s [ i ] == ' i ' or s [ i ] == ' o ' or s [ i ] == ' u ' ) : NEW_LINE INDENT rh += 1 NEW_LINE DEDENT DEDENT if ( lh > rh ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT if ( s [ i - 1 ] == ' a ' or s [ i - 1 ] == ' e ' or s [ i - 1 ] == ' i ' or s [ i - 1 ] == ' o ' or s [ i - 1 ] == ' u ' ) : NEW_LINE INDENT rh += 1 NEW_LINE lh -= 1 NEW_LINE DEDENT if ( s [ ( i - 1 + n // 2 ) % n ] == ' a ' or s [ ( i - 1 + n // 2 ) % n ] == ' e ' or s [ ( i - 1 + n // 2 ) % n ] == ' i ' or s [ ( i - 1 + n // 2 ) % n ] == ' o ' or s [ ( i - 1 + n // 2 ) % n ] == ' u ' ) : NEW_LINE INDENT rh -= 1 NEW_LINE lh += 1 NEW_LINE DEDENT if ( lh > rh ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " abecidft " NEW_LINE n = len ( s ) NEW_LINE print ( cntRotations ( s , n ) ) NEW_LINE DEDENT
Queries for rotation and Kth character of the given string in constant time | Python3 implementation of the approach ; Function to perform the required queries on the given string ; Pointer pointing to the current starting character of the string ; For every query ; If the query is to rotate the string ; Update the pointer pointing to the starting character of the string ; Index of the kth character in the current rotation of the string ; Print the kth character ; Driver code
size = 2 NEW_LINE def performQueries ( string , n , queries , q ) : NEW_LINE INDENT ptr = 0 ; NEW_LINE for i in range ( q ) : NEW_LINE INDENT if ( queries [ i ] [ 0 ] == 1 ) : NEW_LINE INDENT ptr = ( ptr + queries [ i ] [ 1 ] ) % n ; NEW_LINE DEDENT else : NEW_LINE INDENT k = queries [ i ] [ 1 ] ; NEW_LINE index = ( ptr + k - 1 ) % n ; NEW_LINE print ( string [ index ] ) ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " abcdefgh " ; NEW_LINE n = len ( string ) ; NEW_LINE queries = [ [ 1 , 2 ] , [ 2 , 2 ] , [ 1 , 4 ] , [ 2 , 7 ] ] ; NEW_LINE q = len ( queries ) ; NEW_LINE performQueries ( string , n , queries , q ) ; NEW_LINE DEDENT