codeparrot/xlcost-text-to-code huggingface dataset:
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
< ? php function transformString ( & $ s ) { $ ch = " a " ; if ( strlen ( $ s ) < 26 ) return false ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) { if ( ord ( $ ch ) > ord ( " z " ) ) break ; if ( $ s [ $ i ] <= $ ch ) { $ s [ $ i ] = $ ch ; $ ch = chr ( ord ( $ ch ) + 1 ) ; } } if ( $ ch <= " z " ) return false ; return true ; } $ str = " aaaaaaaaaaaaaaaaaaaaaaaaaa " ; if ( transformString ( $ str ) ) echo $ str ; else echo " Not ▁ Possible " ; ? >
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
< ? php function isPanDigital ( $ s ) { $ digits = array ( ) ; $ digits = array_fill ( 0 , 10 , false ) ; for ( $ i = 0 ; $ i < strlen ( $ s ) ; $ i ++ ) $ digits [ ord ( $ s [ $ i ] ) - ord ( '0' ) ] = true ; for ( $ i = 0 ; $ i <= 9 ; $ i ++ ) if ( $ digits [ $ i ] == false ) return false ; return true ; } function countPandigitalPairs ( & $ v ) { $ pairs = 0 ; for ( $ i = 0 ; $ i < count ( $ v ) ; $ i ++ ) { for ( $ j = $ i + 1 ; $ j < count ( $ v ) ; $ j ++ ) { if ( isPanDigital ( $ v [ $ i ] . $ v [ $ j ] ) ) { $ pairs ++ ; } } } return $ pairs ; } $ v = array ( "123567" , "098234" , "14765" , "19804" ) ; echo ( countPandigitalPairs ( $ v ) ) ; ? >
Minimum changes to a string 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
< ? php function minChanges ( $ str ) { $ n = strlen ( $ str ) ; if ( $ n > 26 ) return -1 ; $ dist_count = 0 ; $ count = array_fill ( 0 , 26 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ count [ ord ( $ str [ $ i ] ) - 97 ] == 0 ) $ dist_count ++ ; $ count [ ord ( $ str [ $ i ] ) - 97 ] ++ ; } return ( $ n - $ dist_count ) ; } $ str = " aebaecedabbee " ; echo minChanges ( $ str ) ; ? >
Number of strings of length N with no palindromic sub string | Return the count of strings with no palindromic substring . ; Driver Code
< ? php function numofstring ( $ n , $ m ) { if ( $ n == 1 ) return $ m ; if ( $ n == 2 ) return $ m * ( $ m - 1 ) ; return $ m * ( $ m - 1 ) * pow ( $ m - 2 , $ n - 2 ) ; } { $ n = 2 ; $ m = 3 ; echo numofstring ( $ n , $ m ) ; return 0 ; } ? >
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
< ? php function CountSpecialPalindrome ( $ str ) { $ n = strlen ( $ str ) ; $ result = 0 ; $ sameChar = array_fill ( 0 , $ n , 0 ) ; $ i = 0 ; while ( $ i < $ n ) { $ sameCharCount = 1 ; $ j = $ i + 1 ; while ( $ j < $ n ) { if ( $ str [ $ i ] != $ str [ $ j ] ) break ; $ sameCharCount ++ ; $ j ++ ; } $ result += ( int ) ( $ sameCharCount * ( $ sameCharCount + 1 ) / 2 ) ; $ sameChar [ $ i ] = $ sameCharCount ; $ i = $ j ; } for ( $ j = 1 ; $ j < $ n ; $ j ++ ) { if ( $ str [ $ j ] == $ str [ $ j - 1 ] ) $ sameChar [ $ j ] = $ sameChar [ $ j - 1 ] ; if ( $ j > 0 && $ j < ( $ n - 1 ) && ( $ str [ $ j - 1 ] == $ str [ $ j + 1 ] && $ str [ $ j ] != $ str [ $ j - 1 ] ) ) $ result += $ sameChar [ $ j - 1 ] < $ sameChar [ $ j + 1 ] ? $ sameChar [ $ j - 1 ] : $ sameChar [ $ j + 1 ] ; } return $ result - $ n ; } $ str = " abccba " ; echo CountSpecialPalindrome ( $ str ) ; ? >
Print Bracket Number | function to print the bracket number ; used to print the bracket number for the left bracket ; used to obtain the bracket number for the right bracket ; traverse the given expression ' exp ' ; if current character is a left bracket ; print ' left _ bnum ' , ; push ' left _ bum ' on to the stack ' right _ bnum ' ; increment ' left _ bnum ' by 1 ; else if current character is a right bracket ; print the top element of stack ' right _ bnum ' it will be the right bracket number ; pop the top element from the stack ; Driver Code
< ? php function printBracketNumber ( $ exp , $ n ) { $ left_bnum = 1 ; $ right_bnum = array ( ) ; $ t = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ exp [ $ i ] == ' ( ' ) { echo $ left_bnum . " " ; $ right_bnum [ $ t ++ ] = $ left_bnum ; $ left_bnum ++ ; } else if ( $ exp [ $ i ] == ' ) ' ) { echo $ right_bnum [ $ t - 1 ] . " " ; $ right_bnum [ $ t - 1 ] = 1 ; $ t -- ; } } } $ exp = " ( a + ( b * c ) ) + ( d / e ) " ; $ n = strlen ( $ exp ) ; printBracketNumber ( $ exp , $ n ) ; ? >
Find if a string starts and ends with another given string | PHP 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
< ? php function isCornerPresent ( $ str , $ corner ) { $ n = strlen ( $ str ) ; $ cl = strlen ( $ corner ) ; if ( $ n < $ cl ) return false ; return ( ! strcmp ( substr ( $ str , 0 , $ cl ) , $ corner ) && ! strcmp ( substr ( $ str , $ n - $ cl , $ cl ) , $ corner ) ) ; } $ str = " geeksforgeeks " ; $ corner = " geeks " ; if ( isCornerPresent ( $ str , $ corner ) ) echo ( " Yes " ) ; else echo ( " No " ) ; ? >
Converting one string to other using append and delete last operations | Returns true if it is possible to convert str1 to str2 using k operations . ; Case A ( i ) ; finding common length of both string ; Case A ( ii ) - ; Case B ; Driver Code
< ? php function isConvertible ( $ str1 , $ str2 , $ k ) { if ( ( strlen ( $ str1 ) + strlen ( $ str2 ) ) < $ k ) return true ; $ commonLength = 0 ; for ( $ i = 0 ; $ i < min ( strlen ( $ str1 ) , strlen ( $ str2 ) ) ; $ i ++ ) { if ( $ str1 == $ str2 ) $ commonLength += 1 ; else break ; } if ( ( $ k - strlen ( $ str1 ) - strlen ( $ str2 ) + 2 * $ commonLength ) % 2 == 0 ) return true ; return false ; } $ str1 = " geek " ; $ str2 = " geek " ; $ k = 7 ; if ( isConvertible ( $ str1 , $ str2 , $ k ) ) echo " Yes " . " STRNEWLINE " ; else echo " No " . " STRNEWLINE " ; $ str1 = " geeks " ; $ str2 = " geek " ; $ k = 5 ; if ( isConvertible ( $ str1 , $ str2 , $ k ) ) echo " Yes " . " STRNEWLINE " ; else echo " No " . " STRNEWLINE " ; ? >
Maximum distinct lowercase alphabets between two uppercase | PHP Program to find maximum lowercase alphabets present between two uppercase alphabets ; Function which computes the maximum number of distinct lowercase alphabets between two uppercase alphabets ; Ignoring lowercase characters in the beginning . ; We start from next of first capital letter and traverse through remaining character . ; If character is in uppercase , ; Count all distinct lower case characters ; Update maximum count ; Reset count array ; If character is in lowercase ; Driver Code
< ? php $ MAX_CHAR = 26 ; function maxLower ( $ str ) { global $ MAX_CHAR ; $ n = strlen ( $ str ) ; $ i = 0 ; for ( ; $ i < $ n ; $ i ++ ) { if ( $ str [ $ i ] >= ' A ' && $ str [ $ i ] <= ' Z ' ) { $ i ++ ; break ; } } $ maxCount = 0 ; $ count = array_fill ( 0 , $ MAX_CHAR , NULL ) ; for ( ; $ i < $ n ; $ i ++ ) { if ( $ str [ $ i ] >= ' A ' && $ str [ $ i ] <= ' Z ' ) { $ currCount = 0 ; for ( $ j = 0 ; $ j < $ MAX_CHAR ; $ j ++ ) if ( $ count [ $ j ] > 0 ) $ currCount ++ ; $ maxCount = max ( $ maxCount , $ currCount ) ; $ count = array_fill ( 0 , $ MAX_CHAR , NULL ) ; } if ( $ str [ $ i ] >= ' a ' && $ str [ $ i ] <= ' z ' ) $ count [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] ++ ; } return $ maxCount ; } $ str = " zACaAbbaazzC " ; echo maxLower ( $ str ) ; ? >
First uppercase letter in a string ( Iterative and Recursive ) | Function to find string which has first character of each word . ; Driver code
< ? php function first ( $ str ) { for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) if ( ctype_upper ( $ str [ $ i ] ) ) { return $ str [ $ i ] ; } return 0 ; } $ str = " geeksforGeeKS " ; $ res = first ( $ str ) ; if ( ord ( $ res ) == ord ( 0 ) ) echo " No ▁ uppercase ▁ letter " ; else echo $ res . " STRNEWLINE " ; ? >
Counting even decimal value substrings in a binary string | generate all substring in arr [ 0. . n - 1 ] ; store the count ; Pick starting point ; Pick ending point ; substring between current starting and ending points ; increment power of 2 by one ; Driver Code
< ? php function evenDecimalValue ( $ str , $ n ) { $ result = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = $ i ; $ j < $ n ; $ j ++ ) { $ decimalValue = 0 ; $ powerOf2 = 1 ; for ( $ k = $ i ; $ k <= $ j ; $ k ++ ) { $ decimalValue += ( ( $ str [ $ k ] - '0' ) * $ powerOf2 ) ; $ powerOf2 *= 2 ; } if ( $ decimalValue % 2 == 0 ) $ result ++ ; } } return $ result ; } $ str = "10010" ; $ n = 5 ; echo evenDecimalValue ( $ str , $ n ) ; ? >
Create a new string by alternately combining the characters of two halves of the string in reverse | Function performing calculations ; Calculating the two halves of string s as first and second . The final string p ; It joins the characters to final string in reverse order ; It joins the characters to final string in reverse order ; Driver code ; Calling function
< ? php function solve ( $ s ) { $ l = strlen ( $ s ) ; $ x = $ l / 2 ; $ y = $ l ; $ p = " " ; while ( $ x > 0 && $ y > $ l / 2 ) { $ p = $ p . $ s [ $ x - 1 ] ; $ x -- ; $ p = $ p . $ s [ $ y - 1 ] ; $ y -- ; } if ( $ y > $ l / 2 ) { $ p = $ p . $ s [ $ y - 1 ] ; $ y -- ; } echo $ p ; } $ s = " sunshine " ; solve ( $ s ) ; ? >
Program for length of a string using recursion | Function to calculate length ; if we reach at the end of the string ; Driver Code
< ? php function recLen ( & $ str , $ i ) { if ( $ i == strlen ( $ str ) ) return 0 ; else return 1 + recLen ( $ str , $ i + 1 ) ; } $ str = " GeeksforGeeks " ; echo ( recLen ( $ str , 0 ) ) ; ? >
Count consonants in a string ( Iterative and recursive methods ) | Function to check for consonant ; To handle lower case ; To check is character is Consonant ; Driver code
< ? php function isConsonant ( $ ch ) { $ ch = strtoupper ( $ ch ) ; return ! ( $ ch == ' A ' $ ch == ' E ' $ ch == ' I ' $ ch == ' O ' $ ch == ' U ' ) && ord ( $ ch ) >= 65 && ord ( $ ch ) <= 90 ; } function totalConsonants ( $ str ) { $ count = 0 ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) if ( isConsonant ( $ str [ $ i ] ) ) ++ $ count ; return $ count ; } $ str = " abc ▁ de " ; echo totalConsonants ( $ str ) ; return 0 ; ? >
Check if actual binary representation of a number is palindrome | function to reverse bits of a number ; traversing bits of ' n ' from the right ; bitwise left shift ' rev ' by 1 ; if current bit is '1' ; bitwise right shift ' n ' by 1 ; required number ; function to check whether binary representation of a number is palindrome or not ; get the number by reversing bits in the binary representation of ' n ' ; Driver code
< ? php function reverseBits ( $ n ) { $ rev = 0 ; while ( $ n > 0 ) { $ rev <<= 1 ; if ( $ n & 1 == 1 ) $ rev ^= 1 ; $ n >>= 1 ; } return $ rev ; } function isPalindrome ( $ n ) { $ rev = reverseBits ( $ n ) ; return ( $ n == $ rev ) ; } $ n = 9 ; if ( isPalindrome ( $ n ) ) echo " Yes " ; else echo " No " ; return 0 ; ? >
Find the arrangement of queue at given time | prints the arrangement at time = t ; Checking the entire queue for every moment from time = 1 to time = t . ; If current index contains ' B ' and next index contains ' G ' then swap ; Driver code
< ? php function solve ( $ n , $ t , $ s ) { for ( $ i = 0 ; $ i < $ t ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n - 1 ; $ j ++ ) { if ( $ s [ $ j ] == ' B ' && $ s [ $ j + 1 ] == ' G ' ) { $ temp = $ s [ $ j ] ; $ s [ $ j ] = $ s [ $ j + 1 ] ; $ s [ $ j + 1 ] = $ temp ; $ j ++ ; } } } echo ( $ s ) ; } $ n = 6 ; $ t = 2 ; $ s = " BBGBBG " ; solve ( $ n , $ t , $ s ) ; ? >
Add two numbers represented by two arrays | Return sum of two number represented by the arrays . Size of a [ ] is greater than b [ ] . It is made sure be the wrapper function ; array to store sum . ; Until we reach beginning of array . we are comparing only for second array because we have already compare the size of array in wrapper function . ; find sum of corresponding element of both array . ; Finding carry for next sum . ; If second array size is less than the first array size . ; Add carry to first array elements . ; If there is carry on adding 0 index elements . append 1 to total sum . ; Converting array into number . ; Wrapper Function ; Making first array which have greater number of element ; Driven Code
< ? php function calSumUtil ( $ a , $ b , $ n , $ m ) { $ sum = array ( ) ; $ i = $ n - 1 ; $ j = $ m - 1 ; $ k = $ n - 1 ; $ carry = 0 ; $ s = 0 ; while ( $ j >= 0 ) { $ s = $ a [ $ i ] + $ b [ $ j ] + $ carry ; $ sum [ $ k ] = ( $ s % 10 ) ; $ carry = $ s / 10 ; $ k -- ; $ i -- ; $ j -- ; } while ( $ i >= 0 ) { $ s = $ a [ $ i ] + $ carry ; $ sum [ $ k ] = ( $ s % 10 ) ; $ carry = $ s / 10 ; $ i -- ; $ k -- ; } $ ans = 0 ; if ( $ carry ) $ ans = 10 ; for ( $ i = 0 ; $ i <= $ n - 1 ; $ i ++ ) { $ ans += $ sum [ $ i ] ; $ ans *= 10 ; } return $ ans / 10 ; } function calSum ( $ a , $ b , $ n , $ m ) { if ( $ n >= $ m ) return calSumUtil ( $ a , $ b , $ n , $ m ) ; else return calSumUtil ( $ b , $ a , $ m , $ n ) ; } $ a = array ( 9 , 3 , 9 ) ; $ b = array ( 6 , 1 ) ; $ n = count ( $ a ) ; $ m = count ( $ b ) ; echo calSum ( $ a , $ b , $ n , $ m ) ; ? >
Longest Common Anagram Subsequence | PHP implementation to find the length of the longest common anagram subsequence ; function to find the length of the longest common anagram subsequence ; hash tables for storing frequencies of each character ; calculate frequency of each character of ' str1' ; calculate frequency of each character of ' str2' ; for each character add its minimum frequency out of the two strings in ' len ' ; required length ; Driver Code
< ? php $ SIZE = 26 ; function longCommomAnagramSubseq ( $ str1 , $ str2 , $ n1 , $ n2 ) { global $ SIZE ; $ freq1 = array ( ) ; $ freq2 = array ( ) ; for ( $ i = 0 ; $ i < $ SIZE ; $ i ++ ) { $ freq1 [ $ i ] = 0 ; $ freq2 [ $ i ] = 0 ; } $ len = 0 ; for ( $ i = 0 ; $ i < $ n1 ; $ i ++ ) $ freq1 [ ord ( $ str1 [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < $ n2 ; $ i ++ ) $ freq2 [ ord ( $ str2 [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < $ SIZE ; $ i ++ ) { $ len += min ( $ freq1 [ $ i ] , $ freq2 [ $ i ] ) ; } return $ len ; } $ str1 = " abdacp " ; $ str2 = " ckamb " ; $ n1 = strlen ( $ str1 ) ; $ n2 = strlen ( $ str2 ) ; echo ( " Length ▁ = ▁ " . longCommomAnagramSubseq ( $ str1 , $ str2 , $ n1 , $ n2 ) ) ; ? >
Panalphabetic window in a string | Return if given string contain panalphabetic window . ; traversing the string ; if character of string is equal to ch , increment ch . ; if all characters are found , return true . ; Driver Code
< ? php function isPanalphabeticWindow ( $ s , $ n ) { $ ch = ' a ' ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] == $ ch ) $ ch ++ ; if ( $ ch == ' z ' ) return true ; } return false ; } $ s = " abujm ▁ zvcd ▁ acefc ▁ deghf ▁ gijkle " . " ▁ m ▁ n ▁ o ▁ p ▁ pafqrstuvwxyzfap " ; $ n = strlen ( $ s ) ; if ( isPanalphabeticWindow ( $ s , $ n ) ) echo ( " YES " ) ; else echo ( " NO " ) ; ? >
Program to print characters present at prime indexes in a given string | PHP Program to print Characters at Prime index in a given String ; Corner case ; Check from 2 to n - 1 ; Function to print character at prime index ; Loop to check if index prime or not ; Driver Code
< ? php function isPrime ( $ n ) { if ( $ n <= 1 ) return false ; for ( $ i = 2 ; $ i < $ n ; $ i ++ ) if ( $ n % $ i == 0 ) return false ; return true ; } function prime_index ( $ input ) { $ n = strlen ( $ input ) ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) if ( isPrime ( $ i ) ) echo $ input [ $ i - 1 ] ; } $ input = " GeeksforGeeks " ; prime_index ( $ input ) ; ? >
Check whether a given string is Heterogram or not | PHP Program to check whether the given string is Heterogram or not . ; traversing the string . ; ignore the space ; if already encountered ; else return false . ; Driven Code
< ? php function isHeterogram ( $ s , $ n ) { $ hash = array ( ) ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) $ hash [ $ i ] = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ s [ $ i ] != ' ▁ ' ) { if ( $ hash [ ord ( $ s [ $ i ] ) - ord ( ' a ' ) ] == 0 ) $ hash [ ord ( $ s [ $ i ] ) - ord ( ' a ' ) ] = 1 ; else return false ; } } return true ; } $ s = " the ▁ big ▁ dwarf ▁ only ▁ jumps " ; $ n = strlen ( $ s ) ; if ( isHeterogram ( $ s , $ n ) ) echo ( " YES " ) ; else echo ( " NO " ) ; ? >
Print given sentence into its equivalent ASCII form | Function to compute the ASCII value of each character one by one ; Driver code
< ? php function ASCIISentence ( $ str ) { for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) echo ord ( $ str [ $ i ] ) ; } $ str = " GeeksforGeeks " ; echo " ASCII ▁ Sentence : " . " STRNEWLINE " ; ASCIISentence ( $ str ) ; ? >
Snake case of a given sentence | Function to replace spaces and convert into snake case ; Converting space to underscor ; If not space , convert into lower character ; Driver Code ; Calling function
< ? php function convert ( $ str ) { $ n = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ str [ $ i ] == ' ▁ ' ) $ str [ $ i ] = ' _ ' ; else $ str [ $ i ] = strtolower ( $ str [ $ i ] ) ; } echo $ str ; } $ str = " I ▁ got ▁ intern ▁ at ▁ geeksforgeeks " ; convert ( $ str ) ; ? >
Replace a character c1 with c2 and c2 with c1 in a string S | PHP program to replace c1 with c2 and c2 with c1 ; loop to traverse in the string ; check for c1 and replace ; check for c2 and replace ; Driver Code
< ? php function replace ( $ s , $ c1 , $ c2 ) { $ l = strlen ( $ s ) ; for ( $ i = 0 ; $ i < $ l ; $ i ++ ) { if ( $ s [ $ i ] == $ c1 ) $ s [ $ i ] = $ c2 ; else if ( $ s [ $ i ] == $ c2 ) $ s [ $ i ] = $ c1 ; } return $ s ; } $ s = " grrksfoegrrks " ; $ c1 = ' e ' ; $ c2 = ' r ' ; echo replace ( $ s , $ c1 , $ c2 ) ; ? >
Construct lexicographically smallest palindrome | function for printing palindrome ; iterate till i < j ; continue if str [ i ] == str [ j ] ; update str [ i ] = str [ j ] = ' a ' if both are ' * ' ; update str [ i ] = str [ j ] if only str [ i ] = ' * ' ; update str [ j ] = str [ i ] if only str [ j ] = ' * ' ; else print not possible and return ; Driver Code
< ? php function constructPalin ( $ str , $ len ) { $ i = 0 ; $ j = $ len - 1 ; for ( ; $ i < $ j ; $ i ++ , $ j -- ) { if ( $ str [ $ i ] == $ str [ $ j ] && $ str [ $ i ] != ' * ' ) continue ; else if ( $ str [ $ i ] == $ str [ $ j ] && $ str [ $ i ] == ' * ' ) { $ str [ $ i ] = ' a ' ; $ str [ $ j ] = ' a ' ; continue ; } else if ( $ str [ $ i ] == ' * ' ) { $ str [ $ i ] = $ str [ $ j ] ; continue ; } else if ( $ str [ $ j ] == ' * ' ) { $ str [ $ j ] = $ str [ $ i ] ; continue ; } echo " Not ▁ Possible " ; return " " ; } return $ str ; } $ str = " bca * xc * * b " ; $ len = strlen ( $ str ) ; echo constructPalin ( $ str , $ len ) ; ? >
Printing string in plus ‘ + ’ pattern in the matrix | Function to make a cross in the matrix ; As , it is not possible to make the cross exactly in the middle of the matrix with an even length string . ; declaring a 2D array i . e a matrix ; Now , we will fill all the elements of the array with ' X ' ; Now , we will place the characters of the string in the matrix , such that a cross is formed in it . ; here the characters of the string will be added in the middle column of our array . ; here the characters of the string will be added in the middle row of our array . ; Now finally , we will print the array ; Driver Code
< ? php function carveCross ( $ str ) { $ n = strlen ( $ str ) ; if ( $ n % 2 == 0 ) { echo ( " Not ▁ possible . ▁ Please ▁ enter ▁ " ) ; echo ( " odd ▁ length ▁ string . STRNEWLINE " ) ; } else { $ arr = array ( ) ; $ m = $ n / 2 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ arr [ $ i ] [ $ j ] = ' X ' ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ arr [ $ i ] [ $ m ] = $ str [ $ i ] ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ arr [ $ m ] [ $ i ] = $ str [ $ i ] ; } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { echo ( $ arr [ $ i ] [ $ j ] . " " ) ; } echo ( " STRNEWLINE " ) ; } } } $ str = " PICTURE " ; carveCross ( $ str ) ; ? >
Program to find remainder when large number is divided by 11 | Function to return remainder ; len is variable to store the length of number string . ; loop that find remainder ; Driver code
< ? php function remainder ( $ str ) { $ len = strlen ( $ str ) ; $ num ; $ rem = 0 ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { $ num = $ rem * 10 + ( $ str [ $ i ] - '0' ) ; $ rem = $ num % 11 ; } return $ rem ; } $ str = "3435346456547566345436457867978" ; echo ( remainder ( $ str ) ) ; ? >
Generate permutations with only adjacent swaps allowed | PHP program to generate permutations with only one swap allowed . ; don 't swap the current position ; Swap with the next character and revert the changes . As explained above , swapping with previous is is not needed as it anyways happens for next character . ; Driver code
< ? php function findPermutations ( $ str , $ index , $ n ) { if ( $ index >= $ n || ( $ index + 1 ) >= $ n ) { echo $ str , " STRNEWLINE " ; return ; } findPermutations ( $ str , $ index + 1 , $ n ) ; list ( $ str [ $ index ] , $ str [ $ index + 1 ] ) = array ( $ str [ $ index + 1 ] , $ str [ $ index ] ) ; findPermutations ( $ str , $ index + 2 , $ n ) ; list ( $ str [ $ index ] , $ str [ $ index + 1 ] ) = array ( $ str [ $ index + 1 ] , $ str [ $ index ] ) ; } $ str = "12345" ; $ n = strlen ( $ str ) ; findPermutations ( $ str , 0 , $ n ) ; ? >
Check if an encoding represents a unique binary string | PHP program to check if given encoding represents a single string ; Return true if sum becomes k ; Driver Code
< ? php function isUnique ( $ a , $ n , $ k ) { $ sum = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ sum += $ a [ $ i ] ; $ sum += $ n - 1 ; return ( $ sum == $ k ) ; } $ a = array ( 3 , 3 , 3 ) ; $ n = count ( $ a ) ; $ k = 12 ; if ( isUnique ( $ a , $ n , $ k ) ) echo " Yes " ; else echo " No " ; ? >