복습: 카탈로그 쿼리
Coddy의 R 개발자를 위한 Dart 코스 레슨. 48개 중 42번째.
챌린지
쉬움이전 가격을 대체하는 put(String name,int price), int? price(String name), 그리고 가격이 최대 budget 이하인 이름들을 사전순으로 정렬하여 새로운 리스트로 반환하는 List<String> affordable(int budget) 메서드를 가진 Catalog()를 구현하세요. 입력은 budget에 이어 완전한 이름/가격 쌍으로 구성되며, 가격은 0 이상입니다. 쉼표로 구분된 구매 가능한 이름들을 반환하거나 none을 반환하세요.
편집 가능한 파일에서 출력을 수행하지 마세요. 제공된 exercise.dart는 입력을 파싱하고 결과를 반환하며, 잠긴 드라이버는 공개 API를 독립적으로 테스트하고 결과를 개행 하나와 함께 출력합니다.
코드 작성은 Catalog.dart, exercise.dart에 하세요. main.dart는 제공된 입출력 코드를 담고 있으며 수정할 수 없습니다.
직접 해보기
import 'dart:io';
import 'dart:convert';
import 'exercise.dart';
import 'Catalog.dart';
void check(bool condition){if(!condition)throw StateError('Public API contract failed');}
Future<void> main() async {
final raw=await stdin.transform(utf8.decoder).join();
final input=raw.replaceFirst(RegExp(r'\r?\n$'),'');
final c=Catalog();c.put('x',3);c.put('y',1);check(c.price('x')==3&&c.price('z')==null&&c.affordable(2).join(',')=='y');c.put('x',0);check(c.affordable(2).join(',')=='x,y');
stdout.writeln(solve(input));
}
R 개발자를 위한 Dart의 모든 레슨
직접 연습해 보세요: 온라인 Dart 컴파일러