2017-01-11 12 views
5

गणना नहीं कर सकते मैं निम्नलिखित शब्दकोश है:रिकर्सिव अजगर समारोह - बढ़त मामले

>>> for key, details in relationships.items(): 
       print key, details[2] 

('INVOICE', 'INVOICE') 1 
('INVOICE', 'ORDER2') 0.50000000 
('INVOICE', 'ORDER1') 0.01536410 
('ORDER1', 'ORDER2') 0.05023163 
('INVOICE', 'ORDER4') 0.00573215 
('ORDER4', 'ORDER1') 0.08777898 
('ORDER4', 'ORDER3') 0.01674388 

यह निम्न पदानुक्रम बनाता है:

INVOICE -> ORDER2 
     -> ORDER1 -> ORDER2 
     -> ORDER4 -> ORDER1 -> ORDER2 
        -> ORDER3 

जहां प्रत्येक तीर details[2] के मूल्य का प्रतिनिधित्व करता। चालान के लिए प्रत्येक आदेश के अंतिम 'रिश्ते' की गणना की जानी चाहिए। उम्मीद मान:

> ORDER1: 0.01586726 (0.0153641 + 0.0877898 x 0.00573215) 
> ORDER2: 0.50079704 (0.5 + 0.05023163 x 0.0153641 + 0.05023163 x 0.0877898 x 0.00573215) 
> ORDER3: 0.00009598 (0.01674388 x 0.00573215) 
> ORDER4: 0.00573215 (0.00573215) 

मैं एक पुनरावर्ती समारोह में निम्नलिखित का प्रयास किया है:

for invoice in final_relationships.keys(): 
    calculate(invoice, invoice, Decimal(1)) 

def calculate(orig_ord, curr_ord, contribution): 
    for rel_ID, rel_details in relationships.items(): 
      if rel_ID[1] == curr_ord: 
       if orig_ord == curr_ord: 
        contribution = Decimal(1) 
       if rel_ID[0] != rel_ID[1]: 
        contribution = (contribution * rel_details[2]).quantize(Decimal('0.00000001'), rounding=ROUND_HALF_UP) 
        calculate(orig_ord, rel_ID[0], contribution) 
       else: 
        final_relationships[orig_ord] += contribution 
        contribution = Decimal(0) 

यह सही ढंग से सभी स्थितियों एक को छोड़कर, ORDER2 के लिए गणना करता है। के बाद से योगदान 0.05023163 के बजाय 0.00077176 में शुरू होता है, क्योंकि के लिए यात्रा ('ORDER1', 'ORDER2') (लाइन 2 के बाद) दूसरी बार ऐसा नहीं होता है

------- ORDER2 
1 # rel_ID,    curr_ord,  rel_details[2], contribution 
2 ('ORDER1', 'ORDER2') ORDER2  0.05023163   1 
3 ('INVOICE', 'ORDER1') ORDER1  0.01536410   0.05023163 
4 ('INVOICE', 'INVOICE') INVOICE  1     0.00077176 
5 # final 
6 0.00077176 
7 ('ORDER4', 'ORDER1') ORDER1  0.08777898   0.00077176 
8 ('INVOICE', 'ORDER4') ORDER4  0.00573215   0.00006774 
9 ('INVOICE', 'INVOICE') INVOICE  1     3.9E-7 
10 # final 
11 0.00077215 
12 ('INVOICE', 'ORDER2') ORDER2  0.50000000   0.05023163 
13 ('INVOICE', 'INVOICE') INVOICE  1     0.50000000 
14 # final 
15 0.50077215 

मुद्दा, लाइन 7 पर है। यह संबंध INVOICE -> ORDER4 -> ORDER1 -> ORDER2 है।

मैं फ़ंक्शन को कैसे ठीक कर सकता हूं? अगर 'orig_ord' संसाधित नहीं किया गया था, तो मैंने योगदान को रीसेट करने का प्रयास किया, लेकिन यह पता नहीं लगा कि इसे कहां रखा जाए। अगर पूरी चीज गूंगा है, तो मैं फिर से लिखने के लिए खुला हूं, जब तक मुझे नौकरी मिलती है।

उत्तर

0

मुझे लगता है कि आप एक पेड़ बनाने के लिए, क्योंकि यह एक पेड़

INVOICE -> ORDER2 
     -> ORDER1 -> ORDER2 
     -> ORDER4 -> ORDER1 -> ORDER2 
        -> ORDER3 

तो मैं कार्यान्वित this code सही पेड़

hierarchi = [('INVOICE', 'ORDER2'), 
('INVOICE', 'ORDER1'), 
('ORDER1', 'ORDER2'), 
('INVOICE', 'ORDER4'), 
('ORDER4', 'ORDER1'), 
('ORDER4', 'ORDER3')] 

def make_based_list(base): 
    the_list = [] 
    i = 0 
    for item in hierarchi: 
    if(item[0] == base): 
     the_list.insert(i,item) 
     i+=1 
    return the_list 

class Node: 
    def __init__(self,base): 
    self.base = base 
    self.children = [] 
    self.n_of_child = 0 

    def get_child(self,base): 
    found_child = Node('NOT_FOUND') 
    for child in self.children: 
     if child.base == base: 
     found_child = child 

    if found_child.base!='NOT_FOUND': 
     return found_child 
    else: 
     for child in self.children: 
     found_child = child.get_child(base) 
     if found_child.base!='NOT_FOUND': 
      return found_child 
    return found_child 

    def add_child(self,child_node): 
    self.children.insert(self.n_of_child,child_node) 
    self.n_of_child+=1 

    def update_child(self,base,child_node): 
    for child in self.children: 
     if child.base == base: 
      child = child_node 
      return 1 
    return 0 

    def populate(self,assoc_list): 
    for assoc in assoc_list: 
     child_node = Node(assoc[1]) 
     self.add_child(child_node) 

    def print_childrens(self): 
    for child in self.children: 
     child.print_me() 

    def print_me(self): 
    print("Me:"+self.base) 
    if self.n_of_child>0: 
     print("Mychilds:") 
     self.print_childrens() 
    else: 
     print("No child") 

top_base = 'INVOICE' 
top_based_list = make_based_list(top_base) 
top_node = Node(top_base) 
top_node.populate(top_based_list) 

for item in hierarchi: 
    if item[0]!=top_base: 
    the_child = top_node.get_child(item[1]) 
    the_parent = top_node.get_child(item[0]) 
    if the_child.base=='NOT_FOUND': 
     the_child = Node(item[1]) 
    the_parent.add_child(the_child) 
    top_node.update_child(item[0],the_parent) 

top_node.print_me() 

बनाने के लिए मैं अजगर और मैं में नया हूँ की तरह लग रहा है कि पता है कि कोड बेहतर हो सकता है, लेकिन मैं सिर्फ
की सहायता करना चाहता हूं अब मैं आपकी गणना लागू करूँगा और अपना उत्तर

अपडेट करूँगामुझे उम्मीद है कि यह आपके लिए उपयोगी है

संबंधित मुद्दे