codeparrot/xlcost-text-to-code huggingface dataset:
Find Binary string by converting all 01 or 10 to 11 after M iterations | Function to find the modified binary string after M iterations ; Set the value of M to the minimum of N or M . ; Declaration of current string state ; Loop over M iterations ; Set the current state as null before each iteration ; Check if this zero has exactly one 1 as neighbour ; Flip the zero ; If there is no change , then no need for further iterations . ; Set the current state as the new previous state ; Driver Code ; Given String ; Number of Iterations ; Function Call
def findString ( str , M ) : NEW_LINE INDENT N = len ( str ) NEW_LINE M = min ( M , N ) NEW_LINE s1 = " " NEW_LINE while ( M != 0 ) : NEW_LINE INDENT s1 = " " NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT if ( str [ i ] == '0' ) : NEW_LINE INDENT if ( ( str [ i - 1 ] == '1' and str [ i + 1 ] != '1' ) or ( str [ i - 1 ] != '1' and str [ i + 1 ] == '1' ) ) : NEW_LINE INDENT s1 += '1' NEW_LINE DEDENT else : NEW_LINE INDENT s1 += '0' NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT s1 += '1' NEW_LINE DEDENT DEDENT if ( str == s1 ) : NEW_LINE INDENT break NEW_LINE DEDENT s1 += '1' NEW_LINE str = s1 NEW_LINE M -= 1 NEW_LINE DEDENT print ( s1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "0110100" NEW_LINE M = 3 NEW_LINE findString ( str , M ) NEW_LINE DEDENT
Count of subsequences with a sum in range [ L , R ] and difference between max and min element at least X | Python3 program for the above approach ; Function to find the number of subsequences of the given array with a sum in range [ L , R ] and the difference between the maximum and minimum element is at least X ; Initialize answer as 0 ; Creating mask from [ 0 , 2 ^ n - 1 ] ; Stores the count and sum of selected elements respectively ; Variables to store the value of Minimum and maximum element ; Traverse the array ; If the jth bit of the ith mask is on ; Add the selected element ; Update maxVal and minVal value ; Check if the given conditions are true , increment ans by 1. ; Driver Code ; Given Input ; Function Call
import sys NEW_LINE def numberofSubsequences ( a , L , R , X , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 0 , ( 1 << n ) , 1 ) : NEW_LINE INDENT cnt = 0 NEW_LINE sum = 0 NEW_LINE minVal = sys . maxsize NEW_LINE maxVal = - sys . maxsize - 1 NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( ( i & ( 1 << j ) ) ) : NEW_LINE INDENT cnt += 1 NEW_LINE sum += a [ j ] NEW_LINE maxVal = max ( maxVal , a [ j ] ) NEW_LINE minVal = min ( minVal , a [ j ] ) NEW_LINE DEDENT DEDENT if ( cnt >= 2 and sum >= L and sum <= R and ( maxVal - minVal >= X ) ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 10 , 20 , 30 , 25 ] NEW_LINE L = 40 NEW_LINE R = 50 NEW_LINE X = 10 NEW_LINE N = len ( a ) NEW_LINE print ( numberofSubsequences ( a , L , R , X , N ) ) NEW_LINE DEDENT
Minimum count of numbers needed from 1 to N that yields the sum as K | Function to find minimum number of elements required to obtain sum K ; Stores the maximum sum that can be obtained ; If K is greater than the Maximum sum ; If K is less than or equal to to N ; Stores the sum ; Stores the count of numbers needed ; Iterate until N is greater than or equal to 1 and sum is less than K ; Increment count by 1 ; Increment sum by N ; Update the sum ; Finally , return the count ; Driver Code ; Given Input ; Function Call
def Minimum ( N , K ) : NEW_LINE INDENT sum = N * ( N + 1 ) // 2 NEW_LINE if ( K > sum ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if ( K <= N ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT sum = 0 NEW_LINE count = 0 NEW_LINE while ( N >= 1 and sum < K ) : NEW_LINE INDENT count += 1 NEW_LINE sum += N NEW_LINE N -= 1 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE K = 10 NEW_LINE print ( Minimum ( N , K ) ) NEW_LINE DEDENT
Maximize X such that sum of numbers in range [ 1 , X ] is at most K | Function to count the elements with sum of the first that many natural numbers less than or equal to K ; If K equals to 0 ; Stores the result ; Iterate until low is less than or equal to high ; Stores the sum of first mid natural numbers ; If sum is less than or equal to K ; Update res and low ; Otherwise , ; Update ; Return res ; Driver Code ; Input ; Function call
def Count ( N , K ) : NEW_LINE INDENT if ( K == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT res = 0 NEW_LINE low = 1 NEW_LINE high = N NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE sum = ( mid * mid + mid ) // 2 NEW_LINE if ( sum <= K ) : NEW_LINE INDENT res = max ( res , mid ) NEW_LINE low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 NEW_LINE K = 14 NEW_LINE print ( Count ( N , K ) ) NEW_LINE DEDENT
Largest number having both positive and negative values present in the array | Function to find the largest number k such that both k and - k are present in the array ; Stores the array elements ; Initialize a variable res as 0 to store maximum element while traversing the array ; Iterate through array arr ; Add the current element into the st ; Check if the negative of this element is also present in the st or not ; Return the resultant element ; Drive Code
def largestNum ( arr , n ) : NEW_LINE INDENT st = set ( [ ] ) NEW_LINE res = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT st . add ( arr [ i ] ) NEW_LINE if ( - 1 * arr [ i ] ) in st : NEW_LINE INDENT res = max ( res , abs ( arr [ i ] ) ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT arr = [ 3 , 2 , - 2 , 5 , - 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( largestNum ( arr , n ) ) NEW_LINE
Check if an element is present in an array using at most floor ( N / 2 ) + 2 comparisons | Function to check whether X is present in the array A [ ] ; Initialise a pointer ; Store the number of comparisons ; Variable to store product ; Check is N is odd ; Update i and T ; Traverse the array ; Check if i < N ; Update T ; Check if T is equal to 0 ; Driver Code ; Given Input ; Function Call
def findElement ( A , N , X ) : NEW_LINE INDENT i = 0 NEW_LINE Comparisons = 0 NEW_LINE T = 1 NEW_LINE Found = " No " NEW_LINE Comparisons += 1 NEW_LINE if ( N % 2 == 1 ) : NEW_LINE INDENT i = 1 NEW_LINE T *= ( A [ 0 ] - X ) NEW_LINE DEDENT while ( i < N ) : NEW_LINE INDENT Comparisons += 1 NEW_LINE T *= ( A [ i ] - X ) NEW_LINE T *= ( A [ i + 1 ] - X ) NEW_LINE i += 2 NEW_LINE DEDENT Comparisons += 1 NEW_LINE if ( T == 0 ) : NEW_LINE INDENT print ( " Yes " , Comparisons ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ - 3 , 5 , 11 , 3 , 100 , 2 , 88 , 22 , 7 , 900 , 23 , 4 , 1 ] NEW_LINE N = len ( A ) NEW_LINE X = 1 NEW_LINE findElement ( A , N , X ) NEW_LINE DEDENT
Search an element in a sorted array formed by reversing subarrays from a random index | Function to search an element in a sorted array formed by reversing subarrays from a random index ; Set the boundaries for binary search ; Apply binary search ; Initialize the middle element ; If element found ; Random point is on right side of mid ; From l to mid arr is reverse sorted ; Random point is on the left side of mid ; From mid to h arr is reverse sorted ; Return Not Found ; Driver Code ; Given Input ; Function Call
def find ( arr , N , key ) : NEW_LINE INDENT l = 0 NEW_LINE h = N - 1 NEW_LINE while l <= h : NEW_LINE INDENT mid = ( l + h ) // 2 NEW_LINE if arr [ mid ] == key : NEW_LINE INDENT return mid NEW_LINE DEDENT if arr [ l ] >= arr [ mid ] : NEW_LINE INDENT if arr [ l ] >= key >= arr [ mid ] : NEW_LINE INDENT h = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT l = mid + 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if arr [ mid ] >= key >= arr [ h ] : NEW_LINE INDENT l = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT h = mid - 1 NEW_LINE DEDENT DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 8 , 6 , 5 , 2 , 1 , 13 , 12 ] NEW_LINE N = len ( arr ) NEW_LINE key = 8 NEW_LINE ans = find ( arr , N , key ) NEW_LINE print ( ans ) NEW_LINE DEDENT
Maximum elements that can be removed from front of two arrays such that their sum is at most K | Function to find the maximum number of items that can be removed from both the arrays ; Stores the maximum item count ; Stores the prefix sum of the cost of items ; Build the prefix sum for the array A [ ] ; Update the value of A [ i ] ; Build the prefix sum for the array B [ ] ; Update the value of B [ i ] ; Iterate through each item of the array A [ ] ; If A [ i ] exceeds K ; Store the remaining amount after taking top i elements from the array A ; Store the number of items possible to take from the array B [ ] ; Store low and high bounds for binary search ; Binary search to find number of item that can be taken from stack B in rem amount ; Calculate the mid value ; Update the value of j and lo ; Update the value of the hi ; Store the maximum of total item count ; Print the result ; Driver Code
def maxItems ( n , m , a , b , K ) : NEW_LINE INDENT count = 0 NEW_LINE A = [ 0 for i in range ( n + 1 ) ] NEW_LINE B = [ 0 for i in range ( m + 1 ) ] NEW_LINE for i in range ( 1 , n + 1 , 1 ) : NEW_LINE INDENT A [ i ] = a [ i - 1 ] + A [ i - 1 ] NEW_LINE DEDENT for i in range ( 1 , m + 1 , 1 ) : NEW_LINE INDENT B [ i ] = b [ i - 1 ] + B [ i - 1 ] NEW_LINE DEDENT for i in range ( n + 1 ) : NEW_LINE INDENT if ( A [ i ] > K ) : NEW_LINE INDENT break NEW_LINE DEDENT rem = K - A [ i ] NEW_LINE j = 0 NEW_LINE lo = 0 NEW_LINE hi = m NEW_LINE while ( lo <= hi ) : NEW_LINE INDENT mid = ( lo + hi ) // 2 NEW_LINE if ( B [ mid ] <= rem ) : NEW_LINE INDENT j = mid NEW_LINE lo = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT hi = mid - 1 NEW_LINE DEDENT DEDENT count = max ( j + i , count ) NEW_LINE DEDENT print ( count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE m = 5 NEW_LINE K = 7 NEW_LINE A = [ 2 , 4 , 7 , 3 ] NEW_LINE B = [ 1 , 9 , 3 , 4 , 5 ] NEW_LINE maxItems ( n , m , A , B , K ) NEW_LINE DEDENT
Number which is co | Function to check whether the given number N is prime or not ; Base Case ; If N has more than one factor , then return false ; Otherwise , return true ; Function to find X which is co - prime with the integers from the range [ L , R ] ; Store the resultant number ; Check for prime integers greater than R ; If the current number is prime , then update coPrime and break out of loop ; Print the resultant number ; Driver Code
def isPrime ( N ) : NEW_LINE INDENT if ( N == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , N + 1 ) : NEW_LINE INDENT if i * i > N : NEW_LINE INDENT break NEW_LINE DEDENT if ( N % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def findCoPrime ( L , R ) : NEW_LINE INDENT coPrime , i = 0 , R + 1 NEW_LINE while True : NEW_LINE INDENT if ( isPrime ( i ) ) : NEW_LINE INDENT coPrime = i NEW_LINE break NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return coPrime NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L , R = 16 , 17 NEW_LINE print ( findCoPrime ( L , R ) ) NEW_LINE DEDENT
Maximum value of arr [ i ] + arr [ j ] + i â €“ j for any pair of an array | Function to find the maximum value of arr [ i ] + arr [ j ] + i - j over all pairs ; Stores the required result ; Traverse over all the pairs ( i , j ) ; Calculate the value of the expression and update the overall maximum value ; Print the result ; Driver Code
def maximumValue ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT ans = max ( ans , arr [ i ] + arr [ j ] + i - j ) NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT arr = [ 1 , 9 , 3 , 6 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE maximumValue ( arr , N ) NEW_LINE
Maximum value of arr [ i ] + arr [ j ] + i â €“ j for any pair of an array | Function to find the maximum value of ( arr [ i ] + arr [ j ] + i - j ) possible for a pair in the array ; Stores the maximum value of ( arr [ i ] + i ) ; Stores the required result ; Traverse the array arr [ ] ; Calculate for current pair and update maximum value ; Update maxValue if ( arr [ i ] + I ) is greater than maxValue ; Print the result ; Driver code
def maximumValue ( arr , n ) : NEW_LINE INDENT maxvalue = arr [ 0 ] NEW_LINE result = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT result = max ( result , maxvalue + arr [ i ] - i ) NEW_LINE maxvalue = max ( maxvalue , arr [ i ] + i ) NEW_LINE DEDENT print ( result ) NEW_LINE DEDENT arr = [ 1 , 9 , 3 , 6 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE maximumValue ( arr , N ) NEW_LINE
Minimum sum of absolute differences of pairs in a triplet from three arrays | Lower_bound function ; Function to find the value closest to K in the array A [ ] ; Initialize close value as the end element ; Find lower bound of the array ; If lower_bound is found ; If lower_bound is not the first array element ; If * ( it - 1 ) is closer to k ; Return closest value of k ; Function to find the minimum sum of abs ( arr [ i ] - brr [ j ] ) and abs ( brr [ j ] - crr [ k ] ) ; Sort the vectors arr and crr ; Initialize minimum as LET_MAX ; Traverse the array brr [ ] ; Stores the element closest to val from the array arr [ ] ; Stores the element closest to val from the array crr [ ] ; If sum of differences is minimum ; Update the minimum ; Print the minimum absolute difference possible ; Driver code ; Function Call
def lower_bound ( arr , key ) : NEW_LINE INDENT low = 0 ; NEW_LINE high = len ( arr ) - 1 ; NEW_LINE while ( low < high ) : NEW_LINE INDENT mid = low + ( high - low ) // 2 ; NEW_LINE if ( arr [ mid ] >= key ) : NEW_LINE INDENT high = mid ; NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 ; NEW_LINE DEDENT DEDENT return low ; NEW_LINE DEDENT def closestValue ( A , k ) : NEW_LINE INDENT close = A [ - 1 ] ; NEW_LINE it = lower_bound ( A , k ) ; NEW_LINE if ( it != len ( A ) ) : NEW_LINE INDENT close = A [ it ] ; NEW_LINE if ( it != 0 ) : NEW_LINE INDENT if ( ( k - A [ it - 1 ] ) < ( close - k ) ) : NEW_LINE INDENT close = A [ it - 1 ] ; NEW_LINE DEDENT DEDENT DEDENT return close ; NEW_LINE DEDENT def minPossible ( arr , brr , crr ) : NEW_LINE INDENT arr . sort ( ) ; NEW_LINE crr . sort ( ) ; NEW_LINE minimum = 10 ** 9 ; NEW_LINE for val in brr : NEW_LINE INDENT arr_close = closestValue ( arr , val ) ; NEW_LINE crr_close = closestValue ( crr , val ) ; NEW_LINE if ( abs ( val - arr_close ) + abs ( val - crr_close ) < minimum ) : NEW_LINE INDENT minimum = abs ( val - arr_close ) + abs ( val - crr_close ) ; NEW_LINE DEDENT DEDENT print ( minimum ) ; NEW_LINE DEDENT a = [ 1 , 8 , 5 ] ; NEW_LINE b = [ 2 , 9 ] ; NEW_LINE c = [ 5 , 4 ] ; NEW_LINE minPossible ( a , b , c ) ; NEW_LINE
Minimize swaps required to make the first and last elements the largest and smallest elements in the array respectively | Function to find the minimum number of swaps required to make the first and the last elements the largest and smallest element in the array ; Stores the count of swaps ; Stores the maximum element ; Stores the minimum element ; If the array contains a single distinct element ; Stores the indices of the maximum and minimum elements ; If the first index of the maximum element is found ; If current index has the minimum element ; Update the count of operations to place largest element at the first ; Update the count of operations to place largest element at the last ; If smallest element is present before the largest element initially ; Driver Code
def minimum_swaps ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE max_el = max ( arr ) NEW_LINE min_el = min ( arr ) NEW_LINE if ( min_el == max_el ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT index_max = - 1 NEW_LINE index_min = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == max_el and index_max == - 1 ) : NEW_LINE INDENT index_max = i NEW_LINE DEDENT if ( arr [ i ] == min_el ) : NEW_LINE INDENT index_min = i NEW_LINE DEDENT DEDENT count += index_max NEW_LINE count += ( n - 1 - index_min ) NEW_LINE if ( index_min < index_max ) : NEW_LINE INDENT count -= 1 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 1 , 6 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE print ( minimum_swaps ( arr , N ) ) NEW_LINE DEDENT
Maximum difference between node and its ancestor in a Directed Acyclic Graph ( DAG ) | Python3 program for the above approach ; Function to perform DFS Traversal on the given graph ; Update the value of ans ; Update the currentMin and currentMax ; Traverse the adjacency list of the node src ; Recursively call for the child node ; Function to calculate maximum absolute difference between a node and its ancestor ; Stores the adjacency list of graph ; Create Adjacency list ; Add a directed edge ; Perform DFS Traversal ; Print the maximum absolute difference ; Driver Code
ans = 0 NEW_LINE def DFS ( src , Adj , arr , currentMin , currentMax ) : NEW_LINE INDENT global ans NEW_LINE ans = max ( ans , max ( abs ( currentMax - arr [ src - 1 ] ) , abs ( currentMin - arr [ src - 1 ] ) ) ) NEW_LINE currentMin = min ( currentMin , arr [ src - 1 ] ) NEW_LINE currentMax = min ( currentMax , arr [ src - 1 ] ) NEW_LINE for child in Adj [ src ] : NEW_LINE INDENT DFS ( child , Adj , arr , currentMin , currentMax ) NEW_LINE DEDENT DEDENT def getMaximumDifference ( Edges , arr , N , M ) : NEW_LINE INDENT global ans NEW_LINE Adj = [ [ ] for i in range ( N + 1 ) ] NEW_LINE for i in range ( M ) : NEW_LINE INDENT u = Edges [ i ] [ 0 ] NEW_LINE v = Edges [ i ] [ 1 ] NEW_LINE Adj [ u ] . append ( v ) NEW_LINE DEDENT DFS ( 1 , Adj , arr , arr [ 0 ] , arr [ 0 ] ) NEW_LINE print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE M = 4 NEW_LINE Edges = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 4 , 5 ] , [ 1 , 3 ] ] NEW_LINE arr = [ 13 , 8 , 3 , 15 , 18 ] NEW_LINE getMaximumDifference ( Edges , arr , N , M ) NEW_LINE DEDENT
Minimum cost path from source node to destination node via K intermediate nodes | Function to find the minimum cost path from the source vertex to destination vertex via K intermediate vertices ; Initialize the adjacency list ; Generate the adjacency list ; Initialize the minimum priority queue ; Stores the minimum cost to travel between vertices via K intermediate nodes ; Push the starting vertex , cost to reach and the number of remaining vertices ; Pop the top element of the stack ; If destination is reached ; Return the cost ; If all stops are exhausted ; Find the neighbour with minimum cost ; Pruning ; Update cost ; Update priority queue ; If no path exists ; Driver Code ; Function Call to find the path from src to dist via k nodes having least sum of weights
def leastWeightedSumPath ( n , edges , src , dst , K ) : NEW_LINE INDENT graph = [ [ ] for i in range ( 3 ) ] NEW_LINE for edge in edges : NEW_LINE INDENT graph [ edge [ 0 ] ] . append ( [ edge [ 1 ] , edge [ 2 ] ] ) NEW_LINE DEDENT pq = [ ] NEW_LINE costs = [ [ 10 ** 9 for i in range ( K + 2 ) ] for i in range ( n ) ] NEW_LINE costs [ src ] [ K + 1 ] = 0 NEW_LINE pq . append ( [ 0 , src , K + 1 ] ) NEW_LINE pq = sorted ( pq ) [ : : - 1 ] NEW_LINE while ( len ( pq ) > 0 ) : NEW_LINE INDENT top = pq [ - 1 ] NEW_LINE del pq [ - 1 ] NEW_LINE if ( top [ 1 ] == dst ) : NEW_LINE INDENT return top [ 0 ] NEW_LINE DEDENT if ( top [ 2 ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT for neighbor in graph [ top [ 1 ] ] : NEW_LINE INDENT if ( costs [ neighbor [ 0 ] ] [ top [ 2 ] - 1 ] < neighbor [ 1 ] + top [ 0 ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT costs [ neighbor [ 0 ] ] [ top [ 2 ] - 1 ] = neighbor [ 1 ] + top [ 0 ] NEW_LINE pq . append ( [ neighbor [ 1 ] + top [ 0 ] , neighbor [ 0 ] , top [ 2 ] - 1 ] ) NEW_LINE DEDENT pq = sorted ( pq ) [ : : - 1 ] NEW_LINE DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n , src , dst , k = 3 , 0 , 2 , 1 NEW_LINE edges = [ [ 0 , 1 , 100 ] , [ 1 , 2 , 100 ] , [ 0 , 2 , 500 ] ] NEW_LINE print ( leastWeightedSumPath ( n , edges , src , dst , k ) ) NEW_LINE DEDENT
Check if a given string is a comment or not | Function to check if the given string is a comment or not ; If two continuous slashes preceeds the comment ; Driver Code ; Given string ; Function call to check whether then given string is a comment or not
def isComment ( line ) : NEW_LINE INDENT if ( line [ 0 ] == ' / ' and line [ 1 ] == ' / ' and line [ 2 ] != ' / ' ) : NEW_LINE INDENT print ( " It ▁ is ▁ a ▁ single - line ▁ comment " ) NEW_LINE return NEW_LINE DEDENT if ( line [ len ( line ) - 2 ] == ' * ' and line [ len ( line ) - 1 ] == ' / ' and line [ 0 ] == ' / ' and line [ 1 ] == ' * ' ) : NEW_LINE INDENT print ( " It ▁ is ▁ a ▁ multi - line ▁ comment " ) NEW_LINE return NEW_LINE DEDENT print ( " It ▁ is ▁ not ▁ a ▁ comment " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT line = " GeeksForGeeks ▁ GeeksForGeeks " NEW_LINE isComment ( line ) NEW_LINE DEDENT
Print digits for each array element that does not divide any digit of that element | Function to find digits for each array element that doesn 't divide any digit of the that element ; Traverse the array arr [ ] ; Iterate over the range [ 2 , 9 ] ; Stores if there exists any digit in arr [ i ] which is divisible by j ; If any digit of the number is divisible by j ; If the digit j doesn 't  divide any digit of arr[i] ; Driver Code
def indivisibleDigits ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT num = 0 NEW_LINE print ( arr [ i ] , end = ' ▁ ' ) NEW_LINE for j in range ( 2 , 10 ) : NEW_LINE INDENT temp = arr [ i ] NEW_LINE flag = True NEW_LINE while ( temp > 0 ) : NEW_LINE INDENT if ( ( temp % 10 ) != 0 and ( temp % 10 ) % j == 0 ) : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT temp //= 10 NEW_LINE DEDENT if ( flag ) : NEW_LINE INDENT print ( j , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT print ( ) NEW_LINE DEDENT DEDENT arr = [ 4162 , 1152 , 99842 ] NEW_LINE N = len ( arr ) NEW_LINE indivisibleDigits ( arr , N ) NEW_LINE
Length of longest non | Function to find the longest non - decreasing subsequence with difference between adjacent elements exactly equal to 1 ; Base case ; Sort the array in ascending order ; Stores the maximum length ; Traverse the array ; If difference between current pair of adjacent elements is 1 or 0 ; Extend the current sequence Update len and max_len ; Otherwise , start a new subsequence ; Print the maximum length ; Driver Code ; Given array ; Size of the array ; Function call to find the longest subsequence
def longestSequence ( arr , N ) : NEW_LINE INDENT if ( N == 0 ) : NEW_LINE INDENT print ( 0 ) ; NEW_LINE return ; NEW_LINE DEDENT arr . sort ( ) ; NEW_LINE maxLen = 1 ; NEW_LINE len = 1 ; NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT if ( arr [ i ] == arr [ i - 1 ] or arr [ i ] == arr [ i - 1 ] + 1 ) : NEW_LINE INDENT len += 1 ; NEW_LINE maxLen = max ( maxLen , len ) ; NEW_LINE DEDENT else : NEW_LINE INDENT len = 1 ; NEW_LINE DEDENT DEDENT print ( maxLen ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 8 , 5 , 4 , 8 , 4 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE longestSequence ( arr , N ) ; NEW_LINE DEDENT