fork download
  1. import time
  2. import string
  3.  
  4. def sub_combinations(segment, size=0):
  5. if segment == ():
  6. yield ()
  7. return
  8. stop = min(size or len(segment), len(segment))
  9. for i in range(1, stop + 1):
  10. for j in sub_combinations(segment[i:], size):
  11. yield (segment[:i],) + j
  12.  
  13.  
  14. def subtuples(t):
  15. for i in range(1<< (len(t)-1)):
  16. result = [ [ t[0] ] ]
  17. for j in range(len(t)-1):
  18. if (1<<j) & i:
  19. result[-1].append(t[j+1])
  20. else:
  21. result.append([ t[j+1] ])
  22. yield result
  23.  
  24. f = sub_combinations
  25. data = tuple(string.ascii_lowercase[:22])
  26.  
  27. for f in sub_combinations, subtuples:
  28. t0 = time.time()
  29. for x in f(data):
  30. pass
  31. print('{} {}'.format(f.__name__, time.time() - t0))
Time limit exceeded #stdin #stdout 5s 10104KB
stdin
Standard input is empty
stdout
Standard output is empty