빈 목록 시나리오 처리
Coddy PHP 여정의 기초 섹션에 포함된 레슨 — 71개 중 60번째.
챌린지
쉬움이전 수업에서 완료 상태에 따라 작업을 필터링했습니다. 이제 필터링 결과가 비어 있는 시나리오에 대한 처리를 추가하세요.
작업 설명을 위한 네 줄의 입력을 읽으세요 (초기 세 작업과 하나의 새 작업). 그 다음 다섯 번째 입력을 읽으세요: 완료로 표시할 작업의 인덱스를 나타내는 정수. 여섯 번째 입력을 읽으세요: 제거할 작업의 인덱스를 나타내는 정수. 마지막으로, 작업을 필터링하기 위해 completed 또는 pending인 문자열인 일곱 번째 입력을 읽으세요.
$todos 배열을 만든 후, 지정된 작업을 완료로 표시하고, 주어진 인덱스의 작업을 제거한 다음, 필터 입력에 따라 남은 작업을 필터링하세요.
필터링된 작업을 표시하기 전에, 필터와 일치하는 작업이 있는지 확인하세요:
- 필터와 일치하는 작업이 없으면, 인쇄하세요:
No [filter] tasks found.([filter]는completed또는pending입니다) - 필터와 일치하는 작업이 있으면, 이전과 같은 형식으로 표시하세요
일치하는 작업의 출력 형식:
- 작업이 완료된 경우:
- [task description] (Done) - 작업이 대기 중인 경우:
- [task description] (Pending)
예제 1:
입력값이 Buy groceries, Walk the dog, Finish homework, Call mom, 1, 1, 및 completed인 경우, 출력은 다음과 같아야 합니다:
No completed tasks found.예제 2:
입력값이 Buy groceries, Walk the dog, Finish homework, Call mom, 0, 2, 및 completed인 경우, 출력은 다음과 같아야 합니다:
- Buy groceries (Done)예제 3:
입력값이 Buy groceries, Walk the dog, Finish homework, Call mom, 0, 1, 및 pending인 경우, 출력은 다음과 같아야 합니다:
- Finish homework (Pending)
- Call mom (Pending)직접 해보기
<?php
// 입력 읽기
$input = trim(fgets(STDIN));
// 입력에 기반한 작업 컬렉션 초기화
if ($input == "empty") {
$tasks = [];
} else {
$tasks = [
[
"description" => "Buy groceries",
"completed" => false
],
[
"description" => "Walk the dog",
"completed" => true
]
];
}
// TODO: 여기에 코드를 작성하세요
// count() 함수를 사용하여 tasks 배열이 비어 있는지 확인
// 빈 작업 또는 채워진 작업에 대한 적절한 메시지 표시
// 요약 정보 계산 및 표시
?>기초의 모든 레슨
4Comparison & Logical Operators
Comparison OperatorsEquality & IdentityLogical Operators Part 1Logical Operators Part 2Recap - Simple Logic2Variables and Data Types
NumbersStrings and QuotesBooleansNaming ConventionsRecap - Variable InitEmpty VariablesString ConcatenationGetting User InputCast to Different Types3Basic Operators
Arithmetic OperatorsModulo OperatorExponentiation OperatorCombined AssignmentIncrement/DecrementOperator PrecedenceRecap - Simple CalculationsString Operators