codeparrot/xlcost-text-to-code huggingface dataset:
Count pairs from a given range whose sum is a Prime Number in that range | Python program to implement the above approach ; Function to find all prime numbers in range [ 1 , lmt ] using sieve of Eratosthenes ; segmentedSieve [ i ] : Stores if i is a prime number ( True ) or not ( False ) ; Set 0 and 1 as non - prime ; Iterate over the range [ 2 , lmt ] ; If i is a prime number ; Append i into prime ; Set all multiple of i non - prime ; Update Sieve [ j ] ; Function to find all the prime numbers in the range [ low , high ] ; Stores square root of high + 1 ; Stores all the prime numbers in the range [ 1 , lmt ] ; Find all the prime numbers in the range [ 1 , lmt ] ; Stores count of elements in the range [ low , high ] ; segmentedSieve [ i ] : Check if ( i - low ) is a prime number or not ; Traverse the array prime [ ] ; Store smallest multiple of prime [ i ] in the range [ low , high ] ; If lowLim is less than low ; Update lowLim ; Iterate over all multiples of prime [ i ] ; If j not equal to prime [ i ] ; Update segmentedSieve [ j - low ] ; Function to count the number of pairs in the range [ L , R ] whose sum is a prime number in the range [ L , R ] ; segmentedSieve [ i ] : Check if ( i - L ) is a prime number or not ; Stores count of pairs whose sum of elements is a prime and in range [ L , R ] ; Iterate over [ L , R ] ; If ( i - L ) is a prime ; Update cntPairs ; Driver Code
import math NEW_LINE def simpleSieve ( lmt , prime ) : NEW_LINE INDENT Sieve = [ True ] * ( lmt + 1 ) NEW_LINE Sieve [ 0 ] = Sieve [ 1 ] = False NEW_LINE for i in range ( 2 , lmt + 1 ) : NEW_LINE INDENT if ( Sieve [ i ] == True ) : NEW_LINE INDENT prime . append ( i ) NEW_LINE for j in range ( i * i , int ( math . sqrt ( lmt ) ) + 1 , i ) : NEW_LINE INDENT Sieve [ j ] = false NEW_LINE DEDENT DEDENT DEDENT return prime NEW_LINE DEDENT def SegmentedSieveFn ( low , high ) : NEW_LINE INDENT lmt = int ( math . sqrt ( high ) ) + 1 NEW_LINE prime = [ ] NEW_LINE prime = simpleSieve ( lmt , prime ) NEW_LINE n = high - low + 1 NEW_LINE segmentedSieve = [ True ] * ( n + 1 ) NEW_LINE for i in range ( 0 , len ( prime ) ) : NEW_LINE INDENT lowLim = int ( low // prime [ i ] ) * prime [ i ] NEW_LINE if ( lowLim < low ) : NEW_LINE INDENT lowLim += prime [ i ] NEW_LINE for j in range ( lowLim , high + 1 , prime [ i ] ) : NEW_LINE INDENT if ( j != prime [ i ] ) : NEW_LINE INDENT segmentedSieve [ j - low ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT return segmentedSieve NEW_LINE DEDENT def countPairsWhoseSumPrimeL_R ( L , R ) : NEW_LINE INDENT segmentedSieve = SegmentedSieveFn ( L , R ) NEW_LINE cntPairs = 0 NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE INDENT if ( segmentedSieve [ i - L ] == True ) : NEW_LINE INDENT cntPairs += i / 2 NEW_LINE DEDENT DEDENT return cntPairs NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = 1 NEW_LINE R = 5 NEW_LINE print ( countPairsWhoseSumPrimeL_R ( L , R ) ) NEW_LINE DEDENT
Lexicographically smallest permutation of a string that can be reduced to length K by removing K | Function to count the number of zeroes present in the string ; Traverse the string ; Return the count ; Function to rearrange the string s . t the string can be reduced to a length K as per the given rules ; Distribute the count of 0 s and 1 s in segment of length 2 k ; Store string that are initially have formed lexicographically smallest 2 k length substring ; Store the lexicographically smallest string of length n that satisfy the condition ; Insert temp_str into final_str ( n / 2 k ) times and add ( n % 2 k ) characters of temp_str at end ; Return the final string ; Function to reduce the string to length K that follows the given conditions ; If the string contains either 0 s or 1 s then it always be reduced into a K length string ; If the string contains only 0 s 1 s then it always reduces to a K length string ; If K = 1 ; Check whether the given string is K reducing string or not ; Otherwise recursively find the string ; Driver Code ; Function Call
def count_zeroes ( n , str ) : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( 0 , len ( str ) ) : NEW_LINE INDENT if ( str [ i ] == '0' ) : NEW_LINE cnt += 1 NEW_LINE DEDENT return cnt NEW_LINE DEDENT def kReducingStringUtil ( n , k , str , no_of_zeroes ) : NEW_LINE INDENT zeroes_in_2k = ( ( ( no_of_zeroes ) * ( 2 * k ) ) // n ) NEW_LINE ones_in_2k = 2 * k - zeroes_in_2k NEW_LINE temp_str = " " NEW_LINE for i in range ( 0 , ( zeroes_in_2k ) // 2 ) : NEW_LINE INDENT temp_str += '0' NEW_LINE DEDENT for i in range ( 0 , ( ones_in_2k ) ) : NEW_LINE INDENT temp_str += '1' NEW_LINE DEDENT for i in range ( 0 , ( zeroes_in_2k ) // 2 ) : NEW_LINE INDENT temp_str += '0' NEW_LINE DEDENT final_str = " " NEW_LINE for i in range ( 0 , n // ( 2 * k ) ) : NEW_LINE INDENT final_str += ( temp_str ) NEW_LINE DEDENT for i in range ( 0 , n % ( 2 * k ) ) : NEW_LINE INDENT final_str += ( temp_str [ i ] ) NEW_LINE DEDENT return final_str NEW_LINE DEDENT def kReducingString ( n , k , str ) : NEW_LINE INDENT no_of_zeroes = count_zeroes ( n , str ) NEW_LINE no_of_ones = n - no_of_zeroes NEW_LINE if ( no_of_zeroes == 0 or no_of_zeroes == n ) : NEW_LINE INDENT return str NEW_LINE DEDENT if ( k == 1 ) : NEW_LINE INDENT if ( no_of_zeroes == 0 or no_of_zeroes == n ) : NEW_LINE return str NEW_LINE else : NEW_LINE return " Not ▁ Possible " NEW_LINE DEDENT check = 0 NEW_LINE for i in range ( ( n // k ) , n , ( n // k ) ) : NEW_LINE INDENT if ( no_of_zeroes == i or no_of_ones == i ) : NEW_LINE check = 1 NEW_LINE break NEW_LINE DEDENT if ( check == 0 ) : NEW_LINE INDENT return " Not ▁ Possible " NEW_LINE DEDENT return kReducingStringUtil ( n , k , str , no_of_zeroes ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "0000100001100001" NEW_LINE K = 4 ; NEW_LINE N = len ( str ) NEW_LINE print ( kReducingString ( N , K , str ) ) NEW_LINE DEDENT
Count quadruplets with sum K from given array | Python3 program for the above approach ; Function to return the number of quadruplets having the given sum ; Initialize answer ; Store the frequency of sum of first two elements ; Traverse from 0 to N - 1 , where arr [ i ] is the 3 rd element ; All possible 4 th elements ; Sum of last two element ; Frequency of sum of first two elements ; Store frequency of all possible sums of first two elements ; Return the answer ; Driver Code ; Given array arr [ ] ; Given sum S ; Function Call
from collections import defaultdict NEW_LINE def countSum ( a , n , sum ) : NEW_LINE INDENT count = 0 NEW_LINE m = defaultdict ( int ) NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT temp = a [ i ] + a [ j ] NEW_LINE if ( temp < sum ) : NEW_LINE INDENT count += m [ sum - temp ] NEW_LINE DEDENT DEDENT for j in range ( i ) : NEW_LINE INDENT temp = a [ i ] + a [ j ] NEW_LINE if ( temp < sum ) : NEW_LINE INDENT m [ temp ] += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 5 , 3 , 1 , 2 , 4 ] NEW_LINE S = 13 NEW_LINE N = len ( arr ) NEW_LINE print ( countSum ( arr , N , S ) ) NEW_LINE DEDENT
Count pairs whose product modulo 10 ^ 9 + 7 is equal to 1 | Python3 program to implement the above approach ; Iterative Function to calculate ( x ^ y ) % MOD ; Initialize result ; Update x if it exceeds MOD ; If x is divisible by MOD ; If y is odd ; Multiply x with res ; y must be even now ; Function to count number of pairs whose product modulo 1000000007 is 1 ; Stores the count of desired pairs ; Stores the frequencies of each array element ; Traverse the array and update frequencies in hash ; Calculate modular inverse of arr [ i ] under modulo 1000000007 ; Update desired count of pairs ; If arr [ i ] and its modular inverse is equal under modulo MOD ; Updating count of desired pairs ; Return the final count ; Driver code
from collections import defaultdict NEW_LINE MOD = 1000000007 NEW_LINE def modPower ( x , y ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % MOD NEW_LINE if ( x == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % MOD NEW_LINE DEDENT y = y // 2 NEW_LINE x = ( x * x ) % MOD NEW_LINE DEDENT return res NEW_LINE DEDENT def countPairs ( arr , N ) : NEW_LINE INDENT pairCount = 0 NEW_LINE hash1 = defaultdict ( int ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT hash1 [ arr [ i ] ] += 1 NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT modularInverse = modPower ( arr [ i ] , MOD - 2 ) NEW_LINE pairCount += hash1 [ modularInverse ] NEW_LINE if ( arr [ i ] == modularInverse ) : NEW_LINE INDENT pairCount -= 1 NEW_LINE DEDENT DEDENT return pairCount // 2 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 236426 , 280311812 , 500000004 ] NEW_LINE N = len ( arr ) NEW_LINE print ( countPairs ( arr , N ) ) NEW_LINE DEDENT
Check if all subarrays contains at least one unique element | Python3 program for the above approach ; Function to check if all subarrays of array have at least one unique element ; Stores frequency of subarray elements ; Generate all subarrays ; Insert first element in map ; Update frequency of current subarray in the HashMap ; Check if at least one element occurs once in current subarray ; If any subarray doesn 't  have unique element ; Clear map for next subarray ; Return Yes if all subarray having at least 1 unique element ; Driver Code ; Given array arr [ ] ; Function Call
from collections import defaultdict NEW_LINE def check ( arr , n ) : NEW_LINE INDENT hm = defaultdict ( int ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT hm [ arr [ i ] ] += 1 NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT hm [ arr [ j ] ] += 1 NEW_LINE flag = False NEW_LINE for k in hm . values ( ) : NEW_LINE INDENT if ( k == 1 ) : NEW_LINE INDENT flag = True NEW_LINE break NEW_LINE DEDENT DEDENT if ( not flag ) : NEW_LINE return " No " NEW_LINE DEDENT hm . clear ( ) NEW_LINE DEDENT return " Yes " NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( check ( arr , N ) ) NEW_LINE DEDENT
Search insert position of K in a sorted array | Function to find insert position of K ; Lower and upper bounds ; Traverse the search space ; If K is found ; Return the insert position ; Driver Code
def find_index ( arr , n , B ) : NEW_LINE INDENT start = 0 NEW_LINE end = n - 1 NEW_LINE while start <= end : NEW_LINE INDENT mid = ( start + end ) // 2 NEW_LINE if arr [ mid ] == K : NEW_LINE INDENT return mid NEW_LINE DEDENT elif arr [ mid ] < K : NEW_LINE INDENT start = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT end = mid - 1 NEW_LINE DEDENT DEDENT return end + 1 NEW_LINE DEDENT arr = [ 1 , 3 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE K = 2 NEW_LINE print ( find_index ( arr , n , K ) ) NEW_LINE
Queries to find minimum swaps required to sort given array with updates | Function to return the position of the given value using binary search ; Return 0 if every value is greater than the given value ; Return N - 1 if every value is smaller than the given value ; Perform Binary Search ; Iterate till start < end ; Find the mid ; Update start and end ; Return the position of the given value ; Function to return the number of make the array sorted ; Index x to update ; Increment value by y ; Set newElement equals to x + y ; Compute the new index ; Print the minimum number of swaps ; Driver Code ; Given array arr [ ] ; Given Queries ; Function Call
def computePos ( arr , n , value ) : NEW_LINE INDENT if ( value < arr [ 0 ] ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( value > arr [ n - 1 ] ) : NEW_LINE INDENT return n - 1 NEW_LINE DEDENT start = 0 NEW_LINE end = n - 1 NEW_LINE while ( start < end ) : NEW_LINE INDENT mid = ( start + end + 1 ) // 2 NEW_LINE if ( arr [ mid ] >= value ) : NEW_LINE INDENT end = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT start = mid NEW_LINE DEDENT DEDENT return start NEW_LINE DEDENT def countShift ( arr , n , queries ) : NEW_LINE INDENT for q in queries : NEW_LINE INDENT index = q [ 0 ] NEW_LINE update = q [ 1 ] NEW_LINE newElement = arr [ index ] + update NEW_LINE newIndex = computePos ( arr , n , newElement ) NEW_LINE print ( abs ( newIndex - index ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 5 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE queries = [ [ 0 , - 1 ] , [ 4 , - 11 ] ] NEW_LINE countShift ( arr , N , queries ) NEW_LINE DEDENT
Count nodes having smallest value in the path from root to itself in a Binary Tree | Python3 program for the above approach ; Class of a tree node ; Function to create new tree node ; Function to find the total number of required nodes ; If current node is null then return to the parent node ; Check if current node value is less than or equal to minNodeVal ; Update the value of minNodeVal ; Update the count ; Go to the left subtree ; Go to the right subtree ; Driver Code ; Binary Tree creation 8 / \ / \ 6 5 / \ / \ / \ / \ 6 7 3 9 ; Function Call ; Print the result
import sys NEW_LINE ans = 0 NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . key = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def countReqNodes ( root , minNodeVal ) : NEW_LINE INDENT global ans NEW_LINE if root == None : NEW_LINE INDENT return NEW_LINE DEDENT if root . key <= minNodeVal : NEW_LINE INDENT minNodeVal = root . key NEW_LINE ans += 1 NEW_LINE DEDENT countReqNodes ( root . left , minNodeVal ) NEW_LINE countReqNodes ( root . right , minNodeVal ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = Node ( 8 ) NEW_LINE root . left = Node ( 6 ) NEW_LINE root . right = Node ( 5 ) NEW_LINE root . left . left = Node ( 6 ) NEW_LINE root . left . right = Node ( 7 ) NEW_LINE root . right . left = Node ( 3 ) NEW_LINE root . right . right = Node ( 9 ) NEW_LINE minNodeVal = sys . maxsize NEW_LINE countReqNodes ( root , minNodeVal ) NEW_LINE print ( ans ) NEW_LINE DEDENT
Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings | Function to check if a string is palindrome or not ; Start and end of the string ; Iterate the string until i > j ; If there is a mismatch ; Increment first pointer and decrement the other ; Given string is a palindrome ; Function two check if the strings can be combined to form a palindrome ; Initialize array of characters ; Stores character of string in the character array ; Find left and right parts of strings a and b ; Substring a [ j ... i - 1 ] ; Substring b [ j ... i - 1 ] ; Substring a [ i ... n ] ; Substring b [ i ... n ] ; Check is left part of a + right part of b or vice versa is a palindrome ; Print the result ; Otherwise ; Driver Code
def isPalindrome ( st ) : NEW_LINE INDENT i = 0 NEW_LINE j = len ( st ) - 1 NEW_LINE while ( i < j ) : NEW_LINE INDENT if ( st [ i ] != st [ j ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def formPalindrome ( a , b , n ) : NEW_LINE INDENT aa = [ ' ▁ ' ] * ( n + 2 ) NEW_LINE bb = [ ' ▁ ' ] * ( n + 2 ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT aa [ i ] = a [ i - 1 ] NEW_LINE bb [ i ] = b [ i - 1 ] NEW_LINE DEDENT ok = False NEW_LINE for i in range ( n + 2 ) : NEW_LINE INDENT la = " " NEW_LINE ra = " " NEW_LINE lb = " " NEW_LINE rb = " " NEW_LINE for j in range ( 1 , i ) : NEW_LINE INDENT if ( aa [ j ] == ' ▁ ' ) : NEW_LINE INDENT la += " " NEW_LINE DEDENT else : NEW_LINE INDENT la += aa [ j ] NEW_LINE DEDENT if ( bb [ j ] == ' ▁ ' ) : NEW_LINE INDENT lb += " " NEW_LINE DEDENT else : NEW_LINE INDENT lb += bb [ j ] NEW_LINE DEDENT DEDENT for j in range ( i , n + 2 ) : NEW_LINE INDENT if ( aa [ j ] == ' ▁ ' ) : NEW_LINE INDENT ra += " " NEW_LINE DEDENT else : NEW_LINE INDENT ra += aa [ j ] NEW_LINE DEDENT if ( bb [ j ] == ' ▁ ' ) : NEW_LINE INDENT rb += " " NEW_LINE DEDENT else : NEW_LINE INDENT rb += bb [ j ] NEW_LINE DEDENT DEDENT if ( isPalindrome ( str ( la ) + str ( rb ) ) or isPalindrome ( str ( lb ) + str ( ra ) ) ) : NEW_LINE INDENT ok = True NEW_LINE break NEW_LINE DEDENT DEDENT if ( ok ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = " bdea " NEW_LINE B = " abbb " NEW_LINE N = 4 NEW_LINE formPalindrome ( A , B , N ) NEW_LINE DEDENT
Minimize elements to be added to a given array such that it contains another given array as its subsequence | Set 2 | Function to return minimum element to be added in array B so that array A become subsequence of array B ; Stores indices of the array elements ; Iterate over the array ; Store the indices of the array elements ; Stores the LIS ; Check if element B [ i ] is in array A [ ] ; Perform Binary Search ; Find the value of mid m ; Update l and r ; If found better element ' e ' for pos r + 1 ; Otherwise , extend the current subsequence ; Return the answer ; Driver Code ; Given arrays ; Function call
def minElements ( A , B , N , M ) : NEW_LINE INDENT map = { } NEW_LINE for i in range ( len ( A ) ) : NEW_LINE INDENT map [ A [ i ] ] = i NEW_LINE DEDENT subseq = [ ] NEW_LINE l = 0 NEW_LINE r = - 1 NEW_LINE for i in range ( M ) : NEW_LINE INDENT if B [ i ] in map : NEW_LINE INDENT e = map [ B [ i ] ] NEW_LINE while ( l <= r ) : NEW_LINE INDENT m = l + ( r - l ) // 2 NEW_LINE if ( subseq [ m ] < e ) : NEW_LINE INDENT l = m + 1 NEW_LINE DEDENT else : NEW_LINE INDENT r = m - 1 NEW_LINE DEDENT DEDENT if ( r + 1 < len ( subseq ) ) : NEW_LINE INDENT subseq [ r + 1 ] = e NEW_LINE DEDENT else : NEW_LINE INDENT subseq . append ( e ) NEW_LINE DEDENT l = 0 NEW_LINE r = len ( subseq ) - 1 NEW_LINE DEDENT DEDENT return N - len ( subseq ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE B = [ 2 , 5 , 6 , 4 , 9 , 12 ] NEW_LINE M = len ( A ) NEW_LINE N = len ( B ) NEW_LINE print ( minElements ( A , B , M , N ) ) NEW_LINE DEDENT
Detect a negative cycle in a Graph using Shortest Path Faster Algorithm | Python3 program for the above approach ; Stores the adjacency list of the given graph ; Create Adjacency List ; Stores the distance of all reachable vertex from source ; Check if vertex is present in queue or not ; Counts the relaxation for each vertex ; Distance from src to src is 0 ; Create a queue ; Push source in the queue ; Mark source as visited ; Front vertex of Queue ; Relaxing all edges of vertex from the Queue ; Update the dist [ v ] to minimum distance ; If vertex v is in Queue ; Negative cycle ; No cycle found ; Driver Code ; Number of vertices ; Given source node src ; Number of Edges ; Given Edges with weight ; If cycle is present
import sys NEW_LINE def sfpa ( V , src , Edges , M ) : NEW_LINE INDENT g = [ [ ] for i in range ( V ) ] NEW_LINE for i in range ( M ) : NEW_LINE INDENT u = Edges [ i ] [ 0 ] NEW_LINE v = Edges [ i ] [ 1 ] NEW_LINE w = Edges [ i ] [ 2 ] NEW_LINE g [ u ] . append ( [ v , w ] ) NEW_LINE DEDENT dist = [ sys . maxsize for i in range ( V ) ] NEW_LINE inQueue = [ False for i in range ( V ) ] NEW_LINE cnt = [ 0 for i in range ( V ) ] NEW_LINE dist [ src ] = 0 NEW_LINE q = [ ] NEW_LINE q . append ( src ) NEW_LINE inQueue [ src ] = True NEW_LINE while ( len ( q ) ) : NEW_LINE INDENT u = q [ 0 ] NEW_LINE q . remove ( q [ 0 ] ) NEW_LINE inQueue [ u ] = False NEW_LINE for x in g [ u ] : NEW_LINE INDENT v = x [ 0 ] NEW_LINE cost = x [ 1 ] NEW_LINE if ( dist [ v ] > dist [ u ] + cost ) : NEW_LINE INDENT dist [ v ] = dist [ u ] + cost NEW_LINE if ( inQueue [ v ] == False ) : NEW_LINE INDENT q . append ( v ) NEW_LINE inQueue [ v ] = True NEW_LINE cnt [ v ] += 1 NEW_LINE if ( cnt [ v ] >= V ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE src = 0 NEW_LINE M = 4 NEW_LINE Edges = [ [ 0 , 1 , 1 ] , [ 1 , 2 , - 1 ] , [ 2 , 3 , - 1 ] , [ 3 , 0 , - 1 ] ] NEW_LINE if ( sfpa ( N , src , Edges , M ) == True ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Sum of all pair shortest paths in a Tree | Python3 program for the above approach ; Function that performs the Floyd Warshall to find all shortest paths ; Initialize the distance matrix ; Pick all vertices as source one by one ; Pick all vertices as destination for the above picked source ; If vertex k is on the shortest path from i to j then update dist [ i ] [ j ] ; Sum the upper diagonal of the shortest distance matrix ; Traverse the given dist [ ] [ ] ; Add the distance ; Return the final sum ; Function to generate the tree ; Add edges ; Get source and destination with weight ; Add the edges ; Perform Floyd Warshal Algorithm ; Driver code ; Number of Vertices ; Number of Edges ; Given Edges with weight ; Function Call
INF = 99999 NEW_LINE def floyd_warshall ( graph , V ) : NEW_LINE INDENT dist = [ [ 0 for i in range ( V ) ] for i in range ( V ) ] NEW_LINE for i in range ( V ) : NEW_LINE INDENT for j in range ( V ) : NEW_LINE INDENT dist [ i ] [ j ] = graph [ i ] [ j ] NEW_LINE DEDENT DEDENT for k in range ( V ) : NEW_LINE INDENT for i in range ( V ) : NEW_LINE INDENT for j in range ( V ) : NEW_LINE INDENT if ( dist [ i ] [ k ] + dist [ k ] [ j ] < dist [ i ] [ j ] ) : NEW_LINE INDENT dist [ i ] [ j ] = dist [ i ] [ k ] + dist [ k ] [ j ] NEW_LINE DEDENT DEDENT DEDENT DEDENT sum = 0 NEW_LINE for i in range ( V ) : NEW_LINE INDENT for j in range ( i + 1 , V ) : NEW_LINE INDENT sum += dist [ i ] [ j ] NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT def sumOfshortestPath ( N , E , edges ) : NEW_LINE INDENT g = [ [ INF for i in range ( N ) ] for i in range ( N ) ] NEW_LINE for i in range ( E ) : NEW_LINE INDENT u = edges [ i ] [ 0 ] NEW_LINE v = edges [ i ] [ 1 ] NEW_LINE w = edges [ i ] [ 2 ] NEW_LINE g [ u ] [ v ] = w NEW_LINE g [ v ] [ u ] = w NEW_LINE DEDENT return floyd_warshall ( g , N ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE E = 3 NEW_LINE Edges = [ [ 0 , 1 , 1 ] , [ 1 , 2 , 2 ] , [ 2 , 3 , 3 ] ] NEW_LINE print ( sumOfshortestPath ( N , E , Edges ) ) NEW_LINE DEDENT
Kth space | Function to prkth integer in a given string ; Size of the string ; If space char found decrement K ; If K becomes 1 , the next string is the required one ; Print required number ; Driver Code ; Given string ; Given K ; Function call
def print_kth_string ( s , K ) : NEW_LINE INDENT N = len ( s ) ; NEW_LINE for i in range ( 0 , N , 1 ) : NEW_LINE INDENT if ( s [ i ] == ' ▁ ' ) : NEW_LINE INDENT K -= 1 ; NEW_LINE DEDENT if ( K == 1 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT while ( i < N ) : NEW_LINE INDENT if ( s [ i ] != ' ▁ ' ) : NEW_LINE INDENT print ( s [ i ] , end = " " ) ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "10 ▁ 20 ▁ 30 ▁ 40" ; NEW_LINE K = 4 ; NEW_LINE print_kth_string ( s , K ) ; NEW_LINE DEDENT