fork(1) download
  1. # add lines as input strings
  2. # theoutput must be terminated by a line containing only a minus sign(-)
  3. # the example input describes
  4. # http://i...content-available-to-author-only...r.com/qfQWs.png
  5. # can be described aa
  6. # EAF
  7. # GIH
  8. # BKJLC
  9. # DKGE
  10. # DJIA
  11. # DLHF
  12. # ABG
  13. # AHC
  14. # DB
  15. # DC
  16. # BE
  17. # CF
  18. # -
  19. # find more here
  20. # http://p...content-available-to-author-only...e.com/a/22905/1967
  21.  
  22. strings=[]
  23. lines=[]
  24. string=input()
  25. print("strings:")
  26. while string!='-':
  27. strings.append(string)
  28. lines.append(list(string))
  29. print(string)
  30. string=input()
  31.  
  32. vertices=[]
  33. for line in lines:
  34. for vertex in line:
  35. if vertex not in vertices:
  36. vertices.append(vertex)
  37. vertices.sort()
  38. print("")
  39. print("vertices:")
  40. print( vertices)
  41.  
  42. edges=[]
  43. for line in lines:
  44. for vertex1 in line:
  45. for vertex2 in line:
  46. if vertex1<vertex2:
  47. edges.append((vertex1,vertex2))
  48. edges.sort()
  49. print("")
  50. print("edges:")
  51. for edge in edges:
  52. print(edge)
  53.  
  54. print("")
  55. print("lines:")
  56. for line in lines:
  57. print(line)
  58.  
  59. triangles=[]
  60. cnt=0
  61. for i in vertices:
  62. for j in vertices:
  63. for k in vertices:
  64. if (i<j) and (i,j) in edges:
  65. if (j<k) and (j,k) in edges:
  66. if (i,k) in edges:
  67. is_degenerated=False
  68. for line in lines:
  69. is_degenerated=is_degenerated or (i in line and j in line and k in line)
  70. if not is_degenerated:
  71. cnt=cnt+1
  72. triangles.append((i,j,k))
  73.  
  74. print("")
  75. print("triangles:")
  76. n=0
  77. for triangle in triangles:
  78. n+=1
  79. print(n , triangle)
  80.  
  81. print("")
  82. print("triangles found:")
  83. print(cnt)
  84.  
Success #stdin #stdout 0.02s 9984KB
stdin
EAF
GIH
BKJLC
DKGE
DJIA
DLHF
ABG
AHC
DB
DC
BE
CF
-
stdout
strings:
ABCD
AFGHM
IJKLM
AIN
NJFB
NKGC
NLHD
NME

vertices:
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N']

edges:
('A', 'B')
('A', 'C')
('A', 'D')
('A', 'F')
('A', 'G')
('A', 'H')
('A', 'I')
('A', 'M')
('A', 'N')
('B', 'C')
('B', 'D')
('B', 'F')
('B', 'J')
('B', 'N')
('C', 'D')
('C', 'G')
('C', 'K')
('C', 'N')
('D', 'H')
('D', 'L')
('D', 'N')
('E', 'M')
('E', 'N')
('F', 'G')
('F', 'H')
('F', 'J')
('F', 'M')
('F', 'N')
('G', 'H')
('G', 'K')
('G', 'M')
('G', 'N')
('H', 'L')
('H', 'M')
('H', 'N')
('I', 'J')
('I', 'K')
('I', 'L')
('I', 'M')
('I', 'N')
('J', 'K')
('J', 'L')
('J', 'M')
('J', 'N')
('K', 'L')
('K', 'M')
('K', 'N')
('L', 'M')
('L', 'N')
('M', 'N')

lines:
['A', 'B', 'C', 'D']
['A', 'F', 'G', 'H', 'M']
['I', 'J', 'K', 'L', 'M']
['A', 'I', 'N']
['N', 'J', 'F', 'B']
['N', 'K', 'G', 'C']
['N', 'L', 'H', 'D']
['N', 'M', 'E']

triangles:
1 ('A', 'B', 'F')
2 ('A', 'B', 'N')
3 ('A', 'C', 'G')
4 ('A', 'C', 'N')
5 ('A', 'D', 'H')
6 ('A', 'D', 'N')
7 ('A', 'F', 'N')
8 ('A', 'G', 'N')
9 ('A', 'H', 'N')
10 ('A', 'I', 'M')
11 ('A', 'M', 'N')
12 ('B', 'C', 'N')
13 ('B', 'D', 'N')
14 ('C', 'D', 'N')
15 ('F', 'G', 'N')
16 ('F', 'H', 'N')
17 ('F', 'J', 'M')
18 ('F', 'M', 'N')
19 ('G', 'H', 'N')
20 ('G', 'K', 'M')
21 ('G', 'M', 'N')
22 ('H', 'L', 'M')
23 ('H', 'M', 'N')
24 ('I', 'J', 'N')
25 ('I', 'K', 'N')
26 ('I', 'L', 'N')
27 ('I', 'M', 'N')
28 ('J', 'K', 'N')
29 ('J', 'L', 'N')
30 ('J', 'M', 'N')
31 ('K', 'L', 'N')
32 ('K', 'M', 'N')
33 ('L', 'M', 'N')

triangles found:
33