codeparrot/xlcost-text-to-code huggingface dataset:
Reverse String according to the number of words | Reverse the letters of the word ; Temporary variable to store character ; Swapping the first and last character ; This function forms the required string ; Checking the number of words present in string to reverse ; Reverse the letter of the words ; Driver Code
def reverse ( string , start , end ) : NEW_LINE INDENT temp = ' ' NEW_LINE while start <= end : NEW_LINE INDENT temp = string [ start ] NEW_LINE string [ start ] = string [ end ] NEW_LINE string [ end ] = temp NEW_LINE start += 1 NEW_LINE end -= 1 NEW_LINE DEDENT DEDENT def reverseletter ( string , start , end ) : NEW_LINE INDENT wstart , wend = start , start NEW_LINE while wend < end : NEW_LINE INDENT if string [ wend ] == " ▁ " : NEW_LINE INDENT wend += 1 NEW_LINE continue NEW_LINE DEDENT while wend <= end and string [ wend ] != " ▁ " : NEW_LINE INDENT wend += 1 NEW_LINE DEDENT wend -= 1 NEW_LINE reverse ( string , wstart , wend ) NEW_LINE wend += 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " Ashish ▁ Yadav ▁ Abhishek ▁ Rajput ▁ Sunil ▁ Pundir " NEW_LINE string = list ( string ) NEW_LINE reverseletter ( string , 0 , len ( string ) - 1 ) NEW_LINE print ( ' ' . join ( string ) ) NEW_LINE DEDENT
Minimum bit changes in Binary Circular array to reach a index | finding which path have minimum flip bit and the minimum flip bits ; concatenating given string to itself , to make it circular ; check x is greater than y . marking if output need to be opposite . ; iterate Clockwise ; if current bit is not equal to next index bit . ; iterate Anti - Clockwise ; if current bit is not equal to next index bit . ; Finding whether Clockwise or Anti - clockwise path take minimum flip . ; Driver Code
def minimumFlip ( s , x , y ) : NEW_LINE INDENT s = s + s NEW_LINE isOpposite = False NEW_LINE if ( x > y ) : NEW_LINE INDENT temp = y NEW_LINE y = x ; NEW_LINE x = temp NEW_LINE isOpposite = True NEW_LINE DEDENT valClockwise = 0 NEW_LINE cur = s [ x ] NEW_LINE for i in range ( x , y + 1 , 1 ) : NEW_LINE INDENT if ( s [ i ] != cur ) : NEW_LINE INDENT cur = s [ i ] NEW_LINE valClockwise += 1 NEW_LINE DEDENT DEDENT valAnticlockwise = 0 NEW_LINE cur = s [ y ] NEW_LINE x += len ( s ) - 1 NEW_LINE for i in range ( y , x + 1 , 1 ) : NEW_LINE INDENT if ( s [ i ] != cur ) : NEW_LINE INDENT cur = s [ i ] NEW_LINE valAnticlockwise += 1 NEW_LINE DEDENT DEDENT if ( valClockwise <= valAnticlockwise ) : NEW_LINE INDENT if ( isOpposite == False ) : NEW_LINE INDENT print ( " Clockwise " , valClockwise ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Anti - clockwise " , valAnticlockwise ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( isOpposite == False ) : NEW_LINE INDENT print ( " Anti - clockwise " , valAnticlockwise ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Clockwise " , valClockwise ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 0 NEW_LINE y = 8 NEW_LINE s = "000110" NEW_LINE minimumFlip ( s , x , y ) NEW_LINE DEDENT
Prefixes with more a than b | Function to count prefixes ; calculating for string S ; count == 0 or when N == 1 ; when all characters are a or a - b == 0 ; checking for saturation of string after repetitive addition ; Driver Code
def prefix ( k , n ) : NEW_LINE INDENT a = 0 NEW_LINE b = 0 NEW_LINE count = 0 NEW_LINE i = 0 NEW_LINE Len = len ( k ) NEW_LINE for i in range ( Len ) : NEW_LINE INDENT if ( k [ i ] == " a " ) : NEW_LINE INDENT a += 1 NEW_LINE DEDENT if ( k [ i ] == " b " ) : NEW_LINE INDENT b += 1 NEW_LINE DEDENT if ( a > b ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if ( count == 0 or n == 1 ) : NEW_LINE INDENT print ( count ) NEW_LINE return 0 NEW_LINE DEDENT if ( count == Len or a - b == 0 ) : NEW_LINE INDENT print ( count * n ) NEW_LINE return 0 NEW_LINE DEDENT n2 = n - 1 NEW_LINE count2 = 0 NEW_LINE while ( n2 != 0 ) : NEW_LINE INDENT for i in range ( Len ) : NEW_LINE INDENT if ( k [ i ] == " a " ) : NEW_LINE INDENT a += 1 NEW_LINE DEDENT if ( k [ i ] == " b " ) : NEW_LINE INDENT b += 1 NEW_LINE DEDENT if ( a > b ) : NEW_LINE INDENT count2 += 1 NEW_LINE DEDENT DEDENT count += count2 NEW_LINE n2 -= 1 NEW_LINE if ( count2 == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( count2 == Len ) : NEW_LINE INDENT count += ( n2 * count2 ) NEW_LINE break NEW_LINE DEDENT count2 = 0 NEW_LINE DEDENT return count NEW_LINE DEDENT S = " aba " NEW_LINE N = 2 NEW_LINE print ( prefix ( S , N ) ) NEW_LINE S = " baa " NEW_LINE N = 3 NEW_LINE print ( prefix ( S , N ) ) NEW_LINE
Pairs whose concatenation contain all digits | Python3 Program to find number of pairs whose concatenation contains all digits from 0 to 9. ; Function to return number of pairs whose concatenation contain all digits from 0 to 9 ; Making the mask for each of the number . ; for each of the possible pair which can make OR value equal to 1023 ; Finding the count of pair from the given numbers . ; Driven Program
N = 20 NEW_LINE def countPair ( st , n ) : NEW_LINE INDENT cnt = [ 0 ] * ( 1 << 10 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT mask = 0 NEW_LINE for j in range ( len ( st [ i ] ) ) : NEW_LINE INDENT mask |= ( 1 << ( ord ( st [ i ] [ j ] ) - ord ( '0' ) ) ) NEW_LINE DEDENT cnt [ mask ] += 1 NEW_LINE DEDENT ans = 0 NEW_LINE for m1 in range ( 1024 ) : NEW_LINE INDENT for m2 in range ( 1024 ) : NEW_LINE INDENT if ( ( m1 m2 ) == 1023 ) : NEW_LINE INDENT if ( m1 == m2 ) : NEW_LINE INDENT ans += ( cnt [ m1 ] * ( cnt [ m1 ] - 1 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT ans += ( cnt [ m1 ] * cnt [ m2 ] ) NEW_LINE DEDENT DEDENT DEDENT DEDENT return ans // 2 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE st = [ "129300455" , "5559948277" , "012334556" , "56789" , "123456879" ] NEW_LINE print ( countPair ( st , n ) ) NEW_LINE DEDENT
Count binary strings with twice zeros in first half | pre define some constant ; global values for pre computation ; function to print number of required string ; calculate answer using proposed algorithm ; Driver code
mod = 1000000007 NEW_LINE Max = 1001 NEW_LINE nCr = [ [ 0 for _ in range ( 1003 ) ] for i in range ( 1003 ) ] NEW_LINE def preComputeCoeff ( ) : NEW_LINE INDENT for i in range ( Max ) : NEW_LINE INDENT for j in range ( i + 1 ) : NEW_LINE INDENT if ( j == 0 or j == i ) : NEW_LINE INDENT nCr [ i ] [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT nCr [ i ] [ j ] = ( nCr [ i - 1 ] [ j - 1 ] + nCr [ i - 1 ] [ j ] ) % mod NEW_LINE DEDENT DEDENT DEDENT DEDENT def computeStringCount ( N ) : NEW_LINE INDENT n = N // 2 NEW_LINE ans = 0 NEW_LINE for i in range ( 2 , n + 1 , 2 ) : NEW_LINE INDENT ans = ( ans + ( ( nCr [ n ] [ i ] * nCr [ n ] [ i // 2 ] ) % mod ) ) % mod NEW_LINE DEDENT return ans NEW_LINE DEDENT preComputeCoeff ( ) NEW_LINE N = 3 NEW_LINE print ( computeStringCount ( N ) ) NEW_LINE
Count of ' GFG ' Subsequences in the given string | Python 3 Program to find the " GFG " subsequence in the given string ; Print the count of " GFG " subsequence in the string ; Traversing the given string ; If the character is ' G ' , increment the count of ' G ' , increase the result and update the array . ; If the character is ' F ' , increment the count of ' F ' and update the array . ; Ignore other character . ; Driver Code
MAX = 100 NEW_LINE def countSubsequence ( s , n ) : NEW_LINE INDENT cntG = 0 NEW_LINE cntF = 0 NEW_LINE result = 0 NEW_LINE C = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] == ' G ' ) : NEW_LINE INDENT cntG += 1 NEW_LINE result += C NEW_LINE continue NEW_LINE DEDENT if ( s [ i ] == ' F ' ) : NEW_LINE INDENT cntF += 1 NEW_LINE C += cntG NEW_LINE continue NEW_LINE DEDENT else : NEW_LINE INDENT continue NEW_LINE DEDENT DEDENT print ( result ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " GFGFG " NEW_LINE n = len ( s ) NEW_LINE countSubsequence ( s , n ) NEW_LINE DEDENT
Lexicographical Maximum substring of string | Python 3 program to find the lexicographically maximum substring . ; loop to find the max leicographic substring in the substring array ; Driver code
def LexicographicalMaxString ( str ) : NEW_LINE INDENT mx = " " NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT mx = max ( mx , str [ i : ] ) NEW_LINE DEDENT return mx NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " ababaa " NEW_LINE print ( LexicographicalMaxString ( str ) ) NEW_LINE DEDENT
Lexicographical Maximum substring of string | Python 3 program to find the lexicographically maximum substring . ; We store all the indexes of maximum characters we have in the string ; We form a substring from that maximum character index till end and check if its greater that maxstring ; Driver code
def LexicographicalMaxString ( st ) : NEW_LINE INDENT maxchar = ' a ' NEW_LINE index = [ ] NEW_LINE for i in range ( len ( st ) ) : NEW_LINE INDENT if ( st [ i ] >= maxchar ) : NEW_LINE INDENT maxchar = st [ i ] NEW_LINE index . append ( i ) NEW_LINE DEDENT DEDENT maxstring = " " NEW_LINE for i in range ( len ( index ) ) : NEW_LINE INDENT if ( st [ index [ i ] : len ( st ) ] > maxstring ) : NEW_LINE INDENT maxstring = st [ index [ i ] : len ( st ) ] NEW_LINE DEDENT DEDENT return maxstring NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = " acbacbc " NEW_LINE print ( LexicographicalMaxString ( st ) ) NEW_LINE DEDENT
Print longest palindrome word in a sentence | Function to check if a word is palindrome ; making the check case case insensitive ; loop to check palindrome ; Function to find longest palindrome word ; to check last word for palindrome ; to store each word ; extracting each word ; Driver code
def checkPalin ( word ) : NEW_LINE INDENT n = len ( word ) NEW_LINE word = word . lower ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( word [ i ] != word [ n - 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT n -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def longestPalin ( str ) : NEW_LINE INDENT str = str + " ▁ " NEW_LINE longestword = " " NEW_LINE word = " " NEW_LINE length1 = 0 NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT ch = str [ i ] NEW_LINE if ( ch != ' ▁ ' ) : NEW_LINE INDENT word = word + ch NEW_LINE DEDENT else : NEW_LINE INDENT length = len ( word ) NEW_LINE if ( checkPalin ( word ) and length > length1 ) : NEW_LINE INDENT length1 = length NEW_LINE longestword = word NEW_LINE DEDENT word = " " NEW_LINE DEDENT DEDENT return longestword NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " My ▁ name ▁ is ▁ ava ▁ and ▁ i ▁ love ▁ Geeksforgeeks " NEW_LINE if ( longestPalin ( s ) == " " ) : NEW_LINE INDENT print ( " No ▁ Palindrome ▁ Word " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( longestPalin ( s ) ) NEW_LINE DEDENT DEDENT
Number of common base strings for two strings | function for finding common divisor . ; Checking if ' base ' is base string of 's1 ; Checking if ' base ' is base string of 's2 ; Driver code
def isCommonBase ( base , s1 , s2 ) : NEW_LINE ' NEW_LINE INDENT for j in range ( len ( s1 ) ) : NEW_LINE INDENT if ( base [ j % len ( base ) ] != s1 [ j ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT ' NEW_LINE INDENT for j in range ( len ( s2 ) ) : NEW_LINE INDENT if ( base [ j % len ( base ) ] != s2 [ j ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def countCommonBases ( s1 , s2 ) : NEW_LINE INDENT n1 = len ( s1 ) NEW_LINE n2 = len ( s2 ) NEW_LINE count = 0 NEW_LINE for i in range ( 1 , min ( n1 , n2 ) + 1 ) : NEW_LINE INDENT base = s1 [ 0 : i ] NEW_LINE if ( isCommonBase ( base , s1 , s2 ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = " pqrspqrs " NEW_LINE s2 = " pqrspqrspqrspqrs " NEW_LINE print ( countCommonBases ( s1 , s2 ) ) NEW_LINE DEDENT
Removing elements between the two zeros | Function to find the string after operation ; Travesing through string ; Checking for character Between two zeros ; Deleting the character At specific position ; Updating the length of the string ; Driver code
def findstring ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE s = list ( s ) NEW_LINE i = 1 NEW_LINE while i < n - 1 : NEW_LINE INDENT if ( s [ i - 1 ] == '0' and s [ i + 1 ] == '0' ) : NEW_LINE INDENT s . pop ( i ) NEW_LINE i -= 1 NEW_LINE if i > 0 and s [ i - 1 ] == '0' : NEW_LINE INDENT i -= 1 NEW_LINE DEDENT n = len ( s ) NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return ' ' . join ( s ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT print ( findstring ( '100100' ) ) NEW_LINE DEDENT
Perfect Square String | Python3 program to find if string is a perfect square or not . ; calculating the length of the string ; calculating the ASCII value of the string ; Find floating point value of square root of x . ; If square root is an integer ; Driver code
import math ; NEW_LINE def isPerfectSquareString ( str ) : NEW_LINE INDENT sum = 0 ; NEW_LINE l = len ( str ) ; NEW_LINE for i in range ( l ) : NEW_LINE INDENT sum = sum + ord ( str [ i ] ) ; NEW_LINE DEDENT squareRoot = math . sqrt ( sum ) ; NEW_LINE return ( ( squareRoot - math . floor ( squareRoot ) ) == 0 ) ; NEW_LINE DEDENT str = " d " ; NEW_LINE if ( isPerfectSquareString ( str ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
Remove consecutive vowels from string | function which returns True or False for occurrence of a vowel ; this compares vowel with character 'c ; function to print resultant string ; print 1 st character ; loop to check for each character ; comparison of consecutive characters ; Driver code
def is_vow ( c ) : NEW_LINE ' NEW_LINE INDENT return ( ( c == ' a ' ) or ( c == ' e ' ) or ( c == ' i ' ) or ( c == ' o ' ) or ( c == ' u ' ) ) ; NEW_LINE DEDENT def removeVowels ( str ) : NEW_LINE INDENT print ( str [ 0 ] , end = " " ) ; NEW_LINE for i in range ( 1 , len ( str ) ) : NEW_LINE INDENT if ( ( is_vow ( str [ i - 1 ] ) != True ) or ( is_vow ( str [ i ] ) != True ) ) : NEW_LINE INDENT print ( str [ i ] , end = " " ) ; NEW_LINE DEDENT DEDENT DEDENT str = " ▁ geeks ▁ for ▁ geeks " ; NEW_LINE removeVowels ( str ) ; NEW_LINE
Find the Number which contain the digit d | Python 3 program to print the number which contain the digit d from 0 to n ; function to display the values ; Converting d to character ; Loop to check each digit one by one . ; initialize the string ; checking for digit ; Driver code
def index ( st , ch ) : NEW_LINE INDENT for i in range ( len ( st ) ) : NEW_LINE INDENT if ( st [ i ] == ch ) : NEW_LINE INDENT return i ; NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT def printNumbers ( n , d ) : NEW_LINE INDENT st = " " + str ( d ) NEW_LINE ch = st [ 0 ] NEW_LINE for i in range ( 0 , n + 1 , 1 ) : NEW_LINE INDENT st = " " NEW_LINE st = st + str ( i ) NEW_LINE if ( i == d or index ( st , ch ) >= 0 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 100 NEW_LINE d = 5 NEW_LINE printNumbers ( n , d ) NEW_LINE DEDENT
Transform a string such that it has abcd . . z as a subsequence | function to transform string with string passed as reference ; initializing the variable ch to 'a ; if the length of string is less than 26 , we can 't obtain the required  subsequence ; if ch has reached ' z ' , it means we have transformed our string such that required subsequence can be obtained ; current character is not greater than ch , then replace it with ch and increment ch ; Driver Code
def transformString ( s ) : NEW_LINE ' NEW_LINE INDENT ch = ' a ' NEW_LINE if ( len ( s ) < 26 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT if ( ord ( ch ) > ord ( ' z ' ) ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( s [ i ] <= ch ) : NEW_LINE INDENT s [ i ] = ch NEW_LINE ch = chr ( ord ( ch ) + 1 ) NEW_LINE DEDENT DEDENT if ( ch <= ' z ' ) : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE DEDENT print ( " " . join ( s ) ) NEW_LINE DEDENT s = list ( " aaaaaaaaaaaaaaaaaaaaaaaaaa " ) NEW_LINE transformString ( s ) NEW_LINE
Number of pairs with Pandigital Concatenation | Checks if a given is Pandigital ; digit i is not present thus not pandigital ; Returns number of pairs of strings resulting in Pandigital Concatenations ; iterate over all pair of strings ; Driver code
def isPanDigital ( s ) : NEW_LINE INDENT digits = [ False ] * 10 ; NEW_LINE for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT digits [ int ( s [ i ] ) - int ( '0' ) ] = True NEW_LINE DEDENT for i in range ( 0 , 10 ) : NEW_LINE INDENT if ( digits [ i ] == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def countPandigitalPairs ( v ) : NEW_LINE INDENT pairs = 0 NEW_LINE for i in range ( 0 , len ( v ) ) : NEW_LINE INDENT for j in range ( i + 1 , len ( v ) ) : NEW_LINE INDENT if ( isPanDigital ( v [ i ] + v [ j ] ) ) : NEW_LINE INDENT pairs = pairs + 1 NEW_LINE DEDENT DEDENT DEDENT return pairs NEW_LINE DEDENT v = [ "123567" , "098234" , "14765" , "19804" ] NEW_LINE print ( countPandigitalPairs ( v ) ) NEW_LINE
Minimum changes to a string to make all substrings distinct | Python3 program to count number of changes to make all substrings distinct . ; Returns minimum changes to str so that no substring is repeated . ; If length is more than maximum allowed characters , we cannot get the required string . ; Variable to store count of distinct characters ; To store counts of different characters ; Answer is , n - number of distinct char ; Driver Code
MAX_CHAR = [ 26 ] NEW_LINE def minChanges ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE if ( n > MAX_CHAR [ 0 ] ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT dist_count = 0 NEW_LINE count = [ 0 ] * MAX_CHAR [ 0 ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( count [ ord ( str [ i ] ) - ord ( ' a ' ) ] == 0 ) : NEW_LINE INDENT dist_count += 1 NEW_LINE DEDENT count [ ( ord ( str [ i ] ) - ord ( ' a ' ) ) ] += 1 NEW_LINE DEDENT return ( n - dist_count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " aebaecedabbee " NEW_LINE print ( minChanges ( str ) ) NEW_LINE DEDENT
Reverse each word in a linked list node | Node of a linked list ; Function to create newNode in a linked list ; reverse each node data ; iterate each node and call reverse_word for each node data ; printing linked list ; Driver program
class Node : NEW_LINE INDENT def __init__ ( self , next = None , data = None ) : NEW_LINE INDENT self . next = next NEW_LINE self . data = data NEW_LINE DEDENT DEDENT def newNode ( c ) : NEW_LINE INDENT temp = Node ( ) NEW_LINE temp . c = c NEW_LINE temp . next = None NEW_LINE return temp NEW_LINE DEDENT def reverse_word ( str ) : NEW_LINE INDENT s = " " NEW_LINE i = 0 NEW_LINE while ( i < len ( str ) ) : NEW_LINE INDENT s = str [ i ] + s NEW_LINE i = i + 1 NEW_LINE DEDENT return s NEW_LINE DEDENT def reverse ( head ) : NEW_LINE INDENT ptr = head NEW_LINE while ( ptr != None ) : NEW_LINE INDENT ptr . c = reverse_word ( ptr . c ) NEW_LINE ptr = ptr . next NEW_LINE DEDENT return head NEW_LINE DEDENT def printList ( head ) : NEW_LINE INDENT while ( head != None ) : NEW_LINE INDENT print ( head . c , end = " ▁ " ) NEW_LINE head = head . next NEW_LINE DEDENT DEDENT head = newNode ( " Geeksforgeeks " ) NEW_LINE head . next = newNode ( " a " ) NEW_LINE head . next . next = newNode ( " computer " ) NEW_LINE head . next . next . next = newNode ( " science " ) NEW_LINE head . next . next . next . next = newNode ( " portal " ) NEW_LINE head . next . next . next . next . next = newNode ( " for " ) NEW_LINE head . next . next . next . next . next . next = newNode ( " geeks " ) NEW_LINE print ( " List ▁ before ▁ reverse : ▁ " ) NEW_LINE printList ( head ) NEW_LINE head = reverse ( head ) NEW_LINE print (   " List after reverse :   " ) NEW_LINE printList ( head ) NEW_LINE
Number of strings of length N with no palindromic sub string | Return the count of strings with no palindromic substring . ; Driven Program
def numofstring ( n , m ) : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT return m NEW_LINE DEDENT if n == 2 : NEW_LINE INDENT return m * ( m - 1 ) NEW_LINE DEDENT return m * ( m - 1 ) * pow ( m - 2 , n - 2 ) NEW_LINE DEDENT n = 2 NEW_LINE m = 3 NEW_LINE print ( numofstring ( n , m ) ) NEW_LINE
Count special palindromes in a String | Function to count special Palindromic susbstring ; store count of special Palindromic substring ; it will store the count of continues same char ; traverse string character from left to right ; store same character count ; count smiler character ; Case : 1 so total number of substring that we can generate are : K * ( K + 1 ) / 2 here K is sameCharCount ; store current same char count in sameChar [ ] array ; increment i ; Case 2 : Count all odd length Special Palindromic substring ; if current character is equal to previous one then we assign Previous same character count to current one ; case 2 : odd length ; subtract all single length substring ; Driver Code
def CountSpecialPalindrome ( str ) : NEW_LINE INDENT n = len ( str ) ; NEW_LINE result = 0 ; NEW_LINE sameChar = [ 0 ] * n ; NEW_LINE i = 0 ; NEW_LINE while ( i < n ) : NEW_LINE INDENT sameCharCount = 1 ; NEW_LINE j = i + 1 ; NEW_LINE while ( j < n ) : NEW_LINE INDENT if ( str [ i ] != str [ j ] ) : NEW_LINE INDENT break ; NEW_LINE DEDENT sameCharCount += 1 ; NEW_LINE j += 1 ; NEW_LINE DEDENT result += int ( sameCharCount * ( sameCharCount + 1 ) / 2 ) ; NEW_LINE sameChar [ i ] = sameCharCount ; NEW_LINE i = j ; NEW_LINE DEDENT for j in range ( 1 , n ) : NEW_LINE INDENT if ( str [ j ] == str [ j - 1 ] ) : NEW_LINE INDENT sameChar [ j ] = sameChar [ j - 1 ] ; NEW_LINE DEDENT if ( j > 0 and j < ( n - 1 ) and ( str [ j - 1 ] == str [ j + 1 ] and str [ j ] != str [ j - 1 ] ) ) : NEW_LINE INDENT result += ( sameChar [ j - 1 ] if ( sameChar [ j - 1 ] < sameChar [ j + 1 ] ) else sameChar [ j + 1 ] ) ; NEW_LINE DEDENT DEDENT return result - n ; NEW_LINE DEDENT str = " abccba " ; NEW_LINE print ( CountSpecialPalindrome ( str ) ) ; NEW_LINE
Find if a string starts and ends with another given string | Python program to find if a given corner string is present at corners . ; If length of corner string is more , it cannot be present at corners . ; Return true if corner string is present at both corners of given string . ; Driver Code
def isCornerPresent ( str , corner ) : NEW_LINE INDENT n = len ( str ) NEW_LINE cl = len ( corner ) NEW_LINE if ( n < cl ) : NEW_LINE INDENT return False NEW_LINE DEDENT return ( ( str [ : cl ] == corner ) and ( str [ n - cl : ] == corner ) ) NEW_LINE DEDENT str = " geeksforgeeks " NEW_LINE corner = " geeks " NEW_LINE if ( isCornerPresent ( str , corner ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Find i 'th Index character in a binary string obtained after n iterations | Function to store binary Representation ; Function to find ith character ; Function to change decimal to binary ; Assign s1 string in s string ; Driver code
def binary_conversion ( s , m ) : NEW_LINE INDENT while ( m ) : NEW_LINE INDENT temp = m % 2 NEW_LINE s += str ( temp ) NEW_LINE m = m // 2 NEW_LINE DEDENT return s [ : : - 1 ] NEW_LINE DEDENT def find_character ( n , m , i ) : NEW_LINE INDENT s = " " NEW_LINE s = binary_conversion ( s , m ) NEW_LINE s1 = " " NEW_LINE for x in range ( n ) : NEW_LINE INDENT for j in range ( len ( s ) ) : NEW_LINE INDENT if s [ j ] == "1" : NEW_LINE INDENT s1 += "10" NEW_LINE DEDENT else : NEW_LINE INDENT s1 += "01" NEW_LINE DEDENT DEDENT s = s1 NEW_LINE s1 = " " NEW_LINE DEDENT e = ord ( s [ i ] ) NEW_LINE r = ord ( '0' ) NEW_LINE return e - r NEW_LINE DEDENT m , n , i = 5 , 2 , 8 NEW_LINE print ( find_character ( n , m , i ) ) NEW_LINE