fork download
  1.  
  2. cp_truc_tiep = {
  3. 'a': {'c': 0.2, 'b': 0.1},
  4. 'b': {'c': 0.3, 'd': 0.4},
  5. 'c': {'e': 0.5},
  6. 'd': {'e': 0.6},
  7. }
  8.  
  9. '''
  10. a -> c, b
  11. b -> c, d
  12. c -> e
  13. d -> e
  14.  
  15. a -> e ?
  16.  
  17. '''
  18.  
  19. # lay het cac cty trong list
  20. list_cty = set(cp_truc_tiep.keys())
  21. for cty, l in cp_truc_tiep.items():
  22. list_cty = list_cty.union(set(l.keys()))
  23.  
  24. cp_all = { }
  25.  
  26. def sum_so_huu(link_cty):
  27. result = 1
  28. for i in range(len(link_cty) - 1):
  29. from_cty = link_cty[i]
  30. to_cty = link_cty[i + 1]
  31. result *= cp_truc_tiep[from_cty][to_cty]
  32. return result
  33.  
  34.  
  35. def tinh_cp(current_cty, init_cty, end_cty, link_cty):
  36. if current_cty == end_cty:
  37. sum_link = sum_so_huu(link_cty)
  38. # print(link_cty)
  39. # print('So huu = {}'.format(sum_link))
  40. if init_cty not in cp_all:
  41. cp_all[init_cty] = {}
  42. if end_cty not in cp_all[init_cty]:
  43. cp_all[init_cty][end_cty] = 0
  44. cp_all[init_cty][end_cty] += sum_link
  45. return
  46. if current_cty not in cp_truc_tiep:
  47. return
  48. for cty in cp_truc_tiep[current_cty]:
  49. tinh_cp(cty, init_cty, end_cty, link_cty + [cty])
  50.  
  51.  
  52. def list_cp(cty):
  53. cty1 = cty
  54. for cty2 in list_cty:
  55. if cty1 != cty2:
  56. tinh_cp(cty1, cty1, cty2, [cty1])
  57. val = cp_all.get(cty1, {}).get(cty2, 0)
  58. if val != 0:
  59. print('{} so huu {}: {}'.format(cty1, cty2, val))
  60.  
  61.  
  62. list_cp('a')
Success #stdin #stdout 0.02s 9380KB
stdin
Standard input is empty
stdout
a so huu e: 0.139
a so huu d: 0.04000000000000001
a so huu b: 0.1
a so huu c: 0.23