0

Hi I have given below data but its unindent only using total keyword I can find the right nodes and can build tree structure. Input:

Current Assets  
Cash  
Checking 583961  
Savings 224600  
Petty Cash 89840  
Total Cash 898402  
Accounts Receivable 3593607  
Work in Process 589791  
Other Current Assets  
Prepaid Rent 164593  
Prepaid Liability Insurance 109728  
Total Other Current Assets 274321  
Total Current Assets 274321  

I am looking for below Output:

{
    "Current Assets": {
        "Cash": {
            "Checking": 583961,
            "Savings": 224600,
            "Petty Cash": 89840,
            "Total Cash": 898402
        },
        "Accounts Receivable": 3593607,
        "Work in Process": 589791,
        "Other Current Assets": {
            "Prepaid Rent": 164593,
            "Prepaid Liability Insurance": 109728,
            "Total Other Current Assets": 274321
        },
        "Total Current Assets": 5356121
    }
}

I tried recursion and node concept but nothing worked, It will be great if someone can help me on that trying to achieve using Python.

Rules:

As an example : Actually work in process is not sub item of Account Receivable' Its item of current asset only. As "work in progress" have digit at its end hence no children of it.

As per input data Cash does not have any numeric value at end hence such entries will have child/children,

cash is ending once having total cash with numeric value.

There will not be any children of work in process or Accounts Receivable as they are ending with Numeric value at end

11
  • 1
    You haven't given enough information. Why, for example, should Work in Process be its own item and not a sub item of Accounts Receivable? There's nothing automatic that will know that. Since you didn't show what you have tried, we can't even infer your thinking about this.
    – Mark
    Feb 6 at 3:34
  • 1
    What are the numbers like 1000, or 1320 and where did they come from?
    – dafrandle
    Feb 6 at 3:37
  • Hi @Mark Thans for reviewing the same , Actually work in process is not sub item of Account Receivable' Its item of current asset only. As "work in progress" have digit at his end hence no children of it. Feb 6 at 3:39
  • But how do you know it doesn't belong under Cash then?
    – Mark
    Feb 6 at 3:47
  • Hi @Mark Cash is end with total cash'. Hence Accounts Receivable` and work in process will be new immediate children of Current Assets. There will not be any children of work in process or Accounts Receivable as they are ending with Numeric value at end Feb 6 at 3:52

1 Answer 1

2

You can do this with a recursive function or just use a stack to keep track of the nesting. The basic rule is:

  • No number: increase nesting
  • Starts with "Total": decrease nesting.

With a stack, it might look like:

import re

s = '''Current Assets  
Cash  
Checking 583961  
Savings 224600  
Petty Cash 89840  
Total Cash 898402  
Accounts Receivable 3593607  
Work in Process 589791  
Other Current Assets  
Prepaid Rent 164593  
Prepaid Liability Insurance 109728  
Total Other Current Assets 274321  
Total Current Assets 274321'''


def nest(items):
    res = {}
    stack = [res]
    for item in items:
        components = re.findall(r'(^.*?) (\d+)', item)
        if not components: # no numbers
            cur = {}
            stack[-1][item.strip()] = cur
            stack.append(cur)
        else:
            label, nums = components[0]
            stack[-1][label.strip()] = int(nums)
            if label.startswith("Total"): # end of subdict
                stack.pop()
    return res
        
nest(s.split('\n'))

This will return:

{
  'Current Assets': {
     'Cash': {
           'Checking': 583961,
           'Savings': 224600,
           'Petty Cash': 89840,
           'Total Cash': 898402
      },
      'Accounts Receivable': 3593607,
      'Work in Process': 589791,
      'Other Current Assets': { 
           'Prepaid Rent': 164593,
           'Prepaid Liability Insurance': 109728,
           'Total Other Current Assets': 274321
      },
      'Total Current Assets': 274321
  }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.