move sorting stuff
This commit is contained in:
parent
06ff6449c3
commit
a039be4581
1 changed files with 0 additions and 35 deletions
34
src/env_file_helper.py
Normal file
34
src/env_file_helper.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
from typing import NamedTuple
|
||||
|
||||
|
||||
class Rule(NamedTuple):
|
||||
child: str
|
||||
parent: str
|
||||
|
||||
|
||||
def is_rule_satisfied(in_list: list, rule: Rule) -> bool:
|
||||
child_index = in_list.index(rule.child)
|
||||
parent_index = in_list.index(rule.parent)
|
||||
return parent_index < child_index
|
||||
|
||||
|
||||
def sort_by_rules(in_list: list, rules: list[Rule]) -> bool:
|
||||
def move_item_down(in_list: list, index: int):
|
||||
"""moves item"""
|
||||
# assert index >
|
||||
in_list[index], in_list[index - 1] = in_list[index - 1], in_list[index]
|
||||
|
||||
for _ in range(10000):
|
||||
rule_satisfied: list[bool] = []
|
||||
for rule in rules:
|
||||
if not is_rule_satisfied(in_list, rule):
|
||||
rule_satisfied.append(False)
|
||||
parent_index = in_list.index(rule.parent)
|
||||
move_item_down(in_list, parent_index)
|
||||
else:
|
||||
rule_satisfied.append(True)
|
||||
if all(rule_satisfied):
|
||||
print("success")
|
||||
return True
|
||||
print("failed")
|
||||
return False
|
||||
Loading…
Add table
Add a link
Reference in a new issue