codeparrot/xlcost-text-to-code huggingface dataset:
Check if the number is balanced | Function to check whether N is Balanced Number or not ; Calculating the Leftsum and rightSum simultaneously ; Typecasting each character to integer and adding the digit to respective sums ; Driver Code ; Function call
def BalancedNumber ( s ) : NEW_LINE INDENT Leftsum = 0 NEW_LINE Rightsum = 0 NEW_LINE for i in range ( 0 , int ( len ( s ) / 2 ) ) : NEW_LINE INDENT Leftsum = Leftsum + int ( s [ i ] ) NEW_LINE Rightsum = ( Rightsum + int ( s [ len ( s ) - 1 - i ] ) ) NEW_LINE DEDENT if ( Leftsum == Rightsum ) : NEW_LINE INDENT print ( " Balanced " , end =   ' ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Balanced " , end =   ' ' ) NEW_LINE DEDENT DEDENT s = "12321" NEW_LINE BalancedNumber ( s ) NEW_LINE
Count of N size strings consisting of at least one vowel and one consonant | Python3 program to count all possible strings of length N consisting of atleast one vowel and one consonant ; Function to return base ^ exponent ; Function to count all possible strings ; All possible strings of length N ; vowels only ; consonants only ; Return the final result ; Driver Program
mod = 1e9 + 7 NEW_LINE def expo ( base , exponent ) : NEW_LINE INDENT ans = 1 NEW_LINE while ( exponent != 0 ) : NEW_LINE INDENT if ( ( exponent & 1 ) == 1 ) : NEW_LINE INDENT ans = ans * base NEW_LINE ans = ans % mod NEW_LINE DEDENT base = base * base NEW_LINE base %= mod NEW_LINE exponent >>= 1 NEW_LINE DEDENT return ans % mod NEW_LINE DEDENT def findCount ( N ) : NEW_LINE INDENT ans = ( ( expo ( 26 , N ) - expo ( 5 , N ) - expo ( 21 , N ) ) % mod ) NEW_LINE ans += mod NEW_LINE ans %= mod NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE print ( int ( findCount ( N ) ) ) NEW_LINE DEDENT
Sum of decomposition values of all suffixes of an Array | Python3 implementation to find the sum of Decomposition values of all suffixes of an array ; Function to find the decomposition values of the array ; Stack ; Variable to maintain min value in stack ; Loop to iterate over the array ; Condition to check if the stack is empty ; Condition to check if the top of the stack is greater than the current element ; Loop to pop the element out ; The size of the stack is the max no of subarrays for suffix till index i from the right ; Driver Code
import sys NEW_LINE def decompose ( S ) : NEW_LINE INDENT s = [ ] NEW_LINE N = len ( S ) NEW_LINE ans = 0 NEW_LINE nix = sys . maxsize NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( len ( s ) == 0 ) : NEW_LINE INDENT s . append ( S [ i ] ) NEW_LINE nix = S [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT if ( S [ i ] < s [ - 1 ] ) : NEW_LINE INDENT s . append ( S [ i ] ) NEW_LINE nix = min ( nix , S [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT val = S [ i ] NEW_LINE while ( len ( s ) != 0 and val >= s [ - 1 ] ) : NEW_LINE INDENT s . pop ( ) NEW_LINE DEDENT nix = min ( nix , S [ i ] ) ; NEW_LINE s . append ( nix ) NEW_LINE DEDENT DEDENT ans += len ( s ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = [ 9 , 6 , 9 , 35 ] NEW_LINE print ( decompose ( S ) ) NEW_LINE DEDENT
Maximum number of set bits count in a K | Function that find Maximum number of set bit appears in a substring of size K . ; Traverse string 1 to k ; Increment count if character is set bit ; Traverse string k + 1 to length of string ; Remove the contribution of the ( i - k ) th character which is no longer in the window ; Add the contribution of the current character ; Update maxCount at for each window of size k ; Return maxCount ; Driver code
def maxSetBitCount ( s , k ) : NEW_LINE INDENT maxCount = 0 NEW_LINE n = len ( s ) NEW_LINE count = 0 NEW_LINE for i in range ( k ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT maxCount = count NEW_LINE for i in range ( k , n ) : NEW_LINE INDENT if ( s [ i - k ] == '1' ) : NEW_LINE INDENT count -= 1 NEW_LINE DEDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT maxCount = max ( maxCount , count ) NEW_LINE DEDENT return maxCount NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "100111010" NEW_LINE k = 3 NEW_LINE print ( maxSetBitCount ( s , k ) ) NEW_LINE DEDENT
Minimum characters to be deleted from the beginning of two strings to make them equal | Function that finds minimum character required to be deleted ; Iterate in the strings ; Check if the characters are not equal ; Return the result ; Driver code
def minDel ( s1 , s2 ) : NEW_LINE INDENT i = len ( s1 ) NEW_LINE j = len ( s2 ) NEW_LINE while ( i > 0 and j > 0 ) : NEW_LINE INDENT if ( s1 [ i - 1 ] != s2 [ j - 1 ] ) : NEW_LINE INDENT break NEW_LINE DEDENT i -= 1 NEW_LINE j -= 1 NEW_LINE DEDENT return i + j NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s1 = " geeksforgeeks " NEW_LINE s2 = " peeks " NEW_LINE print ( minDel ( s1 , s2 ) ) NEW_LINE DEDENT
Minimum characters to be deleted from the end to make given two strings equal | Function that finds minimum character required to be deleted ; Iterate in the strings ; Check if the characters are not equal ; Return the result ; Driver code
def minDel ( s1 , s2 ) : NEW_LINE INDENT i = 0 ; NEW_LINE while ( i < min ( len ( s1 ) , len ( s2 ) ) ) : NEW_LINE INDENT if ( s1 [ i ] != s2 [ i ] ) : NEW_LINE INDENT break ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT ans = ( ( len ( s1 ) - i ) + ( len ( s2 ) - i ) ) ; NEW_LINE return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s1 = " geeks " ; NEW_LINE s2 = " geeksfor " ; NEW_LINE print ( minDel ( s1 , s2 ) ) ; NEW_LINE DEDENT
Check if given Parentheses expression is balanced or not | Function to check if parenthesis are balanced ; Initialising Variables ; Traversing the Expression ; It is a closing parenthesis ; This means there are more closing parenthesis than opening ; If count is not zero , it means there are more opening parenthesis ; Driver code
def isBalanced ( exp ) : NEW_LINE INDENT flag = True NEW_LINE count = 0 NEW_LINE for i in range ( len ( exp ) ) : NEW_LINE INDENT if ( exp [ i ] == ' ( ' ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count -= 1 NEW_LINE DEDENT if ( count < 0 ) : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT DEDENT if ( count != 0 ) : NEW_LINE INDENT flag = False NEW_LINE DEDENT return flag NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT exp1 = " ( ( ( ) ) ) ( ) ( ) " NEW_LINE if ( isBalanced ( exp1 ) ) : NEW_LINE INDENT print ( " Balanced " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Balanced " ) NEW_LINE DEDENT exp2 = " ( ) ) ( ( ( ) ) " NEW_LINE if ( isBalanced ( exp2 ) ) : NEW_LINE INDENT print ( " Balanced " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Balanced " ) NEW_LINE DEDENT DEDENT
Check if a given string can be formed using characters of adjacent cells of a Matrix | Function to check if the word exists ; If index exceeds board range ; If the current cell does not contain the required character ; If the cell contains the required character and is the last character of the word required to be matched ; Return true as word is found ; Mark cell visited ; Check Adjacent cells for the next character ; Restore cell value ; Driver Code
def checkWord ( board , word , index , row , col ) : NEW_LINE INDENT if ( row < 0 or col < 0 or row >= len ( board ) or col >= len ( board [ 0 ] ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( board [ row ] [ col ] != word [ index ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT elif ( index == len ( word ) - 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT temp = board [ row ] [ col ] NEW_LINE board [ row ] [ col ] = ' * ' NEW_LINE if ( checkWord ( board , word , index + 1 , row + 1 , col ) or checkWord ( board , word , index + 1 , row - 1 , col ) or checkWord ( board , word , index + 1 , row , col + 1 ) or checkWord ( board , word , index + 1 , row , col - 1 ) ) : NEW_LINE INDENT board [ row ] [ col ] = temp NEW_LINE return True NEW_LINE DEDENT board [ row ] [ col ] = temp NEW_LINE return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT board = [ [ ' A ' , ' B ' , ' C ' , ' E ' ] , [ ' S ' , ' F ' , ' C ' , ' S ' ] , [ ' A ' , ' D ' , ' E ' , ' E ' ] ] NEW_LINE word = " CFDASABCESEE " NEW_LINE f = 0 NEW_LINE for i in range ( len ( board ) ) : NEW_LINE INDENT for j in range ( len ( board [ 0 ] ) ) : NEW_LINE INDENT if ( board [ i ] [ j ] == word [ 0 ] and checkWord ( board , word , 0 , i , j ) ) : NEW_LINE INDENT print ( " True " ) NEW_LINE f = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if f == 1 : NEW_LINE break NEW_LINE DEDENT if f == 0 : NEW_LINE print ( " False " ) NEW_LINE DEDENT
Split the number N by maximizing the count of subparts divisible by K | Function to count the subparts ; Total subStr till now ; If it can be divided , then this substring is one of the possible answer ; Convert string to long long and check if its divisible with X ; Consider there is no vertical cut between this index and the next one , hence take total carrying total substr a . ; If there is vertical cut between this index and next one , then we start again with subStr as " " and add b for the count of subStr upto now ; Return max of both the cases ; Driver code
def count ( N , X , subStr , index , n ) : NEW_LINE INDENT if ( index == n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT a = subStr + N [ index ] NEW_LINE b = 0 NEW_LINE if ( int ( a ) % X == 0 ) : NEW_LINE INDENT b = 1 NEW_LINE DEDENT m1 = count ( N , X , a , index + 1 , n ) NEW_LINE m2 = b + count ( N , X , " " , index + 1 , n ) NEW_LINE return max ( m1 , m2 ) NEW_LINE DEDENT N = "00001242" NEW_LINE K = 3 NEW_LINE l = len ( N ) NEW_LINE print ( count ( N , K , " " , 0 , l ) ) NEW_LINE
Check if a number ends with another number or not | Function to check if B is a suffix of A or not ; Convert numbers into strings ; Find the lengths of strings s1 and s2 ; Base Case ; Traverse the strings s1 & s2 ; If at any index characters are unequals then return false ; Return true ; Given numbers ; Function Call ; If B is a suffix of A , then print " Yes "
def checkSuffix ( A , B ) : NEW_LINE INDENT s1 = str ( A ) ; NEW_LINE s2 = str ( B ) ; NEW_LINE n1 = len ( s1 ) NEW_LINE n2 = len ( s2 ) NEW_LINE if ( n1 < n2 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT for i in range ( n2 ) : NEW_LINE INDENT if ( s1 [ n1 - i - 1 ] != s2 [ n2 - i - 1 ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT A = 12345 NEW_LINE B = 45 ; NEW_LINE result = checkSuffix ( A , B ) ; NEW_LINE if ( result ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Check if a number ends with another number or not | Function to check if B is a suffix of A or not ; Convert numbers into strings ; Check if s2 is a suffix of s1 or not ; If result is true print " Yes " ; Driver code ; Given numbers ; Function call
def checkSuffix ( A , B ) : NEW_LINE INDENT s1 = str ( A ) NEW_LINE s2 = str ( B ) NEW_LINE result = s1 . endswith ( s2 ) NEW_LINE if ( result ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 12345 NEW_LINE B = 45 NEW_LINE checkSuffix ( A , B ) NEW_LINE DEDENT
Binary string with given frequencies of sums of consecutive pairs of characters | A Function that generates and returns the binary string ; P : Frequency of consecutive characters with sum 0 Q : Frequency of consecutive characters with sum 1 R : Frequency of consecutive characters with sum 2 ; If no consecutive character adds up to 1 ; Not possible if both P and Q are non - zero ; If P is not equal to 0 ; Append 0 P + 1 times ; Append 1 R + 1 times ; Append "01" to satisfy Q ; Append "0" P times ; Append "1" R times ; Driver Code
def build_binary_str ( p , q , r ) : NEW_LINE INDENT ans = " " NEW_LINE if ( q == 0 ) : NEW_LINE INDENT if ( p != 0 and r != 0 ) : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT else : NEW_LINE INDENT if ( p ) : NEW_LINE INDENT temp = " " NEW_LINE for i in range ( p + 1 ) : NEW_LINE INDENT temp += '0' NEW_LINE DEDENT ans = temp NEW_LINE DEDENT else : NEW_LINE INDENT temp = " " NEW_LINE for i in range ( r + 1 ) : NEW_LINE INDENT temp += '1' NEW_LINE DEDENT ans = temp NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT for i in range ( 1 , q + 2 ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT ans += '0' NEW_LINE DEDENT else : NEW_LINE INDENT ans += '1' NEW_LINE DEDENT DEDENT temp = " " NEW_LINE for i in range ( p ) : NEW_LINE INDENT temp += '0' NEW_LINE DEDENT st = " " NEW_LINE st += ans [ 0 ] NEW_LINE st += temp NEW_LINE for i in range ( 1 , len ( ans ) ) : NEW_LINE INDENT st += ans [ i ] NEW_LINE DEDENT ans = st NEW_LINE temp = " " NEW_LINE for i in range ( r ) : NEW_LINE INDENT temp += '1' NEW_LINE DEDENT ans = temp + ans NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT p = 1 NEW_LINE q = 2 NEW_LINE r = 2 NEW_LINE print ( build_binary_str ( p , q , r ) ) NEW_LINE DEDENT
Check if there exists a permutation of given string which doesn 't contain any monotonous substring | Function to check a string doesn 't contains a monotonous substring ; Loop to iterate over the string and check that it doesn 't contains  the monotonous substring ; Function to check that there exist a arrangement of string such that it doesn 't contains monotonous substring ; Loop to group the characters of the string into two buckets ; Sorting the two buckets ; Condition to check if the concatenation point doesn 't  contains the monotonous string ; Driver Code
def check ( s ) : NEW_LINE INDENT ok = True NEW_LINE for i in range ( 0 , len ( s ) - 1 , 1 ) : NEW_LINE INDENT ok = ( ok & ( abs ( ord ( s [ i ] ) - ord ( s [ i + 1 ] ) ) != 1 ) ) NEW_LINE DEDENT return ok NEW_LINE DEDENT def monotonousString ( s ) : NEW_LINE INDENT odd = " " NEW_LINE even = " " NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( ord ( s [ i ] ) % 2 == 0 ) : NEW_LINE INDENT odd += s [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT even += s [ i ] NEW_LINE DEDENT DEDENT odd = list ( odd ) NEW_LINE odd . sort ( reverse = False ) NEW_LINE odd = str ( odd ) NEW_LINE even = list ( even ) NEW_LINE even . sort ( reverse = False ) NEW_LINE even = str ( even ) NEW_LINE if ( check ( odd + even ) ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT elif ( check ( even + odd ) ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT return " No " NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " abcd " NEW_LINE ans = monotonousString ( str1 ) NEW_LINE print ( ans ) NEW_LINE DEDENT
Length of the smallest substring which contains all vowels | Function to return the index for respective vowels to increase their count ; Returns - 1 for consonants ; Function to find the minimum length ; Store the starting index of the current substring ; Store the frequencies of vowels ; If the current character is a vowel ; Increase its count ; Move start as much right as possible ; Condition for valid substring ; Driver code
def get_index ( ch ) : NEW_LINE INDENT if ( ch == ' a ' ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif ( ch == ' e ' ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif ( ch == ' i ' ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT elif ( ch == ' o ' ) : NEW_LINE INDENT return 3 NEW_LINE DEDENT elif ( ch == ' u ' ) : NEW_LINE INDENT return 4 NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT def findMinLength ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE ans = n + 1 NEW_LINE start = 0 NEW_LINE count = [ 0 ] * 5 NEW_LINE for x in range ( n ) : NEW_LINE INDENT idx = get_index ( s [ x ] ) NEW_LINE if ( idx != - 1 ) : NEW_LINE INDENT count [ idx ] += 1 NEW_LINE DEDENT idx_start = get_index ( s [ start ] ) NEW_LINE while ( idx_start == - 1 or count [ idx_start ] > 1 ) : NEW_LINE INDENT if ( idx_start != - 1 ) : NEW_LINE INDENT count [ idx_start ] -= 1 NEW_LINE DEDENT start += 1 NEW_LINE if ( start < n ) : NEW_LINE INDENT idx_start = get_index ( s [ start ] ) NEW_LINE DEDENT DEDENT if ( count [ 0 ] > 0 and count [ 1 ] > 0 and count [ 2 ] > 0 and count [ 3 ] > 0 and count [ 4 ] > 0 ) : NEW_LINE INDENT ans = min ( ans , x - start + 1 ) NEW_LINE DEDENT DEDENT if ( ans == n + 1 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " aaeebbeaccaaoiuooooooooiuu " NEW_LINE print ( findMinLength ( s ) ) NEW_LINE DEDENT
Number of strings which starts and ends with same character after rotations | Function to find the count of string with equal end after rotations ; To store the final count ; Traverse the string ; If current character is same as the previous character then increment the count ; Return the final count ; Driver Code ; Given string ; Function call
def countStrings ( s ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE for i in range ( 1 , len ( s ) - 1 ) : NEW_LINE INDENT if ( s [ i ] == s [ i + 1 ] ) : NEW_LINE INDENT cnt += 1 ; NEW_LINE DEDENT DEDENT return cnt ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " aacbb " ; NEW_LINE print ( countStrings ( str ) ) ; NEW_LINE DEDENT
Check if frequency of each character is equal to its position in English Alphabet | Python3 program for the above approach ; Initialise frequency array ; Traverse the string ; Update the frequency ; Check for valid string ; If frequency is non - zero ; If freq is not equals to ( i + 1 ) , then return false ; Return true ; Given string str
def checkValidString ( str ) : NEW_LINE INDENT freq = [ 0 for i in range ( 26 ) ] NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT freq [ ord ( str [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( freq [ i ] != 0 ) : NEW_LINE INDENT if ( freq [ i ] != i + 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT str = " abbcccdddd " NEW_LINE if ( checkValidString ( str ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Print all the non | Function to print all the non - repeating words from the two given sentences ; Concatenate the two strings into one ; Iterating over the whole concatenated string ; Searching for the word in A . If while searching , we reach the end of the string , then the word is not present in the string ; Initialise word for the next iteration ; Driver code
def removeRepeating ( s1 , s2 ) : NEW_LINE INDENT s3 = s1 + " ▁ " + s2 + " ▁ " NEW_LINE words = " " NEW_LINE i = 0 NEW_LINE for x in s3 : NEW_LINE INDENT if ( x == ' ▁ ' ) : NEW_LINE INDENT if ( words not in s1 or words not in s2 ) : NEW_LINE INDENT print ( words , end = " " ) NEW_LINE DEDENT words = " ▁ " NEW_LINE DEDENT else : NEW_LINE INDENT words = words + x NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = " I ▁ have ▁ go ▁ a ▁ pen " NEW_LINE s2 = " I ▁ want ▁ to ▁ go ▁ park " NEW_LINE removeRepeating ( s1 , s2 ) NEW_LINE DEDENT
Convert the number from International system to Indian system | Function to convert a number represented in International numeric system to Indian numeric system . ; Find the length of the input string ; Removing all the separators ( , ) from the input string ; Reverse the input string ; Declaring the output string ; Process the input string ; Add a separator ( , ) after the third number ; Then add a separator ( , ) after every second number ; Reverse the output string ; Return the output string back to the main function ; Driver code
def convert ( input ) : NEW_LINE INDENT Len = len ( input ) NEW_LINE i = 0 NEW_LINE while ( i < Len ) : NEW_LINE INDENT if ( input [ i ] == " , " ) : NEW_LINE INDENT input = input [ : i ] + input [ i + 1 : ] NEW_LINE Len -= 1 NEW_LINE i -= 1 NEW_LINE DEDENT elif ( input [ i ] == " ▁ " ) : NEW_LINE INDENT input = input [ : i ] + input [ i + 1 : ] NEW_LINE Len -= 1 NEW_LINE i -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT i += 1 NEW_LINE DEDENT DEDENT input = input [ : : - 1 ] NEW_LINE output = " " NEW_LINE for i in range ( Len ) : NEW_LINE INDENT if ( i == 2 ) : NEW_LINE INDENT output += input [ i ] NEW_LINE output += " ▁ , " NEW_LINE DEDENT elif ( i > 2 and i % 2 == 0 and i + 1 < Len ) : NEW_LINE INDENT output += input [ i ] NEW_LINE output += " ▁ , " NEW_LINE DEDENT else : NEW_LINE INDENT output += input [ i ] NEW_LINE DEDENT DEDENT output = output [ : : - 1 ] NEW_LINE return output NEW_LINE DEDENT input1 = "123 , ▁ 456 , ▁ 789" NEW_LINE input2 = "90 , ▁ 050 , ▁ 000 , ▁ 000" NEW_LINE print ( convert ( input1 ) ) NEW_LINE print ( convert ( input2 ) ) NEW_LINE
Construct the Cypher string based on the given conditions | Python3 program for the above approach ; Function to check whether a number is prime or not ; Function to check if a prime number can be expressed as sum of two Prime Numbers ; If the N && ( N - 2 ) is Prime ; Function to check semiPrime ; Loop from 2 to sqrt ( num ) ; Increment the count of prime numbers ; If num is greater than 1 , then add 1 to it ; Return '1' if count is 2 else return '0 ; Function to make the Cypher string ; Resultant string ; Make string for the number N ; Check for semiPrime ; Traverse to make Cypher string ; If index is odd add the current character ; Else current character is changed ; Check for sum of two primes ; Traverse to make Cypher string ; If index is odd then current character is changed ; Else add the current character ; If the resultant string is " " then print - 1 ; Else print the resultant string ; Driver Code ; Given number ; Function call
import math NEW_LINE def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT sqt = ( int ) ( math . sqrt ( n ) ) NEW_LINE for i in range ( 2 , sqt ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isPossibleSum ( N ) : NEW_LINE INDENT if ( isPrime ( N ) and isPrime ( N - 2 ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def checkSemiprime ( num ) : NEW_LINE INDENT cnt = 0 NEW_LINE i = 2 NEW_LINE while cnt < 2 and i * i <= num : NEW_LINE INDENT while ( num % i == 0 ) : NEW_LINE INDENT num //= i NEW_LINE cnt += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if ( num > 1 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT return cnt == 2 NEW_LINE DEDENT def makeCypherString ( N ) : NEW_LINE INDENT semiPrime = " " NEW_LINE sumOfPrime = " " NEW_LINE st = str ( N ) NEW_LINE if ( checkSemiprime ( N ) ) : NEW_LINE INDENT for i in range ( len ( st ) ) : NEW_LINE INDENT if ( i & 1 ) : NEW_LINE INDENT semiPrime += st [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT semiPrime += chr ( ord ( st [ i ] ) - ord ( '0' ) + 65 ) NEW_LINE DEDENT DEDENT DEDENT if ( isPossibleSum ( N ) ) : NEW_LINE INDENT for i in range ( len ( st ) ) : NEW_LINE INDENT if ( i & 1 ) : NEW_LINE INDENT sumOfPrime += chr ( ord ( st [ i ] ) - ord ( '0' ) + 65 ) NEW_LINE DEDENT else : NEW_LINE INDENT sumOfPrime += st [ i ] NEW_LINE DEDENT DEDENT DEDENT if ( semiPrime + sumOfPrime == " " ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( semiPrime + sumOfPrime ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 1011243 NEW_LINE makeCypherString ( N ) NEW_LINE DEDENT
Maximum splits in binary string such that each substring is divisible by given odd number | Function to calculate maximum splits possible ; Driver code
def max_segments ( st , K ) : NEW_LINE INDENT n = len ( st ) NEW_LINE s , sum , count = 0 , 0 , 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT a = ord ( st [ i ] ) - 48 NEW_LINE sum += a * pow ( 2 , s ) NEW_LINE s += 1 NEW_LINE if ( sum != 0 and sum % K == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE sum = 0 NEW_LINE s = 0 NEW_LINE DEDENT DEDENT if ( sum != 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( count ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = "10111001" NEW_LINE K = 5 NEW_LINE max_segments ( st , K ) NEW_LINE DEDENT