refactor: support for BFS and DFS

This commit is contained in:
ruthra kumar 2023-09-25 11:05:30 +05:30
parent 26503a205f
commit 03a38ed025
2 changed files with 50 additions and 27 deletions

View File

@ -10,10 +10,8 @@
"from_date",
"column_break_qxbi",
"to_date",
"section_break_3x70",
"period_from",
"column_break_5ett",
"period_to"
"column_break_iwny",
"algorithm"
],
"fields": [
{
@ -31,29 +29,22 @@
"label": "To Date"
},
{
"fieldname": "section_break_3x70",
"fieldtype": "Section Break"
"default": "BFS",
"fieldname": "algorithm",
"fieldtype": "Select",
"label": "Algorithm",
"options": "BFS\nDFS"
},
{
"fieldname": "period_from",
"fieldtype": "Date",
"label": "Period From"
},
{
"fieldname": "column_break_5ett",
"fieldname": "column_break_iwny",
"fieldtype": "Column Break"
},
{
"fieldname": "period_to",
"fieldtype": "Date",
"label": "Period To"
}
],
"hide_toolbar": 1,
"index_web_pages_for_search": 1,
"issingle": 1,
"links": [],
"modified": "2023-09-16 08:02:33.472406",
"modified": "2023-09-25 10:50:53.887235",
"modified_by": "Administrator",
"module": "Accounts",
"name": "Bisect Accounting Statements",

View File

@ -1,6 +1,7 @@
# Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and contributors
# For license information, please see license.txt
from collections import deque
from math import floor
import frappe
@ -10,18 +11,49 @@ from frappe.utils import getdate
class BisectAccountingStatements(Document):
@frappe.whitelist()
def bisect(self):
period_list = [(getdate(self.from_date), getdate(self.to_date))]
def bfs(self):
period_list = deque([(getdate(self.from_date), getdate(self.to_date))])
periods = []
dates = []
while period_list:
cur_frm_date, cur_to_date = period_list.popleft()
delta = cur_to_date - cur_frm_date
periods.append((cur_frm_date, cur_to_date, delta))
if delta.days == 0:
continue
else:
cur_floor = floor(delta.days / 2)
left = (cur_frm_date, (cur_frm_date + relativedelta(days=+cur_floor)))
right = ((cur_frm_date + relativedelta(days=+(cur_floor + 1))), cur_to_date)
period_list.append(left)
period_list.append(right)
return periods
def dfs(self):
period_list = [(getdate(self.from_date), getdate(self.to_date))]
periods = []
while period_list:
cur_frm_date, cur_to_date = period_list.pop()
delta = cur_to_date - cur_frm_date
if not delta.days > 0:
periods.append((cur_frm_date, cur_to_date, delta))
if delta.days == 0:
continue
else:
cur_floor = floor(delta.days / 2)
left = (cur_frm_date, (cur_frm_date + relativedelta(days=+cur_floor)))
right = ((cur_frm_date + relativedelta(days=+(cur_floor + 1))), cur_to_date)
period_list.append(left)
period_list.append(right)
return periods
cur_floor = floor(delta.days / 2)
left = (cur_frm_date, (cur_frm_date + relativedelta(days=+cur_floor)))
right = ((cur_frm_date + relativedelta(days=+(cur_floor + 1))), cur_to_date)
period_list.append(left)
period_list.append(right)
@frappe.whitelist()
def bisect(self):
if self.algorithm == "BFS":
periods = self.bfs()
if self.algorithm == "DFS":
periods = self.dfs()
print("Periods: ", len(periods))
for x in periods:
print(x)