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

#Python #Tools

Note by Nam Diện Lĩnh
5 years, 11 months ago

No vote yet
1 vote

  Easy Math Editor

This discussion board is a place to discuss our Daily Challenges and the math and science related to those challenges. Explanations are more than just a solution — they should explain the steps and thinking strategies that you used to obtain the solution. Comments should further the discussion of math and science.

When posting on Brilliant:

  • Use the emojis to react to an explanation, whether you're congratulating a job well done , or just really confused .
  • Ask specific questions about the challenge or the steps in somebody's explanation. Well-posed questions can add a lot to the discussion, but posting "I don't understand!" doesn't help anyone.
  • Try to contribute something new to the discussion, whether it is an extension, generalization or other idea related to the challenge.
  • Stay on topic — we're all here to learn more about math and science, not to hear about your favorite get-rich-quick scheme or current world events.

MarkdownAppears as
*italics* or _italics_ italics
**bold** or __bold__ bold

- bulleted
- list

  • bulleted
  • list

1. numbered
2. list

  1. numbered
  2. list
Note: you must add a full line of space before and after lists for them to show up correctly
paragraph 1

paragraph 2

paragraph 1

paragraph 2

[example link](https://brilliant.org)example link
> This is a quote
This is a quote
    # I indented these lines
    # 4 spaces, and now they show
    # up as a code block.

    print "hello world"
# I indented these lines
# 4 spaces, and now they show
# up as a code block.

print "hello world"
MathAppears as
Remember to wrap math in \( ... \) or \[ ... \] to ensure proper formatting.
2 \times 3 2×3 2 \times 3
2^{34} 234 2^{34}
a_{i-1} ai1 a_{i-1}
\frac{2}{3} 23 \frac{2}{3}
\sqrt{2} 2 \sqrt{2}
\sum_{i=1}^3 i=13 \sum_{i=1}^3
\sin \theta sinθ \sin \theta
\boxed{123} 123 \boxed{123}

Comments

There are no comments in this discussion.

×

Problem Loading...

Note Loading...

Set Loading...