codeparrot/xlcost-text-to-code huggingface dataset:
Construct an array whose Prefix XOR array starting from X is an N | Function to print the required array ; Iteratie from 1 to N ; Print the i - th element ; Update prev_xor to i ; Driver Code ; Given Input ; Function Call
def GenerateArray ( N , X ) : NEW_LINE INDENT prev_xor = X NEW_LINE for i in range ( 1 , N + 1 , 1 ) : NEW_LINE INDENT print ( i ^ prev_xor , end = " " ) NEW_LINE if ( i != N ) : NEW_LINE INDENT print ( " ▁ " , end = " " ) NEW_LINE DEDENT prev_xor = i NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE X = 3 NEW_LINE print ( " The ▁ generated ▁ array ▁ is ▁ " , end = " " ) NEW_LINE GenerateArray ( N , X ) NEW_LINE DEDENT
Check if elements of a Binary Matrix can be made alternating | Function to create the possible grids ; Function to test if any one of them matches with the given 2 - D array ; Function to print the grid , if possible ; Function to check if the grid can be made alternating or not ; Grids to store the possible grids ; Driver Code
def createGrid ( grid , is1 , N , M ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT if ( is1 ) : NEW_LINE INDENT grid [ i ] [ j ] = '0' NEW_LINE is1 = False NEW_LINE DEDENT else : NEW_LINE INDENT grid [ i ] [ j ] = '1' NEW_LINE is1 = True NEW_LINE DEDENT DEDENT if ( M % 2 == 0 ) : NEW_LINE INDENT is1 = True if is1 == False else False NEW_LINE DEDENT DEDENT DEDENT def testGrid ( testGrid , Grid , N , M ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT if ( Grid [ i ] [ j ] != ' * ' ) : NEW_LINE INDENT if ( Grid [ i ] [ j ] != testGrid [ i ] [ j ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT DEDENT return True NEW_LINE DEDENT def printGrid ( grid , N , M ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT print ( grid [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( " " , end ▁ = ▁ " " ) NEW_LINE DEDENT DEDENT def findPossibleGrid ( N , M , grid ) : NEW_LINE INDENT gridTest1 = [ [ ' ' for i in range ( 1001 ) ] for j in range ( N ) ] NEW_LINE gridTest2 = [ [ ' ' for i in range ( 1001 ) ] for j in range ( N ) ] NEW_LINE createGrid ( gridTest1 , True , N , M ) NEW_LINE createGrid ( gridTest2 , False , N , M ) NEW_LINE if ( testGrid ( gridTest1 , grid , N , M ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE printGrid ( gridTest1 , N , M ) NEW_LINE DEDENT elif ( testGrid ( gridTest2 , grid , N , M ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE printGrid ( gridTest2 , N , M ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE M = 4 NEW_LINE grid = [ [ ' * ' , ' * ' , '1' , '0' ] , [ ' * ' , ' * ' , ' * ' , ' * ' ] , [ ' * ' , ' * ' , ' * ' , ' * ' ] , [ ' * ' , ' * ' , '0' , '1' ] ] NEW_LINE findPossibleGrid ( N , M , grid ) NEW_LINE DEDENT
Find non | Python 3 program for the above approach . ; Function to find the possible output array ; Base case for the recursion ; If ind becomes half of the size then print the array . ; Exit the function . ; Iterate in the range . ; Put the values in the respective indices . ; Call the function to find values for other indices . ; Driver Code
N = 200 * 1000 + 13 NEW_LINE n = 0 NEW_LINE arr = [ 0 for i in range ( N ) ] NEW_LINE brr = [ 0 for i in range ( N ) ] NEW_LINE import sys NEW_LINE def brute ( ind , l , r ) : NEW_LINE INDENT if ( ind == n / 2 ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( brr [ i ] , end = " ▁ " ) NEW_LINE DEDENT sys . exit ( ) NEW_LINE DEDENT for i in range ( l , arr [ ind ] // 2 + 1 , 1 ) : NEW_LINE INDENT if ( arr [ ind ] - i <= r ) : NEW_LINE INDENT brr [ ind ] = i NEW_LINE brr [ n - ind - 1 ] = arr [ ind ] - i NEW_LINE brute ( ind + 1 , i , arr [ ind ] - i ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 2 NEW_LINE n *= 2 NEW_LINE arr [ 0 ] = 5 NEW_LINE arr [ 1 ] = 6 NEW_LINE INF64 = 1000000000000000000 NEW_LINE brute ( 0 , 0 , INF64 ) NEW_LINE DEDENT
Count of minimum numbers having K as the last digit required to obtain sum N | Python3 program for the above approach ; Stores the smallest number that ends with digit i ( 0 , 9 ) ; Stores the minimum number of steps to create a number ending with digit i ; Initialize elements as infinity ; Minimum number ending with digit i ; Minimum steps to create a number ending with digit i ; If N < SmallestNumber then , return - 1 ; Otherwise , return answer ; Driver Code
import sys NEW_LINE def minCount ( N , K ) : NEW_LINE INDENT SmallestNumber = [ 0 for i in range ( 10 ) ] NEW_LINE MinimumSteps = [ 0 for i in range ( 10 ) ] NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT SmallestNumber [ i ] = sys . maxsize ; NEW_LINE MinimumSteps [ i ] = sys . maxsize NEW_LINE DEDENT for i in range ( 1 , 11 , 1 ) : NEW_LINE INDENT num = K * i NEW_LINE SmallestNumber [ num % 10 ] = min ( SmallestNumber [ num % 10 ] , num ) NEW_LINE MinimumSteps [ num % 10 ] = min ( MinimumSteps [ num % 10 ] , i ) NEW_LINE DEDENT if ( N < SmallestNumber [ N % 10 ] ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return MinimumSteps [ N % 10 ] NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 42 NEW_LINE K = 7 NEW_LINE print ( minCount ( N , K ) ) NEW_LINE DEDENT
Minimum number of operations required to make an array non | Function to count the minimum number of steps required to make arr non - decreasing ; Stores differences ; Stores the max number ; Traverse the array arr [ ] ; Update mx ; Update val ; Stores the result ; Iterate until 2 ^ res - 1 is less than val ; Return the answer ; Driver Code ; Given input ; Function call
def countMinSteps ( arr , N ) : NEW_LINE INDENT val = 0 NEW_LINE mx = - 10 ** 9 NEW_LINE for i in range ( N ) : NEW_LINE INDENT curr = arr [ i ] NEW_LINE mx = max ( mx , curr ) NEW_LINE val = max ( val , mx - curr ) NEW_LINE DEDENT res = 0 NEW_LINE while ( ( 1 << res ) - 1 < val ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 7 , 6 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countMinSteps ( arr , N ) ) NEW_LINE DEDENT
Minimum XOR of at most K elements in range [ L , R ] | Function for K = 2 ; Function for K = 2 ; Function for K = 2 ; Function to calculate the minimum XOR of at most K elements in [ L , R ] ; Driver code ; Input ; Function call
def func2 ( L , R , K ) : NEW_LINE INDENT if ( R - L >= 2 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return min ( L , L ^ R ) NEW_LINE DEDENT def func3 ( L , R , K ) : NEW_LINE INDENT if ( ( R ^ L ) > L and ( R ^ L ) < R ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return func2 ( L , R , K ) NEW_LINE DEDENT def func4 ( L , R , K ) : NEW_LINE INDENT if ( R - L >= 4 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT minval = L ^ ( L + 1 ) ^ ( L + 2 ) ^ ( L + 3 ) NEW_LINE return min ( minval , func3 ( L , R , K ) ) NEW_LINE DEDENT def minimumXor ( L , R , K ) : NEW_LINE INDENT if ( K > 4 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif ( K == 4 ) : NEW_LINE INDENT return func4 ( L , R , K ) NEW_LINE DEDENT elif ( K == 3 ) : NEW_LINE INDENT return func3 ( L , R , K ) NEW_LINE DEDENT elif ( K == 2 ) : NEW_LINE INDENT return func2 ( L , R , K ) NEW_LINE DEDENT else : NEW_LINE INDENT return L NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L , R , K = 1 , 3 , 3 NEW_LINE print ( minimumXor ( L , R , K ) ) NEW_LINE DEDENT
Nth term of Ruler Function Series | Function to count the number of set bits in the number N ; Store the number of setbits ; Update the value of n ; Update the count ; Return the total count ; Function to find the Nth term of the Ruler Function Series ; Store the result ; Print the result ; Driver Code
def setBits ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT n = n & ( n - 1 ) NEW_LINE count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def findNthTerm ( N ) : NEW_LINE INDENT x = setBits ( N ^ ( N - 1 ) ) NEW_LINE print ( x ) NEW_LINE DEDENT N = 8 NEW_LINE findNthTerm ( N ) NEW_LINE
Quadratic equation whose roots are reciprocal to the roots of given equation | Function to find the quadratic equation having reciprocal roots ; Print quadratic equation ; Driver Code ; Given coefficients ; Function call to find the quadratic equation having reciprocal roots
def findEquation ( A , B , C ) : NEW_LINE INDENT print ( " ( " + str ( C ) + " ) " + " x ^ 2 ▁ + ( " + str ( B ) + " ) x ▁ + ▁ ( " + str ( A ) + " ) ▁ = ▁ 0" ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = 1 NEW_LINE B = - 5 NEW_LINE C = 6 NEW_LINE findEquation ( A , B , C ) NEW_LINE DEDENT
Minimize subtraction followed by increments of adjacent elements required to make all array elements equal | Function to find the minimum number of moves required to make all array elements equal ; Store the total sum of the array ; Calculate total sum of the array ; If the sum is not divisible by N , then print " - 1" ; Stores the average ; Stores the count of operations ; Traverse the array arr [ ] ; Update number of moves required to make current element equal to avg ; Update the overall count ; Return the minimum operations required ; Driver Code
def findMinMoves ( arr , N ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT if ( sum % N != 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT avg = sum // N NEW_LINE total = 0 NEW_LINE needCount = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT needCount += ( arr [ i ] - avg ) NEW_LINE total = max ( max ( abs ( needCount ) , arr [ i ] - avg ) , total ) NEW_LINE DEDENT return total NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 0 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE print ( findMinMoves ( arr , N ) ) NEW_LINE DEDENT
Maximum amount of money that can be collected by a player in a game of coins | Function to calculate the maximum amount collected by A ; Stores the money obtained by A ; Stores mid elements of odd sized rows ; Size of current row ; Increase money collected by A ; Add coins at even indices to the amount collected by A ; Print the amount ; Driver Code ; Function call to calculate the amount of coins collected by A
def find ( N , Arr ) : NEW_LINE INDENT amount = 0 NEW_LINE mid_odd = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT siz = len ( Arr [ i ] ) NEW_LINE for j in range ( 0 , siz // 2 ) : NEW_LINE INDENT amount = amount + Arr [ i ] [ j ] NEW_LINE DEDENT if ( siz % 2 == 1 ) : NEW_LINE INDENT mid_odd . append ( Arr [ i ] [ siz // 2 ] ) NEW_LINE DEDENT DEDENT mid_odd . sort ( reverse = True ) NEW_LINE for i in range ( len ( mid_odd ) ) : NEW_LINE INDENT if i % 2 == 0 : NEW_LINE INDENT amount = amount + mid_odd [ i ] NEW_LINE DEDENT DEDENT print ( amount ) NEW_LINE DEDENT N = 2 NEW_LINE Arr = [ [ 5 , 2 , 3 , 4 ] , [ 1 , 6 ] ] NEW_LINE find ( N , Arr ) NEW_LINE
Check if an array can be split into K consecutive non | Function to check if array can be split into K consecutive and non - overlapping subarrays of length M consisting of a single distinct element ; Traverse over the range [ 0 , N - M - 1 ] ; Check if arr [ i ] is the same as arr [ i + m ] ; Increment current length t of pattern matched by 1 ; Check if t is equal to m , increment count of total repeated pattern ; Return true if length of total repeated pattern is k ; Update length of the current pattern ; Update count to 1 ; Finally return false if no pattern found ; Driver Code
def checkPattern ( arr , m , k , n ) : NEW_LINE INDENT count = 1 NEW_LINE t = 0 NEW_LINE for i in range ( n - m ) : NEW_LINE INDENT if ( arr [ i ] == arr [ i + m ] ) : NEW_LINE INDENT t += 1 NEW_LINE if ( t == m ) : NEW_LINE INDENT t = 0 NEW_LINE count += 1 NEW_LINE if ( count == k ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT t = 0 NEW_LINE count = 1 NEW_LINE DEDENT DEDENT return " No " NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 6 , 1 , 3 , 3 , 3 , 3 ] NEW_LINE M = 1 NEW_LINE K = 3 NEW_LINE N = len ( arr ) NEW_LINE print ( checkPattern ( arr , M , K , N ) ) NEW_LINE DEDENT
Remove all occurrences of a word from a given string using Z | Function to fill the Z - array for str ; L Stores start index of window which matches with prefix of str ; R Stores end index of window which matches with prefix of str ; Iterate over the characters of str ; If i is greater thn R ; Update L and R ; If substring match with prefix ; Update R ; Update Z [ i ] ; Update R ; Update k ; if Z [ k ] is less than remaining interval ; Update Z [ i ] ; Start from R and check manually ; Update R ; Update Z [ i ] ; Update R ; Function to remove all the occurrences of word from str ; Create concatenated string " P $ T " ; Store Z array of concat ; Stores string , str by removing all the occurrences of word from str ; Stores length of word ; Traverse the array , Z [ ] ; if Z [ i + pSize + 1 ] equal to length of word ; Update i ; Driver Code
def getZarr ( st , Z ) : NEW_LINE INDENT n = len ( st ) NEW_LINE k = 0 NEW_LINE L = 0 NEW_LINE R = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( i > R ) : NEW_LINE INDENT L = R = i NEW_LINE while ( R < n and st [ R - L ] == st [ R ] ) : NEW_LINE INDENT R += 1 NEW_LINE DEDENT Z [ i ] = R - L NEW_LINE R -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT k = i - L NEW_LINE if ( Z [ k ] < R - i + 1 ) : NEW_LINE INDENT Z [ i ] = Z [ k ] NEW_LINE DEDENT else : NEW_LINE INDENT L = i NEW_LINE while ( R < n and st [ R - L ] == st [ R ] ) : NEW_LINE INDENT R += 1 NEW_LINE DEDENT Z [ i ] = R - L NEW_LINE R -= 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT def goodStr ( st , word ) : NEW_LINE INDENT concat = word + " $ " + st NEW_LINE l = len ( concat ) NEW_LINE Z = [ 0 ] * l NEW_LINE getZarr ( concat , Z ) NEW_LINE res = " " NEW_LINE pSize = len ( word ) NEW_LINE for i in range ( l ) : NEW_LINE INDENT if ( i + pSize < l - 1 and Z [ i + pSize + 1 ] == pSize ) : NEW_LINE INDENT i += pSize - 1 NEW_LINE DEDENT elif ( i < len ( st ) ) : NEW_LINE INDENT res += st [ i ] NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = " Z - kmalgorithmkmiskmkmkmhelpfulkminkmsearching " NEW_LINE word = " km " NEW_LINE print ( goodStr ( st , word ) ) NEW_LINE DEDENT
Count of substrings of a string containing another given string as a substring | Set 2 | Function to count the substrings of containing another given as a sub ; Store length of S ; Store length of T ; Store the required count of substrings ; Store the starting index of last occurence of T in S ; Iterate in range [ 0 , n1 - n2 ] ; Check if subfrom i to i + n2 is equal to T ; Check if subfrom i to i + n2 is equal to T ; Mark chk as false and break the loop ; If chk is true ; Add ( i + 1 - last ) * ( n1 - ( i + n2 - 1 ) ) to answer ; Update the last to i + 1 ; Prthe answer ; Driver code ; Function Call
def findOccurrences ( S , T ) : NEW_LINE INDENT n1 = len ( S ) NEW_LINE n2 = len ( T ) NEW_LINE ans = 0 NEW_LINE last = 0 NEW_LINE for i in range ( n1 - n2 + 1 ) : NEW_LINE INDENT chk = True NEW_LINE for j in range ( n2 ) : NEW_LINE INDENT if ( T [ j ] != S [ i + j ] ) : NEW_LINE INDENT chk = False NEW_LINE break NEW_LINE DEDENT DEDENT if ( chk ) : NEW_LINE INDENT ans += ( i + 1 - last ) * ( n1 - ( i + n2 - 1 ) ) NEW_LINE last = i + 1 NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S , T = " dabc " , " ab " NEW_LINE findOccurrences ( S , T ) NEW_LINE DEDENT
Lexicographically largest N | Function to find the lexicographically largest bitonic sequence of size N elements lies in the range [ low , high ] ; Store index of highest element ; If high_index > ( N - 1 ) / 2 , then remaining N / 2 elements cannot be placed in bitonic order ; If high_index <= 0 , then set high_index as 1 ; Stores the resultant sequence ; Store the high value ; Maintain strictly decreasing sequence from index high_index to 0 starting with temp ; Store the value and decrement the temp variable by 1 ; Maintain the strictly decreasing sequence from index high_index + 1 to N - 1 starting with high - 1 ; Store the value and decrement high by 1 ; Print the resultant sequence ; Driver Code ; Function Call
def LargestArray ( N , low , high ) : NEW_LINE INDENT high_index = N - ( high - low + 1 ) NEW_LINE if ( high_index > ( N - 1 ) // 2 ) : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE return NEW_LINE DEDENT if ( high_index <= 0 ) : NEW_LINE INDENT high_index = 1 NEW_LINE DEDENT A = [ 0 ] * N NEW_LINE temp = high NEW_LINE for i in range ( high_index , - 1 , - 1 ) : NEW_LINE INDENT A [ i ] = temp NEW_LINE temp = temp - 1 NEW_LINE DEDENT high -= 1 NEW_LINE for i in range ( high_index + 1 , N ) : NEW_LINE INDENT A [ i ] = high NEW_LINE high = high - 1 NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT print ( A [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT N = 5 NEW_LINE low = 2 NEW_LINE high = 6 NEW_LINE LargestArray ( N , low , high ) NEW_LINE
Print matrix elements from top | Function to traverse the matrix diagonally upwards ; Stores the maximum size of vector from all row of matrix nums [ ] [ ] ; Store elements in desired order ; Store every element on the basis of sum of index ( i + j ) ; Print the stored result ; Reverse all sublist ; Driver code ; Given vector of vectors arr ; Function Call
def printDiagonalTraversal ( nums ) : NEW_LINE INDENT max_size = len ( nums ) NEW_LINE for i in range ( len ( nums ) ) : NEW_LINE INDENT if ( max_size < len ( nums [ i ] ) ) : NEW_LINE INDENT max_size = len ( nums [ i ] ) NEW_LINE DEDENT DEDENT v = [ [ ] for i in range ( 2 * max_size - 1 ) ] NEW_LINE for i in range ( len ( nums ) ) : NEW_LINE INDENT for j in range ( len ( nums [ i ] ) ) : NEW_LINE INDENT v [ i + j ] . append ( nums [ i ] [ j ] ) NEW_LINE DEDENT DEDENT for i in range ( len ( v ) ) : NEW_LINE INDENT v [ i ] = v [ i ] [ : : - 1 ] NEW_LINE for j in range ( len ( v [ i ] ) ) : NEW_LINE INDENT print ( v [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] NEW_LINE printDiagonalTraversal ( arr ) NEW_LINE DEDENT
Check if given string satisfies the following conditions | Python3 program for the above approach ; Function to check if given string satisfies the given conditions ; Dimensions ; Left diagonal ; Right diagonal ; Conditions not satisfied ; Print Yes ; Given String ; Function call
import math NEW_LINE def isValid ( s ) : NEW_LINE INDENT n = int ( math . sqrt ( len ( s ) ) ) NEW_LINE check = s [ 0 ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT x = i NEW_LINE y = i NEW_LINE while ( x >= 0 and y < n ) : NEW_LINE INDENT if ( s [ n * x + y ] != check or s [ n * x + x ] != check ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT x -= 1 NEW_LINE y += 1 NEW_LINE DEDENT DEDENT print ( " Yes " ) NEW_LINE DEDENT str = " abacdaeaafaghaia " NEW_LINE isValid ( str ) NEW_LINE
Sum of all possible strings obtained by removal of non | Python 3 program for the above approach ; Function to convert a character to its equivalent digit ; Function to precompute powers of 10 ; Function to precompute prefix sum of numerical strings ; Function to return the i - th term of Triangular Number ; Function to return the sum of all resulting strings ; Precompute powers of 10 ; Precompute prefix sum ; Initialize result ; Apply the above general formula for every i ; Return the answer ; Driver Code ; Function Call
N = 10 NEW_LINE pref = [ 0 ] * N NEW_LINE power = [ 0 ] * N NEW_LINE def toDigit ( ch ) : NEW_LINE INDENT return ( ord ( ch ) - ord ( '0' ) ) NEW_LINE DEDENT def powerOf10 ( ) : NEW_LINE INDENT power [ 0 ] = 1 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT power [ i ] = power [ i - 1 ] * 10 NEW_LINE DEDENT DEDENT def precomputePrefix ( st , n ) : NEW_LINE INDENT pref [ 0 ] = ( ord ( st [ 0 ] ) - ord ( '0' ) ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT pref [ i ] = ( pref [ i - 1 ] + toDigit ( st [ i ] ) ) NEW_LINE DEDENT DEDENT def triangularNumber ( i ) : NEW_LINE INDENT res = i * ( i + 1 ) // 2 NEW_LINE return res NEW_LINE DEDENT def sumOfSubstrings ( st ) : NEW_LINE INDENT n = len ( st ) NEW_LINE powerOf10 ( ) NEW_LINE precomputePrefix ( st , n ) NEW_LINE ans = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT ans += ( ( pref [ n - i - 2 ] * ( i + 1 ) + toDigit ( st [ n - i - 1 ] ) * triangularNumber ( n - i - 1 ) ) * power [ i ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = "1234" NEW_LINE print ( sumOfSubstrings ( st ) ) NEW_LINE DEDENT
Sum of products of all possible Subarrays | Function that finds the sum of products of all subarray of arr [ ] ; Stores sum of all subarrays ; Iterate array from behind ; Update the ans ; Update the res ; Print the final sum ; Driver Code ; Given array arr [ ] ; Size of array ; Function call
def sumOfSubarrayProd ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE res = 0 NEW_LINE i = n - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT incr = arr [ i ] * ( 1 + res ) NEW_LINE ans += incr NEW_LINE res = incr NEW_LINE i -= 1 NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE sumOfSubarrayProd ( arr , N ) NEW_LINE DEDENT
Sum of all odd length subarrays | Function to calculate the sum of all odd length subarrays ; Stores the sum ; Size of array ; Traverse the array ; Generate all subarray of odd length ; Add the element to sum ; Return the final sum ; Given array arr [ ] ; Function call
def OddLengthSum ( arr ) : NEW_LINE INDENT sum = 0 NEW_LINE l = len ( arr ) NEW_LINE for i in range ( l ) : NEW_LINE INDENT for j in range ( i , l , 2 ) : NEW_LINE INDENT for k in range ( i , j + 1 , 1 ) : NEW_LINE INDENT sum += arr [ k ] NEW_LINE DEDENT DEDENT DEDENT return sum NEW_LINE DEDENT arr = [ 1 , 5 , 3 , 1 , 2 ] NEW_LINE print ( OddLengthSum ( arr ) ) NEW_LINE
Sum of all odd length subarrays | Function that finds the sum of all the element of subarrays of odd length ; Stores the sum ; Size of array ; Traverse the given array arr [ ] ; Add to the sum for each contribution of the arr [ i ] ; Return the final sum ; Given array arr [ ] ; Function call
def OddLengthSum ( arr ) : NEW_LINE INDENT Sum = 0 NEW_LINE l = len ( arr ) NEW_LINE for i in range ( l ) : NEW_LINE INDENT Sum += ( ( ( ( i + 1 ) * ( l - i ) + 1 ) // 2 ) * arr [ i ] ) NEW_LINE DEDENT return Sum NEW_LINE DEDENT arr = [ 1 , 5 , 3 , 1 , 2 ] NEW_LINE print ( OddLengthSum ( arr ) ) NEW_LINE
Check if Euler Totient Function is same for a given number and twice of that number | Function to find the Euler 's Totient Function ; Initialize result as N ; Consider all prime factors of n and subtract their multiples from result ; Return the count ; Function to check if phi ( n ) is equals phi ( 2 * n ) ; Driver Code
def phi ( n ) : NEW_LINE INDENT result = 1 NEW_LINE for p in range ( 2 , n ) : NEW_LINE INDENT if ( __gcd ( p , n ) == 1 ) : NEW_LINE INDENT result += 1 NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT def sameEulerTotient ( n ) : NEW_LINE INDENT return phi ( n ) == phi ( 2 * n ) NEW_LINE DEDENT def __gcd ( a , b ) : NEW_LINE INDENT return a if b == 0 else __gcd ( b , a % b ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 13 NEW_LINE if ( sameEulerTotient ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Check if Euler Totient Function is same for a given number and twice of that number | Function to check if phi ( n ) is equals phi ( 2 * n ) ; Return if N is odd ; Driver code ; Function call
def sameEulerTotient ( N ) : NEW_LINE INDENT return ( N & 1 ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 13 ; NEW_LINE if ( sameEulerTotient ( N ) == 1 ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT