Question

php please numberOfPairs // Return the number of times a pair occurs in array. A pair...

php please

numberOfPairs

// Return the number of times a pair occurs in array. A pair is any two String values that are equal (case

// sensitive) in consecutive array elements. The array may be empty or have only one element. In both of

// these cases, return 0.

//

// numberOfPairs( array('a', 'b', 'c') ) returns 0

// numberOfPairs( array('a', 'a', 'a') ) returns 2

// assert(2 == numberOfPairs( array('a', 'a', 'b', 'b' ) ) );

// numberOfPairs( array ( ) ) returns 0

// numberOfPairs( array ('a') ) returns 0

// Precondition: $arr has all the same type of elements

function numberOfPairs($arr) {

}

echo PHP_EOL . 'numberOfPairs( array(a, a, a) ) 2: ' . numberOfPairs ( array (

'a',

'a',

'a' ) ) . "\n";

echo ' numberOfPairs( array() ) 0: ' . numberOfPairs ( array () ) . "\n";

Homework Answers

Answer #1

PHP CODE:

<?php
// Your code here!
function numberOfPairs($arr)
{
$arrlength = count($arr); #arrlength to find the size of array
$k= 0;
if ($arrlength !=0) #condition to check if array is non empty or not
{
if($arrlength==1) #print 0 if array is having only one element
{echo"0";}
else
{
for ($x = 0; $x < $arrlength; $x++)
{
if ($arr[$x] == $arr[$x+1]){ $k=$k+1;} #if some pair come then k value increase
}
if($k>0)
{echo $k;} #print the number of pair matches
else
{echo"0";}
}
}
else
{
echo"0";
}
}
echo PHP_EOL . 'numberOfPairs( array(a, a, a) ) 2: ' . numberOfPairs ( array (

'a',

'a',

'a' ) ) . "\n";

echo ' numberOfPairs( array() ) 0: ' . numberOfPairs ( array () ) . "\n";

#numberOfPairs(array('a','b','c')); //if run this will get 0

#numberOfPairs(array('a','a','a')); //after running will get 2


?>

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
php please shiftNTimes // Modify array so it is "left shifted" n times -- so shiftNTimes(array(6,...
php please shiftNTimes // Modify array so it is "left shifted" n times -- so shiftNTimes(array(6, 2, 5, 3), 1) // changes the array argument to (2, 5, 3, 6) and shiftNTimes(array(6, 2, 5, 3), 2) // changes the array argument to (5, 3, 6, 2). You must modify the array argument by // changing the parameter array inside method shiftNTimes. A change to the // parameter inside the method shiftNTimes changes the argument if the // argument is passed...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private data member for the triplet is a generic array with three elements. The triplet ADT has the following functions:  default constructor  explicit constructor: initialize the data member using parameters  three accessors (three get functions) which will return the value of each individual element of the array data member  one mutator (set function) which will assign values to the data member...
Write recursive method to return true if a given array of integers, named numbers, with n...
Write recursive method to return true if a given array of integers, named numbers, with n occupied positions is sorted in ascending (increasing) order, or returns false otherwise. Array can be empty or not. //PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary. // An empty array and an array with single element in it, are sorted. Method isSortedRec must be recursive and returns...
IN JAVA: Write recursive method to return true if a given array has element equal to...
IN JAVA: Write recursive method to return true if a given array has element equal to employee emp, or returns false otherwise. Array can be empty or not. //PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary, // and it has method boolean equals(Employee emp) that accept an employee object and returns true if employee calling the equals method is equal as employee emp,...
USE PYTHON LANGUAGE PLEASE FOCUS YOU SHOULD ENTER AN ARRAY AND THEN THE PROGRAM GIVE OUTPUT(...
USE PYTHON LANGUAGE PLEASE FOCUS YOU SHOULD ENTER AN ARRAY AND THEN THE PROGRAM GIVE OUTPUT( TRUE/ FALSE) QUIZ 8 Array Challenge Have the function ArrayChallenge(arr) take the array of numbers stored in arr and return the string true if any two numbers can be multiplied so that the answer is greater than double the sum of all the elements in the array. If not, return the string false. For example: if arr is [2, 5, 6, -6, 16, 2,...
How can this PHP code be changed to use a parameter to the function that you...
How can this PHP code be changed to use a parameter to the function that you would then pass $students in? (instead of using a global). Also, can you show how to use a loop to handle any length of array? (instead of an array that is 3 elements). <?php $students = array(     array('FName'=>'Jane', 'LName'=>'Doe', 'ClassScore'=>90),     array('FName'=>'Bob', 'LName'=>'Joe', 'ClassScore'=>50),     array('FName'=>'John', 'LName'=>'Doe', 'ClassScore'=>20)     );     //1. CalculateSum() Function     function CalculateSum()     {         // Use $students array throughout the function.         global $students;         // Add...
C++ only: Please implement a function that accepts a string parameter as well as two character...
C++ only: Please implement a function that accepts a string parameter as well as two character arguments and a boolean parameter. Your function should return the number of times it finds the character arguments inside the string parameter. When both of the passed character arguments are found in the string parameter, set the boolean argument to true. Otherwise, set that boolean argument to false. Return -1 if the string argument is the empty string. The declaration for this function will...
Finish the code wherever it says TODO /**    * Node type for this list. Each...
Finish the code wherever it says TODO /**    * Node type for this list. Each node holds a maximum of nodeSize elements in an    * array. Empty slots are null.    */    private class Node {        /**        * Array of actual data elements.        */        // Unchecked warning unavoidable.        public E[] data = (E[]) new Comparable[nodeSize];        /**        * Link to next node.       ...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an executable with gcc -Wall -DUNIT_TESTS=1 array-utils5A.c The definitions for is_reverse_sorted and all_different are both defective. Rewrite the definitions so that they are correct. The definition for is_alternating is missing. Write a correct definition for that function, and add unit tests for it, using the unit tests for is_reverse_sorted and all_different as models. Please explain the logic errors present in in the definition of is_reverse_sorted...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT