codeparrot/xlcost-text-to-code huggingface dataset:
Sub | Function that counts all the sub - strings of length ' k ' which have all identical characters ; count of sub - strings , length , initial position of sliding window ; dictionary to store the frequency of the characters of sub - string ; increase the frequency of the character and length of the sub - string ; if the length of the sub - string is greater than K ; remove the character from the beginning of sub - string ; if the length of the sub string is equal to k and frequency of one of its characters is equal to the length of the sub - string i . e . all the characters are same increase the count ; display the number of valid sub - strings ; Driver code
def solve ( s , k ) : NEW_LINE INDENT count , length , pos = 0 , 0 , 0 NEW_LINE m = dict . fromkeys ( s , 0 ) NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT m [ s [ i ] ] += 1 NEW_LINE length += 1 NEW_LINE if length > k : NEW_LINE INDENT m [ s [ pos ] ] -= 1 NEW_LINE pos += 1 NEW_LINE length -= 1 NEW_LINE DEDENT if length == k and m [ s [ i ] ] == length : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " aaaabbbccdddd " NEW_LINE k = 4 NEW_LINE solve ( s , k ) NEW_LINE DEDENT
Program to replace every space in a string with hyphen | Python 3 program to replace space with - ; Get the String ; Traverse the string character by character . ; Changing the ith character to ' - ' if it 's a space. ; Print the modified string .
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " A ▁ computer ▁ science ▁ portal ▁ for ▁ geeks " NEW_LINE for i in range ( 0 , len ( str ) , 1 ) : NEW_LINE INDENT if ( str [ i ] == ' ▁ ' ) : NEW_LINE INDENT str = str . replace ( str [ i ] , ' - ' ) NEW_LINE DEDENT DEDENT print ( str ) NEW_LINE DEDENT
Minimum operation require to make first and last character same | Python program to find minimum operation require to make first and last character same ; Function to find minimum operation require to make first and last character same ; Base conditions ; If answer found ; If string is already visited ; Decrement ending index only ; Increment starting index only ; Increment starting index and decrement index ; Store the minimum value ; Driver code ; Function call
import sys NEW_LINE MAX = sys . maxsize NEW_LINE m = { } NEW_LINE Min = sys . maxsize NEW_LINE def minOperation ( s , i , j , count ) : NEW_LINE INDENT global Min , MAX NEW_LINE if ( ( i >= len ( s ) and j < 0 ) or ( i == j ) ) : NEW_LINE INDENT return MAX NEW_LINE DEDENT if ( s [ i ] == s [ j ] or count >= Min ) : NEW_LINE INDENT return count NEW_LINE DEDENT Str = str ( i ) + " | " + str ( j ) NEW_LINE if Str not in m : NEW_LINE INDENT if ( i >= len ( s ) ) : NEW_LINE INDENT m [ Str ] = minOperation ( s , i , j - 1 , count + 1 ) NEW_LINE DEDENT elif ( j < 0 ) : NEW_LINE INDENT m [ Str ] = minOperation ( s , i + 1 , j , count + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT m [ Str ] = min ( minOperation ( s , i , j - 1 , count + 1 ) , minOperation ( s , i + 1 , j , count + 1 ) ) NEW_LINE DEDENT DEDENT if ( m [ Str ] < Min ) : NEW_LINE INDENT Min = m [ Str ] NEW_LINE DEDENT return m [ Str ] NEW_LINE DEDENT s = " bacdefghipalop " NEW_LINE ans = minOperation ( s , 0 , len ( s ) - 1 , 0 ) NEW_LINE if ( ans == MAX ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ans ) NEW_LINE DEDENT
Check if any permutation of a large number is divisible by 8 | Function to check if any permutation of a large number is divisible by 8 ; Less than three digit number can be checked directly . ; check for the reverse of a number ; Stores the Frequency of characters in the n . ; Iterates for all three digit numbers divisible by 8 ; stores the frequency of all single digit in three - digit number ; check if the original number has the digit ; when all are checked its not possible ; Driver Code
def solve ( n , l ) : NEW_LINE INDENT if l < 3 : NEW_LINE INDENT if int ( n ) % 8 == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT n = n [ : : - 1 ] NEW_LINE if int ( n ) % 8 == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT hash = 10 * [ 0 ] NEW_LINE for i in range ( 0 , l ) : NEW_LINE INDENT hash [ int ( n [ i ] ) - 0 ] += 1 ; NEW_LINE DEDENT for i in range ( 104 , 1000 , 8 ) : NEW_LINE INDENT dup = i NEW_LINE freq = 10 * [ 0 ] NEW_LINE freq [ int ( dup % 10 ) ] += 1 ; NEW_LINE dup = dup / 10 NEW_LINE freq [ int ( dup % 10 ) ] += 1 ; NEW_LINE dup = dup / 10 NEW_LINE freq [ int ( dup % 10 ) ] += 1 ; NEW_LINE dup = i NEW_LINE if ( freq [ int ( dup % 10 ) ] > hash [ int ( dup % 10 ) ] ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT dup = dup / 10 ; NEW_LINE if ( freq [ int ( dup % 10 ) ] > hash [ int ( dup % 10 ) ] ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT dup = dup / 10 NEW_LINE if ( freq [ int ( dup % 10 ) ] > hash [ int ( dup % 10 ) ] ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT number = "31462708" NEW_LINE l = len ( number ) NEW_LINE if solve ( number , l ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Lexicographically smallest string formed by appending a character from the first K characters of a given string | Function to find the new string thus formed by removing characters ; new string ; Remove characters until the string is empty ; Traverse to find the smallest character in the first k characters ; append the smallest character ; removing the lexicographically smallest character from the string ; Driver Code
def newString ( s , k ) : NEW_LINE INDENT X = " " NEW_LINE while ( len ( s ) > 0 ) : NEW_LINE INDENT temp = s [ 0 ] NEW_LINE i = 1 NEW_LINE while ( i < k and i < len ( s ) ) : NEW_LINE INDENT if ( s [ i ] < temp ) : NEW_LINE INDENT temp = s [ i ] NEW_LINE DEDENT i += 1 NEW_LINE DEDENT X = X + temp NEW_LINE for i in range ( k ) : NEW_LINE INDENT if ( s [ i ] == temp ) : NEW_LINE INDENT s = s [ 0 : i ] + s [ i + 1 : ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT return X NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " gaurang " NEW_LINE k = 3 NEW_LINE print ( newString ( s , k ) ) NEW_LINE DEDENT
Given a binary tree , how do you remove all the half nodes ? | A binary tree node ; For inorder traversal ; Removes all nodes with only one child and returns new root ( note that root may change ) ; If current nodes is a half node with left child None then it 's right child is returned and   replaces it in the given tree ; if current nodes is a half node with right child NULL right , then it 's right child is  returned and replaces it in the given tree  ; Driver Program
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def printInorder ( root ) : NEW_LINE INDENT if root is not None : NEW_LINE INDENT printInorder ( root . left ) NEW_LINE print root . data , NEW_LINE printInorder ( root . right ) NEW_LINE DEDENT DEDENT def RemoveHalfNodes ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return None NEW_LINE DEDENT root . left = RemoveHalfNodes ( root . left ) NEW_LINE root . right = RemoveHalfNodes ( root . right ) NEW_LINE if root . left is None and root . right is None : NEW_LINE INDENT return root NEW_LINE DEDENT if root . left is None : NEW_LINE INDENT new_root = root . right NEW_LINE temp = root NEW_LINE root = None NEW_LINE del ( temp ) NEW_LINE return new_root NEW_LINE DEDENT if root . right is None : NEW_LINE INDENT new_root = root . left NEW_LINE temp = root NEW_LINE root = None NEW_LINE del ( temp ) NEW_LINE return new_root NEW_LINE DEDENT return root NEW_LINE DEDENT root = Node ( 2 ) NEW_LINE root . left = Node ( 7 ) NEW_LINE root . right = Node ( 5 ) NEW_LINE root . left . right = Node ( 6 ) NEW_LINE root . left . right . left = Node ( 1 ) NEW_LINE root . left . right . right = Node ( 11 ) NEW_LINE root . right . right = Node ( 9 ) NEW_LINE root . right . right . left = Node ( 4 ) NEW_LINE print " Inorder ▁ traversal ▁ of ▁ given ▁ tree " NEW_LINE printInorder ( root ) NEW_LINE NewRoot = RemoveHalfNodes ( root ) NEW_LINE print   " NEW_LINE Inorder traversal of the modified tree " NEW_LINE printInorder ( NewRoot ) NEW_LINE
Palindrome by swapping only one character | Python3 program palindrome by swapping only one character ; counts the number of differences which prevents the string from being palindrome . ; keeps a record of the characters that prevents the string from being palindrome . ; loops from the start of a string till the midpoint of the string ; difference is encountered preventing the string from being palindrome ; 3 rd differences encountered and its no longer possible to make is palindrome by one swap ; record the different character ; store the different characters ; its already palindrome ; only one difference is found ; if the middleChar matches either of the difference producing characters , return true ; two differences are found ; if the characters contained in the two sets are same , return true ; Driver Code
def isPalindromePossible ( input : str ) -> bool : NEW_LINE INDENT length = len ( input ) NEW_LINE diffCount = 0 NEW_LINE i = 0 NEW_LINE diff = [ [ '0' ] * 2 ] * 2 NEW_LINE while i < length // 2 : NEW_LINE INDENT if input [ i ] != input [ length - i - 1 ] : NEW_LINE INDENT if diffCount == 2 : NEW_LINE INDENT return False NEW_LINE DEDENT diff [ diffCount ] [ 0 ] = input [ i ] NEW_LINE diff [ diffCount ] [ 1 ] = input [ length - i - 1 ] NEW_LINE diffCount += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if diffCount == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT elif diffCount == 1 : NEW_LINE INDENT midChar = input [ i ] NEW_LINE if length % 2 != 0 and ( diff [ 0 ] [ 0 ] == midChar or diff [ 0 ] [ 1 ] == midChar ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT elif diffCount == 2 : NEW_LINE INDENT if ( diff [ 0 ] [ 0 ] == diff [ 1 ] [ 0 ] and diff [ 0 ] [ 1 ] == diff [ 1 ] [ 1 ] ) or ( diff [ 0 ] [ 0 ] == diff [ 1 ] [ 1 ] and diff [ 0 ] [ 1 ] == diff [ 1 ] [ 0 ] ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( isPalindromePossible ( " bbg " ) ) NEW_LINE print ( isPalindromePossible ( " bdababd " ) ) NEW_LINE print ( isPalindromePossible ( " gcagac " ) ) NEW_LINE DEDENT
Convert String into Binary Sequence | utility function ; convert each char to ASCII value ; Convert ASCII value to binary ; Driver Code
def strToBinary ( s ) : NEW_LINE INDENT bin_conv = [ ] NEW_LINE for c in s : NEW_LINE INDENT ascii_val = ord ( c ) NEW_LINE binary_val = bin ( ascii_val ) NEW_LINE bin_conv . append ( binary_val [ 2 : ] ) NEW_LINE DEDENT return ( ' ▁ ' . join ( bin_conv ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = ' geeks ' NEW_LINE DEDENT print ( strToBinary ( s ) ) NEW_LINE
Print distinct sorted permutations with duplicates allowed in input | Python3 program to print all permutations of a string in sorted order . ; Calculating factorial of a number ; Method to find total number of permutations ; Building Map to store frequencies of all characters . ; Traversing map and finding duplicate elements . ; Start traversing from the end and find position ' i - 1' of the first character which is greater than its successor ; Finding smallest character after ' i - 1' and greater than temp [ i - 1 ] ; Swapping the above found characters . ; Sort all digits from position next to ' i - 1' to end of the string ; Print the String ; Sorting String ; Print first permutation ; Finding the total permutations ; Driver Code
from collections import defaultdict NEW_LINE def factorial ( n ) : NEW_LINE INDENT f = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT f = f * i NEW_LINE DEDENT return f NEW_LINE DEDENT def calculateTotal ( temp , n ) : NEW_LINE INDENT f = factorial ( n ) NEW_LINE hm = defaultdict ( int ) NEW_LINE for i in range ( len ( temp ) ) : NEW_LINE INDENT hm [ temp [ i ] ] += 1 NEW_LINE DEDENT for e in hm : NEW_LINE INDENT x = hm [ e ] NEW_LINE if ( x > 1 ) : NEW_LINE INDENT temp5 = factorial ( x ) NEW_LINE f //= temp5 NEW_LINE DEDENT return f NEW_LINE DEDENT DEDENT def nextPermutation ( temp ) : NEW_LINE INDENT for i in range ( len ( temp ) - 1 , 0 , - 1 ) : NEW_LINE INDENT if ( temp [ i ] > temp [ i - 1 ] ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT min = i NEW_LINE x = temp [ i - 1 ] NEW_LINE for j in range ( i + 1 , len ( temp ) ) : NEW_LINE INDENT if ( ( temp [ j ] < temp [ min ] ) and ( temp [ j ] > x ) ) : NEW_LINE INDENT min = j NEW_LINE DEDENT DEDENT temp [ i - 1 ] , temp [ min ] = ( temp [ min ] , temp [ i - 1 ] ) NEW_LINE temp [ i : ] . sort ( ) NEW_LINE print ( ' ' . join ( temp ) ) NEW_LINE DEDENT def printAllPermutations ( s ) : NEW_LINE INDENT temp = list ( s ) NEW_LINE temp . sort ( ) NEW_LINE print ( ' ' . join ( temp ) ) NEW_LINE total = calculateTotal ( temp , len ( temp ) ) NEW_LINE for i in range ( 1 , total ) : NEW_LINE INDENT nextPermutation ( temp ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " AAB " NEW_LINE printAllPermutations ( s ) NEW_LINE DEDENT
Swap Nodes in Binary tree of every k 'th level | constructor to create a new node ; A utility function swap left node and right node of tree of every k 'th level  ; Base Case ; If current level + 1 is present in swap vector then we swap left and right node ; Recur for left and right subtree ; This function mainly calls recursive function swapEveryKLevelUtil ; Call swapEveryKLevelUtil function with initial level as 1 ; Method to find the inorder tree travesal ; 1 / \ 2 3 / / \ 4 7 8
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def swapEveryKLevelUtil ( root , level , k ) : NEW_LINE INDENT if ( root is None or ( root . left is None and root . right is None ) ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( level + 1 ) % k == 0 : NEW_LINE INDENT root . left , root . right = root . right , root . left NEW_LINE DEDENT swapEveryKLevelUtil ( root . left , level + 1 , k ) NEW_LINE swapEveryKLevelUtil ( root . right , level + 1 , k ) NEW_LINE DEDENT def swapEveryKLevel ( root , k ) : NEW_LINE INDENT swapEveryKLevelUtil ( root , 1 , k ) NEW_LINE DEDENT def inorder ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return NEW_LINE DEDENT inorder ( root . left ) NEW_LINE print root . data , NEW_LINE inorder ( root . right ) NEW_LINE DEDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . right . right = Node ( 8 ) NEW_LINE root . right . left = Node ( 7 ) NEW_LINE k = 2 NEW_LINE print " Before ▁ swap ▁ node ▁ : " NEW_LINE inorder ( root ) NEW_LINE swapEveryKLevel ( root , k ) NEW_LINE print   " NEW_LINE After swap Node :   " NEW_LINE inorder ( root ) NEW_LINE
Convert a sentence into its equivalent mobile numeric keypad sequence | Function which computes the sequence ; length of input string ; checking for space ; calculating index for each character ; output sequence ; storing the sequence in array
def printSequence ( arr , input ) : NEW_LINE INDENT n = len ( input ) NEW_LINE output = " " NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( input [ i ] == ' ▁ ' ) : NEW_LINE INDENT output = output + "0" NEW_LINE DEDENT else : NEW_LINE INDENT position = ord ( input [ i ] ) - ord ( ' A ' ) NEW_LINE output = output + arr [ position ] NEW_LINE DEDENT DEDENT return output NEW_LINE DEDENT str = [ "2" , "22" , "222" , "3" , "33" , "333" , "4" , "44" , "444" , "5" , "55" , "555" , "6" , "66" , "666" , "7" , "77" , "777" , "7777" , "8" , "88" , "888" , "9" , "99" , "999" , "9999" ] NEW_LINE input = " GEEKSFORGEEKS " ; NEW_LINE print ( printSequence ( str , input ) ) NEW_LINE
Longest Uncommon Subsequence | function to calculate length of longest uncommon subsequence ; creating an unordered map to map strings to their frequency ; traversing all elements of vector strArr ; Creating all possible subsequences , i . e 2 ^ n ; ( ( i >> j ) & 1 ) determines which character goes into t ; If common subsequence is found , increment its frequency ; traversing the map ; if frequency equals 1 ; input strings
def findLUSlength ( a , b ) : NEW_LINE INDENT map = dict ( ) NEW_LINE strArr = [ ] NEW_LINE strArr . append ( a ) NEW_LINE strArr . append ( b ) NEW_LINE for s in strArr : NEW_LINE INDENT for i in range ( 1 << len ( s ) ) : NEW_LINE INDENT t = " " NEW_LINE for j in range ( len ( s ) ) : NEW_LINE INDENT if ( ( ( i >> j ) & 1 ) != 0 ) : NEW_LINE INDENT t += s [ j ] NEW_LINE DEDENT DEDENT if ( t in map . keys ( ) ) : NEW_LINE INDENT map [ t ] += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT map [ t ] = 1 NEW_LINE DEDENT DEDENT DEDENT res = 0 NEW_LINE for a in map : NEW_LINE INDENT if ( map [ a ] == 1 ) : NEW_LINE INDENT res = max ( res , len ( a ) ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT a = " abcdabcd " NEW_LINE b = " abcabc " NEW_LINE print ( findLUSlength ( a , b ) ) NEW_LINE
Round the given number to nearest multiple of 10 | Function to round the number to the nearest number having one 's digit 0 ; Last character is 0 then return the original string ; If last character is 1 or 2 or 3 or 4 or 5 make it 0 ; Process carry ; Return final string ; Driver code ; Function call
def Round ( s , n ) : NEW_LINE INDENT s = list ( s ) NEW_LINE c = s . copy ( ) NEW_LINE if ( c [ n - 1 ] == '0' ) : NEW_LINE INDENT return ( " " . join ( s ) ) NEW_LINE DEDENT elif ( c [ n - 1 ] == '1' or c [ n - 1 ] == '2' or c [ n - 1 ] == '3' or c [ n - 1 ] == '4' or c [ n - 1 ] == '5' ) : NEW_LINE INDENT c [ n - 1 ] = '0' NEW_LINE return ( " " . join ( c ) ) NEW_LINE DEDENT else : NEW_LINE INDENT c [ n - 1 ] = '0' NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( c [ i ] == '9' ) : NEW_LINE INDENT c [ i ] = '0' NEW_LINE DEDENT else : NEW_LINE INDENT t = ord ( c [ i ] ) - ord ( '0' ) + 1 NEW_LINE c [ i ] = chr ( 48 + t ) NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT s1 = " " . join ( c ) NEW_LINE if ( s1 [ 0 ] == '0' ) : NEW_LINE INDENT s1 = "1" + s1 NEW_LINE DEDENT return s1 NEW_LINE DEDENT s = "5748965412485599999874589965999" NEW_LINE n = len ( s ) NEW_LINE print ( Round ( s , n ) ) NEW_LINE
Check whether given floating point number is even or odd | Function to check even or odd . ; Loop to traverse number from LSB ; We ignore trailing 0 s after dot ; If it is ' . ' we will check next digit and it means decimal part is traversed . ; If digit is divisible by 2 means even number . ; Driver Function
def isEven ( s ) : NEW_LINE INDENT l = len ( s ) NEW_LINE dotSeen = False NEW_LINE for i in range ( l - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( s [ i ] == '0' and dotSeen == False ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( s [ i ] == ' . ' ) : NEW_LINE INDENT dotSeen = True NEW_LINE continue NEW_LINE DEDENT if ( ( int ) ( s [ i ] ) % 2 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT DEDENT s = "100.70" NEW_LINE if ( isEven ( s ) ) : NEW_LINE INDENT print ( " Even " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Odd " ) NEW_LINE DEDENT
Minimum cost to construct a string | Python 3 Program to find minimum cost to construct a string ; Initially all characters are un - seen ; Marking seen characters ; Count total seen character , and that is the cost ; Driver Code ; s is the string that needs to be constructed
def minCost ( s ) : NEW_LINE INDENT alphabets = [ False for i in range ( 26 ) ] NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT alphabets [ ord ( s [ i ] ) - 97 ] = True NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( alphabets [ i ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " geeksforgeeks " NEW_LINE print ( " Total ▁ cost ▁ to ▁ construct " , s , " is " , minCost ( s ) ) NEW_LINE DEDENT
Root to leaf paths having equal lengths in a Binary Tree | utility that allocates a newNode with the given key ; Function to store counts of different root to leaf path lengths in hash map m . ; Base condition ; If leaf node reached , increment count of path length of this root to leaf path . ; Recursively call for left and right subtrees with path lengths more than 1. ; A wrapper over pathCountUtil ( ) ; create an empty hash table ; Recursively check in left and right subtrees . ; Print all path lenghts and their counts . ; Driver Code
class newnode : 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 pathCountUtil ( node , m , path_len ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( node . left == None and node . right == None ) : NEW_LINE INDENT if path_len [ 0 ] not in m : NEW_LINE INDENT m [ path_len [ 0 ] ] = 0 NEW_LINE DEDENT m [ path_len [ 0 ] ] += 1 NEW_LINE return NEW_LINE DEDENT pathCountUtil ( node . left , m , [ path_len [ 0 ] + 1 ] ) NEW_LINE pathCountUtil ( node . right , m , [ path_len [ 0 ] + 1 ] ) NEW_LINE DEDENT def pathCounts ( root ) : NEW_LINE INDENT m = { } NEW_LINE path_len = [ 1 ] NEW_LINE pathCountUtil ( root , m , path_len ) NEW_LINE for itr in sorted ( m , reverse = True ) : NEW_LINE INDENT print ( m [ itr ] , " ▁ paths ▁ have ▁ length ▁ " , itr ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newnode ( 8 ) NEW_LINE root . left = newnode ( 5 ) NEW_LINE root . right = newnode ( 4 ) NEW_LINE root . left . left = newnode ( 9 ) NEW_LINE root . left . right = newnode ( 7 ) NEW_LINE root . right . right = newnode ( 11 ) NEW_LINE root . right . right . left = newnode ( 3 ) NEW_LINE pathCounts ( root ) NEW_LINE DEDENT
Possibility of moving out of maze | Function to check whether it will stay inside or come out ; marks all the positions that is visited ; Initial starting point ; initial assumption is it comes out ; runs till it is inside or comes out ; if the movement is towards left then we move left . The start variable and mark that position as visited if not visited previously . Else we break out ; It will be inside forever ; If the movement is towards right , then we move right . The start variable and mark that position as visited if not visited previously else we break out ; it will be inside forever ; Driver code
def checkingPossibility ( a , n , s ) : NEW_LINE INDENT mark = [ 0 ] * n NEW_LINE start = 0 NEW_LINE possible = 1 NEW_LINE while start >= 0 and start < n : NEW_LINE INDENT if s [ start ] == " < " : NEW_LINE INDENT if mark [ start ] == 0 : NEW_LINE INDENT mark [ start ] = 1 NEW_LINE start -= a [ start ] NEW_LINE DEDENT else : NEW_LINE INDENT possible = 0 NEW_LINE break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if mark [ start ] == 0 : NEW_LINE INDENT mark [ start ] = 1 NEW_LINE start += a [ start ] NEW_LINE DEDENT else : NEW_LINE INDENT possible = 0 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT if possible == 0 : NEW_LINE INDENT print " it ▁ stays ▁ inside ▁ forever " NEW_LINE DEDENT else : NEW_LINE INDENT print " comes ▁ out " NEW_LINE DEDENT DEDENT n = 2 NEW_LINE s = " > < " NEW_LINE a = [ 1 , 2 ] NEW_LINE checkingPossibility ( a , n , s ) NEW_LINE