codeparrot/xlcost-text-to-code huggingface dataset:
Check if the characters in a string form a Palindrome in O ( 1 ) extra space | Utility function to get the position of first character in the string ; Get the position of first character in the string ; Utility function to get the position of last character in the string ; Get the position of last character in the string ; Function to check if the characters in the given string forms a Palindrome in O ( 1 ) extra space ; break , when all letters are checked ; if mismatch found , break the loop ; Driver code
def firstPos ( str , start , end ) : NEW_LINE INDENT firstChar = - 1 NEW_LINE for i in range ( start , end + 1 ) : NEW_LINE INDENT if ( str [ i ] >= ' a ' and str [ i ] <= ' z ' ) : NEW_LINE INDENT firstChar = i NEW_LINE break NEW_LINE DEDENT DEDENT return firstChar NEW_LINE DEDENT def lastPos ( str , start , end ) : NEW_LINE INDENT lastChar = - 1 NEW_LINE for i in range ( start , end - 1 , - 1 ) : NEW_LINE INDENT if ( str [ i ] >= ' a ' and str [ i ] <= ' z ' ) : NEW_LINE INDENT lastChar = i NEW_LINE break NEW_LINE DEDENT DEDENT return lastChar NEW_LINE DEDENT def isPalindrome ( str ) : NEW_LINE INDENT firstChar = 0 NEW_LINE lastChar = len ( str ) - 1 NEW_LINE ch = True NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT firstChar = firstPos ( str , firstChar , lastChar ) ; NEW_LINE lastChar = lastPos ( str , lastChar , firstChar ) ; NEW_LINE if ( lastChar < 0 or firstChar < 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( str [ firstChar ] == str [ lastChar ] ) : NEW_LINE INDENT firstChar += 1 NEW_LINE lastChar -= 1 NEW_LINE continue NEW_LINE DEDENT ch = False NEW_LINE break NEW_LINE DEDENT return ( ch ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " m TABSYMBOL a ▁ 343 ▁ la ▁ y ▁ a ▁ l ▁ am " NEW_LINE if ( isPalindrome ( str ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Maximum length subsequence possible of the form R ^ N K ^ N | Function to calculate the maximum length of substring of the form R ^ nK ^ n ; Count no . Of R 's before a K ; Count no . Of K 's after a K ; Update maximum length ; Driver code
def find ( s ) : NEW_LINE INDENT Max = j = countk = countr = 0 NEW_LINE table = [ [ 0 , 0 ] for i in range ( len ( s ) ) ] NEW_LINE for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT if s [ i ] == ' R ' : NEW_LINE INDENT countr += 1 NEW_LINE DEDENT else : NEW_LINE INDENT table [ j ] [ 0 ] = countr NEW_LINE j += 1 NEW_LINE DEDENT DEDENT j -= 1 NEW_LINE for i in range ( len ( s ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if s [ i ] == ' K ' : NEW_LINE INDENT countk += 1 NEW_LINE table [ j ] [ 1 ] = countk NEW_LINE j -= 1 NEW_LINE DEDENT if min ( table [ j + 1 ] [ 0 ] , table [ j + 1 ] [ 1 ] ) > Max : NEW_LINE INDENT Max = min ( table [ j + 1 ] [ 0 ] , table [ j + 1 ] [ 1 ] ) NEW_LINE DEDENT DEDENT return Max NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " RKRRRKKRRKKKKRR " NEW_LINE print ( find ( s ) ) NEW_LINE DEDENT
Longest common anagram subsequence from N strings | Function to store frequency of each character in each string ; Function to Find longest possible sequence of N strings which is anagram to each other ; to get lexicographical largest sequence . ; find minimum of that character ; print that character minimum number of times ; Driver code ; to store frequency of each character in each string ; To get frequency of each character ; Function call
def frequency ( fre , s , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT string = s [ i ] NEW_LINE for j in range ( 0 , len ( string ) ) : NEW_LINE INDENT fre [ i ] [ ord ( string [ j ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT DEDENT DEDENT def LongestSequence ( fre , n ) : NEW_LINE INDENT for i in range ( MAX_CHAR - 1 , - 1 , - 1 ) : NEW_LINE INDENT mi = fre [ 0 ] [ i ] NEW_LINE for j in range ( 1 , n ) : NEW_LINE INDENT mi = min ( fre [ j ] [ i ] , mi ) NEW_LINE DEDENT while mi : NEW_LINE INDENT print ( chr ( ord ( ' a ' ) + i ) , end = " " ) NEW_LINE mi -= 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = [ " loo " , " lol " , " olive " ] NEW_LINE n = len ( s ) NEW_LINE MAX_CHAR = 26 NEW_LINE fre = [ [ 0 for i in range ( 26 ) ] for j in range ( n ) ] NEW_LINE frequency ( fre , s , n ) NEW_LINE LongestSequence ( fre , n ) NEW_LINE DEDENT
Check if two strings are permutation of each other | function to check whether two strings are Permutation of each other ; Get lengths of both strings ; If length of both strings is not same , then they cannot be Permutation ; Sort both strings ; Compare sorted strings ; Driver Code
def arePermutation ( str1 , str2 ) : NEW_LINE INDENT n1 = len ( str1 ) NEW_LINE n2 = len ( str2 ) NEW_LINE if ( n1 != n2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT a = sorted ( str1 ) NEW_LINE str1 = " ▁ " . join ( a ) NEW_LINE b = sorted ( str2 ) NEW_LINE str2 = " ▁ " . join ( b ) NEW_LINE for i in range ( 0 , n1 , 1 ) : NEW_LINE INDENT if ( str1 [ i ] != str2 [ i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " test " NEW_LINE str2 = " ttew " NEW_LINE if ( arePermutation ( str1 , str2 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Check if two strings are permutation of each other | Python3 function to check whether two strings are Permutations of each other ; Create a count array and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; If both strings are of different length . Removing this condition will make the program fail for strings like " aaca " and " aca " ; See if there is any non - zero value in count array
def arePermutation ( str1 , str2 ) : NEW_LINE INDENT count = [ 0 for i in range ( NO_OF_CHARS ) ] NEW_LINE i = 0 NEW_LINE while ( str1 [ i ] and str2 [ i ] ) : NEW_LINE INDENT count [ str1 [ i ] ] += 1 NEW_LINE count [ str2 [ i ] ] -= 1 NEW_LINE DEDENT if ( str1 [ i ] or str2 [ i ] ) : NEW_LINE return False ; NEW_LINE for i in range ( NO_OF_CHARS ) : NEW_LINE INDENT if ( count [ i ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT
Maximum power of jump required to reach the end of string | Function to calculate the maximum power of the jump ; Initialize the count with 1 ; Find the character at last index ; Start traversing the string ; Check if the current char is equal to the last character ; max_so_far stores maximum value of the power of the jump from starting to ith position ; Reset the count to 1 ; Else , increment the number of jumps / count ; Return the maximum number of jumps ; Driver Code
def powerOfJump ( s ) : NEW_LINE INDENT count = 1 NEW_LINE max_so_far = 0 NEW_LINE ch = s [ - 1 ] NEW_LINE for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT if s [ i ] == ch : NEW_LINE INDENT if count > max_so_far : NEW_LINE INDENT max_so_far = count NEW_LINE DEDENT count = 1 NEW_LINE DEDENT else : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return max_so_far NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = "1010101" NEW_LINE print ( powerOfJump ( st ) ) NEW_LINE DEDENT
Longest substring with count of 1 s more than 0 s | Function to find longest substring having count of 1 s more than count of 0 s . ; To store sum . ; To store first occurrence of each sum value . ; To store maximum length . ; To store current substring length . ; Add 1 if current character is 1 else subtract 1. ; If sum is positive , then maximum length substring is bin1 [ 0. . i ] ; If sum is negative , then maximum length substring is bin1 [ j + 1. . i ] , where sum of substring bin1 [ 0. . j ] is sum - 1. ; Make entry for this sum value in hash table if this value is not present . ; Driver code
def findLongestSub ( bin1 ) : NEW_LINE INDENT n = len ( bin1 ) NEW_LINE sum = 0 NEW_LINE prevSum = { i : 0 for i in range ( n ) } NEW_LINE maxlen = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( bin1 [ i ] == '1' ) : NEW_LINE INDENT sum += 1 NEW_LINE DEDENT else : NEW_LINE INDENT sum -= 1 NEW_LINE DEDENT if ( sum > 0 ) : NEW_LINE INDENT maxlen = i + 1 NEW_LINE DEDENT elif ( sum <= 0 ) : NEW_LINE INDENT if ( ( sum - 1 ) in prevSum ) : NEW_LINE INDENT currlen = i - prevSum [ sum - 1 ] NEW_LINE maxlen = max ( maxlen , currlen ) NEW_LINE DEDENT DEDENT if ( ( sum ) not in prevSum ) : NEW_LINE INDENT prevSum [ sum ] = i NEW_LINE DEDENT DEDENT return maxlen NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT bin1 = "1010" NEW_LINE print ( findLongestSub ( bin1 ) ) NEW_LINE DEDENT
Average of ASCII values of characters of a given string | Function to find average of ASCII value of chars ; loop to sum the ascii value of chars ; Returning average of chars ; Driver code
def averageValue ( s ) : NEW_LINE INDENT sum_char = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT sum_char += ord ( s [ i ] ) NEW_LINE DEDENT return sum_char // len ( s ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " GeeksforGeeks " NEW_LINE print ( averageValue ( s ) ) NEW_LINE DEDENT
Check if two same sub | Function to check if similar subsequences occur in a string or not ; iterate and count the frequency ; counting frequency of the letters ; check if frequency is more than once of any character ; Driver Code
def check ( s , l ) : NEW_LINE INDENT freq = [ 0 for i in range ( 26 ) ] NEW_LINE for i in range ( l ) : NEW_LINE INDENT freq [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( freq [ i ] >= 2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " geeksforgeeks " NEW_LINE l = len ( s ) NEW_LINE if ( check ( s , l ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Find the winner of a game where scores are given as a binary string | function for winner prediction ; increase count ; check losing condition ; check winning condition ; check tie on n - 1 point ; increase count ; check for 2 point lead ; condition of lost ; condition of win ; Driver Code
def predictWinner ( score , n ) : NEW_LINE INDENT count = [ 0 for i in range ( 2 ) ] NEW_LINE for i in range ( 0 , len ( score ) , 1 ) : NEW_LINE INDENT index = ord ( score [ i ] ) - ord ( '0' ) NEW_LINE count [ index ] += 1 NEW_LINE if ( count [ 0 ] == n and count [ 1 ] < n - 1 ) : NEW_LINE INDENT print ( " GEEKS ▁ lost " , end = " ▁ " ) NEW_LINE return NEW_LINE DEDENT if ( count [ 1 ] == n and count [ 0 ] < n - 1 ) : NEW_LINE INDENT print ( " GEEKS ▁ won " , end = " ▁ " ) NEW_LINE return NEW_LINE DEDENT if ( count [ 0 ] == n - 1 and count [ 1 ] == n - 1 ) : NEW_LINE INDENT count [ 0 ] = 0 NEW_LINE count [ 1 ] = 0 NEW_LINE break NEW_LINE DEDENT DEDENT i += 1 NEW_LINE for i in range ( i , len ( score ) , 1 ) : NEW_LINE INDENT index = ord ( score [ i ] ) - ord ( '0' ) NEW_LINE count [ index ] += 1 NEW_LINE if ( abs ( count [ 0 ] - count [ 1 ] ) == 2 ) : NEW_LINE INDENT if ( count [ 0 ] > count [ 1 ] ) : NEW_LINE INDENT print ( " GEEKS ▁ lost " , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " GEEKS ▁ won " , end = " ▁ " ) ; NEW_LINE DEDENT return NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT score = "1001010101111011101111" NEW_LINE n = 15 NEW_LINE predictWinner ( score , n ) NEW_LINE DEDENT
Number of Counterclockwise shifts to make a string palindrome | Function to check if given string is palindrome or not . ; Function to find counter clockwise shifts to make string palindrome . ; Pointer to starting of current shifted string . ; Pointer to ending of current shifted string . ; Concatenate string with itself ; To store counterclockwise shifts ; Move left and right pointers one step at a time . ; Check if current shifted string is palindrome or not ; If string is not palindrome then increase count of number of shifts by 1. ; Driver code .
def isPalindrome ( str , l , r ) : NEW_LINE INDENT while ( l < r ) : NEW_LINE INDENT if ( str [ l ] != str [ r ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT l += 1 NEW_LINE r -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def CyclicShifts ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE left = 0 NEW_LINE right = n - 1 NEW_LINE str = str + str NEW_LINE cnt = 0 NEW_LINE while ( right < 2 * n - 1 ) : NEW_LINE INDENT if ( isPalindrome ( str , left , right ) ) : NEW_LINE INDENT break NEW_LINE DEDENT cnt += 1 NEW_LINE left += 1 NEW_LINE right += 1 NEW_LINE DEDENT return cnt NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " bccbbaab " ; NEW_LINE print ( CyclicShifts ( str ) ) NEW_LINE DEDENT
Find a string such that every character is lexicographically greater than its immediate next character | Function that prints the required string ; Find modulus with 26 ; Print extra characters required ; Print the given reverse string countOfStr times ; Driver Code ; Initialize a string in reverse order
def printString ( n , str ) : NEW_LINE INDENT str2 = " " NEW_LINE extraChar = n % 26 NEW_LINE if ( extraChar >= 1 ) : NEW_LINE INDENT for i in range ( 26 - ( extraChar + 1 ) , 26 ) : NEW_LINE INDENT str2 += str [ i ] NEW_LINE DEDENT DEDENT countOfStr = n // 26 NEW_LINE for i in range ( 1 , countOfStr + 1 ) : NEW_LINE INDENT for j in range ( 26 ) : NEW_LINE INDENT str2 += str [ j ] NEW_LINE DEDENT DEDENT return str2 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 30 NEW_LINE str = " zyxwvutsrqponmlkjihgfedcba " NEW_LINE print ( printString ( n , str ) ) NEW_LINE DEDENT
Longest Common Prefix Matching | Set | A Utility Function to find the common prefix between first and last strings ; Compare str1 and str2 ; A Function that returns the longest common prefix from the array of strings ; sorts the N set of strings ; prints the common prefix of the first and the last string of the set of strings ; Driver Code
def commonPrefixUtil ( str1 , str2 ) : NEW_LINE INDENT n1 = len ( str1 ) NEW_LINE n2 = len ( str2 ) NEW_LINE result = " " NEW_LINE j = 0 NEW_LINE i = 0 NEW_LINE while ( i <= n1 - 1 and j <= n2 - 1 ) : NEW_LINE INDENT if ( str1 [ i ] != str2 [ j ] ) : NEW_LINE INDENT break NEW_LINE DEDENT result += ( str1 [ i ] ) NEW_LINE i += 1 NEW_LINE j += 1 NEW_LINE DEDENT return ( result ) NEW_LINE DEDENT def commonPrefix ( arr , n ) : NEW_LINE INDENT arr . sort ( reverse = False ) NEW_LINE print ( commonPrefixUtil ( arr [ 0 ] , arr [ n - 1 ] ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ " geeksforgeeks " , " geeks " , " geek " , " geezer " ] NEW_LINE n = len ( arr ) NEW_LINE commonPrefix ( arr , n ) NEW_LINE DEDENT
Square of large number represented as String | Multiplies str1 and str2 , and prints result . ; Will keep the result number in vector in reverse order ; Below two indexes are used to find positions in result . ; Go from right to left in num1 ; To shift position to left after every multiplication of a digit in num2 ; Go from right to left in num2 ; Take current digit of second number ; Multiply with current digit of first number and add result to previously stored result at current position . ; Carry for next iteration ; Store result ; Store carry in next cell ; To shift position to left after every multiplication of a digit in num1 . ; Ignore '0' s from the right ; If all were '0' s - means either both or one of num1 or num2 were '0 ; Generate the result string ; Driver code
def multiply ( num1 , num2 ) : NEW_LINE INDENT n1 = len ( num1 ) NEW_LINE n2 = len ( num2 ) NEW_LINE if ( n1 == 0 or n2 == 0 ) : NEW_LINE INDENT return "0" NEW_LINE DEDENT result = [ 0 ] * ( n1 + n2 ) NEW_LINE i_n1 = 0 NEW_LINE i_n2 = 0 NEW_LINE for i in range ( n1 - 1 , - 1 , - 1 ) : NEW_LINE INDENT carry = 0 NEW_LINE n_1 = ord ( num1 [ i ] ) - ord ( '0' ) NEW_LINE i_n2 = 0 NEW_LINE for j in range ( n2 - 1 , - 1 , - 1 ) : NEW_LINE INDENT n_2 = ord ( num2 [ j ] ) - ord ( '0' ) NEW_LINE sum = n_1 * n_2 + result [ i_n1 + i_n2 ] + carry NEW_LINE carry = sum // 10 NEW_LINE result [ i_n1 + i_n2 ] = sum % 10 NEW_LINE i_n2 += 1 NEW_LINE DEDENT if ( carry > 0 ) : NEW_LINE INDENT result [ i_n1 + i_n2 ] += carry NEW_LINE DEDENT i_n1 += 1 NEW_LINE DEDENT i = len ( result ) - 1 NEW_LINE while ( i >= 0 and result [ i ] == 0 ) : NEW_LINE INDENT i -= 1 NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT if ( i == - 1 ) : NEW_LINE INDENT return "0" NEW_LINE DEDENT s = " " NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT s += str ( result [ i ] ) NEW_LINE i -= 1 NEW_LINE DEDENT return s NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str1 = "454545454545454545" NEW_LINE print ( multiply ( str1 , str1 ) ) NEW_LINE DEDENT
Perform n steps to convert every digit of a number in the format [ count ] [ digit ] | Function to perform every step ; perform N steps ; Traverse in the string ; for last digit ; recur for current string ; Driver Code
def countDigits ( st , n ) : NEW_LINE INDENT if ( n > 0 ) : NEW_LINE INDENT cnt = 1 NEW_LINE i = 0 NEW_LINE st2 = " " NEW_LINE i = 1 NEW_LINE while ( i < len ( st ) ) : NEW_LINE INDENT if ( st [ i ] == st [ i - 1 ] ) : NEW_LINE INDENT cnt = cnt + 1 NEW_LINE DEDENT else : NEW_LINE INDENT st2 += chr ( 48 + cnt ) NEW_LINE st2 += st [ i - 1 ] NEW_LINE cnt = 1 NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT st2 += chr ( 48 + cnt ) NEW_LINE st2 += st [ i - 1 ] NEW_LINE countDigits ( st2 , n - 1 ) NEW_LINE n = n - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( st ) NEW_LINE DEDENT DEDENT num = "123" NEW_LINE n = 3 NEW_LINE countDigits ( num , n ) NEW_LINE
Sudo Placement | Special Subsequences | Python3 program to find all special subsequences of the given type ; Function to generate the required subsequences ; If the index pointer has reached the end of input string ; Skip empty ( " ▁ " ) subsequence ; Exclude current character in output string ; Include current character in output string ; Remove the current included character and and include it in its uppercase form ; Function to print the required subsequences ; Output String to store every subsequence ; Set of strings that will store all special subsequences in lexicographical sorted order ; Sort the strings to print in sorted order ; Driver Code
from typing import List NEW_LINE def generateSubsequences ( input : str , output : str , idx : int , ans : List [ str ] ) -> None : NEW_LINE INDENT if idx == len ( input ) : NEW_LINE INDENT if ( len ( output ) ) : NEW_LINE INDENT ans . append ( output ) NEW_LINE DEDENT return NEW_LINE DEDENT generateSubsequences ( input , output , idx + 1 , ans ) NEW_LINE output += input [ idx ] NEW_LINE generateSubsequences ( input , output , idx + 1 , ans ) NEW_LINE output = output [ : - 1 ] NEW_LINE upper = input [ idx ] NEW_LINE upper = upper . upper ( ) NEW_LINE output += upper NEW_LINE generateSubsequences ( input , output , idx + 1 , ans ) NEW_LINE DEDENT def printSubsequences ( S : str ) -> None : NEW_LINE INDENT output = " " NEW_LINE ans = [ ] NEW_LINE generateSubsequences ( S , output , 0 , ans ) NEW_LINE ans . sort ( ) NEW_LINE for string in ans : NEW_LINE INDENT print ( string , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " ab " NEW_LINE printSubsequences ( S ) NEW_LINE DEDENT
Check if the given string of words can be formed from words present in the dictionary | Function to check if the word is in the dictionary or not ; map to store all words in dictionary with their count ; adding all words in map ; search in map for all words in the sentence ; all words of sentence are present ; Driver Code ; Calling function to check if words are present in the dictionary or not
def match_words ( dictionary , sentence , n , m ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ dictionary [ i ] ] = mp . get ( dictionary [ i ] , 0 ) + 1 NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT if ( mp [ sentence [ i ] ] ) : NEW_LINE INDENT mp [ sentence [ i ] ] -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT dictionary = [ " find " , " a " , " geeks " , " all " , " for " , " on " , " geeks " , " answers " , " inter " ] NEW_LINE n = len ( dictionary ) NEW_LINE sentence = [ " find " , " all " , " answers " , " on " , " geeks " , " for " , " geeks " ] NEW_LINE m = len ( sentence ) NEW_LINE if ( match_words ( dictionary , sentence , n , m ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Print all 3 digit repeating numbers in a very large number | Function to print 3 digit repeating numbers ; Hashmap to store the frequency of a 3 digit number ; first three digit number ; if key already exists increase value by 1 ; Output the three digit numbers with frequency > 1 ; Driver Code ; Input string ; Calling Function
def printNum ( s ) : NEW_LINE INDENT i , j , val = 0 , 0 , 0 NEW_LINE mp = { } NEW_LINE val = ( ( ord ( s [ 0 ] ) - ord ( '0' ) ) * 100 + ( ord ( s [ 1 ] ) - ord ( '0' ) ) * 10 + ( ord ( s [ 2 ] ) - ord ( '0' ) ) ) NEW_LINE mp [ val ] = 1 NEW_LINE for i in range ( 3 , len ( s ) ) : NEW_LINE INDENT val = ( val % 100 ) * 10 + ord ( s [ i ] ) - ord ( '0' ) NEW_LINE if ( val in mp ) : NEW_LINE INDENT mp [ val ] = mp [ val ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ val ] = 1 NEW_LINE DEDENT DEDENT for m in mp : NEW_LINE INDENT key = m NEW_LINE value = mp [ m ] NEW_LINE if ( value > 1 ) : NEW_LINE INDENT print ( key , " ▁ - ▁ " , value , " ▁ times " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT input = "123412345123456" NEW_LINE printNum ( input ) NEW_LINE DEDENT
Count occurrences of a substring recursively | Recursive function to count the number of occurrences of " hi " in str . ; Base Case ; Recursive Case Checking if the first substring matches ; Otherwise , return the count from the remaining index ; Driver Code
def countSubstrig ( str1 , str2 ) : NEW_LINE INDENT n1 = len ( str1 ) ; NEW_LINE n2 = len ( str2 ) ; NEW_LINE if ( n1 == 0 or n1 < n2 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( str1 [ 0 : n2 ] == str2 ) : NEW_LINE INDENT return countSubstrig ( str1 [ n2 - 1 : ] , str2 ) + 1 ; NEW_LINE DEDENT return countSubstrig ( str1 [ n2 - 1 : ] , str2 ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " geeksforgeeks " ; NEW_LINE str2 = " geeks " ; NEW_LINE print ( countSubstrig ( str1 , str2 ) ) ; NEW_LINE str1 = " hikakashi " ; NEW_LINE str2 = " hi " ; NEW_LINE print ( countSubstrig ( str1 , str2 ) ) ; NEW_LINE DEDENT
Check if a binary string contains all permutations of length k | Python3 Program to Check If a String Contains All Binary Codes of Size K ; Unordered map of type string ; Driver code
def hasAllcodes ( s , k ) : NEW_LINE INDENT us = set ( ) NEW_LINE for i in range ( len ( s ) + 1 ) : NEW_LINE INDENT us . add ( s [ i : k ] ) NEW_LINE DEDENT return len ( us ) == 1 << k NEW_LINE DEDENT s = "00110110" NEW_LINE k = 2 NEW_LINE if ( hasAllcodes ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Add n binary strings | This function adds two binary strings and return result as a third string ; Initialize result ; Initialize digit sum ; Traverse both strings starting from last characters ; Compute sum of last digits and carry ; If current digit sum is 1 or 3 , add 1 to result ; Compute carry ; Move to next digits ; function to add n binary strings ; Driver code
def addBinaryUtil ( a , b ) : NEW_LINE result = " " ; NEW_LINE s = 0 ; NEW_LINE INDENT i = len ( a ) - 1 ; NEW_LINE j = len ( b ) - 1 ; NEW_LINE while ( i >= 0 or j >= 0 or s == 1 ) : NEW_LINE INDENT s += ( ord ( a [ i ] ) - ord ( '0' ) ) if ( i >= 0 ) else 0 ; NEW_LINE s += ( ord ( b [ j ] ) - ord ( '0' ) ) if ( j >= 0 ) else 0 ; NEW_LINE result = chr ( s % 2 + ord ( '0' ) ) + result ; NEW_LINE s //= 2 ; NEW_LINE i -= 1 ; NEW_LINE j -= 1 ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT def addBinary ( arr , n ) : NEW_LINE INDENT result = " " ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT result = addBinaryUtil ( result , arr [ i ] ) ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT arr = [ "1" , "10" , "11" ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( addBinary ( arr , n ) ) ; NEW_LINE