codeparrot/xlcost-text-to-code huggingface dataset:
Program to print the initials of a name with the surname | Python program to print the initials of a name with the surname ; to remove any leading or trailing spaces ; to store extracted words ; forming the word ; when space is encountered it means the name is completed and thereby extracted ; printing the first letter of the name in capital letters ; for the surname , we have to print the entire surname and not just the initial string " t " has the surname now ; first letter of surname in capital letter ; rest of the letters in small ; printing surname ; Driver Code
def printInitials ( string : str ) : NEW_LINE INDENT length = len ( string ) NEW_LINE string . strip ( ) NEW_LINE t = " " NEW_LINE for i in range ( length ) : NEW_LINE INDENT ch = string [ i ] NEW_LINE if ch != ' ▁ ' : NEW_LINE INDENT t += ch NEW_LINE DEDENT else : NEW_LINE INDENT print ( t [ 0 ] . upper ( ) + " . ▁ " , end = " " ) NEW_LINE t = " " NEW_LINE DEDENT DEDENT temp = " " NEW_LINE for j in range ( len ( t ) ) : NEW_LINE INDENT if j == 0 : NEW_LINE INDENT temp += t [ 0 ] . upper ( ) NEW_LINE DEDENT else : NEW_LINE INDENT temp += t [ j ] . lower ( ) NEW_LINE DEDENT DEDENT print ( temp ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " ishita ▁ bhuiya " NEW_LINE printInitials ( string ) NEW_LINE DEDENT
Count of strings that can be formed from another string using each character at | Python3 program to print the number of times str2 can be formed from str1 using the characters of str1 only once ; Function to find the number of str2 that can be formed using characters of str1 ; iterate and mark the frequencies of all characters in str1 ; find the minimum frequency of every character in str1 ; Driver Code
import sys NEW_LINE def findNumberOfTimes ( str1 , str2 ) : NEW_LINE INDENT freq = [ 0 ] * 26 NEW_LINE l1 = len ( str1 ) NEW_LINE freq2 = [ 0 ] * 26 NEW_LINE l2 = len ( str2 ) NEW_LINE for i in range ( l1 ) : NEW_LINE INDENT freq [ ord ( str1 [ i ] ) - ord ( " a " ) ] += 1 NEW_LINE DEDENT for i in range ( l2 ) : NEW_LINE INDENT freq2 [ ord ( str2 [ i ] ) - ord ( " a " ) ] += 1 NEW_LINE DEDENT count = sys . maxsize NEW_LINE for i in range ( l2 ) : NEW_LINE count = min ( count , freq [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] / freq2 [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] ) NEW_LINE return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " foreeksgekseg " NEW_LINE str2 = " geeks " NEW_LINE print ( findNumberOfTimes ( str1 , str2 ) ) NEW_LINE DEDENT
String transformation using XOR and OR | function to check if conversion is possible or not ; if lengths are different ; iterate to check if both strings have 1 ; to check if there is even one 1 in string s1 ; to check if there is even one 1 in string s2 ; if both string do not have a '1' . ; Driver code
def solve ( s1 , s2 ) : NEW_LINE INDENT flag1 = 0 NEW_LINE flag2 = 0 NEW_LINE if ( len ( s1 ) != len ( s2 ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT l = len ( s1 ) NEW_LINE for i in range ( 0 , l ) : NEW_LINE INDENT if ( s1 [ i ] == '1' ) : NEW_LINE INDENT flag1 = 1 ; NEW_LINE DEDENT if ( s2 [ i ] == '1' ) : NEW_LINE INDENT flag2 = 1 NEW_LINE DEDENT if ( flag1 & flag2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( ! flag1 &   ! flag2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT s1 = "100101" NEW_LINE s2 = "100000" NEW_LINE if solve ( s1 , s2 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Smallest alphabet greater than a given character | Returns the smallest character from the given set of letters that is greater than K ; Take the first element as l and the rightmost element as r ; if this while condition does not satisfy simply return the first element . ; Return the smallest element ; Driver Code ; Function call
def nextGreatestAlphabet ( alphabets , K ) : NEW_LINE INDENT n = len ( alphabets ) NEW_LINE if ( K >= alphabets [ n - 1 ] ) : NEW_LINE return alphabets [ 0 ] NEW_LINE l = 0 NEW_LINE r = len ( alphabets ) - 1 NEW_LINE ans = - 1 NEW_LINE while ( l <= r ) : NEW_LINE INDENT mid = int ( ( l + r ) / 2 ) NEW_LINE if ( alphabets [ mid ] > K ) : NEW_LINE INDENT r = mid - 1 NEW_LINE ans = mid NEW_LINE DEDENT else : NEW_LINE INDENT l = mid + 1 NEW_LINE DEDENT DEDENT if ( alphabets [ ans ] < K ) : NEW_LINE INDENT return alphabets [ 0 ] NEW_LINE DEDENT else : NEW_LINE INDENT return alphabets [ ans ] NEW_LINE DEDENT DEDENT letters = [ ' A ' , ' r ' , ' z ' ] NEW_LINE K = ' z ' NEW_LINE result = nextGreatestAlphabet ( letters , K ) NEW_LINE print ( result ) NEW_LINE
Longest substring of 0 s in a string formed by k concatenations | Function to calculate maximum length of substring containing only zero ; loop to calculate longest substring in string containing 0 's ; if all elements in string a are '0 ; Else , find prefix and suffix containing only zeroes ; Calculate length of the prefix containing only zeroes ; Calculate length of the suffix containing only zeroes ; if k <= 1 , no need to take suffix + prefix into account ; Driver program to test above function
def subzero ( str , k ) : NEW_LINE INDENT ans = 0 NEW_LINE curr = 0 NEW_LINE n = len ( str ) NEW_LINE for i in str : NEW_LINE INDENT if ( i == '0' ) : NEW_LINE INDENT curr += 1 NEW_LINE DEDENT else : NEW_LINE INDENT curr = 0 NEW_LINE DEDENT ans = max ( ans , curr ) NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if ( ans == n ) : NEW_LINE INDENT print ( n * k ) NEW_LINE return NEW_LINE DEDENT else : NEW_LINE INDENT pre = suff = 0 NEW_LINE for i in str : NEW_LINE INDENT if ( i == '0' ) : NEW_LINE INDENT pre += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( str [ i ] == '0' ) : NEW_LINE INDENT suff += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT if ( k > 1 ) : NEW_LINE INDENT ans = max ( ans , pre + suff ) NEW_LINE DEDENT print ( ans ) NEW_LINE return NEW_LINE DEDENT k = 5 NEW_LINE str = '00100110' NEW_LINE subzero ( str , k ) NEW_LINE
Number of substrings of a string | Python3 program to count number of substrings of a string ; driver code
def countNonEmptySubstr ( str ) : NEW_LINE INDENT n = len ( str ) ; NEW_LINE return int ( n * ( n + 1 ) / 2 ) ; NEW_LINE DEDENT s = " abcde " ; NEW_LINE print ( countNonEmptySubstr ( s ) ) ; NEW_LINE
Count substrings with each character occurring at most k times | Python 3 program to count number of substrings in which each character has count less than or equal to k . ; variable to store count of substrings . ; array to store count of each character . ; increment character count ; check only the count of current character because only the count of this character is changed . The ending point is incremented to current position only if all other characters have count at most k and hence their count is not checked . If count is less than k , then increase ans by 1. ; if count is less than k , then break as subsequent substrings for this starting point will also have count greater than k and hence are reduntant to check . ; return the final count of substrings . ; Driver code
def findSubstrings ( s , k ) : NEW_LINE INDENT ans = 0 NEW_LINE n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT cnt = [ 0 ] * 26 NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT cnt [ ord ( s [ j ] ) - ord ( ' a ' ) ] += 1 NEW_LINE if ( cnt [ ord ( s [ j ] ) - ord ( ' a ' ) ] <= k ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " aaabb " NEW_LINE k = 2 NEW_LINE print ( findSubstrings ( S , k ) ) NEW_LINE DEDENT
Check if characters of one string can be swapped to form other | Python3 program to check if characters of one string can be swapped to form other ; if length is not same print no ; Count frequencies of character in first string . ; iterate through the second string decrement counts of characters in second string ; Since lengths are same , some value would definitely become negative if result is false . ; Driver Code
MAX = 26 NEW_LINE def targetstring ( str1 , str2 ) : NEW_LINE INDENT l1 = len ( str1 ) NEW_LINE l2 = len ( str2 ) NEW_LINE if ( l1 != l2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT map = [ 0 ] * MAX NEW_LINE for i in range ( l1 ) : NEW_LINE INDENT map [ ord ( str1 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( l2 ) : NEW_LINE INDENT map [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] -= 1 NEW_LINE if ( map [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] < 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str1 = " geeksforgeeks " NEW_LINE str2 = " geegeeksksfor " NEW_LINE if ( targetstring ( str1 , str2 ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Find the Number which contain the digit d | Returns true if d is present as digit in number x . ; Breal loop if d is present as digit ; If loop broke ; function to display the values ; Check all numbers one by one ; checking for digit ; Driver code
def isDigitPresent ( x , d ) : NEW_LINE INDENT while ( x > 0 ) : NEW_LINE INDENT if ( x % 10 == d ) : NEW_LINE INDENT break NEW_LINE DEDENT x = x / 10 NEW_LINE DEDENT return ( x > 0 ) NEW_LINE DEDENT def printNumbers ( n , d ) : NEW_LINE INDENT for i in range ( 0 , n + 1 ) : NEW_LINE INDENT if ( i == d or isDigitPresent ( i , d ) ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT n = 47 NEW_LINE d = 7 NEW_LINE print ( " The ▁ number ▁ of ▁ values ▁ are " ) NEW_LINE printNumbers ( n , d ) NEW_LINE
Find one extra character in a string | Python3 program to find extra character in one string ; store string values in map ; store second string in map with frequency ; store first string in map with frequency ; if the frequency is 1 then this character is which is added extra ; Driver Code ; given string ; find Extra Character
def findExtraCharacter ( strA , strB ) : NEW_LINE INDENT m1 = { } NEW_LINE for i in strB : NEW_LINE INDENT if i in m1 : NEW_LINE INDENT m1 [ i ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT m1 [ i ] = 1 NEW_LINE DEDENT DEDENT for i in strA : NEW_LINE INDENT m1 [ i ] -= 1 NEW_LINE DEDENT for h1 in m1 : NEW_LINE INDENT if m1 [ h1 ] == 1 : NEW_LINE INDENT return h1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT strA = ' abcd ' NEW_LINE strB = ' cbdad ' NEW_LINE print ( findExtraCharacter ( strA , strB ) ) NEW_LINE DEDENT
Find one extra character in a string | Python 3 program to find extra character in one string ; result store the result ; traverse string A till end and xor with res ; xor with res ; traverse string B till end and xor with res ; xor with res ; print result at the end ; given string
def findExtraCharcter ( strA , strB ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( 0 , len ( strA ) ) : NEW_LINE INDENT res = res ^ ( ord ) ( strA [ i ] ) NEW_LINE DEDENT for i in range ( 0 , len ( strB ) ) : NEW_LINE INDENT res = res ^ ( ord ) ( strB [ i ] ) NEW_LINE DEDENT return ( ( chr ) ( res ) ) ; NEW_LINE DEDENT strA = " abcd " NEW_LINE strB = " cbdad " NEW_LINE print ( findExtraCharcter ( strA , strB ) ) NEW_LINE
Find one extra character in a string | Python Program to find extra character in one string ; Determine string with extra character ; Add Character codes of both the strings ; Add last character code of large string ; Minus the character code of smaller string from the character code of large string The result will be the extra character code ; Driver code
def findExtraCharacter ( s1 , s2 ) : NEW_LINE INDENT smallStr = " " NEW_LINE largeStr = " " NEW_LINE if ( len ( s1 ) > len ( s2 ) ) : NEW_LINE INDENT smallStr = s2 NEW_LINE largeStr = s1 NEW_LINE DEDENT else : NEW_LINE INDENT smallStr = s1 NEW_LINE largeStr = s2 NEW_LINE DEDENT smallStrCodeTotal = 0 NEW_LINE largeStrCodeTotal = 0 NEW_LINE i = 0 NEW_LINE while ( i < len ( smallStr ) ) : NEW_LINE INDENT smallStrCodeTotal += ord ( smallStr [ i ] ) NEW_LINE largeStrCodeTotal += ord ( largeStr [ i ] ) NEW_LINE i += 1 NEW_LINE DEDENT largeStrCodeTotal += ord ( largeStr [ i ] ) NEW_LINE intChar = largeStrCodeTotal - smallStrCodeTotal NEW_LINE return chr ( intChar ) NEW_LINE DEDENT s1 = " abcd " NEW_LINE s2 = " cbdae " NEW_LINE extraChar = findExtraCharacter ( s1 , s2 ) NEW_LINE print ( " Extra ▁ Character : " , extraChar ) NEW_LINE
Function to copy string ( Iterative and Recursive ) | Function to copy one string to other ; Driver code
def copy_str ( x , y ) : NEW_LINE INDENT if len ( y ) == 0 : NEW_LINE INDENT return x NEW_LINE DEDENT else : NEW_LINE INDENT c = copy_str ( x , ( y ) [ 1 : - 1 ] ) NEW_LINE return c NEW_LINE DEDENT DEDENT x = input ( ) NEW_LINE y = input ( ) NEW_LINE print ( copy_str ( x , y ) ) NEW_LINE
Evaluate an array expression with numbers , + and | Function to find the sum of given array ; if string is empty ; stoi function to convert string into integer ; stoi function to convert string into integer ; Find operator ; If operator is equal to ' + ' , add value in sum variable else subtract ; Driver Function
def calculateSum ( arr , n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT s = arr [ 0 ] NEW_LINE value = int ( s ) NEW_LINE sum = value NEW_LINE for i in range ( 2 , n , 2 ) : NEW_LINE INDENT s = arr [ i ] NEW_LINE value = int ( s ) NEW_LINE operation = arr [ i - 1 ] [ 0 ] NEW_LINE if ( operation == ' + ' ) : NEW_LINE INDENT sum += value NEW_LINE DEDENT else : NEW_LINE INDENT sum -= value NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT arr = [ "3" , " + " , "4" , " - " , "7" , " + " , "13" ] NEW_LINE n = len ( arr ) NEW_LINE print ( calculateSum ( arr , n ) ) NEW_LINE
String with maximum number of unique characters | Function to find string with maximum number of unique characters . ; Index of string with maximum unique characters ; Iterate through all strings ; Array indicating any alphabet included or not included ; count number of unique alphabets in each string ; keep track of maximum number of alphabets ; print result ; Driver code
def LargestString ( na ) : NEW_LINE INDENT N = len ( na ) NEW_LINE c = [ 0 ] * N NEW_LINE m = 0 NEW_LINE for j in range ( N ) : NEW_LINE INDENT character = [ False ] * 26 NEW_LINE for k in range ( len ( na [ j ] ) ) : NEW_LINE INDENT x = ord ( na [ j ] [ k ] ) - ord ( ' A ' ) NEW_LINE if ( ( na [ j ] [ k ] != ' ▁ ' ) and ( character [ x ] == False ) ) : NEW_LINE INDENT c [ j ] += 1 NEW_LINE character [ x ] = True NEW_LINE DEDENT if ( c [ j ] > c [ m ] ) : NEW_LINE INDENT m = j NEW_LINE DEDENT DEDENT DEDENT print ( na [ m ] ) NEW_LINE DEDENT na = [ " BOB " , " A ▁ AB ▁ C ▁ JOHNSON " , " ANJALI " , " ASKRIT " , " ARMAN ▁ MALLIK " ] NEW_LINE LargestString ( na ) NEW_LINE
Program for length of the longest word in a sentence | Python program to find the number of characters in the longest word in the sentence . ; Driver Code
def longestWordLength ( string ) : NEW_LINE INDENT length = 0 NEW_LINE for word in string . split ( ) : NEW_LINE INDENT if ( len ( word ) > length ) : NEW_LINE INDENT length = len ( word ) NEW_LINE DEDENT DEDENT return length NEW_LINE DEDENT string = " I ▁ am ▁ an ▁ intern ▁ at ▁ geeksforgeeks " NEW_LINE print ( longestWordLength ( string ) ) NEW_LINE
Morse Code Implementation | function to encode a alphabet as Morse code ; refer to the Morse table image attached in the article ; for space ; character by character print Morse code ; Driver Code
def morseEncode ( x ) : NEW_LINE INDENT if x is ' a ' : NEW_LINE INDENT return " . - " NEW_LINE DEDENT elif x is ' b ' : NEW_LINE INDENT return " - . . . " NEW_LINE DEDENT elif x is ' c ' : NEW_LINE INDENT return " - . - . " NEW_LINE DEDENT elif x is ' d ' : NEW_LINE INDENT return " - . . " NEW_LINE DEDENT elif x is ' e ' : NEW_LINE INDENT return " . " NEW_LINE DEDENT elif x is ' f ' : NEW_LINE INDENT return " . . - . " NEW_LINE DEDENT elif x is ' g ' : NEW_LINE INDENT return " - - . " NEW_LINE DEDENT elif x is ' h ' : NEW_LINE INDENT return " . . . . " NEW_LINE DEDENT elif x is ' i ' : NEW_LINE INDENT return " . . " NEW_LINE DEDENT elif x is ' j ' : NEW_LINE INDENT return " . - - - " NEW_LINE DEDENT elif x is ' k ' : NEW_LINE INDENT return " - . - " NEW_LINE DEDENT elif x is ' l ' : NEW_LINE INDENT return " . - . . " NEW_LINE DEDENT elif x is ' m ' : NEW_LINE INDENT return " - - " NEW_LINE DEDENT elif x is ' n ' : NEW_LINE INDENT return " - . " NEW_LINE DEDENT elif x is ' o ' : NEW_LINE INDENT return " - - - " NEW_LINE DEDENT elif x is ' p ' : NEW_LINE INDENT return " . - - . " NEW_LINE DEDENT elif x is ' q ' : NEW_LINE INDENT return " - - . - " NEW_LINE DEDENT elif x is ' r ' : NEW_LINE INDENT return " . - . " NEW_LINE DEDENT elif x is ' s ' : NEW_LINE INDENT return " . . . " NEW_LINE DEDENT elif x is ' t ' : NEW_LINE INDENT return " - " NEW_LINE DEDENT elif x is ' u ' : NEW_LINE INDENT return " . . - " NEW_LINE DEDENT elif x is ' v ' : NEW_LINE INDENT return " . . . - " NEW_LINE DEDENT elif x is ' w ' : NEW_LINE INDENT return " . - - " NEW_LINE DEDENT elif x is ' x ' : NEW_LINE INDENT return " - . . - " NEW_LINE DEDENT elif x is ' y ' : NEW_LINE INDENT return " - . - - " NEW_LINE DEDENT elif x is ' z ' : NEW_LINE INDENT return " - - . . " NEW_LINE DEDENT elif x is '1' : NEW_LINE INDENT return " . - - - - " ; NEW_LINE DEDENT elif x is '2' : NEW_LINE INDENT return " . . - - - " ; NEW_LINE DEDENT elif x is '3' : NEW_LINE INDENT return " . . . - - " ; NEW_LINE DEDENT elif x is '4' : NEW_LINE INDENT return " . . . . - " ; NEW_LINE DEDENT elif x is '5' : NEW_LINE INDENT return " . . . . . " ; NEW_LINE DEDENT elif x is '6' : NEW_LINE INDENT return " - . . . . " ; NEW_LINE DEDENT elif x is '7' : NEW_LINE INDENT return " - - . . . " ; NEW_LINE DEDENT elif x is '8' : NEW_LINE INDENT return " - - - . . " ; NEW_LINE DEDENT elif x is '9' : NEW_LINE INDENT return " - - - - . " ; NEW_LINE DEDENT elif x is '0' : NEW_LINE INDENT return " - - - - - " ; NEW_LINE DEDENT DEDENT def morseCode ( s ) : NEW_LINE INDENT for character in s : NEW_LINE INDENT print ( morseEncode ( character ) , end = " " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " geeksforgeeks " NEW_LINE morseCode ( s ) NEW_LINE DEDENT
Polybius Square Cipher | function to display polybius cipher text ; convert each character to its encrypted code ; finding row of the table ; finding column of the table ; if character is 'k ; if character is greater than 'j ; Driver 's Code ; print the cipher of " geeksforgeeks "
def polybiusCipher ( s ) : NEW_LINE INDENT for char in s : NEW_LINE INDENT row = int ( ( ord ( char ) - ord ( ' a ' ) ) / 5 ) + 1 NEW_LINE col = ( ( ord ( char ) - ord ( ' a ' ) ) % 5 ) + 1 NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if char == ' k ' : NEW_LINE INDENT row = row - 1 NEW_LINE col = 5 - col + 1 NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT elif ord ( char ) >= ord ( ' j ' ) : NEW_LINE INDENT if col == 1 : NEW_LINE INDENT col = 6 NEW_LINE row = row - 1 NEW_LINE DEDENT col = col - 1 NEW_LINE DEDENT print ( row , col , end = ' ' , sep = ' ' ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " geeksforgeeks " NEW_LINE polybiusCipher ( s ) NEW_LINE DEDENT
Minimum removal to make palindrome permutation | function to find minimum removal of characters ; hash to store frequency of each character to set hash array to zeros ; count frequency of each character ; count the odd frequency characters ; if count is 0 , return 0 otherwise return count ; Driver 's Code
def minRemoval ( strr ) : NEW_LINE INDENT hash = [ 0 ] * 26 NEW_LINE for char in strr : NEW_LINE INDENT hash [ ord ( char ) - ord ( ' a ' ) ] = hash [ ord ( char ) - ord ( ' a ' ) ] + 1 NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if hash [ i ] % 2 : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT return 0 if count == 0 else count - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT strr = " geeksforgeeks " ; NEW_LINE DEDENT
Union | Python3 program to implement Union - Find with union by rank and path compression . ; Arr to represent parent of index i ; Size to represent the number of nodes in subgxrph rooted at index i ; set parent of every node to itself and size of node to one ; Each time we follow a path , find function compresses it further until the path length is greater than or equal to 1. ; while we reach a node whose parent is equal to itself ; Skip one level ; Move to the new level ; A function that does union of two nodes x and y where xr is root node of x and yr is root node of y ; Make yr parent of xr ; Make xr parent of yr ; The main function to check whether a given graph contains cycle or not ; Itexrte through all edges of gxrph , find nodes connecting them . If root nodes of both are same , then there is cycle in gxrph . ; find root of i ; find root of adj [ i ] [ j ] ; If same parent ; Make them connect ; Driver Code ; Initialize the values for arxry Arr and Size ; Adjacency list for graph ; call is_cycle to check if it contains cycle
MAX_VERTEX = 101 NEW_LINE Arr = [ None ] * MAX_VERTEX NEW_LINE size = [ None ] * MAX_VERTEX NEW_LINE def initialize ( n ) : NEW_LINE INDENT global Arr , size NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT Arr [ i ] = i NEW_LINE size [ i ] = 1 NEW_LINE DEDENT DEDENT def find ( i ) : NEW_LINE INDENT global Arr , size NEW_LINE while ( Arr [ i ] != i ) : NEW_LINE INDENT Arr [ i ] = Arr [ Arr [ i ] ] NEW_LINE i = Arr [ i ] NEW_LINE DEDENT return i NEW_LINE DEDENT def _union ( xr , yr ) : NEW_LINE INDENT global Arr , size NEW_LINE DEDENT if ( size [ xr ] < size [ yr ] ) : NEW_LINE INDENT Arr [ xr ] = Arr [ yr ] NEW_LINE size [ yr ] += size [ xr ] NEW_LINE DEDENT else : NEW_LINE INDENT Arr [ yr ] = Arr [ xr ] NEW_LINE size [ xr ] += size [ yr ] NEW_LINE DEDENT def isCycle ( adj , V ) : NEW_LINE INDENT global Arr , size NEW_LINE for i in range ( V ) : NEW_LINE INDENT for j in range ( len ( adj [ i ] ) ) : NEW_LINE DEDENT DEDENT x = find ( i ) NEW_LINE y = find ( adj [ i ] [ j ] ) NEW_LINE INDENT if ( x == y ) : NEW_LINE DEDENT return 1 NEW_LINE _union ( x , y ) NEW_LINE INDENT return 0 NEW_LINE DEDENT V = 3 NEW_LINE initialize ( V ) NEW_LINE adj = [ [ ] for i in range ( V ) ] NEW_LINE adj [ 0 ] . append ( 1 ) NEW_LINE adj [ 0 ] . append ( 2 ) NEW_LINE adj [ 1 ] . append ( 2 ) NEW_LINE if ( isCycle ( adj , V ) ) : NEW_LINE INDENT print ( " Graph ▁ contains ▁ Cycle . " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Graph ▁ does ▁ not ▁ contain ▁ Cycle . " ) NEW_LINE DEDENT
Queries for frequencies of characters in substrings | Python3 program to find occurrence of character in substring l to r ; To store count of all character ; To pre - process string from 0 to size of string ; Store occurrence of character i ; Store occurrence o all character upto i ; To return occurrence of character in range l to r ; Return occurrence of character from 0 to r minus its occurrence from 0 to l ; Driver Code
MAX_LEN , MAX_CHAR = 1005 , 26 NEW_LINE cnt = [ [ 0 for i in range ( MAX_CHAR ) ] for j in range ( MAX_LEN ) ] NEW_LINE def preProcess ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT cnt [ i ] [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT for j in range ( 0 , 26 ) : NEW_LINE INDENT cnt [ i ] [ j ] += cnt [ i - 1 ] [ j ] NEW_LINE DEDENT DEDENT DEDENT def findCharFreq ( l , r , c ) : NEW_LINE INDENT return ( cnt [ r ] [ ord ( c ) - 97 ] - cnt [ l - 1 ] [ ord ( c ) - 97 ] ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " geeksforgeeks " NEW_LINE Q = 4 NEW_LINE preProcess ( s ) NEW_LINE print ( findCharFreq ( 0 , 5 , ' e ' ) ) NEW_LINE print ( findCharFreq ( 2 , 6 , ' f ' ) ) NEW_LINE print ( findCharFreq ( 4 , 7 , ' m ' ) ) NEW_LINE print ( findCharFreq ( 0 , 12 , ' e ' ) ) NEW_LINE DEDENT