回顾:原子批次
Coddy 面向 Kotlin 开发者的 Dart 课程的第 26 课,共 48 课。
挑战
简单实现 appendBatch(List<int> target,String batch):解析以逗号分隔的整数,范围为 -9 到 9,并去除各字段两端的空白。空批次或仅包含空白的批次会成功,且添加 0 个值。整数无效或超出范围时返回 -1,且不更改 target。有效批次会追加所有值,并返回数量。输入的第一行是以空白分隔的整数列表,第二行是批次。输出格式为 count|values,最终值以逗号分隔。十进制整数语法使用可选符号和 ASCII 数字。
不要在可编辑文件中打印内容。提供的 exercise.dart 会解析输入并返回输出;锁定的驱动程序会独立测试公共 API,并在结果后输出一个换行符。
你的代码应写在 functions.dart、exercise.dart 中。main.dart 包含提供的输入输出代码,不能编辑。
亲手试试
import 'dart:io';
import 'dart:convert';
import 'exercise.dart';
import 'functions.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 a=[8];check(appendBatch(a,'1,x')==-1 && a.join(',')=='8');check(appendBatch(a,'2')==1 && a.join(',')=='8,2');
stdout.writeln(solve(input));
}
面向 Kotlin 开发者的 Dart中的所有课
自己练习: Dart 在线编译器