-
-
Save zolotyh/49b058587879eba5ee826e7df5aa5e74 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
//Дана строка, содержащая буквы и числа (без знаков препиная, пробелов и пр, например 1323dart23123dart). | |
//Необходимо подсчитать количество вхождений каждого слова вне зависимости от регистра (используйте нижний регистр для возвращаемого значения, например {dart:2}) | |
Map<String, int> calculateWordsCount(String str){ | |
var tokens = str.split(new RegExp(r'[0-9]')).where((t)=>t!=''); | |
var map = new Map<String, int>(); | |
for(var token in tokens){ | |
token = token.toLowerCase(); | |
if(!map.containsKey(token)){ | |
map[token]=0; | |
} | |
map[token] = map[token]+1; | |
} | |
return map; | |
} | |
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
//Дана строка, содержащая буквы и символы (без знаков препиная, пробелов и пр, например 1323dart23123dart). | |
//Необходимо подсчитать вхождение каждого слова вне зависимости от регистра (используйте нижний для возвращаемого значения, например {dart:2}) | |
Map<String, int> calculateWordsCount(String str){ | |
return null | |
} | |
bool hasErrors = false; | |
List<String> messages = []; | |
void main() { | |
test('8809Dart123123is098899cool213232', () { | |
expect(calculateWordsCount('8809Dart123123is098899cool213232'), equals({'dart':1,'is':1, 'cool':1})); | |
}); | |
test('Okey123OKEY13okeY13', () { | |
expect(calculateWordsCount('Okey123OKEY13okeY13'), equals({'okey':3})); | |
}); | |
test('213123yep0Hey0hey9880hey23123hey12323123HEY123123123yep', () { | |
expect(calculateWordsCount('213123yep0Hey0hey9880hey23123hey12323123HEY123123123yep'), equals({'yep':2,'hey':5})); | |
}); | |
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment