<jupyter_start><jupyter_text>Optimisation II - Lab 1 Instructions* Read all the instructions carefully.* **Numpy** has a help file for every function if you get stuck. See: https://docs.scipy.org/doc/numpy-1.14.5/reference/* See these useful links:    * https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html    * https://docs.scipy.org/doc/numpy/user/quickstart.html* **Numpy** is not always required.* There are also numerous sources available on the internet, Google is your friend!  Warm-up ExercisesComplete the following warm-up tasks: Question 1Write a function that will convert a decimal number to a binary number. The function must be able to handle a decimal of any length.<jupyter_code>def dec_to_bin(n):
    """
    Inputs
        n: a scalar real value
    
    Output
        s: a binary string equal to n
    """
    # YOUR CODE HERE
    raise NotImplementedError()
# Run this test cell to check your code
# Do not delete this cell
nt.assert_string_equal('1101', dec_to_bin(13))
print('All Tests Passed!!!')<jupyter_output><empty_output><jupyter_text>Question 2A string is simply an ordered collection of symbols selected from some alphabet and formed into a word; the length of a string is the number of symbols that it contains. An example of a length 21 DNA string (whose alphabet contains the symbols 'A', 'C', 'G', and 'T') is "ATGCTTCAGAAAGGTCTTACG." Write a function that takes as input, a vector  $s$  and returns a vector of four integers counting the respective number of times that the symbols 'A', 'C', 'G', and 'T' occur in  $s$.<jupyter_code>def dna(s):
    """
    Inputs
    s: a string of DNA sequence
    
    Outputs
    occ_seq: a list of the number of times A, C, G and T occur (in this order)
    """
    # YOUR CODE HERE
    raise NotImplementedError()
    
# Run this test cell to check your code
# Do not delete this cell
s = 'ATGCTTCAGAAAGGTCTTACG'
nt.assert_array_equal([6, 4, 5, 6], dna(s), err_msg='incorrect function')
print('All Tests Passed!!!')<jupyter_output><empty_output><jupyter_text>Question 3Write a function that takes a matrix  $M$  as input and returns a matrix that contains only those elements of  $M$  that are in odd rows and columns.<jupyter_code>def matrix_extraction(M):
    """
    Inputs:
        M: a random numpy matrix
    
    Outputs:
        N: a matrix with the odd rows and columns of M only
    """
    # YOUR CODE HERE
    raise NotImplementedError()
    
# Run this test cell to check your code
# Do not delete this cell
M = np.array([[0.29977941, 0.05186872, 0.57587171, 0.56806655, 0.38424249],
             [0.25568574, 0.53849314, 0.76387274, 0.29669909, 0.68029403],
             [0.9338883,  0.44839289, 0.48052527, 0.60921226, 0.89123473],
             [0.76114271, 0.61475746, 0.04465618, 0.85599524, 0.29450567],
             [0.43419249, 0.04936524, 0.14366823, 0.30798834, 0.91016638]])
N = np.array([[0.29977941, 0.57587171, 0.38424249],
             [0.9338883,  0.48052527, 0.89123473],
             [0.43419249, 0.14366823, 0.91016638]])
nt.assert_array_almost_equal(N, matrix_extraction(M), err_msg='incorrect function')
print('All Tests Passed!!!')<jupyter_output><empty_output><jupyter_text>Question 4Write a function that computes how much money we have. It takes a single input argument, a row vector  $c$, whose elements specify the number of five cent, 10 cent, 20 cent and 50 cent coins we have (in the order listed here). The output should be a scalar value of the total in Rands - not cents.<jupyter_code>def rand_compute(c):
    """
    Inputs
        c: a list of the number of 5, 10, 20 and 50 cent coins
        
    Outputs
        total: the scalar Rand value of all the coins in the list
    """
    # YOUR CODE HERE
    raise NotImplementedError()
    
# Run this test cell to check your code
# Do not delete this cell
c = [0,1,0,2]
nt.assert_equal(1.1, rand_compute(c), err_msg= 'incorrect function')
print('All Tests Passed!!!')<jupyter_output><empty_output><jupyter_text>Main Exercises Question 1Write a function which randomly samples a local neighbourhood. This neighbourhood maybe circular or square in nature. The function takes as input, a vector of (x, y) coordinate points, i.e. $[x, y]$. A scalar, $r$, a radial value, a scalar, $n$, the number of samples and scalar, $s$, the shape to explore ($s = 1$ - a circular radius and $s =2$ a square neighbourhood). The function should return array containing the coordinates of the randomly generated samples. Finally, plot these coordinates and its "origin" on the same set of axes to see your function in action. Explore with different values for your inputs.<jupyter_code>def neighbourhood(c, r, n, s):
    # YOUR CODE HERE
    raise NotImplementedError()

# Question 1 is ungraded. You see run the below code to see if your code correctly generates the neigbourhoods
c = [np.random.randint(10), np.random.randint(10)]
r = 5
n = 1000
s = 2

neighbourhood(c, r, n, s)<jupyter_output><empty_output><jupyter_text>Question 2Write a function which takes in as input an unordered vector of values. Your function should return a row vector ($1 \times 3$) containing the minimum value, median value and maximum value (in this order). Do not use the builtin sort function. Look up sorting algorithms, *bubble sort* for example.<jupyter_code>def lst_arrange(x):
    """
    Inputs:
        x: an array of random integers
    
    Output:
        vals: an array of 3 elements, in the order of min, median and max values of the input
    """
    # YOUR CODE HERE
    raise NotImplementedError()


# Run this test cell to check your code
# Do not delete this cell
y = np.random.randint(1, 10, 5)
nt.assert_array_almost_equal([np.min(y), np.median(y), np.max(y)], lst_arrange(y), err_msg= 'incorrect function')
print('All Tests Passed!!!')<jupyter_output><empty_output><jupyter_text>Question 3Write a function which takes as input an array of points, i.e. $[(x_1, y_1), (x_2, y_2), \ldots]$. It should return as output, a smaller array containing the points of the Convex Hull vertices (ensure no duplicates). Finally, plot this Convex Hull and the original set of points on the same axes to determine if your function is doing as instructed. Some algorithms which can find the convex hull are *Andrew's monotone chain algorithm* and *gift wrapping*, there are others - choose whichever one you like.<jupyter_code>def convex_hull(points):
    """
    Input: 
        points: an array of coordinates, i.e. [(x1, y1), (x2, y2), (x3, y3), ....]
    Output: 
        lst: a smaller array of coordinates, now only contain vertices of the hull with no duplicates
    """
    # YOUR CODE HERE
    raise NotImplementedError()
# Run this test cell to check your code
# Do not delete this cell

assert sorted(convex_hull([(i//10, i%10) for i in range(100)])) == sorted([(0, 0), (9, 0), (9, 9), (0, 9)])
assert sorted(convex_hull([(4, 82), (80, 43), (74, 67), (77, 83), (60, 41), (4, 6), (38, 23), 
                    (4, 19), (92, 70), (62, 74), (84, 30), (63, 22), (37, 45), (56, 81), 
                    (31, 19), (31, 0), (91, 48), (34, 22), (58, 72), (22, 78), (78, 28), 
                    (15, 15), (49, 89), (13, 39), (11, 68), (18, 28), (49, 61), (11, 35), 
                    (15, 6), (61, 22), (63, 82), (31, 39), (1, 50), (50, 74), (77, 67), 
                    (58, 83), (46, 42), (62, 24), (66, 39), (93, 11), (0, 47), (29, 94), 
                    (2, 60), (43, 83), (67, 97), (32, 59), (97, 73), (43, 72), (33, 85), 
                    (48, 87), (41, 48), (95, 0), (98, 28), (17, 70), (55, 26), (7, 23), 
                    (60, 31), (79, 89), (47, 2), (43, 34), (94, 84), (12, 92), (47, 69), 
                    (25, 67), (95, 90), (16, 31), (5, 58), (84, 71), (15, 6), (23, 12), 
                    (57, 48), (36, 59), (84, 61), (95, 18), (16, 66), (84, 94), (2, 5), 
                    (9, 5), (97, 77), (0, 58), (37, 17), (25, 70), (51, 42), (78, 63), 
                    (42, 70), (31, 63), (93, 9), (33, 10), (2, 46), (66, 68), (74, 57), 
                    (69, 93), (86, 76), (78, 73), (55, 22), (68, 41), (69, 43), (97, 5), 
                    (95, 22), (27, 89)])) == sorted([(0, 47), (2, 5), (31, 0), (95, 0), (97, 5), 
                                             (98, 28), (97, 77), (95, 90), (84, 94), (67, 97), 
                                             (29, 94), (12, 92), (4, 82), (0, 58)])
print('All Tests Passed!!!')


# To plot and see if you convex hull looks correct. This is just a sanity check
# You should be aiming to see all the points plotted in blue dots and the convex
# hull wrapping the points in red.

lst = []
n   = 100
for i in range(100):
    lst.append((np.random.randint(100), np.random.randint(100)))
    
plt.figure()
for x in lst:
    plt.plot(x[0], x[1], 'bo')
    
lst2 = convex_hull(lst)
nn   = len(lst2)

xx = [lst2[i][0] for i in list(range(nn))]
xx.append(lst2[0][0])

yy = [lst2[i][1] for i in list(range(nn))]
yy.append(lst2[0][1])
plt.plot(xx, yy, 'r-')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13710928840611838, 'fraction_numerical': 0.09354910219463533, 'mean_word_length': 3.1088342440801457, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 19, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '583204', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 3635, 'n_tokens_neox': 2973, 'n_words': 1192, 'path': '2018/release/Lab1/Lab1.ipynb', 'repo_name': 'mattw89/APPM2007-Optimisation-II', 'size': '9022'}