Last active
December 31, 2015 06:19
-
-
Save carymrobbins/30f7b0da3fe00d5c11bb 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
| import Data.List | |
| type Dict = [(String, String)] | |
| views :: Dict | |
| views = [ ("foo", "select bar") | |
| , ("bar", "select shoe") | |
| , ("shoe", "select none") | |
| , ("cat", "select foo") | |
| , ("dog", "select mouse") | |
| , ("mouse", "select cat") | |
| ] | |
| answer = sortByDependency views [] | |
| sortByDependency :: Dict -> Dict -> Dict | |
| sortByDependency [] partial = partial | |
| sortByDependency initial partial = sortByDependency remaining result | |
| where | |
| result = partial ++ excludeDependencies initial | |
| remaining = filter (\(k, v) -> k `notElem` (map fst result)) initial | |
| excludeDependencies :: Dict -> Dict | |
| excludeDependencies d = | |
| filter (\(k, v) -> not . any (`isInfixOf` v) $ map fst d) d |
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
| from django.utils.datastructures import SortedDict | |
| views = SortedDict() | |
| views['foo'] = 'select bar' | |
| views['bar'] = 'select shoe' | |
| views['shoe'] = 'select none' | |
| views['cat'] = 'select foo' | |
| views['dog'] = 'select mouse' | |
| views['mouse'] = 'select cat' | |
| def answer(): | |
| """ :rtype: SortedDict """ | |
| return sort_by_dependency(views, SortedDict()) | |
| def sort_by_dependency(initial, partial): | |
| """ | |
| :type initial: SortedDict | |
| :type partial: SortedDict | |
| :rtype: SortedDict | |
| """ | |
| if not initial: | |
| return partial | |
| result = SortedDict(partial.items() + exclude_dependencies(initial).items()) | |
| remaining = SortedDict((k, v) for k, v in initial.items() | |
| if k not in result.keys()) | |
| return sort_by_dependency(remaining, result) | |
| def exclude_dependencies(d): | |
| """ | |
| :type d: SortedDict | |
| :rtype: SortedDict | |
| """ | |
| return SortedDict((k, v) for k, v in d.items() | |
| if not any(k2 in v for k2 in d.keys())) | |
| print answer() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment