Menu
CoddyTech

Recap: Charge Gate

Lesson 11 of 48 in Coddy's Dart for Swift Developers course.

challenge icon

Challenge

Easy

Implement charge(int initial,List<int> costs): apply requests in order, paying only costs no greater than current charge, and return remaining:accepted. All input integers are nonnegative and at most 100. The first is initial charge; zero costs count as accepted.

Do not print from editable files. The supplied exercise.dart parses input and returns output; the locked driver independently probes the public APIs and prints the result with one newline.

Your code goes in functions.dart, exercise.dart. main.dart holds the supplied input/output code and cannot be edited.

Try it yourself

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$'),'');
check(charge(6,[7,2,4])=='0:2');
stdout.writeln(solve(input));
}

All lessons in Dart for Swift Developers

Practice on your own: Online Dart compiler