codeparrot/xlcost-text-to-code huggingface dataset:
How to validate an IP address using ReGex | Python3 program to validate IP address using Regex ; Function for Validating IP ; Regex expression for validating IPv4 ; Regex expression for validating IPv6 ; Checking if it is a valid IPv4 addresses ; Checking if it is a valid IPv6 addresses ; Return Invalid ; IP addresses to validate
import re NEW_LINE def Validate_It ( IP ) : NEW_LINE INDENT regex = " ( ( [ 0-9 ] ▁ [ 1-9 ] [ 0-9 ] ▁ 1[0-9 ] [ 0-9 ] ▁ " \ "2[0-4 ] [ 0-9 ] ▁ 25[0-5 ] ) \\ . ) { 3 } " " ( [0-9 ] ▁ [ 1-9 ] [ 0-9 ] ▁ 1[0-9 ] [ 0-9 ] ▁ " \ "2[0-4 ] [ 0-9 ] ▁ 25[0-5 ] ) " NEW_LINE regex1 = " ( ( ( [0-9a - fA - F ] ) { 1,4 } ) \\ : ) { 7 } " p = re . compile ( regex ) NEW_LINE p1 = re . compile ( regex1 ) NEW_LINE if ( re . search ( p , IP ) ) : NEW_LINE INDENT return " Valid ▁ IPv4" NEW_LINE DEDENT elif ( re . search ( p1 , IP ) ) : NEW_LINE INDENT return " Valid ▁ IPv6" NEW_LINE DEDENT return " Invalid ▁ IP " NEW_LINE DEDENT IP = "203.120.223.13" NEW_LINE print ( Validate_It ( IP ) ) NEW_LINE IP = " fffe : 3465 : efab : 23fe : 2235:6565 : aaab : 0001" NEW_LINE print ( Validate_It ( IP ) ) NEW_LINE IP = "2F33:12a0:3Ea0:0302" NEW_LINE print ( Validate_It ( IP ) ) NEW_LINE
Count of prime factors of N to be added at each step to convert N to M | Array to store shortest prime factor of every integer ; Function to precompute shortest prime factors ; Function to append distinct prime factors of every integer into a set ; Store distinct prime factors ; Function to return minimum steps using BFS ; Queue of pairs to store the current number and distance from root . ; Set to store distinct prime factors ; Run BFS ; Find out the prime factors of newNum ; Iterate over every prime factor of newNum . ; If M is obtained ; Return number of operations ; If M is exceeded ; Otherwise ; Update and store the new number obtained by prime factor ; If M cannot be obtained ; Driver code
spf = [ - 1 for i in range ( 100009 ) ] ; NEW_LINE def sieve ( ) : NEW_LINE INDENT i = 2 NEW_LINE while ( i * i <= 100006 ) : NEW_LINE INDENT for j in range ( i , 100006 , i ) : NEW_LINE INDENT if ( spf [ j ] == - 1 ) : NEW_LINE INDENT spf [ j ] = i ; NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT DEDENT def findPrimeFactors ( s , n ) : NEW_LINE INDENT while ( n > 1 ) : NEW_LINE INDENT s . add ( spf [ n ] ) ; NEW_LINE n //= spf [ n ] ; NEW_LINE DEDENT return s ; NEW_LINE DEDENT def MinimumSteps ( n , m ) : NEW_LINE INDENT q = [ ] NEW_LINE s = set ( ) NEW_LINE q . append ( [ n , 0 ] ) NEW_LINE while ( len ( q ) != 0 ) : NEW_LINE INDENT newNum = q [ 0 ] [ 0 ] NEW_LINE distance = q [ 0 ] [ 1 ] NEW_LINE q . pop ( 0 ) ; NEW_LINE k = findPrimeFactors ( s , newNum ) ; NEW_LINE for i in k : NEW_LINE INDENT if ( newNum == m ) : NEW_LINE INDENT return distance ; NEW_LINE DEDENT elif ( newNum > m ) : NEW_LINE INDENT break ; NEW_LINE DEDENT else : NEW_LINE INDENT q . append ( [ newNum + i , distance + 1 ] ) ; NEW_LINE DEDENT DEDENT DEDENT return - 1 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 7 NEW_LINE M = 16 ; NEW_LINE sieve ( ) ; NEW_LINE print ( MinimumSteps ( N , M ) ) NEW_LINE DEDENT
Count of elements which is product of a pair or an element square | Python3 program to implement the above approach ; Stores all factors a number ; Function to calculate and store in a vector ; Function to return the count of array elements which are a product of two array elements ; Copy elements into a a duplicate array ; Sort the duplicate array ; Store the count of elements ; If the factors are not calculated already ; Traverse its factors ; If a pair of factors is found ; Driver Code
import math NEW_LINE v = [ [ ] for i in range ( 100000 ) ] NEW_LINE def div ( n ) : NEW_LINE INDENT global v NEW_LINE for i in range ( 2 , int ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT v [ n ] . append ( i ) NEW_LINE DEDENT DEDENT DEDENT def prodof2elements ( arr , n ) : NEW_LINE INDENT arr2 = arr . copy ( ) NEW_LINE arr2 . sort ( ) NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( len ( v [ arr [ i ] ] ) == 0 ) : NEW_LINE INDENT div ( arr [ i ] ) NEW_LINE DEDENT for j in v [ arr [ i ] ] : NEW_LINE INDENT if j in arr2 : NEW_LINE INDENT if int ( arr [ i ] / j ) in arr2 : NEW_LINE INDENT ans += 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 2 , 1 , 8 , 4 , 32 , 18 ] NEW_LINE N = len ( arr ) NEW_LINE print ( prodof2elements ( arr , N ) ) NEW_LINE
Largest subarray with frequency of all elements same | Function to find maximum subarray size ; Generating all subarray i -> starting index j -> end index ; Map 1 to hash frequency of all elements in subarray ; Map 2 to hash frequency of all frequencies of elements ; Finding previous frequency of arr [ j ] in map 1 ; Increasing frequency of arr [ j ] by 1 ; Check if previous frequency is present in map 2 ; Delete previous frequency if hash is equal to 1 ; Decrement the hash of previous frequency ; Incrementing hash of new frequency in map 2 ; Check if map2 size is 1 and updating answer ; Return the maximum size of subarray ; Given array arr [ ] ; Function Call
def max_subarray_size ( N , arr ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT map1 = { } NEW_LINE map2 = { } NEW_LINE for j in range ( i , N ) : NEW_LINE INDENT if ( arr [ j ] not in map1 ) : NEW_LINE INDENT ele_count = 0 NEW_LINE DEDENT else : NEW_LINE INDENT ele_count = map1 [ arr [ j ] ] NEW_LINE DEDENT if arr [ j ] in map1 : NEW_LINE INDENT map1 [ arr [ j ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT map1 [ arr [ j ] ] = 1 NEW_LINE DEDENT if ( ele_count in map2 ) : NEW_LINE INDENT if ( map2 [ ele_count ] == 1 ) : NEW_LINE INDENT del map2 [ ele_count ] NEW_LINE DEDENT else : NEW_LINE INDENT map2 [ ele_count ] -= 1 NEW_LINE DEDENT DEDENT if ele_count + 1 in map2 : NEW_LINE INDENT map2 [ ele_count + 1 ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT map2 [ ele_count + 1 ] = 1 NEW_LINE DEDENT if ( len ( map2 ) == 1 ) : NEW_LINE INDENT ans = max ( ans , j - i + 1 ) NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 2 , 2 , 5 , 6 , 5 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE print ( max_subarray_size ( N , arr ) ) NEW_LINE
Longest substring consisting of vowels using Binary Search | Function to check if a character is vowel or not ; 0 - a 1 - b 2 - c and so on 25 - z ; Function to check if any substring of length k exists which contains only vowels ; Applying sliding window to get all substrings of length K ; Remove the occurence of ( i - k + 1 ) th character ; Function to perform Binary Search ; Doing binary search on the lengths ; Driver Code
def vowel ( vo ) : NEW_LINE INDENT if ( vo == 0 or vo == 4 or vo == 8 or vo == 14 or vo == 20 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def check ( s , k ) : NEW_LINE INDENT cnt = [ 0 ] * 26 NEW_LINE for i in range ( k - 1 ) : NEW_LINE INDENT cnt [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( k - 1 , len ( s ) ) : NEW_LINE INDENT cnt [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE flag1 = 0 NEW_LINE for j in range ( 26 ) : NEW_LINE INDENT if ( vowel ( j ) == False and cnt [ j ] > 0 ) : NEW_LINE INDENT flag1 = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag1 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT cnt [ ord ( s [ i - k + 1 ] ) - ord ( ' a ' ) ] -= 1 NEW_LINE DEDENT return False NEW_LINE DEDENT def longestSubstring ( s ) : NEW_LINE INDENT l = 1 NEW_LINE r = len ( s ) NEW_LINE maxi = 0 NEW_LINE while ( l <= r ) : NEW_LINE INDENT mid = ( l + r ) // 2 NEW_LINE if ( check ( s , mid ) ) : NEW_LINE INDENT l = mid + 1 NEW_LINE maxi = max ( maxi , mid ) NEW_LINE DEDENT else : NEW_LINE INDENT r = mid - 1 NEW_LINE DEDENT DEDENT return maxi NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " sedrewaefhoiu " NEW_LINE print ( longestSubstring ( s ) ) NEW_LINE DEDENT
Find number of pairs ( x , y ) in an Array such that x ^ y > y ^ x | Set 2 | Function to return the count of pairs ; Compute suffix sums till i = 3 ; Base Case : x = 0 ; No valid pairs ; Base Case : x = 1 ; Store the count of 0 's ; Base Case : x = 2 ; Store suffix sum upto 5 ; Base Case : x = 3 ; Store count of 2 and suffix sum upto 4 ; For all other values of x ; For all x >= 2 , every y = 0 and every y = 1 makes a valid pair ; Return the count of pairs ; Driver code
def countPairs ( X , Y , m , n ) : NEW_LINE INDENT suffix = [ 0 ] * 1005 NEW_LINE total_pairs = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT suffix [ Y [ i ] ] += 1 NEW_LINE DEDENT for i in range ( int ( 1e3 ) , 2 , - 1 ) : NEW_LINE INDENT suffix [ i ] += suffix [ i + 1 ] NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT if ( X [ i ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT elif ( X [ i ] == 1 ) : NEW_LINE INDENT total_pairs += suffix [ 0 ] NEW_LINE continue NEW_LINE DEDENT elif ( X [ i ] == 2 ) : NEW_LINE INDENT total_pairs += suffix [ 5 ] NEW_LINE DEDENT elif ( X [ i ] == 3 ) : NEW_LINE INDENT total_pairs += ( suffix [ 2 ] + suffix [ 4 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT total_pairs += suffix [ X [ i ] + 1 ] NEW_LINE DEDENT total_pairs += suffix [ 0 ] + suffix [ 1 ] NEW_LINE DEDENT return total_pairs NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = [ 10 , 19 , 18 ] NEW_LINE Y = [ 11 , 15 , 9 ] NEW_LINE m = len ( X ) NEW_LINE n = len ( Y ) NEW_LINE print ( countPairs ( X , Y , m , n ) ) NEW_LINE DEDENT
Min number of moves to traverse entire Matrix through connected cells with equal values | Function to find the minimum number of moves to traverse the given matrix ; Constructing another matrix consisting of distinct values ; Updating the array B by checking the values of A that if there are same values connected through an edge or not ; Check for boundary condition of the matrix ; If adjacent cells have same value ; Check for boundary condition of the matrix ; If adjacent cells have same value ; Check for boundary condition of the matrix ; If adjacent cells have same value ; Check for boundary condition of the matrix ; If adjacent cells have same value ; Store all distinct elements in a set ; Return answer ; Driver code ; Function call
def solve ( A , N , M ) : NEW_LINE INDENT B = [ ] NEW_LINE c = 1 NEW_LINE s = set ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT new = [ ] NEW_LINE for j in range ( M ) : NEW_LINE INDENT new . append ( c ) NEW_LINE c = c + 1 NEW_LINE DEDENT B . append ( new ) NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT if i != 0 : NEW_LINE INDENT if A [ i - 1 ] [ j ] == A [ i ] [ j ] : NEW_LINE INDENT B [ i - 1 ] [ j ] = B [ i ] [ j ] NEW_LINE DEDENT DEDENT if ( i != N - 1 ) : NEW_LINE INDENT if A [ i + 1 ] [ j ] == A [ i ] [ j ] : NEW_LINE INDENT B [ i + 1 ] [ j ] = B [ i ] [ j ] NEW_LINE DEDENT DEDENT if ( j != 0 ) : NEW_LINE INDENT if A [ i ] [ j - 1 ] == A [ i ] [ j ] : NEW_LINE INDENT B [ i ] [ j - 1 ] = B [ i ] [ j ] NEW_LINE DEDENT DEDENT if ( j != M - 1 ) : NEW_LINE INDENT if ( A [ i ] [ j + 1 ] == A [ i ] [ j ] ) : NEW_LINE INDENT B [ i ] [ j + 1 ] = B [ i ] [ j ] NEW_LINE DEDENT DEDENT DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT s . add ( B [ i ] [ j ] ) NEW_LINE DEDENT DEDENT return len ( s ) NEW_LINE DEDENT N = 2 NEW_LINE M = 3 NEW_LINE A = [ [ 2 , 1 , 3 ] , [ 1 , 1 , 2 ] ] NEW_LINE print ( solve ( A , N , M ) ) NEW_LINE
Number of times an array can be partitioned repetitively into two subarrays with equal sum | Recursion Function to calculate the possible splitting ; If there are less than two elements , we cannot partition the sub - array . ; Iterate from the start to end - 1. ; Recursive call to the left and the right sub - array . ; If there is no such partition , then return 0 ; Function to find the total splitting ; Prefix array to store the prefix - sum using 1 based indexing ; Store the prefix - sum ; Function Call to count the number of splitting ; Given array ; Function call
def splitArray ( start , end , arr , prefix_sum ) : NEW_LINE INDENT if ( start >= end ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT for k in range ( start , end ) : NEW_LINE INDENT if ( ( prefix_sum [ k ] - prefix_sum [ start - 1 ] ) == ( prefix_sum [ end ] - prefix_sum [ k ] ) ) : NEW_LINE INDENT return ( 1 + splitArray ( start , k , arr , prefix_sum ) + splitArray ( k + 1 , end , arr , prefix_sum ) ) NEW_LINE DEDENT DEDENT return 0 NEW_LINE DEDENT def solve ( arr , n ) : NEW_LINE INDENT prefix_sum = [ 0 ] * ( n + 1 ) NEW_LINE prefix_sum [ 0 ] = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT prefix_sum [ i ] = ( prefix_sum [ i - 1 ] + arr [ i - 1 ] ) NEW_LINE DEDENT print ( splitArray ( 1 , n , arr , prefix_sum ) ) NEW_LINE DEDENT arr = [ 12 , 3 , 3 , 0 , 3 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE solve ( arr , N ) NEW_LINE
Minimise the maximum element of N subarrays of size K | Function to choose N subarrays of size K such that the maximum element of subarrays is minimum ; Condition to check if it is not possible to choose k sized N subarrays ; Using binary search ; calculating mid ; Loop to find the count of the K sized subarrays possible with elements less than mid ; Condition to check if the answer is in right subarray ; Driver Code ; Function Call
def minDays ( arr , n , k ) : NEW_LINE INDENT l = len ( arr ) NEW_LINE left = 1 NEW_LINE right = 1e9 NEW_LINE if ( n * k > l ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT while ( left < right ) : NEW_LINE INDENT mid = ( left + right ) // 2 NEW_LINE cnt = 0 NEW_LINE product = 0 NEW_LINE for j in range ( l ) : NEW_LINE INDENT if ( arr [ j ] > mid ) : NEW_LINE INDENT cnt = 0 NEW_LINE DEDENT else : NEW_LINE INDENT cnt += 1 NEW_LINE if ( cnt >= k ) : NEW_LINE INDENT product += 1 NEW_LINE cnt = 0 NEW_LINE DEDENT DEDENT DEDENT if ( product < n ) : NEW_LINE INDENT left = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT right = mid NEW_LINE DEDENT DEDENT return left NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 10 , 3 , 10 , 2 ] NEW_LINE n = 3 NEW_LINE k = 1 NEW_LINE print ( int ( minDays ( arr , n , k ) ) ) NEW_LINE DEDENT
Count of distinct power of prime factor of N | Function to count the number of distinct positive power of prime factor of integer N ; Iterate for all prime factor ; If it is a prime factor , count the total number of times it divides n . ; Find the Number of distinct possible positive numbers ; Return the final count ; Given number N ; Function call
def countFac ( n ) : NEW_LINE INDENT m = n NEW_LINE count = 0 NEW_LINE i = 2 NEW_LINE while ( ( i * i ) <= m ) : NEW_LINE INDENT total = 0 NEW_LINE while ( n % i == 0 ) : NEW_LINE INDENT n /= i NEW_LINE total += 1 NEW_LINE DEDENT temp = 0 NEW_LINE j = 1 NEW_LINE while ( ( temp + j ) <= total ) : NEW_LINE INDENT temp += j NEW_LINE count += 1 NEW_LINE j += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if ( n != 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT N = 24 NEW_LINE print ( countFac ( N ) ) NEW_LINE
Minimum total sum from the given two arrays | Function that prints minimum sum after selecting N elements ; Initialise the dp array ; Base Case ; Adding the element of array a if previous element is also from array a ; Adding the element of array a if previous element is from array b ; Adding the element of array b if previous element is from array a with an extra penalty of integer C ; Adding the element of array b if previous element is also from array b ; Print the minimum sum ; Driver code ; Given array arr [ ] ; Given cost ; Function Call
def minimumSum ( a , b , c , n ) : NEW_LINE INDENT dp = [ [ 1e6 for i in range ( 2 ) ] for j in range ( n ) ] NEW_LINE dp [ 0 ] [ 0 ] = a [ 0 ] NEW_LINE dp [ 0 ] [ 1 ] = b [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT dp [ i ] [ 0 ] = min ( dp [ i ] [ 0 ] , dp [ i - 1 ] [ 0 ] + a [ i ] ) NEW_LINE dp [ i ] [ 0 ] = min ( dp [ i ] [ 0 ] , dp [ i - 1 ] [ 1 ] + a [ i ] + c ) NEW_LINE dp [ i ] [ 1 ] = min ( dp [ i ] [ 1 ] , dp [ i - 1 ] [ 0 ] + b [ i ] + c ) NEW_LINE dp [ i ] [ 1 ] = min ( dp [ i ] [ 1 ] , dp [ i - 1 ] [ 1 ] + b [ i ] ) NEW_LINE DEDENT print ( min ( dp [ n - 1 ] [ 0 ] , dp [ n - 1 ] [ 1 ] ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 7 , 6 , 18 , 6 , 16 , 18 , 1 , 17 , 17 ] NEW_LINE arr2 = [ 6 , 9 , 3 , 10 , 9 , 1 , 10 , 1 , 5 ] NEW_LINE C = 2 NEW_LINE N = len ( arr1 ) NEW_LINE minimumSum ( arr1 , arr2 , C , N ) NEW_LINE DEDENT
Minimum flips required in a binary string such that all K | Function to calculate and return the minimum number of flips to make string valid ; Stores the count of required flips ; Stores the last index of '1' in the string ; Check for the first substring of length K ; If i - th character is '1 ; If the substring had no '1 ; Increase the count of required flips ; Flip the last index of the window ; Update the last index which contains 1 ; Check for remaining substrings ; If last_idx does not belong to current window make it - 1 ; If the last character of the current substring is '1' , then update last_idx to i + k - 1 ; ; If last_idx == - 1 , then the current substring has no 1 ; Increase the count of flips ; Update the last index of the current window ; Store the last index of current window as the index of last '1' in the string ; Return the number of operations ; Driver Code
def minimumMoves ( S , K ) : NEW_LINE INDENT N = len ( S ) NEW_LINE ops = 0 NEW_LINE last_idx = - 1 NEW_LINE for i in range ( K ) : NEW_LINE DEDENT ' NEW_LINE INDENT if ( S [ i ] == '1' ) : NEW_LINE INDENT last_idx = i NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if ( last_idx == - 1 ) : NEW_LINE INDENT ops += 1 NEW_LINE S [ K - 1 ] = '1' NEW_LINE last_idx = K - 1 NEW_LINE DEDENT for i in range ( N - K + 1 ) : NEW_LINE INDENT if ( last_idx < i ) : NEW_LINE INDENT last_idx = - 1 NEW_LINE DEDENT if ( S [ i + K - 1 ] == '1' ) : NEW_LINE INDENT last_idx = i + K - 1 NEW_LINE DEDENT if ( last_idx == - 1 ) : NEW_LINE INDENT ops += 1 NEW_LINE S = S [ : i + K - 1 ] + '1' + S [ i + K : ] NEW_LINE last_idx = i + K - 1 NEW_LINE DEDENT DEDENT return ops NEW_LINE DEDENT S = "001010000" NEW_LINE K = 3 ; NEW_LINE print ( minimumMoves ( S , K ) ) NEW_LINE
Lexicographically smallest string which differs from given strings at exactly K indices | Python3 program for the above approach ; Function to find the string which differ at exactly K positions ; Initialise s3 as s2 ; Number of places at which s3 differ from s2 ; Minimum possible value is ceil ( d / 2 ) if it is not possible then - 1 ; Case 2 when K is less equal d ; X show the modification such that this position only differ from only one string ; Modify the position such that this differ from both S1 & S2 & decrease the T at each step ; Finding the character which is different from both S1 and S2 ; After we done T start modification to meet our requirement for X type ; Resultant string ; Case 1 when K > d In first step , modify all the character which are not same in S1 and S3 ; Finding character which is different from both S1 and S2 ; Our requirement not satisfied by performing step 1. We need to modify the position which matches in both string ; Finding the character which is different from both S1 and S2 ; Resultant string ; Driver Code ; Given two strings ; Function call
arr = [ ' a ' , ' b ' , ' c ' ] NEW_LINE def findString ( n , k , s1 , s2 ) : NEW_LINE INDENT s3 = s2 NEW_LINE s3 = list ( s3 ) NEW_LINE d = 0 NEW_LINE for i in range ( len ( s1 ) ) : NEW_LINE INDENT if ( s1 [ i ] != s2 [ i ] ) : NEW_LINE INDENT d += 1 NEW_LINE DEDENT DEDENT if ( ( d + 1 ) // 2 > k ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT else : NEW_LINE INDENT if ( k <= d ) : NEW_LINE INDENT X = d - k NEW_LINE T = 2 * k - d NEW_LINE for i in range ( len ( s3 ) ) : NEW_LINE INDENT if ( s1 [ i ] != s2 [ i ] ) : NEW_LINE INDENT if ( T > 0 ) : NEW_LINE INDENT for j in range ( 3 ) : NEW_LINE INDENT if ( arr [ j ] != s1 [ i ] and arr [ j ] != s2 [ i ] ) : NEW_LINE INDENT s3 [ i ] = arr [ j ] NEW_LINE T -= 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT elif ( X > 0 ) : NEW_LINE INDENT s3 [ i ] = s1 [ i ] NEW_LINE X -= 1 NEW_LINE DEDENT DEDENT DEDENT print ( " " . join ( s3 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( len ( s1 ) ) : NEW_LINE INDENT if ( s1 [ i ] != s3 [ i ] ) : NEW_LINE INDENT for j in range ( 3 ) : NEW_LINE INDENT if ( arr [ j ] != s1 [ i ] and arr [ j ] != s3 [ i ] ) : NEW_LINE INDENT s3 [ i ] = arr [ j ] NEW_LINE k -= 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT for i in range ( len ( s1 ) ) : NEW_LINE INDENT if ( s1 [ i ] == s3 [ i ] and k ) : NEW_LINE INDENT for j in range ( 3 ) : NEW_LINE INDENT if ( arr [ j ] != s1 [ i ] and arr [ j ] != s3 [ i ] ) : NEW_LINE INDENT s3 [ i ] = arr [ j ] NEW_LINE k -= 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT print ( " " . join ( s3 ) ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE k = 2 NEW_LINE S1 = " zzyy " NEW_LINE S2 = " zxxy " NEW_LINE findString ( N , k , S1 , S2 ) NEW_LINE DEDENT
Count of pairs of integers whose difference of squares is equal to N | Python3 program for the above approach ; Function to find the integral solutions of the given equation ; Initialise count to 0 ; Iterate till sqrt ( N ) ; If divisor 's pair sum is even ; Print the total possible solutions ; Given number N ; Function Call
import math ; NEW_LINE def findSolutions ( N ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( 1 , int ( math . sqrt ( N ) ) + 1 ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT if ( ( i + N // i ) % 2 == 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT DEDENT print ( 4 * count ) ; NEW_LINE DEDENT N = 80 ; NEW_LINE findSolutions ( N ) ; NEW_LINE
Find the integral roots of a given Cubic equation | Function to find the value at x of the given equation ; Find the value equation at x ; Return the value of ans ; Function to find the integral solution of the given equation ; Initialise start and end ; Implement Binary Search ; Find mid ; Find the value of f ( x ) using current mid ; Check if current mid satisfy the equation ; Print mid and return ; Print " NA " if not found any integral solution ; Driver Code ; Function Call
def check ( A , B , C , D , x ) : NEW_LINE INDENT ans = 0 ; NEW_LINE ans = ( A * x * x * x + B * x * x + C * x + D ) ; NEW_LINE return ans ; NEW_LINE DEDENT def findSolution ( A , B , C , D , E ) : NEW_LINE INDENT start = 0 ; end = 100000 ; NEW_LINE mid = 0 ; NEW_LINE ans = 0 ; NEW_LINE while ( start <= end ) : NEW_LINE INDENT mid = start + ( end - start ) // 2 ; NEW_LINE ans = check ( A , B , C , D , mid ) ; NEW_LINE if ( ans == E ) : NEW_LINE INDENT print ( mid ) ; NEW_LINE return ; NEW_LINE DEDENT if ( ans < E ) : NEW_LINE INDENT start = mid + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT end = mid - 1 ; NEW_LINE DEDENT DEDENT print ( " NA " ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = 1 ; B = 0 ; C = 0 ; NEW_LINE D = 0 ; E = 27 ; NEW_LINE findSolution ( A , B , C , D , E ) ; NEW_LINE DEDENT
Count of ordered triplets ( R , G , B ) in a given original string | function to count the ordered triplets ( R , G , B ) ; count the B ( blue ) colour ; Driver Code
def countTriplets ( color ) : NEW_LINE INDENT result = 0 ; Blue_Count = 0 ; NEW_LINE Red_Count = 0 ; NEW_LINE for c in color : NEW_LINE INDENT if ( c == ' B ' ) : NEW_LINE INDENT Blue_Count += 1 ; NEW_LINE DEDENT DEDENT for c in color : NEW_LINE INDENT if ( c == ' B ' ) : NEW_LINE INDENT Blue_Count -= 1 ; NEW_LINE DEDENT if ( c == ' R ' ) : NEW_LINE INDENT Red_Count += 1 ; NEW_LINE DEDENT if ( c == ' G ' ) : NEW_LINE INDENT result += Red_Count * Blue_Count ; NEW_LINE DEDENT DEDENT return result ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT color = " RRGGBBRGGBB " ; NEW_LINE print ( countTriplets ( color ) ) ; NEW_LINE DEDENT