r子集代码实现(递归)

#!/usr/bin/env python
#coding:utf-8

SET_START = 1
SET_END   = 9
SUB_LEN   = 10

def r_subset(i, r, pre, array, sub):
    if i == r:
        print sub
        return
    j = pre 
    len_ = len(array) - (r - i)
    while j <= len_:
        sub[i] = array[j]
        r_subset(i+1, r, j+1, array, sub)
        j += 1

if __name__ == "__main__":
    array = xrange(SET_START, SET_END)
    i = 0
    r = SUB_LEN
    sub = [0 for _ in xrange(r)]
    r_subset(i, r, 0, array, sub)
posted @ 2015-09-15 17:10  coder_zhang1  阅读(283)  评论(0编辑  收藏  举报