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

View File

@ -1,6 +1,7 @@
# Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and contributors # Copyright (c) 2023, Frappe Technologies Pvt. Ltd. and contributors
# For license information, please see license.txt # For license information, please see license.txt
from collections import deque
from math import floor from math import floor
import frappe import frappe
@ -10,18 +11,49 @@ from frappe.utils import getdate
class BisectAccountingStatements(Document): class BisectAccountingStatements(Document):
@frappe.whitelist() def bfs(self):
def bisect(self): period_list = deque([(getdate(self.from_date), getdate(self.to_date))])
period_list = [(getdate(self.from_date), getdate(self.to_date))] periods = []
dates = [] dates = []
while period_list: while period_list:
cur_frm_date, cur_to_date = period_list.pop() cur_frm_date, cur_to_date = period_list.popleft()
delta = cur_to_date - cur_frm_date 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 continue
else:
cur_floor = floor(delta.days / 2) cur_floor = floor(delta.days / 2)
left = (cur_frm_date, (cur_frm_date + relativedelta(days=+cur_floor))) left = (cur_frm_date, (cur_frm_date + relativedelta(days=+cur_floor)))
right = ((cur_frm_date + relativedelta(days=+(cur_floor + 1))), cur_to_date) right = ((cur_frm_date + relativedelta(days=+(cur_floor + 1))), cur_to_date)
period_list.append(left) period_list.append(left)
period_list.append(right) 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
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
@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)