codeparrot/xlcost-text-to-code huggingface dataset:
a | A iterative function that removes consecutive duplicates from string S ; We don 't need to do anything for  empty or single character string. ; j is used to store index is result string ( or index of current distinct character ) ; Traversing string ; If current character S [ i ] is different from S [ j ] ; Driver Code
def removeDuplicates ( S ) : NEW_LINE INDENT n = len ( S ) NEW_LINE if ( n < 2 ) : NEW_LINE INDENT return NEW_LINE DEDENT j = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( S [ j ] != S [ i ] ) : NEW_LINE INDENT j += 1 NEW_LINE S [ j ] = S [ i ] NEW_LINE DEDENT DEDENT j += 1 NEW_LINE S = S [ : j ] NEW_LINE return S NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S1 = " geeksforgeeks " NEW_LINE S1 = list ( S1 . rstrip ( ) ) NEW_LINE S1 = removeDuplicates ( S1 ) NEW_LINE print ( * S1 , sep = " " ) NEW_LINE S2 = " aabcca " NEW_LINE S2 = list ( S2 . rstrip ( ) ) NEW_LINE S2 = removeDuplicates ( S2 ) NEW_LINE print ( * S2 , sep = " " ) NEW_LINE DEDENT
Simplify the directory path ( Unix like ) | function to simplify a Unix - styled absolute path ; using vector in place of stack ; forming the current directory . ; if " . . " , we pop . ; do nothing ( added for better understanding . ) ; push the current directory into the vector . ; forming the ans ; vector is empty ; absolute path which we have to simplify .
def simplify ( path ) : NEW_LINE INDENT v = [ ] NEW_LINE n = len ( path ) NEW_LINE ans = " " NEW_LINE for i in range ( n ) : NEW_LINE INDENT Dir = " " NEW_LINE while ( i < n and path [ i ] != ' / ' ) : NEW_LINE INDENT Dir += path [ i ] NEW_LINE i += 1 NEW_LINE DEDENT if ( Dir == " . . " ) : NEW_LINE INDENT if ( len ( v ) > 0 ) : NEW_LINE INDENT v . pop ( ) NEW_LINE DEDENT DEDENT elif ( Dir == " . " or Dir == " " ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT v . append ( Dir ) NEW_LINE DEDENT DEDENT for i in v : NEW_LINE INDENT ans += " / " + i NEW_LINE DEDENT if ( ans == " " ) : NEW_LINE INDENT return " / " NEW_LINE DEDENT return ans NEW_LINE DEDENT Str = " / a / . / b / . . / . . / c / " NEW_LINE res = simplify ( Str ) NEW_LINE print ( res ) NEW_LINE
Evaluate a boolean expression represented as string | Python3 program to evaluate value of an expression . ; Evaluates boolean expression and returns the result ; Traverse all operands by jumping a character after every iteration . ; If operator next to current operand is AND . ; If operator next to current operand is OR . ; If operator next to current operand is XOR ( Assuming a valid input ) ; Driver code
import math as mt NEW_LINE def evaluateBoolExpr ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE for i in range ( 0 , n - 2 , 2 ) : NEW_LINE INDENT if ( s [ i + 1 ] == " A " ) : NEW_LINE INDENT if ( s [ i + 2 ] == "0" or s [ i ] == "0" ) : NEW_LINE INDENT s [ i + 2 ] = "0" NEW_LINE DEDENT else : NEW_LINE INDENT s [ i + 2 ] = "1" NEW_LINE DEDENT DEDENT elif ( s [ i + 1 ] == " B " ) : NEW_LINE INDENT if ( s [ i + 2 ] == "1" or s [ i ] == "1" ) : NEW_LINE INDENT s [ i + 2 ] = "1" NEW_LINE DEDENT else : NEW_LINE INDENT s [ i + 2 ] = "0" NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( s [ i + 2 ] == s [ i ] ) : NEW_LINE INDENT s [ i + 2 ] = "0" NEW_LINE DEDENT else : NEW_LINE INDENT s [ i + 2 ] = "1" NEW_LINE DEDENT DEDENT DEDENT return ord ( s [ n - 1 ] ) - ord ( "0" ) NEW_LINE DEDENT s = "1C1B1B0A0" NEW_LINE string = [ s [ i ] for i in range ( len ( s ) ) ] NEW_LINE print ( evaluateBoolExpr ( string ) ) NEW_LINE
Efficiently find first repeated character in a string without using any additional data structure in one traversal | Returns - 1 if all characters of str are unique . Assumptions : ( 1 ) str contains only characters from ' a ' to ' z ' ( 2 ) integers are stored using 32 bits ; An integer to store presence / absence of 26 characters using its 32 bits . ; If bit corresponding to current character is already set ; set bit in checker ; Driver code
def FirstRepeated ( string ) : NEW_LINE INDENT checker = 0 NEW_LINE pos = 0 NEW_LINE for i in string : NEW_LINE INDENT val = ord ( i ) - ord ( ' a ' ) ; NEW_LINE if ( ( checker & ( 1 << val ) ) > 0 ) : NEW_LINE INDENT return pos NEW_LINE DEDENT checker |= ( 1 << val ) NEW_LINE pos += 1 NEW_LINE DEDENT return - 1 NEW_LINE DEDENT string = " abcfdeacf " NEW_LINE i = FirstRepeated ( string ) NEW_LINE if i != - 1 : NEW_LINE INDENT print " Char ▁ = ▁ " , string [ i ] , " ▁ and ▁ Index ▁ = ▁ " , i ; NEW_LINE DEDENT else : NEW_LINE INDENT print " No ▁ repeated ▁ Char " NEW_LINE DEDENT
Check if a string is Pangrammatic Lipogram | collection of letters ; function to check for a Pangrammatic Lipogram ;  ; variable to keep count of all the letters not found in the string ; traverses the string for every letter of the alphabet ; character not found in string then increment count ; Driver program to test above function
alphabets = ' abcdefghijklmnopqrstuvwxyz ' NEW_LINE def panLipogramChecker ( s ) : NEW_LINE / * convert string to lower case * / NEW_LINE INDENT s . lower ( ) NEW_LINE counter = 0 NEW_LINE for ch in alphabets : NEW_LINE INDENT if ( s . find ( ch ) < 0 ) : NEW_LINE INDENT counter += 1 NEW_LINE DEDENT DEDENT if ( counter == 0 ) : NEW_LINE INDENT result = " Pangram " NEW_LINE DEDENT elif ( counter == 1 ) : NEW_LINE INDENT result = " Pangrammatic ▁ Lipogram " NEW_LINE DEDENT else : NEW_LINE INDENT result = " Not ▁ a ▁ pangram ▁ but ▁ might ▁ a ▁ lipogram " NEW_LINE DEDENT return result NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT print ( panLipogramChecker ( " The ▁ quick ▁ brown ▁ fox ▁ \ ▁ jumped ▁ over ▁ the ▁ lazy ▁ dog " ) ) NEW_LINE print ( panLipogramChecker ( " The ▁ quick ▁ brown ▁ fox ▁ \ ▁ jumps ▁ over ▁ the ▁ lazy ▁ dog " ) ) NEW_LINE print ( panLipogramChecker ( " The ▁ quick ▁ brown ▁ fox ▁ jum\ ▁ over ▁ the ▁ lazy ▁ dog " ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT
Lexicographically n | Function to print nth permutation using next_permute ( ) ; Sort the string in lexicographically ascending order ; Keep iterating until we reach nth position ; check for nth iteration ; print string after nth iteration ; next_permutation method implementation ; Driver Code
def nPermute ( string , n ) : NEW_LINE INDENT string = list ( string ) NEW_LINE new_string = [ ] NEW_LINE string . sort ( ) NEW_LINE j = 2 NEW_LINE while next_permutation ( string ) : NEW_LINE INDENT new_string = string NEW_LINE if j == n : NEW_LINE INDENT break NEW_LINE DEDENT j += 1 NEW_LINE DEDENT print ( ' ' . join ( new_string ) ) NEW_LINE DEDENT def next_permutation ( L ) : NEW_LINE INDENT n = len ( L ) NEW_LINE i = n - 2 NEW_LINE while i >= 0 and L [ i ] >= L [ i + 1 ] : NEW_LINE INDENT i -= 1 NEW_LINE DEDENT if i == - 1 : NEW_LINE INDENT return False NEW_LINE DEDENT j = i + 1 NEW_LINE while j < n and L [ j ] > L [ i ] : NEW_LINE INDENT j += 1 NEW_LINE DEDENT j -= 1 NEW_LINE L [ i ] , L [ j ] = L [ j ] , L [ i ] NEW_LINE left = i + 1 NEW_LINE right = n - 1 NEW_LINE while left < right : NEW_LINE INDENT L [ left ] , L [ right ] = L [ right ] , L [ left ] NEW_LINE left += 1 NEW_LINE right -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " GEEKSFORGEEKS " NEW_LINE n = 100 NEW_LINE nPermute ( string , n ) NEW_LINE DEDENT
Split numeric , alphabetic and special symbols from a String | Python 3 program to split an alphanumeric string using STL ; Driver code
def splitString ( str ) : NEW_LINE INDENT alpha = " " NEW_LINE num = " " NEW_LINE special = " " NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT if ( str [ i ] . isdigit ( ) ) : NEW_LINE INDENT num = num + str [ i ] NEW_LINE DEDENT elif ( ( str [ i ] >= ' A ' and str [ i ] <= ' Z ' ) or ( str [ i ] >= ' a ' and str [ i ] <= ' z ' ) ) : NEW_LINE INDENT alpha += str [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT special += str [ i ] NEW_LINE DEDENT DEDENT print ( alpha ) NEW_LINE print ( num ) NEW_LINE print ( special ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " geeks01 $ $ for02geeks03 ! @ ! ! " NEW_LINE splitString ( str ) NEW_LINE DEDENT
Program to print all substrings of a given string | Function to print all sub strings ; Pick starting point in outer loop and lengths of different strings for a given starting point ; Driver program to test above function
def subString ( s , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for len in range ( i + 1 , n + 1 ) : NEW_LINE INDENT print ( s [ i : len ] ) ; NEW_LINE DEDENT DEDENT DEDENT s = " abcd " ; NEW_LINE subString ( s , len ( s ) ) ; NEW_LINE
Program to print all substrings of a given string | Python program for the above approach ; outermost for loop this is for the selection of starting point ; 2 nd for loop is for selection of ending point ; 3 rd loop is for printing from starting point to ending point ; changing the line after printing from starting point to ending point ; Driver Code ; calling the method to print the substring
def printSubstrings ( string , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE INDENT for k in range ( i , ( j + 1 ) ) : NEW_LINE INDENT print ( string [ k ] , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT DEDENT string = " abcd " NEW_LINE printSubstrings ( string , len ( string ) ) NEW_LINE
Maximum Tip Calculator | Recursive function to calculate sum of maximum tip order taken by X and Y ; When all orders have been taken ; When X cannot take more orders ; When Y cannot take more orders ; When both can take order calculate maximum out of two ; Driver code
def solve ( i , X , Y , a , b , n ) : NEW_LINE INDENT if ( i == n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( X <= 0 ) : NEW_LINE INDENT return ( b [ i ] + solve ( i + 1 , X , Y - 1 , a , b , n ) ) NEW_LINE DEDENT if ( Y <= 0 ) : NEW_LINE INDENT return ( a [ i ] + solve ( i + 1 , X - 1 , Y , a , b , n ) ) NEW_LINE DEDENT else : NEW_LINE INDENT return max ( a [ i ] + solve ( i + 1 , X - 1 , Y , a , b , n ) , b [ i ] + solve ( i + 1 , X , Y - 1 , a , b , n ) ) NEW_LINE DEDENT DEDENT a = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE b = [ 5 , 4 , 3 , 2 , 1 ] NEW_LINE n = len ( a ) NEW_LINE x = 3 NEW_LINE y = 3 NEW_LINE print ( solve ( 0 , x , y , a , b , n ) ) NEW_LINE
Number of even substrings in a string of digits | Return the even number substrings . ; If current digit is even , add count of substrings ending with it . The count is ( i + 1 ) ; Driven Program
def evenNumSubstring ( str ) : NEW_LINE INDENT length = len ( str ) NEW_LINE count = 0 NEW_LINE for i in range ( 0 , length , 1 ) : NEW_LINE INDENT temp = ord ( str [ i ] ) - ord ( '0' ) NEW_LINE if ( temp % 2 == 0 ) : NEW_LINE INDENT count += ( i + 1 ) NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = [ '1' , '2' , '3' , '4' ] NEW_LINE print ( evenNumSubstring ( str ) ) NEW_LINE DEDENT
Maximum Consecutive Increasing Path Length in Binary Tree | A binary tree node ; Returns the maximum consecutive path length ; Get the vlue of current node The value of the current node will be prev node for its left and right children ; If current node has to be a part of the consecutive path then it should be 1 greater thn the value of the previous node ; a ) Find the length of the left path b ) Find the length of the right path Return the maximum of left path and right path ; Find the length of the maximum path under subtree rooted with this node ; Take the maximum previous path and path under subtree rooted with this node ; A Wrapper over maxPathLenUtil ( ) ; Return 0 if root is None ; Else compute maximum consecutive increasing path length using maxPathLenUtil ; Driver program to test above function
class Node : NEW_LINE INDENT def __init__ ( self , val ) : NEW_LINE INDENT self . val = val NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def maxPathLenUtil ( root , prev_val , prev_len ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return prev_len NEW_LINE DEDENT curr_val = root . val NEW_LINE if curr_val == prev_val + 1 : NEW_LINE INDENT return max ( maxPathLenUtil ( root . left , curr_val , prev_len + 1 ) , maxPathLenUtil ( root . right , curr_val , prev_len + 1 ) ) NEW_LINE DEDENT newPathLen = max ( maxPathLenUtil ( root . left , curr_val , 1 ) , maxPathLenUtil ( root . right , curr_val , 1 ) ) NEW_LINE return max ( prev_len , newPathLen ) NEW_LINE DEDENT def maxConsecutivePathLength ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return 0 NEW_LINE DEDENT return maxPathLenUtil ( root , root . val - 1 , 0 ) NEW_LINE DEDENT root = Node ( 10 ) NEW_LINE root . left = Node ( 11 ) NEW_LINE root . right = Node ( 9 ) NEW_LINE root . left . left = Node ( 13 ) NEW_LINE root . left . right = Node ( 12 ) NEW_LINE root . right . left = Node ( 13 ) NEW_LINE root . right . right = Node ( 8 ) NEW_LINE print " Maximum ▁ Consecutive ▁ Increasing ▁ Path ▁ Length ▁ is " , NEW_LINE print maxConsecutivePathLength ( root ) NEW_LINE
Find largest word in dictionary by deleting some characters of given string | Returns true if str1 [ ] is a subsequence of str2 [ ] . m is length of str1 and n is length of str2 ; Traverse str2 and str1 , and compare current character of str2 with first unmatched char of str1 , if matched then move ahead in str1 ; If all characters of str1 were found in str2 ; Returns the longest string in dictionary which is a subsequence of str . ; Traverse through all words of dictionary ; If current word is subsequence of str and is largest such word so far . ; Return longest string ; Driver program to test above function
def isSubSequence ( str1 , str2 ) : NEW_LINE INDENT m = len ( str1 ) ; NEW_LINE n = len ( str2 ) ; NEW_LINE i = 0 ; NEW_LINE while ( i < n and j < m ) : NEW_LINE INDENT if ( str1 [ j ] == str2 [ i ] ) : NEW_LINE INDENT j += 1 ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT return ( j == m ) ; NEW_LINE DEDENT def findLongestString ( dict1 , str1 ) : NEW_LINE INDENT result = " " ; NEW_LINE length = 0 ; NEW_LINE for word in dict1 : NEW_LINE INDENT if ( length < len ( word ) and isSubSequence ( word , str1 ) ) : NEW_LINE INDENT result = word ; NEW_LINE length = len ( word ) ; NEW_LINE DEDENT DEDENT return result ; NEW_LINE DEDENT dict1 = [ " ale " , " apple " , " monkey " , " plea " ] ; NEW_LINE str1 = " abpcplea " ; NEW_LINE print ( findLongestString ( dict1 , str1 ) ) ; NEW_LINE
XOR Cipher | The same function is used to encrypt and decrypt ; Define XOR key Any character value will work ; calculate length of input string ; perform XOR operation of key with every character in string ; Driver Code ; Encrypt the string ; Decrypt the string
def encryptDecrypt ( inpString ) : NEW_LINE INDENT xorKey = ' P ' ; NEW_LINE length = len ( inpString ) ; NEW_LINE for i in range ( length ) : NEW_LINE INDENT inpString = ( inpString [ : i ] + chr ( ord ( inpString [ i ] ) ^ ord ( xorKey ) ) + inpString [ i + 1 : ] ) ; NEW_LINE print ( inpString [ i ] , end = " " ) ; NEW_LINE DEDENT return inpString ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT sampleString = " GeeksforGeeks " ; NEW_LINE print ( " Encrypted ▁ String : ▁ " , end = " " ) ; NEW_LINE sampleString = encryptDecrypt ( sampleString ) ; NEW_LINE print ( " " ) ; NEW_LINE print ( " Decrypted ▁ String : ▁ " , end = " " ) ; NEW_LINE encryptDecrypt ( sampleString ) ; NEW_LINE DEDENT
Repeated subsequence of length 2 or more | Python3 program to check if any repeated subsequence exists in the String ; A function to check if a String Str is palindrome ; l and h are leftmost and rightmost corners of Str Keep comparing characters while they are same ; The main function that checks if repeated subsequence exists in the String ; Find length of input String ; Create an array to store all characters and their frequencies in Str [ ] ; Traverse the input String and store frequencies of all characters in freq [ ] array . ; If the character count is more than 2 we found a repetition ; In - place remove non - repeating characters from the String ; check if the resultant String is palindrome ; special case - if length is odd return true if the middle character is same as previous one ; return false if String is a palindrome ; return true if String is not a palindrome ; Driver code
MAX_CHAR = 256 NEW_LINE def isPalindrome ( Str , l , h ) : NEW_LINE INDENT while ( h > l ) : NEW_LINE INDENT if ( Str [ l ] != Str [ h ] ) : NEW_LINE INDENT l += 1 NEW_LINE h -= 1 NEW_LINE return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def check ( Str ) : NEW_LINE INDENT n = len ( Str ) NEW_LINE freq = [ 0 for i in range ( MAX_CHAR ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ ord ( Str [ i ] ) ] += 1 NEW_LINE if ( freq [ ord ( Str [ i ] ) ] > 2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT k = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( freq [ ord ( Str [ i ] ) ] > 1 ) : NEW_LINE INDENT Str [ k ] = Str [ i ] NEW_LINE k += 1 NEW_LINE DEDENT DEDENT Str [ k ] = ' \0' NEW_LINE if ( isPalindrome ( Str , 0 , k - 1 ) ) : NEW_LINE INDENT if ( k & 1 ) : NEW_LINE INDENT return Str [ k // 2 ] == Str [ k // 2 - 1 ] NEW_LINE DEDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT S = " ABCABD " NEW_LINE Str = [ i for i in S ] NEW_LINE if ( check ( Str ) ) : NEW_LINE INDENT print ( " Repeated ▁ Subsequence ▁ Exists " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Repeated ▁ Subsequence ▁ Doesn ' t ▁ Exists " ) NEW_LINE DEDENT
How to find Lexicographically previous permutation ? | Function to compute the previous permutation ; Find index of the last element of the string ; Find largest index i such that str [ i - 1 ] > str [ i ] ; if string is sorted in ascending order we 're at the last permutation ; Note - str [ i . . n ] is sorted in ascending order . Find rightmost element 's index  that is less than str[i - 1] ; Swap character at i - 1 with j ; Reverse the substring [ i . . n ] ; Driver code
def prevPermutation ( str ) : NEW_LINE INDENT n = len ( str ) - 1 NEW_LINE i = n NEW_LINE while ( i > 0 and str [ i - 1 ] <= str [ i ] ) : NEW_LINE INDENT i -= 1 NEW_LINE DEDENT if ( i <= 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT j = i - 1 NEW_LINE while ( j + 1 <= n and str [ j + 1 ] <= str [ i - 1 ] ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT str = list ( str ) NEW_LINE temp = str [ i - 1 ] NEW_LINE str [ i - 1 ] = str [ j ] NEW_LINE str [ j ] = temp NEW_LINE str = ' ' . join ( str ) NEW_LINE str [ : : - 1 ] NEW_LINE return True , str NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "4321" NEW_LINE b , str = prevPermutation ( str ) NEW_LINE if ( b == True ) : NEW_LINE INDENT print ( " Previous ▁ permutation ▁ is " , str ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Previous ▁ permutation ▁ doesn ' t ▁ exist " ) NEW_LINE DEDENT DEDENT
Longest Path with Same Values in a Binary Tree | Function to print the longest path of same values ; Recursive calls to check for subtrees ; Variables to store maximum lengths in two directions ; If curr node and it 's left child has same value  ; If curr node and it 's right child has same value  ; Driver function to find length of longest same value path ; Helper function that allocates a new node with the given data and None left and right pointers . ; Driver code ; Let us construct a Binary Tree 4 / \ 4 4 / \ \ 4 9 5
def length ( node , ans ) : NEW_LINE INDENT if ( not node ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT left = length ( node . left , ans ) NEW_LINE right = length ( node . right , ans ) NEW_LINE Leftmax = 0 NEW_LINE Rightmax = 0 NEW_LINE if ( node . left and node . left . val == node . val ) : NEW_LINE INDENT Leftmax += left + 1 NEW_LINE DEDENT if ( node . right and node . right . val == node . val ) : NEW_LINE INDENT Rightmax += right + 1 NEW_LINE DEDENT ans [ 0 ] = max ( ans [ 0 ] , Leftmax + Rightmax ) NEW_LINE return max ( Leftmax , Rightmax ) NEW_LINE DEDENT def longestSameValuePath ( root ) : NEW_LINE INDENT ans = [ 0 ] NEW_LINE length ( root , ans ) NEW_LINE return ans [ 0 ] NEW_LINE DEDENT class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . val = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = None NEW_LINE root = newNode ( 4 ) NEW_LINE root . left = newNode ( 4 ) NEW_LINE root . right = newNode ( 4 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 9 ) NEW_LINE root . right . right = newNode ( 5 ) NEW_LINE print ( longestSameValuePath ( root ) ) NEW_LINE DEDENT
Check if edit distance between two strings is one | Returns true if edit distance between s1 and s2 is one , else false ; Find lengths of given strings ; If difference between lengths is more than 1 , then strings can 't be at one distance ; count = 0 Count of isEditDistanceOne ; If current characters dont match ; If length of one string is more , then only possible edit is to remove a character ; else : If lengths of both strings is same ; Increment count of edits ; else : if current characters match ; if last character is extra in any string ; Driver program
def isEditDistanceOne ( s1 , s2 ) : NEW_LINE INDENT m = len ( s1 ) NEW_LINE n = len ( s2 ) NEW_LINE if abs ( m - n ) > 1 : NEW_LINE INDENT return false NEW_LINE DEDENT i = 0 NEW_LINE j = 0 NEW_LINE while i < m and j < n : NEW_LINE INDENT if s1 [ i ] != s2 [ j ] : NEW_LINE INDENT if count == 1 : NEW_LINE INDENT return false NEW_LINE DEDENT if m > n : NEW_LINE INDENT i += 1 NEW_LINE DEDENT elif m < n : NEW_LINE INDENT j += 1 NEW_LINE i += 1 NEW_LINE j += 1 NEW_LINE DEDENT count += 1 NEW_LINE i += 1 NEW_LINE j += 1 NEW_LINE DEDENT DEDENT if i < m or j < n : NEW_LINE INDENT count += 1 NEW_LINE DEDENT return count == 1 NEW_LINE DEDENT s1 = " gfg " NEW_LINE s2 = " gf " NEW_LINE if isEditDistanceOne ( s1 , s2 ) : NEW_LINE INDENT print " Yes " NEW_LINE DEDENT else : NEW_LINE INDENT print " No " NEW_LINE DEDENT
Count of numbers from range [ L , R ] whose sum of digits is Y | Set 2 | Python program for the above approach ; Function to find the sum of digits of numbers in the range [ 0 , X ] ; Check if count of digits in a number greater than count of digits in X ; Check if sum of digits of a number is equal to Y ; Check if current subproblem has already been comrputed ; Stores count of numbers whose sum of digits is Y ; Check if the number exceeds Y or not ; Iterate over all possible values of i - th digits ; Update res ; Return res ; Utility function to count the numbers in the range [ L , R ] whose sum of digits is Y ; Base Case ; Stores numbers in the form of its equivalent ; Stores overlapping subproblems ; Stores count of numbers in the range [ 0 , R ] ; Update str ; Stores count of numbers in the range [ 0 , L - 1 ] ; Driver Code
M = 1000 NEW_LINE def cntNum ( X , i , sum , tight , dp ) : NEW_LINE INDENT if ( i >= len ( X ) or sum < 0 ) : NEW_LINE INDENT if ( sum == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT if ( dp [ sum ] [ i ] [ tight ] != - 1 ) : NEW_LINE INDENT return dp [ sum ] [ i ] [ tight ] NEW_LINE DEDENT res , end = 0 , 9 NEW_LINE if tight : NEW_LINE INDENT end = ord ( X [ i ] ) - ord ( '0' ) NEW_LINE DEDENT for j in range ( end + 1 ) : NEW_LINE INDENT res += cntNum ( X , i + 1 , sum - j , ( tight & ( j == end ) ) , dp ) NEW_LINE DEDENT dp [ sum ] [ i ] [ tight ] = res NEW_LINE return res NEW_LINE DEDENT def UtilCntNumRange ( L , R , Y ) : NEW_LINE INDENT if ( R == 0 and Y == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT strr = str ( R ) NEW_LINE dp = [ [ [ - 1 for i in range ( 2 ) ] for i in range ( M ) ] for i in range ( M ) ] NEW_LINE cntR = cntNum ( strr , 0 , Y , True , dp ) NEW_LINE strr = str ( L - 1 ) NEW_LINE cntL = cntNum ( strr , 0 , Y , True , dp ) NEW_LINE return ( cntR - cntL ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L , R , Y = 20 , 10000 , 14 NEW_LINE print ( UtilCntNumRange ( L , R , Y ) ) NEW_LINE DEDENT
Remove spaces from a given string | Function to remove all spaces from a given string ; Driver program
def removeSpaces ( string ) : NEW_LINE INDENT string = string . replace ( ' ▁ ' , ' ' ) NEW_LINE return string NEW_LINE DEDENT string = " g ▁ eeks ▁ for ▁ ge ▁ eeks ▁ " NEW_LINE print ( removeSpaces ( string ) ) NEW_LINE