diff options
| author | Michael Manring <michael@manring> | 2022-08-11 15:47:54 +0700 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2022-08-13 00:52:43 +0700 |
| commit | 3c4e895bc5cb56f5803ae20e92e48ebb582dd79b (patch) | |
| tree | d00776607c48127f7384db940425adc1b73045c7 /challenge-019 | |
| parent | 89819967055c5f5d39f78d143f7880f6b602fe48 (diff) | |
| download | perlweeklychallenge-club-3c4e895bc5cb56f5803ae20e92e48ebb582dd79b.tar.gz perlweeklychallenge-club-3c4e895bc5cb56f5803ae20e92e48ebb582dd79b.tar.bz2 perlweeklychallenge-club-3c4e895bc5cb56f5803ae20e92e48ebb582dd79b.zip | |
pwc019 solution in dart
Diffstat (limited to 'challenge-019')
| -rw-r--r-- | challenge-019/pokgopun/dart/ch-1.dart | 19 | ||||
| -rw-r--r-- | challenge-019/pokgopun/dart/ch-2.dart | 37 |
2 files changed, 56 insertions, 0 deletions
diff --git a/challenge-019/pokgopun/dart/ch-1.dart b/challenge-019/pokgopun/dart/ch-1.dart new file mode 100644 index 0000000000..a270979141 --- /dev/null +++ b/challenge-019/pokgopun/dart/ch-1.dart @@ -0,0 +1,19 @@ +final monthsWith31Days = const { + DateTime.january: 'January', + DateTime.march: 'March', + DateTime.may: 'May', + DateTime.july: 'July', + DateTime.august: 'August', + DateTime.october: 'October', + DateTime.december: 'December', +}; +void main() { + int startY = 1900; + int endY = 2019; + for (var y = startY; y <= endY; y++) { + print("$y: ${monthsWith31Days.keys // use key which is month number to build DateTime + .where((m) => DateTime(y, m, 1).weekday == DateTime.friday) // filter in only when 1st of the month is friday + .map((m) => monthsWith31Days[m]) // map month number to month label + .join(", ")}"); + } +} diff --git a/challenge-019/pokgopun/dart/ch-2.dart b/challenge-019/pokgopun/dart/ch-2.dart new file mode 100644 index 0000000000..659d27d9bf --- /dev/null +++ b/challenge-019/pokgopun/dart/ch-2.dart @@ -0,0 +1,37 @@ +void main(List<String> args) { + final int lw = + args.isNotEmpty && int.tryParse(args[0]) != null ? int.parse(args[0]) : 0; + final input = + '''Write a script that can wrap the given paragraph at a specified column using the greedy algorithm.'''; + //Iterable<Match> matches = RegExp(r'(.+?(?:\s|\n|$))').allMatches(input); + final Iterable<Match> matches = RegExp(r'\S+').allMatches(input); + var buf = StringBuffer(); + var sl = lw; + for (final Match m in matches) { + String w = m[0]!; + if (w.length + 1 > sl) { + /* + if (buf.isNotEmpty) { + print(buf); + } + buf.clear(); + buf.write(w); + */ + /**/ + if (buf.isNotEmpty) { + buf.write('\n'); + } + buf.write(w); + /**/ + sl = lw - w.length; + } else { + if (buf.isNotEmpty) { + buf.write(' '); + sl--; + } + buf.write(w); + sl -= w.length; + } + } + print(buf); +} |
