Question

In: Computer Science

php please // 2) mirrorEnds // Complete method mirrorEnds that given a string, looks for a...

php please

// 2) mirrorEnds

// Complete method mirrorEnds that given a string, looks for a mirror

// image (backwards) string at both the beginning and end of the given string.

// In other words, zero or more characters at the very beginning of the given

// string, and at the very end of the string in reverse order (possibly overlapping).

// For example, "abXYZba" has the mirror end "ab".

//

// assert("" == mirrorEnds(""));

// assert("" == mirrorEnds("abcde"));

// assert("a" == mirrorEnds("abca"));

// assert("aba" == mirrorEnds("aba"));

//

function mirrorEnds($str) {

}

echo PHP_EOL . "mirrorEnds(abcdefcba) abc: " . mirrorEnds ( 'abcdefcba' ) . "\n";

assert ( 'abc' == mirrorEnds ( 'abcdefcba' ) );

echo " mirrorEnds(zzRSTzz) zz: " . mirrorEnds ( 'zzRSTzz' ) . "\n";

Solutions

Expert Solution

The code is given below followed by the output snapshot for some test cases. The basic logic used is as follows: A copy of the reversed string is made, and then compared with the original character by character - as soon as a mismatch is found, the loop is broken.

function mirrorEnds($str) {
$rev = strrev($str);
$mir_end = "";

for($i = 0; $i < strlen($str); ++$i)
{
    if($rev[$i] != $str[$i])
      break;
    $mir_end = $mir_end . $str[$i];
}
return $mir_end;
}

echo PHP_EOL . "mirrorEnds(abcdefcba) abc: " . mirrorEnds ( 'abcdefcba' ) . "\n";
echo " mirrorEnds(zzRSTzz) zz: " . mirrorEnds ( 'zzRSTzz' ) . "\n";
echo " mirrorEnds() : " . mirrorEnds ('') . "\n";
echo " mirrorEnds(abcde) : " . mirrorEnds('abcde') . "\n";
echo " mirrorEnds('abca') a: " . mirrorEnds('abca') . "\n";
echo " mirrorEnds('aba') aba: " . mirrorEnds('aba') . "\n";


Related Solutions

php please // 5) isArraySorted // Given an array , return true if the element are...
php please // 5) isArraySorted // Given an array , return true if the element are in ascending order. // Note: 'abe' < 'ben' and 5 > 3 // Precondition $arr has all the same type of elements function isArraySorted($arr) {    } echo PHP_EOL . 'isArraySorted(array(1, 2, 2, 99) 1: ' . isArraySorted ( array ( 1, 2, 2, 99 ) ) . "\n"; assert ( isArraySorted ( array ( 1, 2, 2, 99 ) ) ); echo 'isArraySorted(array(2,...
Given a string, write a method called removeRepthat returns another string where adjacent characters that are...
Given a string, write a method called removeRepthat returns another string where adjacent characters that are the same have been reduced to a single character. Test the program by calling the method from the main method. For example: removeRep(“yyzzza”) à “yza” removeRep(“aabbbccd”) à “abcd” removeRep(“122333”) à “123”
1. PHP OOP Create a complete PHP enabled website that defines and uses a PineApple class...
1. PHP OOP Create a complete PHP enabled website that defines and uses a PineApple class in PHP. The class has following members. Properties $color: string type $taste: string type $weight: number type Standard constructor, with 3 parameters Member functions eat(): no return, no parameter, print a short paragraph in English to describe how to eat a pineapple. grow(): no return, no parameter, print a short paragraph in English to describe how to grow a pineapple plant. display(): no return,...
Please implement Sample string toString()method for each class and return itself a string, not the output....
Please implement Sample string toString()method for each class and return itself a string, not the output. import java.util.ArrayList; public class Customer extends User{ private ArrayList orders; public Customer(String display_name, String password, String email) { super(display_name, password, email); } @Override public String getPermissionLevel() { return "CUSTOMER"; } public void addOrder(Order order){ this.orders.add(order); } public ArrayList listOrders(){ return this.orders; }; } ---------------- public class ElectronicProduct extends Product{ private long SNo; private String warranty_period; public ElectronicProduct(long SNo, String warranty_period, String productId, String productName,...
Complete the method sortSuits(). This method takes one argument:a deck of cards (Stack<String> deck).It should return...
Complete the method sortSuits(). This method takes one argument:a deck of cards (Stack<String> deck).It should return an ArrayList of Stacks, each stack representing all the cards of one suit(i.e., if you have four suits -Spades,Hearts,Clubs,Diamonds, it should return ArrayLists of Spades,Hearts,Clubs,and Diamonds). The returned ArrayList should be in the same order as the SUITSarray. Use a loop to look at each card, determine its suit (methods such as peek()/pop()and String.contains()may be helpful), and move the card from the deck to...
PLEASE USE MEHODES (Print part of the string) Write a method with the following header that...
PLEASE USE MEHODES (Print part of the string) Write a method with the following header that returns a partial string: public static String getPartOfString(int n, String firstOrLast, String inWord) Write a test program that uses this method to display the first or last number of characters of a string provided by the user. The program should output an error if the number requested is larger than the string. SAMPLE RUN #1: java PartOfString Enter a string: abracadabra↵ Enter the number...
In JAVA Implement the moveMinToFront method in IntSinglyLinkedList. The moveMinToFront method looks through the list to...
In JAVA Implement the moveMinToFront method in IntSinglyLinkedList. The moveMinToFront method looks through the list to find the element with the minimum value. It moves that element to the front of the list. Before abstract view: [7, 3, 2] After Abstract view: [2,7,3] Before Abstract view: [4,1,7] After Abstract view: [1,4,7] public void moveMinToFront() { } Test: package net.datastructures; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.Random; public class IntSinglyLinkedListTest { @Test public void moveMinToFrontTestEmpty() { IntSinglyLinkedList s...
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int...
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int length, int width)
PHP    1. Create a new PHP document (nit Objective 1)    2. Write a comment...
PHP    1. Create a new PHP document (nit Objective 1)    2. Write a comment similar to the following: "This is my first PHP document, which displays some data in the web browser"(Unit Objective 1)    3. Assign your name as a string value into the variable labeled myName (Unit Objective 2)    4. Assign 53870 numeric value into the variable named randomNumber (Unit Objective 2) 5. ssign the name of the web browser and operating system of the...
In java please Question: You are given a string s. Your task is to count the...
In java please Question: You are given a string s. Your task is to count the number of ways of splitting s into three non-empty parts a, b and c (s = a + b + c) in such a way that a + b, b + c and c + a are all different strings. For s = "xzxzx", the output should be countWaysToSplit(s) = 5. Consider all the ways to split s into three non-empty parts:
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT