Created
November 28, 2018 20:58
-
-
Save NevRA/b33f0bf8c77de04940216843fd9cac55 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Найдите самую длинную подстроку в алфавитном порядке. | |
// Например: самая длинная алфавитная подстрока в asdfaaaabbbbcttavvfffffdf является aaaabbbbctt. | |
// Входные параметры будут состоять только из символов нижнего регистра и будет содержать как минимум одну букву. | |
// Удачи :) | |
// P.S. Чтобы получить список символов в строке можно воспользоваться 'str.codeUnits' | |
// P.P.S Чтобы получить строку из символа можно вызвать String.fromCharCode(char), где char символ из str.codeUnits | |
String longest(String str) { | |
return 'abc'; | |
} | |
bool hasErrors = false; | |
List<String> messages = []; | |
void main() { | |
test('should work with single value', () { | |
expect(longest('z'), equals('z')); | |
}); | |
test('should work with different strings', () { | |
expect(longest('asdfaaaabbbbcttavvfffffdf'), equals('aaaabbbbctt')); | |
expect(longest('asd'), equals('as')); | |
expect(longest('nab'), equals('ab')); | |
expect(longest('abcdeapbcdef'), equals('abcde')); | |
expect(longest('asdfbyfgiklag'), equals('fgikl')); | |
expect(longest('zyba'), equals('z')); | |
expect(longest('aadcccvvvaaabb'), equals('cccvvv')); | |
}); | |
_longest(''); | |
if(!hasErrors ){ | |
print('💪Tests are passed! \n\n'); | |
} else { | |
print('💩 Tests aren\'t passed! \n\n'); | |
} | |
print(messages.join('\n')); | |
} | |
typedef bool Checker(dynamic input); | |
Checker equals(dynamic input) { | |
return (dynamic internalInput) { | |
input.toString() == internalInput.toString() | |
? true | |
: throw AssertionError('value: $input is not equal: $internalInput'); | |
}; | |
} | |
void test(String name, Function input) { | |
try { | |
input(); | |
messages.add('✓ $name'); | |
} catch (e) { | |
hasErrors = true; | |
if (e is AssertionError) { | |
messages.add('✗ $test failed \n name: $name\n exception: ${e.message}'); | |
} | |
} | |
} | |
void expect(dynamic input, bool validator(dynamic validatorInput)) { | |
validator(input); | |
} | |
String _longest(String str) { | |
if (str == null) return null; | |
final chars = str.codeUnits; | |
final result = <int>[]; | |
final tmp = <int>[]; | |
for (var pos = 0; pos < chars.length; pos++) { | |
final char = chars[pos]; | |
if (tmp.isNotEmpty && char < tmp.last) { | |
tmp.clear(); | |
} | |
tmp.add(char); | |
if (tmp.length > result.length) { | |
result..clear()..addAll(tmp); | |
} | |
} | |
return result.map((u) => String.fromCharCode(u)).join(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment