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 a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
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,...
Write recursive method to return true if a given array has element equal to employee emp,...
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, and returns...
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.       ...