ASKSAGE: Sage Q&A Forum - RSS feed Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 17 May 2016 08:51:38 +0200Is there a functional way to manipulate cycles extracted from a digraph? I wrote a worksheet that defines a digraph and extracts all of its simple cycles. pr = prime_range(3600) wief = DiGraph([pr, lambda p, q: power_mod(p, q - 1, q^2) == 1]) # could take a while! sc = wief.all_simple_cycles() sc.append([0, 0]) Then it uses mostly procedural code to: 1. Remove the duplicate endpoints 2. Reverse-sort the cycle 3. Group cycles by length 4. Sort the group of cycles 5. Return the first (lexicographically earliest) cycle of each length > maxn = 0 for c in sc: del c[0] # duplicate maxn = max(maxn, len(c)) c.sort(reverse=True) # reverse lexicographic # group by length tbl = [[c for c in sc if len(c) == n] for n in range(1,maxn)] n = 1 for t in tbl: # sort tuples by max element, get smallest tuple t.sort() print 'n=%d:'%(n), # why can't join handle a list of integers? print ', '.join(map(lambda m: '%s'%(m), t[0])) n = n + 1 The worksheet is adequate - it performs step 5 correctly, but a listing of the code will be posted as the PROG paragraph of an OEIS entry. So I would **really** like to reduce the numbered steps to as few lines as possible. Are there more graceful ways to tweak the output of one function before passing it on to another?Maybeso83Tue, 17 May 2016 08:51:38 +0200 a group of similar functions with similar names namelist = ['f'+str(i) for i in range(10)]; namelist for i in range(len(namelist)): namelist[i] = lambda x: 1/x^i 1 - create a list of function names (f0, f1, f2, ...) 2 - for each item in namelist 3 - take that that item and make it a function that raises *x* to a negative power equal to the position of that item (f0(x) = 1/x^0, f1(x) = 1/x^1, ...) Is this possible? EDIT: Made some progress by changing the type of namelist[i] to Expression by var(i)...daniel.e2718Sat, 02 Jun 2012 15:26:12 +0200