songleash

generate chansons en laisse
git clone https://a3nm.net/git/songleash/
Log | Files | Refs

cycle.py (1125B)


      1 #!/usr/bin/python3
      2 
      3 import sys
      4 from common import fem, vowels_script, strip_accents, semivowels, start
      5 from collections import defaultdict
      6 
      7 g = defaultdict(lambda: set())
      8 
      9 bestlen = 0
     10 visited = set()
     11 
     12 def dfs(l):
     13     global g
     14     global start
     15     global bestlen
     16     global visited
     17     #print(l)
     18     if l[-1] == start and len(l) > 1:
     19         if len(l) > bestlen:
     20             #print_list(l)
     21             bestlen = len(l)
     22             print(bestlen-1)
     23             f = open(out, 'w')
     24             for w in l:
     25                 print('%s %s' % ('f' if w[0] else 'm', w[1]), file=f)
     26             f.close()
     27             return
     28     if l[-1] in visited:
     29         return
     30     v = l[-1]
     31     visited.add(v)
     32     for t in g[v]:
     33         #if (len(l) % 2 != 0) in [fem(w) for w in g[v][t]]:
     34         #if fcache[v][t][len(l) % 2 == 0]:
     35         dfs(l+[t])
     36     visited.remove(v)
     37 
     38 out = sys.argv[1]
     39 
     40 for l in sys.stdin.readlines():
     41     l = l.strip().split('\t')
     42     if len(l) < 6:
     43         l.append('')
     44     fr = (l[0] == 'f', l[1])
     45     to = (l[2] == 'f', l[3])
     46     #g[fr][to].add((l[4], l[5]))
     47     g[fr].add(to)
     48     #g[fr][to] = (l[4], l[5])
     49 
     50 dfs([start])
     51