Menu
Coddy logo textTech

Gestire i casi di lista vuota

Fa parte della sezione Fondamenti del percorso PHP di Coddy. Lezione 60 di 71.

challenge icon

Sfida

Facile

Nella lezione precedente, hai filtrato le attività in base al loro stato di completamento. Ora aggiungi la gestione dei casi in cui il risultato filtrato è vuoto.

Leggi quattro righe di input con le descrizioni delle attività (tre attività iniziali più una nuova attività). Poi leggi un quinto input: un intero che rappresenta l’indice dell’attività da contrassegnare come completata. Leggi un sesto input: un intero che rappresenta l’indice dell’attività da rimuovere. Infine, leggi un settimo input: una stringa che può essere completed oppure pending, da usare per filtrare le attività.

Dopo aver creato l’array $todos, contrassegnato come completata l’attività specificata e rimosso l’attività all’indice indicato, filtra le attività rimanenti in base all’input del filtro.

Prima di visualizzare le attività filtrate, controlla se ce ne sono che corrispondono al filtro:

  • Se nessuna attività corrisponde al filtro, stampa: No [filter] tasks found. (dove [filter] può essere completed oppure pending)
  • Se ci sono attività che corrispondono al filtro, visualizzale usando lo stesso formato di prima

Formato dell’output per le attività corrispondenti:

  • Se l’attività è completata: - [task description] (Done)
  • Se l’attività è in sospeso: - [task description] (Pending)

Esempio 1:

Se gli input sono Buy groceries, Walk the dog, Finish homework, Call mom, 1, 1 e completed, l’output dovrebbe essere:

No completed tasks found.

Esempio 2:

Se gli input sono Buy groceries, Walk the dog, Finish homework, Call mom, 0, 2 e completed, l’output dovrebbe essere:

- Buy groceries (Done)

Esempio 3:

Se gli input sono Buy groceries, Walk the dog, Finish homework, Call mom, 0, 1 e pending, l’output dovrebbe essere:

- Finish homework (Pending)
- Call mom (Pending)

Provalo tu

<?php
// Read three task descriptions
$task1 = trim(fgets(STDIN));
$task2 = trim(fgets(STDIN));
$task3 = trim(fgets(STDIN));

// Read the new task to add
$newTask = trim(fgets(STDIN));

// Read the index of the task to mark as complete
$completeIndex = (int)trim(fgets(STDIN));

// Read the index of the task to remove
$removeIndex = (int)trim(fgets(STDIN));

// Read the filter type (completed or pending)
$filter = trim(fgets(STDIN));

// Create a $todos array containing three task arrays
// Each task should be an associative array with "task" and "completed" keys
$todos = [
    ["task" => $task1, "completed" => false],
    ["task" => $task2, "completed" => false],
    ["task" => $task3, "completed" => false]
];

// Add the new task to the end of the array
$todos[] = ["task" => $newTask, "completed" => false];

// Mark the task at the specified index as complete
$todos[$completeIndex]["completed"] = true;

// Remove the task at the specified removal index
array_splice($todos, $removeIndex, 1);

// Loop through the array and print each task that matches the filter
foreach ($todos as $todo) {
    if ($filter === "completed" && $todo["completed"]) {
        echo "- " . $todo["task"] . " (Done)\n";
    } elseif ($filter === "pending" && !$todo["completed"]) {
        echo "- " . $todo["task"] . " (Pending)\n";
    }
}
?>

Tutte le lezioni di Fondamenti

Esercitati da solo: Compilatore PHP online