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...
Writing method in Java Given a string, return the sum of the numbers appearing in the...
Writing method in Java Given a string, return the sum of the numbers appearing in the string, ignoring all other characters. A number is a series of 1 or more digit chars in a row. (Note: Character.isDigit(char) tests if a char is one of the chars '0', '1', .. '9'. Integer.parseInt(string) converts a string to an int.) sumNumbers("abc123xyz") → 123 sumNumbers("aa11b33") → 44 sumNumbers("7 11") → 18
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...
Write a PHP program that checks the elements of a string array named $Passwords. Use regular...
Write a PHP program that checks the elements of a string array named $Passwords. Use regular expressions to test whether each element is a strong password. For this exercise, a strong password must have at least one number, one lowercase letter, one uppercase letter, no spaces, and at least one character that is not a letter or number. (Hint: Use the [^0-9A-Za-z] character class.) The string should also be between 8 and 16 characters long. The $Passwords array should contain...
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)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT