[Solved] Easter Shopping - PHP Exam Task

  

3
Topic starter

You have decided to go on an Easter shopping spree to take advantage of the promotions.

Create a program that helps you keep track of the shops that you want to visit. You will receive the list of shops you have planned on checking out on a single line, separated by a single space in the following format:

"{shop1} {shop2} {shop3}… {shopn}"

Then you will receive a number – n - a count of commands you need to execute over your list.

There are four possible commands:

"Include {shop}":

  • Add the shop at the end of your list.

"Visit {first/last} {numberOfShops}":

  • Remove either the "first" or the "last" number of shops from your list, depending on the input. If you have less shops on your list than the given number, skip this command.

"Prefer {shopIndex1} {shopIndex2}":

  • If both of the shop indexes exist in your list, take the shops that are on them and change their places.

"Place {shop} {shopIndex}":

  • Insert the shop after the given index, only if the resulted index exists.

In the end print the manipulated list in the following format:

"Shops left:
{shop1} {shop2}… {shopn}"

Input / Constraints:

  • On the 1st line, you will receive the starting list with the names of the shops separated by a single space.
  • On the 2nd line, you will receive the number of commands - n – an integer in range [1…100]
  • On the next n lines you will be receiving commands in the format described above.

Output:

  • Print the list after the manipulations in the format described above.

Examples:

Easter shopping php exam task

 

Comments:

  • First we receive the "Include" and the name of the store and we add the store to our list. The list should look like this: Bershka CandyStore ThriftShop Armani Groceries ToyStore PeakStore HM

     

  • After, we receive the "Visit" command and "first", which means we have to visit the first 2 stores, so we remove them from our list and the collection should look like this: ThriftShop Armani Groceries ToyStore PeakStore HM.
  • After that, we receive the "Visit" command again, but this time we need to visit the "last" 1 store, so we remove it and the collection should look like this: ThriftShop Armani Groceries ToyStore PeakStore

     

  • After that we receive the "Prefer" command, which means we need to find the shop on the first given index – 3 and change it with the one that is on index – 1, and the collection should look like this: ThriftShop ToyStore Groceries Armani PeakStore
1 Answer
1

Here is my solution:

<?php
 
$shops = explode(" ", readline());
$n = intval(readline());
 
for ($i = 0; $i < $n; $i++) {
    $args = explode(" ", readline());
 
    $command = $args[0];
 
    switch ($command) {
        case 'Include':
            $shops[] = $args[1];
            break;
        case 'Visit':
            $count = intval($args[2]);
            if ($args[1] == "first") {
                if ($args[2] > 0 && $args[2] <= count($shops)) {
                    array_splice($shops, 0, $count);//remove(replace) with nothing from the beginning
                }
            } else {
                if ($args[2] > 0 && $args[2] <= count($shops)) {
                    $start = count($shops) - $count;
                    array_splice($shops, $start, $count);//replace from $start
                }
            }
            break;
        case 'Prefer':
            $index1 = $args[1];
            $index2 = $args[2];
            if ($index1 >= 0 && $index1 < count($shops) && $index2 >= 0 && $index2 < count($shops)) {
                $temp = $shops[$index1];
                $shops[$index1] = $shops[$index2];
                $shops[$index2] = $temp;
                break;
            }
        case 'Place':
            if ($args[2] < count($shops) && $args[2] >= 0) {
                array_splice($shops, $args[2] + 1, 0, $args[1]);//add(replace) $args[1] at position $args[2]
            }
    }
}
 
echo "Shops left:" . PHP_EOL;
echo implode(" ", $shops);
Share: