codeparrot/xlcost-text-to-code huggingface dataset:
Make the string lexicographically smallest and non palindromic by replacing exactly one character | Function to find the required string ; Length of the string ; Iterate till half of the string ; Replacing a non ' a ' char with 'a ; Check if there is no ' a ' in string we replace last char of string by 'b ; If the input is a single character ; Driver Code
def findStr ( S ) : NEW_LINE INDENT S = list ( S ) NEW_LINE n = len ( S ) NEW_LINE for i in range ( 0 , n // 2 ) : NEW_LINE DEDENT ' NEW_LINE INDENT if S [ i ] != ' a ' : NEW_LINE INDENT S [ i ] = ' a ' NEW_LINE return ( ' ' . join ( S ) ) NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT S [ n - 1 ] = ' b ' NEW_LINE if n < 2 : NEW_LINE INDENT return ' - 1' NEW_LINE DEDENT else : NEW_LINE INDENT return ( ' ' . join ( S ) ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = ' a ' NEW_LINE print ( findStr ( str1 ) ) NEW_LINE str2 = ' abccba ' NEW_LINE print ( findStr ( str2 ) ) NEW_LINE DEDENT
Find the k | Function to find the Kth string in lexicographical order which consists of N - 2 xs and 2 ys ; Iterate for all possible positions of left most Y ; If i is the left most position of Y ; Put Y in their positions ; Driver code ; Function call
def kth_string ( n , k ) : NEW_LINE INDENT for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT if k <= ( n - i - 1 ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( j == i or j == n - k ) : NEW_LINE INDENT print ( ' Y ' , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' X ' , end = " " ) NEW_LINE DEDENT DEDENT break NEW_LINE DEDENT k -= ( n - i - 1 ) NEW_LINE DEDENT DEDENT n = 5 NEW_LINE k = 7 NEW_LINE kth_string ( n , k ) NEW_LINE
Transform string str1 into str2 by taking characters from string str3 | Function to check whether str1 can be transformed to str2 ; To store the frequency of characters of string str3 ; Declare two pointers & flag ; Traverse both the string and check whether it can be transformed ; If both pointers point to same characters increment them ; If the letters don 't match check  if we can find it in string C ; If the letter is available in string str3 , decrement it 's  frequency & increment the ptr2 ; If letter isn 't present in str3[]  set the flag to false and break ; If the flag is true and both pointers points to their end of respective strings then it is possible to transformed str1 into str2 , otherwise not . ; Driver Code ; Function Call
def convertString ( str1 , str2 , str3 ) : NEW_LINE INDENT freq = { } NEW_LINE for i in range ( len ( str3 ) ) : NEW_LINE INDENT if ( freq . get ( str3 [ i ] ) == None ) : NEW_LINE INDENT freq [ str3 [ i ] ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT freq . get ( str3 [ i ] , 1 ) NEW_LINE DEDENT DEDENT ptr1 = 0 NEW_LINE ptr2 = 0 ; NEW_LINE flag = True NEW_LINE while ( ptr1 < len ( str1 ) and ptr2 < len ( str2 ) ) : NEW_LINE INDENT if ( str1 [ ptr1 ] == str2 [ ptr2 ] ) : NEW_LINE INDENT ptr1 += 1 NEW_LINE ptr2 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( freq [ str3 [ ptr2 ] ] > 0 ) : NEW_LINE INDENT freq [ str3 [ ptr2 ] ] -= 1 NEW_LINE ptr2 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT if ( flag and ptr1 == len ( str1 ) and ptr2 == len ( str2 ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " abyzfe " NEW_LINE str2 = " abcdeyzf " NEW_LINE str3 = " popode " NEW_LINE convertString ( str1 , str2 , str3 ) NEW_LINE DEDENT
Decrypt the String according to given algorithm | Python3 implementation of the approach ; Function that returns ( num % 26 ) ; Initialize result ; One by one process all digits of 'num ; Function to return the decrypted string ; To store the final decrypted answer ; One by one check for each character if it is a numeric character ; Modulo the number found in the string by 26 ; Driver code ; Print the decrypted string
MOD = 26 NEW_LINE def modulo_by_26 ( num ) : NEW_LINE INDENT res = 0 NEW_LINE DEDENT ' NEW_LINE INDENT for i in range ( len ( num ) ) : NEW_LINE INDENT res = ( ( res * 10 + ord ( num [ i ] ) - ord ( '0' ) ) % MOD ) NEW_LINE DEDENT return res NEW_LINE DEDENT def decrypt_message ( s ) : NEW_LINE INDENT decrypted_str = " " NEW_LINE num_found_so_far = " " NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] >= '0' and s [ i ] <= '9' ) : NEW_LINE INDENT num_found_so_far += s [ i ] NEW_LINE DEDENT elif ( len ( num_found_so_far ) > 0 ) : NEW_LINE INDENT decrypted_str += chr ( ord ( ' a ' ) + ( modulo_by_26 ( num_found_so_far ) ) ) NEW_LINE num_found_so_far = " " NEW_LINE DEDENT DEDENT if ( len ( num_found_so_far ) > 0 ) : NEW_LINE INDENT decrypted_str += chr ( ord ( ' a ' ) + ( modulo_by_26 ( num_found_so_far ) ) ) NEW_LINE DEDENT return decrypted_str NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "32ytAAcV4ui30hf10hj18" NEW_LINE print ( decrypt_message ( s ) ) NEW_LINE DEDENT
Maximum number of overlapping string | Function to find the maximum overlapping strings ; Get the current character ; Condition to check if the current character is the first character of the string T then increment the overlapping count ; Condition to check previous character is also occurred ; Update count of previous and current character ; Condition to check the current character is the last character of the string T ; Condition to check the every subsequence is a valid string T ; Driver Code ; Function Call
def maxOverlap ( S , T ) : NEW_LINE INDENT str = T NEW_LINE count = [ 0 for i in range ( len ( T ) ) ] NEW_LINE overlap = 0 NEW_LINE max_overlap = 0 NEW_LINE for i in range ( 0 , len ( S ) ) : NEW_LINE INDENT index = str . find ( S [ i ] ) NEW_LINE if ( index == 0 ) : NEW_LINE INDENT overlap += 1 NEW_LINE if ( overlap >= 2 ) : NEW_LINE INDENT max_overlap = max ( overlap , max_overlap ) NEW_LINE DEDENT count [ index ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( count [ index - 1 ] <= 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT count [ index ] += 1 NEW_LINE count [ index - 1 ] -= 1 NEW_LINE DEDENT if ( index == 4 ) : NEW_LINE INDENT overlap -= 1 NEW_LINE DEDENT DEDENT if ( overlap == 0 ) : NEW_LINE INDENT return max_overlap NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT S = " chcirphirp " NEW_LINE T = " chirp " NEW_LINE print ( maxOverlap ( S , T ) ) NEW_LINE
Minimum number of operations required to make two strings equal | Python3 implementation to find the minimum number of operations to make two strings equal ; Function to find out parent of an alphabet ; Function to merge two different alphabets ; Merge a and b using rank compression ; Function to find the minimum number of operations required ; Initializing parent to i and rank ( size ) to 1 ; We will store our answerin this list ; Traversing strings ; If they have different parents ; Find their respective parents and merge them ; Store this in our Answer list ; Number of operations ; Driver code ; Two strings S1 and S2 ; Function Call
MAX = 500001 NEW_LINE parent = [ 0 ] * MAX NEW_LINE Rank = [ 0 ] * MAX NEW_LINE def find ( x ) : NEW_LINE INDENT if parent [ x ] == x : NEW_LINE INDENT return x NEW_LINE DEDENT else : NEW_LINE INDENT return find ( parent [ x ] ) NEW_LINE DEDENT DEDENT def merge ( r1 , r2 ) : NEW_LINE INDENT if ( r1 != r2 ) : NEW_LINE INDENT if ( Rank [ r1 ] > Rank [ r2 ] ) : NEW_LINE INDENT parent [ r2 ] = r1 NEW_LINE Rank [ r1 ] += Rank [ r2 ] NEW_LINE DEDENT else : NEW_LINE INDENT parent [ r1 ] = r2 NEW_LINE Rank [ r2 ] += Rank [ r1 ] NEW_LINE DEDENT DEDENT DEDENT def minimumOperations ( s1 , s2 ) : NEW_LINE INDENT for i in range ( 1 , 26 + 1 ) : NEW_LINE INDENT parent [ i ] = i NEW_LINE Rank [ i ] = 1 NEW_LINE DEDENT ans = [ ] NEW_LINE for i in range ( len ( s1 ) ) : NEW_LINE INDENT if ( s1 [ i ] != s2 [ i ] ) : NEW_LINE INDENT if ( find ( ord ( s1 [ i ] ) - 96 ) != find ( ord ( s2 [ i ] ) - 96 ) ) : NEW_LINE INDENT x = find ( ord ( s1 [ i ] ) - 96 ) NEW_LINE y = find ( ord ( s2 [ i ] ) - 96 ) NEW_LINE merge ( x , y ) NEW_LINE ans . append ( [ s1 [ i ] , s2 [ i ] ] ) NEW_LINE DEDENT DEDENT DEDENT print ( len ( ans ) ) NEW_LINE for i in ans : NEW_LINE INDENT print ( i [ 0 ] , " - > " , i [ 1 ] ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s1 = " abb " NEW_LINE s2 = " dad " NEW_LINE minimumOperations ( s1 , s2 ) NEW_LINE DEDENT
Find the N | Function to calculate nth permutation of string ; Creating an empty list ; Subtracting 1 from N because the permutations start from 0 in factroid method ; Loop to generate the factroid of the sequence ; Loop to generate nth permutation ; Remove 1 - element in each cycle ; Final answer ; Driver code
def string_permutation ( n , str ) : NEW_LINE INDENT s = [ ] NEW_LINE result = " " NEW_LINE str = list ( str ) NEW_LINE n = n - 1 NEW_LINE for i in range ( 1 , len ( str ) + 1 ) : NEW_LINE INDENT s . append ( n % i ) NEW_LINE n = int ( n / i ) NEW_LINE DEDENT for i in range ( len ( str ) ) : NEW_LINE INDENT a = s [ - 1 ] NEW_LINE result += str [ a ] NEW_LINE for j in range ( a , len ( str ) - 1 ) : NEW_LINE INDENT str [ j ] = str [ j + 1 ] NEW_LINE DEDENT str [ j + 1 ] = ' \0' NEW_LINE s . pop ( ) NEW_LINE DEDENT print ( result ) NEW_LINE DEDENT str = " abcde " NEW_LINE n = 11 NEW_LINE string_permutation ( n , str ) NEW_LINE
Defanged Version of Internet Protocol Address | Function to generate a defanged version of IP address . ; Loop to iterate over the characters of the string ; Driver Code
def GeberateDefangIP ( str ) : NEW_LINE INDENT defangIP = " " ; NEW_LINE for c in str : NEW_LINE INDENT if ( c == ' . ' ) : NEW_LINE INDENT defangIP += " [ . ] " NEW_LINE DEDENT else : NEW_LINE INDENT defangIP += c ; NEW_LINE DEDENT DEDENT return defangIP ; NEW_LINE DEDENT str = "255.100.50.0" ; NEW_LINE print ( GeberateDefangIP ( str ) ) ; NEW_LINE
Longest palindromic string formed by concatenation of prefix and suffix of a string | Function used to calculate the longest prefix which is also a suffix ; Traverse the string ; Update the lps size ; Returns size of lps ; Function to calculate the length of longest palindromic substring which is either a suffix or prefix ; Append a character to separate the string and reverse of the string ; Reverse the string ; Append the reversed string ; Function to find the Longest palindromic string formed from concatenation of prefix and suffix of a given string ; Calculating the length for which prefix is reverse of suffix ; Append prefix to the answer ; Store the remaining string ; If the remaining string is not empty that means that there can be a palindrome substring which can be added between the suffix & prefix ; Calculate the length of longest prefix palindromic substring ; Reverse the given string to find the longest palindromic suffix ; Calculate the length of longest prefix palindromic substring ; If the prefix palindrome is greater than the suffix palindrome ; Append the prefix to the answer ; If the suffix palindrome is greater than the prefix palindrome ; Append the suffix to the answer ; Finally append the suffix to the answer ; Return the answer string ; Driver Code
def kmp ( s ) : NEW_LINE INDENT lps = [ 0 ] * ( len ( s ) ) NEW_LINE for i in range ( 1 , len ( s ) ) : NEW_LINE INDENT previous_index = lps [ i - 1 ] NEW_LINE while ( previous_index > 0 and s [ i ] != s [ previous_index ] ) : NEW_LINE INDENT previous_index = lps [ previous_index - 1 ] NEW_LINE DEDENT lps [ i ] = previous_index NEW_LINE if ( s [ i ] == s [ previous_index ] ) : NEW_LINE INDENT lps [ i ] += 1 NEW_LINE DEDENT DEDENT return lps [ - 1 ] NEW_LINE DEDENT def remainingStringLongestPallindrome ( s ) : NEW_LINE INDENT t = s + " ? " NEW_LINE s = s [ : : - 1 ] NEW_LINE t += s NEW_LINE return kmp ( t ) NEW_LINE DEDENT def longestPrefixSuffixPallindrome ( s ) : NEW_LINE INDENT length = 0 NEW_LINE n = len ( s ) NEW_LINE i = 0 NEW_LINE j = n - 1 NEW_LINE while i < j : NEW_LINE INDENT if ( s [ i ] != s [ j ] ) : NEW_LINE INDENT break NEW_LINE DEDENT i += 1 NEW_LINE j -= 1 NEW_LINE length += 1 NEW_LINE DEDENT ans = s [ 0 : length ] NEW_LINE remaining = s [ length : length + ( n - ( 2 * length ) ) ] NEW_LINE if ( len ( remaining ) ) : NEW_LINE INDENT longest_prefix = remainingStringLongestPallindrome ( remaining ) ; NEW_LINE remaining = remaining [ : : - 1 ] NEW_LINE longest_suffix = remainingStringLongestPallindrome ( remaining ) ; NEW_LINE if ( longest_prefix > longest_suffix ) : NEW_LINE INDENT remaining = remaining [ : : - 1 ] NEW_LINE ans += remaining [ 0 : longest_prefix ] NEW_LINE DEDENT else : NEW_LINE INDENT ans += remaining [ 0 : longest_suffix ] NEW_LINE DEDENT DEDENT ans += s [ n - length : n ] NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = " rombobinnimor " NEW_LINE print ( longestPrefixSuffixPallindrome ( st ) ) NEW_LINE DEDENT
Generate a string with maximum possible alphabets with odd frequencies | Function to generate a string of length n with maximum possible alphabets each occuring odd number of times . ; If n is odd ; Add all characters from b - y ; Append a to fill the remaining length ; If n is even ; Add all characters from b - z ; Append a to fill the remaining length ; Driver code
def generateTheString ( n ) : NEW_LINE INDENT ans = " " NEW_LINE if ( n % 2 ) : NEW_LINE INDENT for i in range ( min ( n , 24 ) ) : NEW_LINE INDENT ans += chr ( ord ( ' b ' ) + i ) NEW_LINE DEDENT if ( n > 24 ) : NEW_LINE INDENT for i in range ( ( n - 24 ) ) : NEW_LINE INDENT ans += ' a ' NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT for i in range ( min ( n , 25 ) ) : NEW_LINE INDENT ans += chr ( ord ( ' b ' ) + i ) NEW_LINE DEDENT if ( n > 25 ) : NEW_LINE INDENT for i in range ( ( n - 25 ) ) : NEW_LINE INDENT ans += ' a ' NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 34 NEW_LINE print ( generateTheString ( n ) ) NEW_LINE DEDENT
Move all occurrence of letter ' x ' from the string s to the end using Recursion | Function to move all ' x ' in the end ; Store current character ; Check if current character is not 'x ; Recursive function call ; Check if current character is 'x ; Driver code
def moveAtEnd ( s , i , l ) : NEW_LINE INDENT if ( i >= l ) : NEW_LINE return NEW_LINE curr = s [ i ] NEW_LINE DEDENT ' NEW_LINE INDENT if ( curr != ' x ' ) : NEW_LINE INDENT print ( curr , end = " " ) NEW_LINE DEDENT moveAtEnd ( s , i + 1 , l ) NEW_LINE DEDENT ' NEW_LINE INDENT if ( curr == ' x ' ) : NEW_LINE INDENT print ( curr , end = " " ) NEW_LINE DEDENT return NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " geekxsforgexxeksxx " NEW_LINE l = len ( s ) NEW_LINE moveAtEnd ( s , 0 , l ) NEW_LINE DEDENT
Construct a string of length L such that each substring of length X has exactly Y distinct letters | Python implementation to construct a string of length L such that each substring of length X has exactly Y distinct letters . ; Initialize p equal to the ASCII value of a ; Iterate till the length of the string ; Driver code
def String ( l , x , y ) : NEW_LINE INDENT p = 97 NEW_LINE for j in range ( l ) : NEW_LINE INDENT ans = chr ( p + j % y ) NEW_LINE print ( ans , end = " " ) NEW_LINE DEDENT DEDENT l = 6 NEW_LINE x = 5 NEW_LINE y = 3 NEW_LINE String ( l , x , y ) NEW_LINE
Find the final co | Function to print the final position of the point after traversing through the given directions ; Traversing through the given directions ; If its north or south the point will move left or right ; If its west or east the point will move upwards or downwards ; Returning the final position ; Driver Code
def finalCoordinates ( SX , SY , D ) : NEW_LINE INDENT for i in range ( len ( D ) ) : NEW_LINE INDENT if ( D [ i ] == ' N ' ) : NEW_LINE INDENT SY += 1 NEW_LINE DEDENT elif ( D [ i ] == ' S ' ) : NEW_LINE INDENT SY -= 1 NEW_LINE DEDENT elif ( D [ i ] == ' E ' ) : NEW_LINE INDENT SX += 1 NEW_LINE DEDENT else : NEW_LINE INDENT SX -= 1 NEW_LINE DEDENT DEDENT ans = ' ( ' + str ( SX ) + ' , ' + str ( SY ) + ' ) ' NEW_LINE print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT SX , SY = 2 , 2 NEW_LINE D = " NSSE " NEW_LINE finalCoordinates ( SX , SY , D ) NEW_LINE DEDENT
Frequency of smallest character in first sentence less than that of second sentence | Python3 program for the above approach ; Function to count the frequency of minimum character ; Sort the string s ; Return the count with smallest character ; Function to count number of frequency of smallest character of string arr1 [ ] is less than the string in arr2 [ ] ; To store the frequency of smallest character in each string of arr2 ; Traverse the arr2 [ ] ; Count the frequency of smallest character in string s ; Append the frequency to freq [ ] ; Sort the frequency array ; Traverse the array arr1 [ ] ; Count the frequency of smallest character in string s ; find the element greater than f ; Find the count such that arr1 [ i ] < arr2 [ j ] ; Print the count ; Driver Code ; Function Call
from bisect import bisect_right as upper_bound NEW_LINE def countMinFreq ( s ) : NEW_LINE INDENT s = sorted ( s ) NEW_LINE x = 0 NEW_LINE for i in s : NEW_LINE INDENT if i == s [ 0 ] : NEW_LINE INDENT x += 1 NEW_LINE DEDENT DEDENT return x NEW_LINE DEDENT def countLessThan ( arr1 , arr2 ) : NEW_LINE INDENT freq = [ ] NEW_LINE for s in arr2 : NEW_LINE INDENT f = countMinFreq ( s ) NEW_LINE freq . append ( f ) NEW_LINE DEDENT feq = sorted ( freq ) NEW_LINE for s in arr1 : NEW_LINE INDENT f = countMinFreq ( s ) ; NEW_LINE it = upper_bound ( freq , f ) NEW_LINE cnt = len ( freq ) - it NEW_LINE print ( cnt , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ " yyy " , " zz " ] NEW_LINE arr2 = [ " x " , " xx " , " xxx " , " xxxx " ] NEW_LINE countLessThan ( arr1 , arr2 ) ; NEW_LINE DEDENT
Lexicographically all Shortest Palindromic Substrings from a given string | Function to find all lexicographically shortest palindromic substring ; Array to keep track of alphabetic characters ; Iterate to print all lexicographically shortest substring ; Driver code
def shortestPalindrome ( s ) : NEW_LINE INDENT abcd = [ 0 ] * 26 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT abcd [ ord ( s [ i ] ) - 97 ] = 1 NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT if abcd [ i ] == 1 : NEW_LINE INDENT print ( chr ( i + 97 ) , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT DEDENT s = " geeksforgeeks " NEW_LINE shortestPalindrome ( s ) NEW_LINE
Remove duplicates from string keeping the order according to last occurrences | Python3 program to remove duplicate character from character array and print sorted order ; Used as index in the modified string ; Traverse through all characters ; Check if str [ i ] is present before it ; If not present , then add it to result . ; Driver code
def removeDuplicates ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE res = " " NEW_LINE for i in range ( n ) : NEW_LINE INDENT j = i + 1 NEW_LINE while j < n : NEW_LINE INDENT if ( str [ i ] == str [ j ] ) : NEW_LINE INDENT break NEW_LINE DEDENT j += 1 NEW_LINE DEDENT if ( j == n ) : NEW_LINE INDENT res = res + str [ i ] NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " geeksforgeeks " NEW_LINE print ( removeDuplicates ( str ) ) NEW_LINE DEDENT
Remove duplicates from string keeping the order according to last occurrences | Python3 program to remove duplicate character from character array and prin sorted order ; Used as index in the modified string ; Create an empty hash table ; Traverse through all characters from right to left ; If current character is not in ; Reverse the result string ; Driver code
def removeDuplicates ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE s = set ( ) NEW_LINE res = " " NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( str [ i ] not in s ) : NEW_LINE INDENT res = res + str [ i ] NEW_LINE s . add ( str [ i ] ) NEW_LINE DEDENT DEDENT res = res [ : : - 1 ] NEW_LINE return res NEW_LINE DEDENT str = " geeksforgeeks " NEW_LINE print ( removeDuplicates ( str ) ) NEW_LINE
Count of numbers in a range that does not contain the digit M and which is divisible by M . | Function to count all the numbers which does not contain the digit ' M ' and is divisible by M ; Storing all the distinct digits of a number ; Checking if the two conditions are satisfied or not ; Driver code ; Lower Range ; Upper Range ; The digit
def contain ( L , U , M ) : NEW_LINE INDENT count = 0 NEW_LINE for j in range ( L , U + 1 ) : NEW_LINE INDENT num = set ( str ( j ) ) NEW_LINE if ( j % M == 0 and str ( M ) not in num ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE L = 106 NEW_LINE U = 200 NEW_LINE M = 7 NEW_LINE INDENT contain ( L , U , M ) NEW_LINE DEDENT
Longest string which is prefix string of at least two strings | Python3 program to find longest string which is prefix string of at least two strings ; Function to find max length of the prefix ; Base case ; Iterating over all the alphabets ; Checking if char exists in current string or not ; If atleast 2 string have that character ; Recursive call to i + 1 ; Driver code ; Initialising strings ; Push strings into vectors .
max1 = 0 NEW_LINE def MaxLength ( v , i , m ) : NEW_LINE INDENT global max1 NEW_LINE if ( i >= m ) : NEW_LINE INDENT return m - 1 NEW_LINE DEDENT for k in range ( 26 ) : NEW_LINE INDENT c = chr ( ord ( ' a ' ) + k ) NEW_LINE v1 = [ ] NEW_LINE for j in range ( len ( v ) ) : NEW_LINE INDENT if ( v [ j ] [ i ] == c ) : NEW_LINE INDENT v1 . append ( v [ j ] ) NEW_LINE DEDENT DEDENT if ( len ( v1 ) >= 2 ) : NEW_LINE INDENT max1 = max ( max1 , MaxLength ( v1 , i + 1 , m ) ) NEW_LINE DEDENT else : NEW_LINE INDENT max1 = max ( max1 , i - 1 ) NEW_LINE DEDENT DEDENT return max1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s1 = " abcde " NEW_LINE s2 = " abcsd " NEW_LINE s3 = " bcsdf " NEW_LINE s4 = " abcda " NEW_LINE s5 = " abced " NEW_LINE v = [ ] NEW_LINE v . append ( s1 ) NEW_LINE v . append ( s2 ) NEW_LINE v . append ( s3 ) NEW_LINE v . append ( s4 ) NEW_LINE v . append ( s5 ) NEW_LINE m = len ( v [ 0 ] ) NEW_LINE print ( MaxLength ( v , 0 , m ) + 1 ) NEW_LINE DEDENT
Periodic Binary String With Minimum Period and a Given Binary String as Subsequence . | Function to find the periodic string with minimum period ; Print the S if it consists of similar elements ; Find the required periodic with period 2 ; Driver Code
def findPeriodicString ( S ) : NEW_LINE INDENT l = 2 * len ( S ) NEW_LINE count = 0 NEW_LINE for i in range ( len ( S ) ) : NEW_LINE INDENT if ( S [ i ] == '1' ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if ( count == len ( S ) or count == 0 ) : NEW_LINE INDENT print ( S ) NEW_LINE DEDENT else : NEW_LINE INDENT arr = [ '0' ] * l NEW_LINE for i in range ( 0 , l , 2 ) : NEW_LINE INDENT arr [ i ] = '1' NEW_LINE arr [ i + 1 ] = '0' NEW_LINE DEDENT for i in range ( l ) : NEW_LINE INDENT print ( arr [ i ] , end = " " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = "1111001" NEW_LINE findPeriodicString ( S ) NEW_LINE DEDENT