Loading...
gina78 avatar gina78 4 Точки

03. House Party

Здравейте, как се решава тая задача?някой може ли да помогни,

 

1.House Party

Write a program that keeps track of a guests that are going to a house party. On the first input line you are going to receive how many commands you are going to have. On the next lines you are going to receive some of the following inputs:

"{name} is going!"

"{name} is not going!"

If you receive the first type of input, you have to add the person if he/she is not in the list (If he/she is in the list print on the console: "{name} is already in the list!"). If you receive the second type of input, you have to remove the person if he/she is in the list (if not print: "{name} is not in the list!"). At the end print each guest on a new line.

Examples

Input

Output

4

Allie is going!

George is going!

John is not going!

George is not going!

 

John is not in the list!

Allie

5

Tom is going!

Annie is going!

Tom is going!

Garry is going!

Jerry is going!

Tom is already in the list!

Tom

Annie

Garry

Jerry

Тагове:
1
PHP Fundamentals 22/06/2019 12:44:07
willystyle avatar willystyle 2472 Точки
<?php
$count = intval(readline());
$list = [];
for ($i=0; $i < $count; $i++) {
    $command = readline();
    $name = explode(' ', $command)[0];
    if (strpos($command, 'is going!') !== false) {
        if (in_array($name, $list)) {
            echo "{$name} is already in the list!" . PHP_EOL;
        } else {
            $list[] = $name;
        }
    } else if (strpos($command, 'is not going!') !== false) {
        if (!in_array($name, $list)) {
            echo "{$name} is not in the list!" . PHP_EOL;
        } else {
            $index = array_search($name, $list);
            unset($list[$index]);
        }
    }
}
echo implode(PHP_EOL, $list);

 

1
gina78 avatar gina78 4 Точки

благодаря

0
gina78 avatar gina78 4 Точки

04.Array Advanced Operations

Shift right 1

Shift left 2

в тая задача всичко но това не мога да направя незнам как, можеш ли да ми помогнеш?благодаря

 

0
willystyle avatar willystyle 2472 Точки

Сигурно мога, но дай кода до където си стигнала, не ми се пише цялата задача от нула.

1
gina78 avatar gina78 4 Точки

<?php

$arr = explode(" ", readline());
while (true) {
    $input = explode(" ", readline());
    $comand = $input[0]; 
    switch ($comand) {
        case "Add":
            $arr [] = $element;
            break;
        case "Insert":
            //Insert {number} {index} - insert number at given index
            $indexArr = $input[2];
            $number = $input[1];
            if ($indexArr >= 0 && $indexArr <= count($arr)) {
                array_splice($arr, $indexArr, 0, $number); 
            }
            break;
        case "Remove":
            $indexArr = $input[1];
            if (in_array($indexArr, $arr)) {
                array_splice($arr, $indexArr, 1);
            } else {
                echo "Invalid index" . PHP_EOL;
            }
            break;
        case "Shift":
            if ($input[1] === "left") {
                $shiftIndex = $input[2];
                if (!in_array($shiftIndex, $arr)) {
                    //array_splice($arr, $index, 0,$index);
                }
            }
            break;
        default :
        // 
    }


    if ($comand == "End") {
        break;
    }
}
//print_r($arr);
echo implode(" ",$arr);

0
willystyle avatar willystyle 2472 Точки

рефактор, ако нещо не е ясно питай:

<?php
$arr = explode(" ", readline());
while (true) {
    $input = explode(" ", readline());
    $comand = $input[0];
    if ($comand == "End") {
        break;
    }
    switch ($comand) {
        case "Add":
            $element = $input[1];
            $arr[] = $element;
            break;
        case "Insert":
            //Insert {number} {index} - insert number at given index
            $indexArr = $input[2];
            $number = $input[1];
            if ($indexArr >= 0 && $indexArr < count($arr)) {
                array_splice($arr, $indexArr, 0, $number);
            } else {
                echo "Invalid index" . PHP_EOL;
            }
            break;
        case "Remove":
            $indexArr = $input[1];
            if ($indexArr >= 0 && $indexArr < count($arr)) {
                array_splice($arr, $indexArr, 1);
            } else {
                echo "Invalid index" . PHP_EOL;
            }
            break;
        case "Shift":
            $count = $input[2];
            if ($input[1] === "left") {
                for ($i=0; $i < $count; $i++) {
                    $first = array_shift($arr);
                    $arr[] = $first;
                }
            } elseif ($input[1] === "right") {
                for ($i=0; $i < $count; $i++) {
                    $last = array_pop($arr);
                    array_splice($arr, 0, 0, $last);
                }
            }
            break;
        default:
            //
    }
}
//print_r($arr);
echo implode(" ", $arr);

 

1
23/06/2019 18:01:25
gina78 avatar gina78 4 Точки

Благодаря 

0
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.