Waste less time on Facebook — follow Brilliant.
×

Python tools for Brilliant

We can use these functions for some problems in Brilliant. To use these functions, copy it to a python file (I named it brilliant.py) and use execfile("path/to/brilliant.py") to include the file and use these functions. Enjoy using it :)

PS: have bugs? Report in the comment section

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
def c_recur(result, list, start, end, left): #recursive function, do not use it
    if start>=end or left<=0:
        result.append(tuple(list));
        return;

    for i in range(start, end):
        list.append(i);
        c_recur(result, list, i+1, end+1, left-1);
        list.pop();

def p_recur(result, list, marks, start, n, left): #recursive function, do not use it
    if left<=0:
        result.append(tuple(list));
        return;

    for i in range(0, n):
        if not marks[i]:
            marks[i]=True;
            list.append(start+i);
            p_recur(result, list, marks, start, n, left-1);
            list.pop();
            marks[i]=False;

def c(k, n): #generate all k-combinations from range(0, n)
    if k<=0 or n<=0 or k>n:
        return [];

    result=[];

    c_recur(result, [], 0, n-k+1, k);
    return result;

def p(k, n): #generate all k-permutations from range(0, n)
    if k<=0 or n<=0 or k>n:
        return [];

    result=[];

    p_recur(result, [], [False]*n, 0, n, k);
    return result;

def select(k, list): #generate all k-combinations from list
    result=[]
    n=len(list)

    for indices in c(k, n):
        l=[]
        for i in indices:
            l.append(list[i])
        result.append(tuple(l))

    return result

def permute(list): #generate all k-permutations from list
    result=[]
    n=len(list)

    for indices in p(n, n):
        l=[]
        for i in indices:
            l.append(list[i])
        result.append(tuple(l))

    return result

def diff(l1, l2): #return l1\l2
    l=[]
    for i in l1:
        if i not in l2:
            l.append(i)

    return l

def factor(n): #factorize n
    r=n;
    i=2;
    h={};

    i=2;
    while r>1:
        while r%i==0:
            r//=i;
            if i not in h:
                h[i]=0;
            h[i]+=1;
        i+=1;

    return h;

def ffactor(n): #factorize n!
    h={}
    for i in xrange(2, n+1):
        f=factor(i)
        for k in f:
            if k not in h:
                h[k]=0
            h[k]+=f[k]
    return h

def gcd(m, n):
    if m==0:
        return n

    while m!=0:
        n%=m
        m,n=n,m

    return n

Note by Nam Diện Lĩnh
2 years ago

No vote yet
1 vote

Comments

There are no comments in this discussion.

×

Problem Loading...

Note Loading...

Set Loading...