Last active
November 29, 2018 22:16
-
-
Save kalachevmax/a958b4ec454149a4474cd5c441566b99 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Поиск файла. | |
// Среди иерархии папок затерялся файл. Напиши функцию для поиска файла. Файловая система представлена в виде мапки. | |
// Гарантируем, что на файловой системе не может быть более одного файла. | |
// Результат нужно вернуть в ввиде строки: пути к файлу, либо 'File not found' в случае отсутствия файла. | |
// Пример: | |
// { | |
// 'f1': { | |
// 'f11': { | |
// 'f111': { | |
// 'file': '' | |
// } | |
// }, | |
// 'f12': {} | |
// }, | |
// 'f2': {}, | |
// 'f3': { | |
// 'f31': {} | |
// } | |
// }; | |
// должно вернуть: 'f1/f11/f111/file' | |
// Требуемую мапку в дарте можно закодировать как Map<String, Object>. | |
// Object - базовый класс, наследником которого являются как String, так и Map. | |
// Проверить тип можно следующим образом: | |
// value is Map | |
// value is String | |
// Получить ключи мапки можно следующим образом: | |
// map.keys.toList() | |
String findFileName(Map<String, Object> fs) { | |
return ''; | |
} | |
bool hasErrors = false; | |
List<String> messages = []; | |
Map<String, Object> fs; | |
void main() { | |
test('should return correct path for exsisting file', () { | |
fs = { | |
'f1': { | |
'f11': { | |
'f111': { | |
'file': '' | |
} | |
}, | |
'f12': {} | |
}, | |
'f2': {}, | |
'f3': { | |
'f31': {} | |
} | |
}; | |
expect(findFileName(fs), equals('f1/f11/f111/file')); | |
}); | |
test('should return "File not found" for non existing file', () { | |
fs = { | |
'f1': { | |
'f11': { | |
'f111': {} | |
}, | |
'f12': {} | |
}, | |
'f2': {}, | |
'f3': { | |
'f31': {} | |
} | |
}; | |
expect(findFileName(fs), equals('File not found')); | |
}); | |
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); | |
} | |
class Solver { | |
final Map<String, Object> _fs; | |
final List<String> _path = []; | |
String _fileName; | |
Solver(this._fs); | |
Object _getNodeValue(String node) { | |
Map<String, Object> map = _fs; | |
for (int i = 1; i < _path.length; i++) { | |
map = map[_path[i]]; | |
} | |
return map[node]; | |
} | |
String _getFilePath(String fileName) => _path.join('/').substring(2) + '/' + fileName; | |
void visitSubTree(String root, List<String> nodes) { | |
_path.add(root); | |
while (nodes.isNotEmpty) { | |
final String node = nodes.removeLast(); | |
final Object value = _getNodeValue(node); | |
if (value is Map) { | |
if (value.isNotEmpty) { | |
visitSubTree(node, value.keys.toList()); | |
} | |
} else { | |
_fileName = _getFilePath(node); | |
} | |
} | |
_path.removeLast(); | |
} | |
String solve() { | |
visitSubTree('/', _fs.keys.toList()); | |
return _fileName ?? 'File not found'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment