codeparrot/xlcost-text-to-code huggingface dataset:
Count words in a given string | PHP program to count no of words from given input string ; returns number of words in str ; word count ; Scan all characters one by one ; If next character is a separator , set the state as OUT ; If next character is not a word separator and state is OUT , then set the state as IN and increment word count ; Move to next character ; Driver Code
< ? php $ OUT = 0 ; $ IN = 1 ; function countWords ( $ str ) { global $ OUT , $ IN ; $ state = $ OUT ; $ wc = 0 ; $ i = 0 ; while ( $ i < strlen ( $ str ) ) { if ( $ str [ $ i ] == " ▁ " $ str [ $ i ] == " STRNEWLINE " $ str [ $ i ] == " TABSYMBOL " ) $ state = $ OUT ; else if ( $ state == $ OUT ) { $ state = $ IN ; ++ $ wc ; } ++ $ i ; } return $ wc ; } $ str = " One ▁ two TABSYMBOL TABSYMBOL three STRNEWLINE ▁ four TABSYMBOL five ▁ " ; echo " No ▁ of ▁ words ▁ : ▁ " . countWords ( $ str ) ; ? >
Enneadecagonal number | Function to calculate Enneadecagonal number ; Formula for finding nth Enneadecagonal number ; Driver Code
< ? php function nthEnneadecagonal ( $ n ) { return ( 17 * $ n * $ n - 15 * $ n ) / 2 ; } $ n = 6 ; echo $ n , " th ▁ Enneadecagonal ▁ number ▁ : " , nthEnneadecagonal ( $ n ) ; ? >
Area of a Circumscribed Circle of a Square | PHP Program to find the area of a circumscribed circle ; function returns the area ; Driver code
< ? php $ PI = 3.14159265 ; function areacircumscribed ( $ a ) { global $ PI ; return ( $ a * $ a * ( $ PI / 2 ) ) ; } $ a = 6 ; echo " ▁ Area ▁ of ▁ an ▁ circumscribed ▁ circle ▁ is ▁ : ▁ " , areacircumscribed ( $ a ) ; ? >
Tetranacci Numbers | Function to return the N - th tetranacci number ; base cases ; base cases ; base cases ; function to print the nth tetranacci number ; Driver code
< ? php function printTetraRec ( $ n ) { if ( $ n == 0 ) return 0 ; if ( $ n == 1 $ n == 2 ) return 1 ; if ( $ n == 3 ) return 2 ; else return printTetraRec ( $ n - 1 ) + printTetraRec ( $ n - 2 ) + printTetraRec ( $ n - 3 ) + printTetraRec ( $ n - 4 ) ; } function printTetra ( $ n ) { echo printTetraRec ( $ n ) . " " ; } $ n = 10 ; printTetra ( $ n ) ; ? >
Dynamic Programming | High | A DP based PHP program to find maximum tasks . Returns the maximum among the 2 numbers ; Returns the maximum among the 2 numbers ; Returns maximum amount of task that can be done till day n ; An array task_dp that stores the maximum task done ; If n = 0 , no solution exists ; If n = 1 , high effort task on that day will be the solution ; Fill the entire array determining which task to choose on day i ; Driver code
< ? php function max1 ( $ x , $ y ) { return ( $ x > $ y ? $ x : $ y ) ; } return ( $ x > $ y ? $ x : $ y ) ; } function maxTasks ( $ high , $ low , $ n ) { $ task_dp = array ( $ n + 1 ) ; $ task_dp [ 0 ] = 0 ; $ task_dp [ 1 ] = $ high [ 0 ] ; for ( $ i = 2 ; $ i <= $ n ; $ i ++ ) $ task_dp [ $ i ] = max ( $ high [ $ i - 1 ] + $ task_dp [ $ i - 2 ] , $ low [ $ i - 1 ] + $ task_dp [ $ i - 1 ] ) ; return $ task_dp [ $ n ] ; } { $ n = 5 ; $ high = array ( 3 , 6 , 8 , 7 , 6 ) ; $ low = array ( 1 , 5 , 4 , 5 , 3 ) ; echo ( maxTasks ( $ high , $ low , $ n ) ) ; }
Permutation Coefficient | A O ( n ) time and O ( 1 ) extra space PHP solution to calculate the Permutation Coefficient ; Compute n ! and ( n - k ) ! ; Driver Code
< ? php function PermutationCoeff ( $ n , $ k ) { $ Fn = 1 ; $ Fk ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { $ Fn *= $ i ; if ( $ i == $ n - $ k ) $ Fk = $ Fn ; } $ coeff = $ Fn / $ Fk ; return $ coeff ; } $ n = 10 ; $ k = 2 ; echo " Value ▁ of ▁ P ( " , $ n , " , ▁ " , $ k , " ) STRNEWLINE TABSYMBOL TABSYMBOL is ▁ " , PermutationCoeff ( $ n , $ k ) ; ? >
DFA for Strings not ending with " THE " | dfa tells the number associated with the present state ; This function is for the starting state ( zeroth ) of DFA ; On receiving ' T ' or ' t ' goto first state ( 1 ) ; This function is for the first state of DFA ; On receiving ' T ' or ' t ' goto first state ( 1 ) ; On receiving ' H ' or ' h ' goto second state ( 2 ) ; else goto starting state ( 0 ) ; This function is for the second state of DFA ; On receiving ' E ' or ' e ' goto third state ( 3 ) else goto starting state ( 0 ) ; This function is for the third state of DFA ; On receiving ' T ' or ' t ' goto first state ( 1 ) else goto starting state ( 0 ) ; store length of string ; Driver Code
< ? php $ dfa = 0 ; function start ( $ c ) { global $ dfa ; if ( $ c == ' t ' $ c == ' T ' ) $ dfa = 1 ; } function state1 ( $ c ) { global $ dfa ; if ( $ c == ' t ' $ c == ' T ' ) $ dfa = 1 ; else if ( $ c == ' h ' $ c == ' H ' ) $ dfa = 2 ; else $ dfa = 0 ; } function state2 ( $ c ) { global $ dfa ; if ( $ c == ' e ' $ c == ' E ' ) $ dfa = 3 ; else $ dfa = 0 ; } function state3 ( $ c ) { global $ dfa ; if ( $ c == ' t ' $ c == ' T ' ) $ dfa = 1 ; else $ dfa = 0 ; } function isAccepted ( $ str ) { global $ dfa ; $ len = strlen ( $ str ) ; for ( $ i = 0 ; $ i < $ len ; $ i ++ ) { if ( $ dfa == 0 ) start ( $ str [ $ i ] ) ; else if ( $ dfa == 1 ) state1 ( $ str [ $ i ] ) ; else if ( $ dfa == 2 ) state2 ( $ str [ $ i ] ) ; else state3 ( $ str [ $ i ] ) ; } return ( $ dfa != 3 ) ; } $ str = " forTHEgeeks " ; if ( isAccepted ( $ str ) == true ) echo " ACCEPTED STRNEWLINE " ; else echo " NOT ▁ ACCEPTED STRNEWLINE " ; ? >
Check whether given string can be generated after concatenating given strings | Function that return true if pre is a prefix of str ; While there are characters to match ; If characters differ at any position ; str starts with pre ; Function that return true if suff is a suffix of str ; While there are characters to match ; I$f characters differ at any position ; str ends with suff ; Function that returns true if str = a + b or str = b + a ; str cannot be generated by concatenating a and b ; If str starts with a i . e . a is a prefix of str ; Check if the rest of the characters are equal to b i . e . b is a suffix of str ; If str starts with b i . e . b is a prefix of str ; Check if the rest of the characters are equal to a i . e . a is a suffix of str ; Driver code
< ? php function startsWith ( $ str , $ pre ) { $ strLen = strlen ( $ str ) ; $ preLen = strlen ( $ pre ) ; $ i = 0 ; $ j = 0 ; while ( $ i < $ strLen && $ j < $ preLen ) { if ( $ str [ $ i ] != $ pre [ $ j ] ) return false ; $ i ++ ; $ j ++ ; } return true ; } function endsWith ( $ str , $ suff ) { $ i = strlen ( $ str ) - 0 ; $ j = strlen ( $ suff ) - 0 ; while ( $ i >= 0 && $ j >= 0 ) { if ( $ str [ $ i ] != $ suff [ $ j ] ) return false ; $ i -- ; $ j -- ; } return true ; } function checkString ( $ str , $ a , $ b ) { if ( strlen ( $ str ) != strlen ( $ a ) + strlen ( $ b ) ) return false ; if ( startsWith ( $ str , $ a ) ) { if ( endsWith ( $ str , $ b ) ) return true ; } if ( startsWith ( $ str , $ b ) ) { if ( endsWith ( $ str , $ a ) ) return true ; } return false ; } $ str = " GeeksforGeeks " ; $ a = " Geeksfo " ; $ b = " rGeeks " ; if ( checkString ( $ str , $ a , $ b ) ) echo " Yes " ; else echo " No " ; ? >
Minimum number of operations to move all uppercase characters before all lower case characters | Function to return the minimum number of operations required ; To store the indices of the last uppercase and the first lowercase character ; Find the last uppercase character ; Find the first lowercase character ; If all the characters are either uppercase or lowercase ; Count of uppercase characters that appear after the first lowercase character ; Count of lowercase characters that appear before the last uppercase character ; Return the minimum operations required ; Driver Code
< ? php function minOperations ( $ str , $ n ) { $ i ; $ lastUpper = -1 ; $ firstLower = -1 ; for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { if ( ctype_upper ( $ str [ $ i ] ) ) { $ lastUpper = $ i ; break ; } } for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( ctype_lower ( $ str [ $ i ] ) ) { $ firstLower = $ i ; break ; } } if ( $ lastUpper == -1 $ firstLower == -1 ) return 0 ; $ countUpper = 0 ; for ( $ i = $ firstLower ; $ i < $ n ; $ i ++ ) { if ( ctype_upper ( $ str [ $ i ] ) ) { $ countUpper ++ ; } } $ countLower = 0 ; for ( $ i = 0 ; $ i < $ lastUpper ; $ i ++ ) { if ( ctype_lower ( $ str [ $ i ] ) ) { $ countLower ++ ; } } return min ( $ countLower , $ countUpper ) ; } { $ str = " geEksFOrGEekS " ; $ n = strlen ( $ str ) ; echo ( minOperations ( $ str , $ n ) ) ; } ? >
Probability of rain on N + 1 th day | Function to find the probability ; count 1 ; find probability ; Driver Code
< ? php function rainDayProbability ( $ a , $ n ) { $ count = 0 ; $ m ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ a [ $ i ] == 1 ) $ count ++ ; } $ m = $ count / $ n ; return $ m ; } $ a = array ( 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1 ) ; $ n = count ( $ a ) ; echo rainDayProbability ( $ a , $ n ) ; ? >
Program to find the sum of a Series 1 + 1 / 2 ^ 2 + 1 / 3 ^ 3 + â €¦ . . + 1 / n ^ n | Function to calculate the following series ; Driver Code
< ? php function Series ( $ n ) { $ i ; $ sums = 0.0 ; $ ser ; for ( $ i = 1 ; $ i <= $ n ; ++ $ i ) { $ ser = 1 / pow ( $ i , $ i ) ; $ sums += $ ser ; } return $ sums ; } $ n = 3 ; $ res = Series ( $ n ) ; echo $ res ; ? >
Ternary Search | Function to perform Ternary Search ; Find the mid1 and mid2 ; Check if key is present at any mid ; Since key is not present at mid , check in which region it is present then repeat the Search operation in that region ; The key lies in between l and mid1 ; The key lies in between mid2 and r ; The key lies in between mid1 and mid2 ; Key not found ; Get the array Sort the array if not sorted ; Starting index ; length of array ; Key to be searched in the array ; Search the key using ternarySearch ; Print the result ; Key to be searched in the array ; Search the key using ternarySearch ; Print the result
< ? php function ternarySearch ( $ l , $ r , $ key , $ ar ) { if ( $ r >= $ l ) { $ mid1 = ( int ) ( $ l + ( $ r - $ l ) / 3 ) ; $ mid2 = ( int ) ( $ r - ( $ r - $ l ) / 3 ) ; if ( $ ar [ $ mid1 ] == $ key ) { return $ mid1 ; } if ( $ ar [ $ mid2 ] == $ key ) { return $ mid2 ; } if ( $ key < $ ar [ $ mid1 ] ) { return ternarySearch ( $ l , $ mid1 - 1 , $ key , $ ar ) ; } else if ( $ key > $ ar [ $ mid2 ] ) { return ternarySearch ( $ mid2 + 1 , $ r , $ key , $ ar ) ; } else { return ternarySearch ( $ mid1 + 1 , $ mid2 - 1 , $ key , $ ar ) ; } } return -1 ; } $ ar = array ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ) ; $ l = 0 ; $ r = 9 ; $ key = 5 ; $ p = ternarySearch ( $ l , $ r , $ key , $ ar ) ; echo " Index ▁ of ▁ " , $ key , " ▁ is ▁ " , ( int ) $ p , " STRNEWLINE " ; $ key = 50 ; $ p = ternarySearch ( $ l , $ r , $ key , $ ar ) ; echo " Index ▁ of ▁ " , $ key , " ▁ is ▁ " , ( int ) $ p , " STRNEWLINE " ; ? >
Print characters and their frequencies in order of occurrence | PHP implementation to print the character and its frequency in order of its occurrence ; function to print the character and its frequency in order of its occurrence ; size of the string ' str ' ; ' freq [ ] ' implemented as hash table ; accumulate frequency of each character in ' str ' ; traverse ' str ' from left to right ; if frequency of character str [ i ] is not equal to 0 ; print the character along with its frequency ; update frequency of str [ i ] to 0 so that the same character is not printed again ; Driver Code
< ? php $ SIZE = 26 ; function printCharWithFreq ( $ str ) { global $ SIZE ; $ n = strlen ( $ str ) ; $ freq = array_fill ( 0 , $ SIZE , NULL ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ freq [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] ++ ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ freq [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] != 0 ) { echo $ str [ $ i ] . $ freq [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] . " " ; $ freq [ ord ( $ str [ $ i ] ) - ord ( ' a ' ) ] = 0 ; } } } $ str = " geeksforgeeks " ; printCharWithFreq ( $ str ) ; ? >
Check horizontal and vertical symmetry in binary matrix | PHP program to find if a matrix is symmetric . ; Initializing as both horizontal and vertical symmetric . ; Checking for Horizontal Symmetry . We compare first row with last row , second row with second last row and so on . ; Checking each cell of a column . ; check if every cell is identical ; Checking for Vertical Symmetry . We compare first column with last column , second xolumn with second last column and so on . ; Checking each cell of a row . ; check if every cell is identical ; Driver Code
< ? php function checkHV ( $ arr , $ N , $ M ) { $ horizontal = true ; $ vertical = true ; for ( $ i = 0 , $ k = $ N - 1 ; $ i < $ N / 2 ; $ i ++ , $ k -- ) { for ( $ j = 0 ; $ j < $ M ; $ j ++ ) { if ( $ arr [ $ i ] [ $ j ] != $ arr [ $ k ] [ $ j ] ) { $ horizontal = false ; break ; } } } for ( $ i = 0 , $ k = $ M - 1 ; $ i < $ M / 2 ; $ i ++ , $ k -- ) { for ( $ j = 0 ; $ j < $ N ; $ j ++ ) { if ( $ arr [ $ i ] [ $ j ] != $ arr [ $ k ] [ $ j ] ) { $ horizontal = false ; break ; } } } if ( ! $ horizontal && ! $ vertical ) echo " NO STRNEWLINE " ; else if ( $ horizontal && ! $ vertical ) cout << " HORIZONTAL STRNEWLINE " ; else if ( $ vertical && ! $ horizontal ) echo " VERTICAL STRNEWLINE " ; else echo " BOTH STRNEWLINE " ; } $ mat = array ( array ( 1 , 0 , 1 ) , array ( 0 , 0 , 0 ) , array ( 1 , 0 , 1 ) ) ; checkHV ( $ mat , 3 , 3 ) ; ? >
Program for addition of two matrices | PHP program for addition of two matrices ; This function adds A [ ] [ ] and B [ ] [ ] , and stores the result in C [ ] [ ] ; Driver code
< ? php $ N = 4 ; function add ( & $ A , & $ B , & $ C ) { for ( $ i = 0 ; $ i < $ N ; $ i ++ ) for ( $ j = 0 ; $ j < $ N ; $ j ++ ) $ C [ $ i ] [ $ j ] = $ A [ $ i ] [ $ j ] + $ B [ $ i ] [ $ j ] ; } $ A = array ( array ( 1 , 1 , 1 , 1 ) , array ( 2 , 2 , 2 , 2 ) , array ( 3 , 3 , 3 , 3 ) , array ( 4 , 4 , 4 , 4 ) ) ; $ B = array ( array ( 1 , 1 , 1 , 1 ) , array ( 2 , 2 , 2 , 2 ) , array ( 3 , 3 , 3 , 3 ) , array ( 4 , 4 , 4 , 4 ) ) ; $ N = 4 ; add ( $ A , $ B , $ C ) ; echo " Result ▁ matrix ▁ is ▁ STRNEWLINE " ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { for ( $ j = 0 ; $ j < $ N ; $ j ++ ) { echo $ C [ $ i ] [ $ j ] ; echo " ▁ " ; } echo " STRNEWLINE " ; } ? >
Program for subtraction of matrices | This function subtracts B [ ] [ ] from A [ ] [ ] , and stores the result in C [ ] [ ] ; Driver code
< ? php function subtract ( & $ A , & $ B , & $ C ) { $ N = 4 ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) for ( $ j = 0 ; $ j < $ N ; $ j ++ ) $ C [ $ i ] [ $ j ] = $ A [ $ i ] [ $ j ] - $ B [ $ i ] [ $ j ] ; } $ N = 4 ; $ A = array ( array ( 1 , 1 , 1 , 1 ) , array ( 2 , 2 , 2 , 2 ) , array ( 3 , 3 , 3 , 3 ) , array ( 4 , 4 , 4 , 4 ) ) ; $ B = array ( array ( 1 , 1 , 1 , 1 ) , array ( 2 , 2 , 2 , 2 ) , array ( 3 , 3 , 3 , 3 ) , array ( 4 , 4 , 4 , 4 ) ) ; subtract ( $ A , $ B , $ C ) ; echo " Result ▁ matrix ▁ is ▁ STRNEWLINE " ; for ( $ i = 0 ; $ i < $ N ; $ i ++ ) { for ( $ j = 0 ; $ j < $ N ; $ j ++ ) { echo $ C [ $ i ] [ $ j ] ; echo " ▁ " ; } echo " STRNEWLINE " ; } ? >
Find a Fixed Point ( Value equal to index ) in a given array | PHP program to check fixed point in an array using linear search ; If no fixed point present then return - 1 ; Driver Code
< ? php function linearSearch ( $ arr , $ n ) { for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] == $ i ) return $ i ; } return -1 ; } $ arr = array ( -10 , -1 , 0 , 3 , 10 , 11 , 30 , 50 , 100 ) ; $ n = count ( $ arr ) ; echo " Fixed ▁ Point ▁ is ▁ " . linearSearch ( $ arr , $ n ) ; ? >
Find a Fixed Point ( Value equal to index ) in a given array | PHP program to check fixed po in an array using binary search ; low + ( high - low ) / 2 ; ; Return - 1 if there is no Fixed Po ; Driver Code
< ? php function binarySearch ( $ arr , $ low , $ high ) { if ( $ high >= $ low ) { $ mid = ( int ) ( ( $ low + $ high ) / 2 ) ; if ( $ mid == $ arr [ $ mid ] ) return $ mid ; if ( $ mid > $ arr [ $ mid ] ) return binarySearch ( $ arr , ( $ mid + 1 ) , $ high ) ; else return binarySearch ( $ arr , $ low , ( $ mid - 1 ) ) ; } return -1 ; } $ arr = array ( -10 , -1 , 0 , 3 , 10 , 11 , 30 , 50 , 100 ) ; $ n = count ( $ arr ) ; echo " Fixed ▁ Point ▁ is : ▁ " . binarySearch ( $ arr , 0 , $ n - 1 ) ; ? >
Maximum triplet sum in array | PHP code to find maximum triplet sum ; Initialize sum with INT_MIN ; Driver Code
< ? php function maxTripletSum ( $ arr , $ n ) { $ sum = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) for ( $ j = $ i + 1 ; $ j < $ n ; $ j ++ ) for ( $ k = $ j + 1 ; $ k < $ n ; $ k ++ ) if ( $ sum < $ arr [ $ i ] + $ arr [ $ j ] + $ arr [ $ k ] ) $ sum = $ arr [ $ i ] + $ arr [ $ j ] + $ arr [ $ k ] ; return $ sum ; } $ arr = array ( 1 , 0 , 8 , 6 , 4 , 2 ) ; $ n = count ( $ arr ) ; echo maxTripletSum ( $ arr , $ n ) ; ? >
Maximum triplet sum in array | This function assumes that there are at least three elements in arr [ ] . ; sort the given array ; After sorting the array . Add last three element of the given array ; Driver code
< ? php function maxTripletSum ( $ arr , $ n ) { sort ( $ arr ) ; return $ arr [ $ n - 1 ] + $ arr [ $ n - 2 ] + $ arr [ $ n - 3 ] ; } $ arr = array ( 1 , 0 , 8 , 6 , 4 , 2 ) ; $ n = count ( $ arr ) ; echo maxTripletSum ( $ arr , $ n ) ; ? >
Maximum triplet sum in array | This function assumes that there are at least three elements in arr [ ] . ; Initialize Maximum , second maximum and third maximum element ; Update Maximum , second maximum and third maximum element ; Update second maximum and third maximum element ; Update third maximum element ; Driven code
< ? php function maxTripletSum ( $ arr , $ n ) { $ maxA = PHP_INT_MIN ; $ maxB = PHP_INT_MIN ; $ maxC = PHP_INT_MIN ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] > $ maxA ) { $ maxC = $ maxB ; $ maxB = $ maxA ; $ maxA = $ arr [ $ i ] ; } else if ( $ arr [ $ i ] > $ maxB ) { $ maxC = $ maxB ; $ maxB = $ arr [ $ i ] ; } else if ( $ arr [ $ i ] > $ maxC ) $ maxC = $ arr [ $ i ] ; } return ( $ maxA + $ maxB + $ maxC ) ; } $ arr = array ( 1 , 0 , 8 , 6 , 4 , 2 ) ; $ n = count ( $ arr ) ; echo maxTripletSum ( $ arr , $ n ) ; ? >
Linear Search | PHP code for linearly search x in arr [ ] . If x is present then return its location , otherwise return - 1 ; Driver Code ; Function call
< ? php function search ( $ arr , $ x ) { $ n = sizeof ( $ arr ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] == $ x ) return $ i ; } return -1 ; } $ arr = array ( 2 , 3 , 4 , 10 , 40 ) ; $ x = 10 ; $ result = search ( $ arr , $ x ) ; if ( $ result == -1 ) echo " Element ▁ is ▁ not ▁ present ▁ in ▁ array " ; else echo " Element ▁ is ▁ present ▁ at ▁ index ▁ " , $ result ; ? >
Counting Sort | PHP Program for counting sort ; The main function that sort the given string arr [ ] in alphabatical order ; The output character array that will have sorted arr ; Create a count array to store count of inidividul characters and initialize count array as 0 ; Store count of each character ; Change count [ i ] so that count [ i ] now contains actual position of this character in output array ; Build the output character array To make it stable we are operating in reverse order . ; Copy the output array to arr , so that arr now contains sorted characters ; Driver Code
< ? php $ RANGE = 255 ; function countSort ( $ arr ) { global $ RANGE ; $ output = array ( strlen ( $ arr ) ) ; $ len = strlen ( $ arr ) ; $ count = array_fill ( 0 , $ RANGE + 1 , 0 ) ; for ( $ i = 0 ; $ i < $ len ; ++ $ i ) ++ $ count [ ord ( $ arr [ $ i ] ) ] ; for ( $ i = 1 ; $ i <= $ RANGE ; ++ $ i ) $ count [ $ i ] += $ count [ $ i - 1 ] ; for ( $ i = $ len - 1 ; $ i >= 0 ; $ i -- ) { $ output [ $ count [ ord ( $ arr [ $ i ] ) ] - 1 ] = $ arr [ $ i ] ; -- $ count [ ord ( $ arr [ $ i ] ) ] ; } for ( $ i = 0 ; $ i < $ len ; ++ $ i ) $ arr [ $ i ] = $ output [ $ i ] ; return $ arr ; } $ arr = " geeksforgeeks " ; $ arr = countSort ( $ arr ) ; echo " Sorted ▁ character ▁ array ▁ is ▁ " . $ arr ; ? >
Binomial Coefficient | DP | Returns value of Binomial Coefficient C ( n , k ) ; Base Cases ; Recur ; Driver Code
< ? php function binomialCoeff ( $ n , $ k ) { if ( $ k > $ n ) return 0 ; if ( $ k == 0 $ k == $ n ) return 1 ; return binomialCoeff ( $ n - 1 , $ k - 1 ) + binomialCoeff ( $ n - 1 , $ k ) ; } $ n = 5 ; $ k = 2 ; echo " Value ▁ of ▁ C " , " ( " , $ n , $ k , " ) ▁ is ▁ " , binomialCoeff ( $ n , $ k ) ; ? >
Binomial Coefficient | DP | PHP program for space optimized Dynamic Programming Solution of Binomial Coefficient ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Driver Code
< ? php function binomialCoeff ( $ n , $ k ) { $ C = array_fill ( 0 , $ k + 1 , 0 ) ; $ C [ 0 ] = 1 ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = min ( $ i , $ k ) ; $ j > 0 ; $ j -- ) $ C [ $ j ] = $ C [ $ j ] + $ C [ $ j - 1 ] ; } return $ C [ $ k ] ; } $ n = 5 ; $ k = 2 ; echo " Value ▁ of ▁ C [ $ n , ▁ $ k ] ▁ is ▁ " . binomialCoeff ( $ n , $ k ) ; ? >
Dynamic Programming | Returns true if there is a subset of set [ ] with sun equal to given sum ; The value of subset [ i ] [ j ] will be true if there is a subset of set [ 0. . j - 1 ] with sum equal to i ; If sum is 0 , then answer is true ; If sum is not 0 and set is empty , then answer is false ; Fill the subset table in botton up manner ; print table ; Driver code
< ? php function isSubsetSum ( $ set , $ n , $ sum ) { $ subset = array ( array ( ) ) ; for ( $ i = 0 ; $ i <= $ n ; $ i ++ ) $ subset [ $ i ] [ 0 ] = true ; for ( $ i = 1 ; $ i <= $ sum ; $ i ++ ) $ subset [ 0 ] [ $ i ] = false ; for ( $ i = 1 ; $ i <= $ n ; $ i ++ ) { for ( $ j = 1 ; $ j <= $ sum ; $ j ++ ) { if ( $ j < $ set [ $ i - 1 ] ) $ subset [ $ i ] [ $ j ] = $ subset [ $ i - 1 ] [ $ j ] ; if ( $ j >= $ set [ $ i - 1 ] ) $ subset [ $ i ] [ $ j ] = $ subset [ $ i - 1 ] [ $ j ] || $ subset [ $ i - 1 ] [ $ j - $ set [ $ i - 1 ] ] ; } } for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= sum ; j ++ ) printf ( " % 4d " , subset [ i ] [ j ] ) ; printf ( " n " ) ; } return $ subset [ $ n ] [ $ sum ] ; } $ set = array ( 3 , 34 , 4 , 12 , 5 , 2 ) ; $ sum = 9 ; $ n = count ( $ set ) ; if ( isSubsetSum ( $ set , $ n , $ sum ) == true ) echo " Found ▁ a ▁ subset ▁ with ▁ given ▁ sum " ; else echo " No ▁ subset ▁ with ▁ given ▁ sum " ; ? >
How to print maximum number of A 's using given four keys | A recursive function that returns the optimal length string for N keystrokes ; The optimal string length is N when N is smaller than 7 ; Initialize result ; TRY ALL POSSIBLE BREAK - POINTS For any keystroke N , we need to loop from N - 3 keystrokes back to 1 keystroke to find a breakpoint ' b ' after which we will have Ctrl - A , Ctrl - C and then only Ctrl - V all the way . ; If the breakpoint is s at b 'th keystroke then  the optimal string would have length  (n-b-1)*screen[b-1]; ; Driver code ; for the rest of the array we will rely on the previous entries to compute new ones
< ? php function findoptimal ( $ N ) { if ( $ N <= 6 ) return $ N ; $ max = 0 ; $ b ; for ( $ b = $ N - 3 ; $ b >= 1 ; $ b -= 1 ) { $ curr = ( $ N - $ b - 1 ) * findoptimal ( $ b ) ; if ( $ curr > $ max ) $ max = $ curr ; } return $ max ; } $ N ; for ( $ N = 1 ; $ N <= 20 ; $ N += 1 ) echo ( " Maximum ▁ Number ▁ of ▁ A ' s ▁ with " . $ N . " keystrokes ▁ is ▁ " . findoptimal ( $ N ) . " STRNEWLINE " ) ; ? >
Write a program to calculate pow ( x , n ) | Function to calculate x raised to the power y ; Driver Code
< ? php function power ( $ x , $ y ) { if ( $ y == 0 ) return 1 ; else if ( $ y % 2 == 0 ) return power ( $ x , ( int ) $ y / 2 ) * power ( $ x , ( int ) $ y / 2 ) ; else return $ x * power ( $ x , ( int ) $ y / 2 ) * power ( $ x , ( int ) $ y / 2 ) ; } $ x = 2 ; $ y = 3 ; echo power ( $ x , $ y ) ; ? >
Write a program to calculate pow ( x , n ) | Extended version of power function that can work for float x and negative y ; Driver Code
< ? php function power ( $ x , $ y ) { $ temp ; if ( $ y == 0 ) return 1 ; $ temp = power ( $ x , $ y / 2 ) ; if ( $ y % 2 == 0 ) return $ temp * $ temp ; else { if ( $ y > 0 ) return $ x * $ temp * $ temp ; else return ( $ temp * $ temp ) / $ x ; } } $ x = 2 ; $ y = -3 ; echo power ( $ x , $ y ) ; ? >
Babylonian method for square root | Returns the square root of n . Note that the function ; We are using n itself as initial approximation This can definitely be improved ; e decides the accuracy level ; Driver Code
< ? php function squareRoot ( $ n ) { $ x = $ n ; $ y = 1 ; $ e = 0.000001 ; while ( $ x - $ y > $ e ) { $ x = ( $ x + $ y ) / 2 ; $ y = $ n / $ x ; } return $ x ; } { $ n = 50 ; echo " Square ▁ root ▁ of ▁ $ n ▁ is ▁ " , squareRoot ( $ n ) ; } ? >