codeparrot/xlcost-text-to-code huggingface dataset:
Smallest occurring element in each subsequence | Function that count the subsequence such that each element as the minimum element in the subsequence ; Store index in a map ; Sort the array ; To store count of subsequence ; Traverse the array ; Store count of subsequence ; Print the count of subsequence ; Driver code ; Function call
def solve ( arr , N ) : NEW_LINE INDENT M = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT M [ i ] = arr [ i ] NEW_LINE DEDENT arr . sort ( ) NEW_LINE Count = { } NEW_LINE for i in range ( N ) : NEW_LINE INDENT Count [ arr [ i ] ] = pow ( 2 , N - i - 1 ) NEW_LINE DEDENT for it in Count . values ( ) : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 2 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE solve ( arr , N ) NEW_LINE DEDENT
Program to insert dashes between two adjacent odd digits in given Number | Function to check if char ch is odd or not ; Function to insert dash - between any 2 consecutive digit in string str ; Traverse the string character by character ; Compare every consecutive character with the odd value ; Print the resultant string ; Given number in form of string ; Function call
def checkOdd ( ch ) : NEW_LINE INDENT return ( ( ord ( ch ) - 48 ) & 1 ) NEW_LINE DEDENT def Insert_dash ( num_str ) : NEW_LINE INDENT result_str = num_str NEW_LINE x = 0 NEW_LINE while ( x < len ( num_str ) - 1 ) : NEW_LINE INDENT if ( checkOdd ( num_str [ x ] ) and checkOdd ( num_str [ x + 1 ] ) ) : NEW_LINE INDENT result_str = ( result_str [ : x + 1 ] + ' - ' + result_str [ x + 1 : ] ) NEW_LINE num_str = result_str NEW_LINE x += 1 NEW_LINE DEDENT x += 1 NEW_LINE DEDENT return result_str NEW_LINE DEDENT str = "1745389" NEW_LINE print ( Insert_dash ( str ) ) NEW_LINE
Check if a Matrix is Reverse Bitonic or Not | Python3 program to check if a matrix is Reverse Bitonic or not ; Function to check if an array is Reverse Bitonic or not ; Check for decreasing sequence ; Check for increasing sequence ; Function to check whether given matrix is bitonic or not ; Check row - wise ; Check column wise ; Generate an array consisting of elements of the current column ; Driver Code
N = 3 NEW_LINE M = 3 NEW_LINE def checkReverseBitonic ( arr , n ) : NEW_LINE INDENT f = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] < arr [ i - 1 ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( arr [ i ] == arr [ i - 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT f = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if ( i == n ) : NEW_LINE INDENT return True NEW_LINE DEDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ j ] > arr [ j - 1 ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( arr [ i ] == arr [ i - 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT if ( f == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT def check ( arr ) : NEW_LINE INDENT f = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( not checkReverseBitonic ( arr [ i ] , M ) ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT temp = [ 0 ] * N NEW_LINE for j in range ( N ) : NEW_LINE INDENT temp [ j ] = arr [ j ] [ i ] NEW_LINE DEDENT if ( not checkReverseBitonic ( temp , N ) ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " Yes " ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT m = [ [ 2 , 3 , 4 ] , [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] NEW_LINE check ( m ) NEW_LINE DEDENT
Find the element at the specified index of a Spirally Filled Matrix | Function to return the element at ( x , y ) ; If y is greater ; If y is odd ; If y is even ; If x is even ; If x is odd ; Driver code
def SpiralElement ( x , y ) : NEW_LINE INDENT r = 0 NEW_LINE if ( x < y ) : NEW_LINE INDENT if ( y % 2 == 1 ) : NEW_LINE INDENT r = y * y NEW_LINE return ( r - x + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT r = ( y - 1 ) * ( y - 1 ) NEW_LINE return ( r + x ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( x % 2 == 0 ) : NEW_LINE INDENT r = x * x NEW_LINE return ( r - y + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT r = ( x - 1 ) * ( x - 1 ) NEW_LINE return ( r + y ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 2 NEW_LINE y = 3 NEW_LINE print ( SpiralElement ( x , y ) ) NEW_LINE DEDENT
Make the string in AP by changing a character | Function to modify the given string and find the index where modification is needed ; Array to store the ASCII values of alphabets ; Loop to compute the ASCII values of characters a - z ; Set to store all the possible differences between consecutive elements ; Loop to find out the differences between consecutive elements and storing them in the set ; Checks if any character of the string disobeys the pattern ; Constructing the strings with all possible values of consecutive difference and comparing them with staring string S . ; Driver code
def string_modify ( s ) : NEW_LINE INDENT alphabets = [ ] NEW_LINE flag , hold_i = 0 , 0 NEW_LINE hold_l = s [ 0 ] NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT alphabets . append ( chr ( i + ord ( ' a ' ) ) ) NEW_LINE DEDENT difference = set ( ) NEW_LINE reconstruct = " " NEW_LINE for i in range ( 1 , len ( s ) ) : NEW_LINE INDENT difference . add ( ord ( s [ i ] ) - ord ( s [ i - 1 ] ) ) NEW_LINE DEDENT if ( len ( difference ) == 1 ) : NEW_LINE INDENT print ( " No ▁ modifications ▁ required " ) NEW_LINE return NEW_LINE DEDENT for it in difference : NEW_LINE INDENT index = ord ( s [ 0 ] ) - ord ( ' a ' ) NEW_LINE reconstruct = " " NEW_LINE flag = 0 NEW_LINE i = 0 NEW_LINE while ( ( i < len ( s ) ) and ( flag <= 1 ) ) : NEW_LINE INDENT reconstruct += alphabets [ index ] NEW_LINE index += it NEW_LINE if ( index < 0 ) : NEW_LINE INDENT index += 26 NEW_LINE DEDENT index %= 26 NEW_LINE if ( reconstruct [ i ] != s [ i ] ) : NEW_LINE INDENT flag += 1 NEW_LINE hold_i = i NEW_LINE hold_l = s [ i ] NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if ( flag == 1 ) : NEW_LINE INDENT s [ hold_i ] = reconstruct [ hold_i ] NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag > 1 ) : NEW_LINE INDENT hold_i = 0 NEW_LINE hold_l = s [ 0 ] NEW_LINE temp = ( ord ( s [ 1 ] ) - ord ( ' a ' ) - ( ord ( s [ 2 ] ) - ord ( s [ 1 ] ) ) ) % 26 NEW_LINE if ( temp < 0 ) : NEW_LINE INDENT temp += 26 NEW_LINE DEDENT s [ 0 ] = alphabets [ temp ] NEW_LINE DEDENT print ( hold_i , " - > " , hold_l ) NEW_LINE print ( " " . join ( s ) ) NEW_LINE DEDENT s = list ( " aeimqux " ) NEW_LINE string_modify ( s ) NEW_LINE
Create matrix whose sum of diagonals in each sub matrix is even | Function to prN * N order matrix with all sub - matrix of even order is sum of its diagonal also even ; Even index ; Odd index ; Iterate two nested loop ; For even index the element should be consecutive odd ; For odd index the element should be consecutive even ; Given order of matrix ; Function call
def evenSubMatrix ( N ) : NEW_LINE INDENT even = 1 NEW_LINE odd = 2 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT if ( ( i + j ) % 2 == 0 ) : NEW_LINE INDENT print ( even , end = " ▁ " ) NEW_LINE even += 2 NEW_LINE DEDENT else : NEW_LINE INDENT print ( odd , end = " ▁ " ) NEW_LINE odd += 2 NEW_LINE DEDENT DEDENT print ( ) NEW_LINE DEDENT DEDENT N = 4 NEW_LINE evenSubMatrix ( N ) NEW_LINE
Remove leading zeros from a Number given as a string | Python3 Program to implement the above approach ; Function to remove all leading zeros from a a given string ; Regex to remove leading zeros from a string ; Replaces the matched value with given string ; Driver Code
import re NEW_LINE def removeLeadingZeros ( str ) : NEW_LINE INDENT regex = " ^ 0 + ( ? ! $ ) " NEW_LINE str = re . sub ( regex , " " , str ) NEW_LINE print ( str ) NEW_LINE DEDENT str = "0001234" NEW_LINE removeLeadingZeros ( str ) NEW_LINE
Maximum inversions in a sequence of 1 to N after performing given operations at most K times | Function which computes the maximum number of inversions ; ' answer ' will store the required number of inversions ; We do this because we will never require more than floor ( n / 2 ) operations ; left pointer in the array ; right pointer in the array ; Doing k operations ; Incrementing ans by number of inversions increase due to this swapping ; Driver Code ; Input 1 ; Input 2
def maximum_inversion ( n , k ) : NEW_LINE INDENT answer = 0 ; NEW_LINE k = min ( k , n // 2 ) ; NEW_LINE left = 1 ; NEW_LINE right = n ; NEW_LINE while ( k > 0 ) : NEW_LINE INDENT k -= 1 ; NEW_LINE answer += 2 * ( right - left ) - 1 ; NEW_LINE left += 1 ; NEW_LINE right -= 1 ; NEW_LINE DEDENT print ( answer ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 ; NEW_LINE K = 3 ; NEW_LINE maximum_inversion ( N , K ) ; NEW_LINE N = 4 ; NEW_LINE K = 1 ; NEW_LINE maximum_inversion ( N , K ) ; NEW_LINE DEDENT
First number to leave an odd remainder after repetitive division by 2 | Python3 program to implement the above approach ; Function to return the position least significant set bit ; Function return the first number to be converted to an odd integer ; Stores the positions of the first set bit ; If both are same ; If A has the least significant set bit ; Otherwise ; Driver code
from math import log NEW_LINE def getFirstSetBitPos ( n ) : NEW_LINE INDENT return log ( n & - n , 2 ) + 1 NEW_LINE DEDENT def oddFirst ( a , b ) : NEW_LINE INDENT steps_a = getFirstSetBitPos ( a ) NEW_LINE steps_b = getFirstSetBitPos ( b ) NEW_LINE if ( steps_a == steps_b ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if ( steps_a > steps_b ) : NEW_LINE INDENT return b NEW_LINE DEDENT if ( steps_a < steps_b ) : NEW_LINE INDENT return a NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 10 NEW_LINE b = 8 NEW_LINE print ( oddFirst ( a , b ) ) NEW_LINE DEDENT
Check if a string can be split into substrings starting with N followed by N characters | Function to check if the given can be split into desired substrings ; Length of the string ; Traverse the string ; Extract the digit ; Check if the extracted number does not exceed the remaining length ; Check for the remaining string ; If generating desired substrings is not possible ; Driver Code
def helper ( s , pos ) : NEW_LINE INDENT size = len ( s ) NEW_LINE if ( pos >= size ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( s [ pos ] . isdigit ( ) == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT num = 0 NEW_LINE for i in range ( pos , size ) : NEW_LINE INDENT num = num * 10 + ord ( s [ pos ] ) - 48 NEW_LINE if ( i + 1 + num > size ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( helper ( s , i + 1 + num ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT s = "123abc4db1c " ; NEW_LINE if ( helper ( s , 0 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Represent K as sum of N | Array to store the N - Bonacci series ; Function to express K as sum of several N_bonacci numbers ; Driver code
N_bonacci = [ 0 ] * 100 NEW_LINE def N_bonacci_nums ( n , k ) : NEW_LINE INDENT N_bonacci [ 0 ] = 1 NEW_LINE for i in range ( 1 , 51 ) : NEW_LINE INDENT j = i - 1 NEW_LINE while j >= i - k and j >= 0 : NEW_LINE INDENT N_bonacci [ i ] += N_bonacci [ j ] NEW_LINE j -= 1 NEW_LINE DEDENT DEDENT ans = [ ] NEW_LINE for i in range ( 50 , - 1 , - 1 ) : NEW_LINE INDENT if ( n - N_bonacci [ i ] >= 0 ) : NEW_LINE INDENT ans . append ( N_bonacci [ i ] ) NEW_LINE n -= N_bonacci [ i ] NEW_LINE DEDENT DEDENT if ( len ( ans ) == 1 ) : NEW_LINE INDENT ans . append ( 0 ) NEW_LINE DEDENT print ( len ( ans ) ) NEW_LINE for i in ans : NEW_LINE INDENT print ( i , end = " , ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 21 NEW_LINE k = 5 NEW_LINE N_bonacci_nums ( n , k ) NEW_LINE DEDENT
Divide N into K parts in the form ( X , 2 X , ... , KX ) for some value of X | Function to find the division ; Calculating value of x1 ; Print - 1 if division is not possible ; Get the first number ie x1 then successively multiply it by x1 k times by index number to get the required answer ; Given N and K ; Function Call
def solve ( n , k ) : NEW_LINE INDENT d = k * ( k + 1 ) ; NEW_LINE if ( ( 2 * n ) % d != 0 ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE return ; NEW_LINE DEDENT x1 = 2 * n // d ; NEW_LINE for i in range ( 1 , k + 1 ) : NEW_LINE INDENT print ( x1 * i , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT n = 10 ; k = 4 ; NEW_LINE solve ( n , k ) ; NEW_LINE
Bitonic string | Function to check if the given string is bitonic ; Check for increasing sequence ; If end of string has been reached ; Check for decreasing sequence ; If the end of string hasn 't  been reached ; Return true if bitonic ; Given string ; Function Call
def checkBitonic ( s ) : NEW_LINE INDENT i = 0 NEW_LINE j = 0 NEW_LINE for i in range ( 1 , len ( s ) ) : NEW_LINE INDENT if ( s [ i ] > s [ i - 1 ] ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT if ( s [ i ] <= s [ i - 1 ] ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT if ( i == ( len ( s ) - 1 ) ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT for j in range ( i + 1 , len ( s ) ) : NEW_LINE INDENT if ( s [ j ] < s [ j - 1 ] ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT if ( s [ j ] >= s [ j - 1 ] ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT i = j ; NEW_LINE if ( i != len ( s ) - 1 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT return True ; NEW_LINE DEDENT s = " abcdfgcba " NEW_LINE if ( checkBitonic ( s ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Find pair with maximum GCD for integers in range 2 to N | Function to find the required pair whose GCD is maximum ; If N is even ; If N is odd ; Driver Code
def solve ( N ) : NEW_LINE INDENT if ( N % 2 == 0 ) : NEW_LINE INDENT print ( N // 2 , N ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ( N - 1 ) // 2 , ( N - 1 ) ) NEW_LINE DEDENT DEDENT N = 10 NEW_LINE solve ( N ) NEW_LINE
Find initial sequence that produces a given Array by cyclic increments upto index P | Function to generate and return the required initial arrangement ; Store the minimum element in the array ; Store the number of increments ; Subtract mi - 1 from every index ; Start from the last index which had been incremented ; Stores the index chosen to distribute its element ; Traverse the array cyclically and find the index whose element was distributed ; If any index has its value reduced to 0 ; Index whose element was distributed ; Store the number of increments at the starting index ; Print the original array ; Driver Code
def findArray ( a , n , P ) : NEW_LINE INDENT mi = min ( a ) NEW_LINE ctr = 0 NEW_LINE mi = max ( 0 , mi - 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT a [ i ] -= mi NEW_LINE ctr += mi NEW_LINE DEDENT i = P - 1 NEW_LINE start = - 1 NEW_LINE while ( 1 ) : NEW_LINE INDENT if ( a [ i ] == 0 ) : NEW_LINE INDENT start = i NEW_LINE break NEW_LINE DEDENT a [ i ] -= 1 NEW_LINE ctr += 1 NEW_LINE i = ( i - 1 + n ) % n NEW_LINE DEDENT a [ start ] = ctr NEW_LINE print ( * a , sep = ' , ▁ ' ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE P = 2 NEW_LINE arr = [ 3 , 2 , 0 , 2 , 7 ] NEW_LINE findArray ( arr , N , P ) NEW_LINE DEDENT
Generate a unique Array of length N with sum of all subarrays divisible by N | Function to print the required array ; Print Array ; Driver code
def makeArray ( n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( ( i + 1 ) * n , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 6 ; NEW_LINE makeArray ( n ) ; NEW_LINE
Find elements in a given range having at least one odd divisor | Function to prints all numbers with at least one odd divisor ; Check if the number is not a power of two ; Driver Code
def printOddFactorNumber ( n , m ) : NEW_LINE INDENT for i in range ( n , m + 1 ) : NEW_LINE INDENT if ( ( i > 0 ) and ( ( i & ( i - 1 ) ) != 0 ) ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT N = 2 NEW_LINE M = 10 NEW_LINE printOddFactorNumber ( N , M ) NEW_LINE
Print the first N terms of the series 6 , 28 , 66 , 120 , 190 , 276 , ... | Function to print the series ; Initialise the value of k with 2 ; Iterate from 1 to n ; Print each number ; Increment the value of K by 2 for next number ; Given number ; Function Call
def PrintSeries ( n ) : NEW_LINE INDENT k = 2 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT print ( k * ( 2 * k - 1 ) , end = ' ▁ ' ) NEW_LINE k = k + 2 NEW_LINE DEDENT DEDENT n = 12 NEW_LINE PrintSeries ( n ) NEW_LINE
Find a string which matches all the patterns in the given array | Function to find a common string which matches all the pattern ; For storing prefix till first most * without conflicts ; For storing suffix till last most * without conflicts ; For storing all middle characters between first and last * ; Loop to iterate over every pattern of the array ; Index of the first " * " ; Index of Last " * " ; Iterate over the first " * " ; Prefix till first most * without conflicts ; Iterate till last most * from last ; Make suffix till last most * without conflicts ; Take all middle characters in between first and last most * ; Driver Code ; Take all the strings ; Method for finding common string
def find ( S , N ) : NEW_LINE INDENT pref = " " NEW_LINE suff = " " NEW_LINE mid = " " NEW_LINE for i in range ( N ) : NEW_LINE INDENT first = int ( S [ i ] . index ( " * " ) ) NEW_LINE last = int ( S [ i ] . rindex ( " * " ) ) NEW_LINE for z in range ( len ( pref ) ) : NEW_LINE INDENT if ( z < first ) : NEW_LINE INDENT if ( pref [ z ] != S [ i ] [ z ] ) : NEW_LINE INDENT return " * " NEW_LINE DEDENT DEDENT DEDENT for z in range ( len ( pref ) , first ) : NEW_LINE INDENT pref += S [ i ] [ z ] ; NEW_LINE DEDENT for z in range ( len ( suff ) ) : NEW_LINE INDENT if ( len ( S [ i ] ) - 1 - z > last ) : NEW_LINE INDENT if ( suff [ z ] != S [ i ] [ len ( S [ i ] ) - 1 - z ] ) : NEW_LINE INDENT return " * " NEW_LINE DEDENT DEDENT DEDENT for z in range ( len ( suff ) , len ( S [ i ] ) - 1 - last ) : NEW_LINE INDENT suff += S [ i ] [ len ( S [ i ] ) - 1 - z ] NEW_LINE DEDENT for z in range ( first , last + 1 ) : NEW_LINE INDENT if ( S [ i ] [ z ] != ' * ' ) : NEW_LINE INDENT mid += S [ i ] [ z ] NEW_LINE DEDENT DEDENT DEDENT suff = suff [ : : - 1 ] NEW_LINE return pref + mid + suff NEW_LINE DEDENT N = 3 NEW_LINE s = [ " " for i in range ( N ) ] NEW_LINE s [ 0 ] = " pq * du * q " NEW_LINE s [ 1 ] = " pq * abc * q " NEW_LINE s [ 2 ] = " p * d * q " NEW_LINE print ( find ( s , N ) ) NEW_LINE
Mountain Sequence Pattern | Python3 program for the above approach ; Function to print pattern recursively ; Base Case ; Condition to check row limit ; Condition for assigning gaps ; Conditions to print * ; Else print '  ; Recursive call for columns ; Recursive call for rows ; Given Number N ; Function Call
k1 = 2 NEW_LINE k2 = 2 NEW_LINE gap = 5 NEW_LINE def printPattern ( i , j , n ) : NEW_LINE INDENT global k1 NEW_LINE global k2 NEW_LINE global gap NEW_LINE if ( j >= n ) : NEW_LINE INDENT k1 = 2 NEW_LINE k2 = 2 NEW_LINE k1 -= 1 NEW_LINE k2 += 1 NEW_LINE if ( i == 2 ) : NEW_LINE INDENT k1 = 0 NEW_LINE k2 = n - 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT if ( i >= 3 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( j > k2 ) : NEW_LINE INDENT k1 += gap NEW_LINE k2 += gap NEW_LINE DEDENT if ( j >= k1 and j <= k2 or i == 2 ) : NEW_LINE INDENT print ( " * " , end = " " ) NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT else : NEW_LINE INDENT print ( " ▁ " , end = " " ) NEW_LINE DEDENT if ( printPattern ( i , j + 1 , n ) == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT print ( ) NEW_LINE return ( printPattern ( i + 1 , 0 , n ) ) NEW_LINE DEDENT N = 3 NEW_LINE printPattern ( 0 , 0 , N * 5 ) NEW_LINE
Build a DFA to accept Binary strings that starts or ends with "01" | Function for transition state A ; State transition to B if the character is 0 ; State transition to D if the character is 1 ; Function for transition state B ; Check if the string has ended ; State transition to C if the character is 1 ; State transition to D if the character is 0 ; Function for transition state C ; Function for transition state D ; State transition to D if the character is 1 ; State transition to E if the character is 0 ; Function for transition state E ; State transition to E if the character is 0 ; State transition to F if the character is 1 ; Function for transition state F ; State transition to D if the character is 1 ; State transition to E if the character is 0 ; Driver code
def checkstateA ( n ) : NEW_LINE INDENT if ( n [ 0 ] == '0' ) : NEW_LINE INDENT stateB ( n [ 1 : ] ) NEW_LINE DEDENT else : NEW_LINE INDENT stateD ( n [ 1 : ] ) NEW_LINE DEDENT DEDENT def stateB ( n ) : NEW_LINE INDENT if ( len ( n ) == 0 ) : NEW_LINE INDENT print ( " string ▁ not ▁ accepted " ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( n [ 0 ] == '1' ) : NEW_LINE INDENT stateC ( n [ 1 : ] ) NEW_LINE DEDENT else : NEW_LINE INDENT stateD ( n [ 1 : ] ) NEW_LINE DEDENT DEDENT DEDENT def stateC ( n ) : NEW_LINE INDENT print ( " String ▁ accepted " ) NEW_LINE DEDENT def stateD ( n ) : NEW_LINE INDENT if ( len ( n ) == 0 ) : NEW_LINE INDENT print ( " string ▁ not ▁ accepted " ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( n [ 0 ] == '1' ) : NEW_LINE INDENT stateD ( n [ 1 : ] ) NEW_LINE DEDENT else : NEW_LINE INDENT stateE ( n [ 1 : ] ) NEW_LINE DEDENT DEDENT DEDENT def stateE ( n ) : NEW_LINE INDENT if ( len ( n ) == 0 ) : NEW_LINE INDENT print ( " string ▁ not ▁ accepted " ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( n [ 0 ] == '0' ) : NEW_LINE INDENT stateE ( n [ 1 : ] ) NEW_LINE DEDENT else : NEW_LINE INDENT stateF ( n [ 1 : ] ) NEW_LINE DEDENT DEDENT DEDENT def stateF ( n ) : NEW_LINE INDENT if ( len ( n ) == 0 ) : NEW_LINE INDENT print ( " string ▁ accepred " ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( n [ 0 ] == '1' ) : NEW_LINE INDENT stateD ( n [ 1 : ] ) NEW_LINE DEDENT else : NEW_LINE INDENT stateE ( n [ 1 : ] ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = "0100101" NEW_LINE checkstateA ( n ) NEW_LINE DEDENT
Find the Nth Hogben Numbers | Function returns N - th Hogben Number ; Driver code
def HogbenNumber ( a ) : NEW_LINE INDENT p = ( pow ( a , 2 ) - a + 1 ) NEW_LINE return p NEW_LINE DEDENT N = 10 NEW_LINE print ( HogbenNumber ( N ) ) NEW_LINE
Check if left and right shift of any string results into given string | Function to check string exist or not as per above approach ; Check if any character at position i and i + 2 are not equal , then string doesnot exist ; Driver Code
def check_string_exist ( S ) : NEW_LINE INDENT size = len ( S ) NEW_LINE check = True NEW_LINE for i in range ( size ) : NEW_LINE INDENT if S [ i ] != S [ ( i + 2 ) % size ] : NEW_LINE INDENT check = False NEW_LINE break NEW_LINE DEDENT DEDENT if check : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT S = " papa " NEW_LINE check_string_exist ( S ) NEW_LINE
Cumulative product of digits of all numbers in the given range | Function to get product of digits ; Function to find the product of digits of all natural numbers in range L to R ; Iterate between L to R ; Driver Code
def getProduct ( n ) : NEW_LINE INDENT product = 1 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT product = product * ( n % 10 ) NEW_LINE n = int ( n / 10 ) NEW_LINE DEDENT return product NEW_LINE DEDENT def productinRange ( l , r ) : NEW_LINE INDENT if ( r - l > 9 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT p = 1 NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT p = p * getProduct ( i ) NEW_LINE DEDENT return p NEW_LINE DEDENT DEDENT l = 11 NEW_LINE r = 15 NEW_LINE print ( productinRange ( l , r ) , end = ' ' ) NEW_LINE l = 1 NEW_LINE r = 15 NEW_LINE print ( productinRange ( l , r ) ) NEW_LINE