File size: 2,189 Bytes
26bead7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Example codes for the AI Python C Extensions Generator application."""

# Prints 'Hello, World!' from a Python function.
example_hello_world = """
def hello_world():
    print("Hello, World!")
"""

# Iterates over an array and returns the sum of all its elements.
example_sum = """
def array_sum(arr):
    total = 0
    for x in arr:
        total += x
    return total
"""

# Returns the nth Fibonacci number using an iterative approach.
example_fibonacci = """
def fibonacci(n):
    a, b = 0, 1
    for _ in range(n):
        a, b = b, a + b
    return a
"""

# Approximates π using the Leibniz alternating series formula.
example_leibniz_pi = """
def leibniz_pi(iterations):
    result = 1.0
    for i in range(1, iterations + 1):
        j = i * 4 - 1
        result -= (1 / j)
        j = i * 4 + 1
        result += (1 / j)
    return result * 4
"""

# Finds the total maximum subarray sum over 20 runs using LCG RNG.
# * <https://en.wikipedia.org/wiki/Maximum_subarray_problem>
example_subarray = """
def lcg(seed, a=1664525, c=1013904223, m=2**32):
    value = seed
    while True:
        value = (a * value + c) % m
        yield value

def max_subarray_sum(n, seed, min_val, max_val):
    lcg_gen = lcg(seed)
    random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val
                      for _ in range(n)]
    max_sum = float('-inf')
    for i in range(n):
        current_sum = 0
        for j in range(i, n):
            current_sum += random_numbers[j]
            if current_sum > max_sum:
                max_sum = current_sum
    return max_sum

def total_max_subarray_sum(n, initial_seed, min_val, max_val):
    total_sum = 0
    lcg_gen = lcg(initial_seed)
    for _ in range(20):
        seed = next(lcg_gen)
        total_sum += max_subarray_sum(n, seed, min_val, max_val)
    return total_sum
"""

# Dictionary of examples for easy access in the interface.
example_dict = {
    "Hello world": [example_hello_world, "hello_world"],
    "Sum array": [example_sum, "array_sum"],
    "Fibonacci": [example_fibonacci, "fibonacci"],
    "Leibniz pi": [example_leibniz_pi, "calculate_pi"],
    "Max subarray sum": [example_subarray, "max_subarray_sum"]}