<jupyter_start><jupyter_text>Codility coding challenges By <NAME>, PhDThis is a potential coding challenge that you may come across during an interview process. The service provider is Codility where they allow employers to gauge the coding ability of prospective team members. Codility also provides additional challenges which are freely accessible. I picked one challenge on;Link: https://app.codility.com/programmers/lessons/14-binary_search_algorithm/nailing_planks/  Challege 14_2:You are given two non-empty arrays A and B consisting of N integers. These arrays represent N planks. More precisely, A[K] is the start and B[K] the end of the K−th plank.Next, you are given a non-empty array C consisting of M integers. This array represents M nails. More precisely, C[I] is the position where you can hammer in the I−th nail.We say that a plank (A[K], B[K]) is nailed if there exists a nail C[I] such that A[K] ≤ C[I] ≤ B[K].The goal is to find the minimum number of nails that must be used until all the planks are nailed. In other words, you should find a value J such that all planks will be nailed after using only the first J nails. More precisely, for every plank (A[K], B[K]) such that 0 ≤ K < N, there should exist a nail C[I] such that I < J and A[K] ≤ C[I] ≤ B[K].For example, given arrays A, B such that:    A[0] = 1    B[0] = 4    A[1] = 4    B[1] = 5    A[2] = 5    B[2] = 9    A[3] = 8    B[3] = 10    four planks are represented: [1, 4], [4, 5], [5, 9] and [8, 10].Given array C such that:    C[0] = 4    C[1] = 6    C[2] = 7    C[3] = 10    C[4] = 2if we use the following nails:0, then planks [1, 4] and [4, 5] will both be nailed.0, 1, then planks [1, 4], [4, 5] and [5, 9] will be nailed.0, 1, 2, then planks [1, 4], [4, 5] and [5, 9] will be nailed.0, 1, 2, 3, then all the planks will be nailed.Thus, four is the minimum number of nails that, used sequentially, allow all the planks to be nailed.Write a function:def solution(A, B, C)that, given two non-empty arrays A and B consisting of N integers and a non-empty array C consisting of M integers, returns the minimum number of nails that, used sequentially, allow all the planks to be nailed.If it is not possible to nail all the planks, the function should return −1.For example, given arrays A, B, C such that:    A[0] = 1    B[0] = 4    A[1] = 4    B[1] = 5    A[2] = 5    B[2] = 9    A[3] = 8    B[3] = 10    C[0] = 4    C[1] = 6    C[2] = 7    C[3] = 10    C[4] = 2the function should return 4, as explained above.Write an efficient algorithm for the following assumptions:N and M are integers within the range [1..30,000];each element of arrays A, B, C is an integer within the range [1..2*M];A[K] ≤ B[K].  Outline:This was a bit of a challenge as I developed without referring to any reading materials. However, going over the proposed reading material would help me better understand coding techniques which I will attend to in due course. The presented solution is a hard-wired solution, which is presented in two parts as workings and then the implementation of the solution. Working:Here I work within a single line so I can get everything to work before I adopt it to a funnction<jupyter_code>A = [1,4,5,8,12,11]          # Startning point of planks
B = [4,5,9,10,15,12]         # Ending point of planks
C = [4,6,7,10,2,13]          # Positions of nails

Lc = len (C)                 # Number of nails
La = len (A)                 # Number of planks
N = 0                        # Counter to count used nails
P = 0                        # Counter to count nailed down planks
TP = 0                       # Counter to track total numbre of nailed down planks 
D = [1]*La                   # Array to trak which planks are nailed down

# First check whether there is a plank which is outside the bounds of nails. 
# If there is one then there is no point in continuing further as the nails will not get the planks down
if min(C) > min(B) or max(C) < max(A):
    N = -1
    print(N)

# Pick a nail and check how many planks it strikes down

print('A / B / C / D / N / P')   # Output

for i in range(Lc):          # For each nail
    for j in range(La):      # What panks are struck down by this nail
        print(A,B,C,D,N,P)   # Output
        if (C[i] >= A[j]) & (C[i] <= B[j]) & D[j] == 1: # If a nail is within the plank
            P = P+1          # Strike down the plank
    print('------')
    C[i] = 0                 # Remove the nail
    if sum(D) != 0:          # If the plank is not already struck down
        N = N+1              # Used a nail

    for k in range(P):       # This nail has struck dwon
        D[TP+k] = 0          # Planks in these locations
    TP = TP + P              # Total planks struck donw by the nail
    P = 0                    # Reset counter to see how many planks are taken down by the next nail
    print(A,B,C,D,N,P)       # Output
print(N)                     # Output

if La > TP:                  # If there are left over planks then return -1
    N = -1
print(N)<jupyter_output>A / B / C / D / N / P
[1, 4, 5, 8, 12, 11] [4, 5, 9, 10, 15, 12] [4, 6, 7, 10, 2, 13] [1, 1, 1, 1, 1, 1] 0 0
[1, 4, 5, 8, 12, 11] [4, 5, 9, 10, 15, 12] [4, 6, 7, 10, 2, 13] [1, 1, 1, 1, 1, 1] 0 1
[1, 4, 5, 8, 12, 11] [4, 5, 9, 10, 15, 12] [4, 6, 7, 10, 2, 13] [1, 1, 1, 1, 1, 1] 0 2
[1, 4, 5, 8, 12, 11] [4, 5, 9, 10, 15, 12] [4, 6, 7, 10, 2, 13] [1, 1, 1, 1, 1, 1] 0 2
[1, 4, 5, 8, 12, 11] [4, 5, 9, 10, 15, 12] [4, 6, 7, 10, 2, 13] [1, 1, 1, 1, 1, 1] 0 2
[1, 4, 5, 8, 12, 11] [4, 5, 9, 10, 15, 12] [4, 6, 7, 10, 2, 13] [1, 1, 1, 1, 1, 1] 0 2
------
[1, 4, 5, 8, 12, 11] [4, 5, 9, 10, 15, 12] [0, 6, 7, 10, 2, 13] [0, 0, 1, 1, 1, 1] 1 0
[1, 4, 5, 8, 12, 11] [4, 5, 9, 10, 15, 12] [0, 6, 7, 10, 2, 13] [0, 0, 1, 1, 1, 1] 1 0
[1, 4, 5, 8, 12, 11] [4, 5, 9, 10, 15, 12] [0, 6, 7, 10, 2, 13] [0, 0, 1, 1, 1, 1] 1 0
[1, 4, 5, 8, 12, 11] [4, 5, 9, 10, 15, 12] [0, 6, 7, 10, 2, 13] [0, 0, 1, 1, 1, 1] 1 0
[1, 4, 5, 8, 12, 11] [4, 5, 9, 10, 15, 12] [0, 6, 7, 10, 2, 13] [0, 0, 1, 1, 1, 1] 1 1
[1, 4, 5, 8, 1[...]<jupyter_text>The above shows the scenario when there is a plank located in between nails which returns the correct value of -1 as the set of nails cannot pin down all the planks Solution:<jupyter_code>def solution(A, B, C):
    Lc = len (C)
    La = len (A)
    N = 0
    P = 0
    TP = 0
    D = [1]*La

    if min(C) > min(B) or max(C) < max(A):
        N = -1
        return N

    for i in range(Lc):
        for j in range(La):
            if (C[i] >= A[j]) & (C[i] <= B[j]) & D[j] == 1:
                P = P+1
        C[i] = 0
        if sum(D) != 0:
            N = N+1

        for k in range(P):
            D[TP+k] = 0
        TP = TP + P
        P = 0
    return N<jupyter_output><empty_output><jupyter_text>Testing:<jupyter_code>A = [1,4,5,8,9]
B = [4,5,9,10,10]
C = [4,6,7,10,12]

O = solution(A, B, C)
print(O)<jupyter_output>4
{'fraction_non_alphanumeric': 0.13308095515433896, 'fraction_numerical': 0.07964472917880024, 'mean_word_length': 2.405552801189886, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 17, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '480595', 'license': 'MIT', 'n_tokens_mistral': 2973, 'n_tokens_neox': 2523, 'n_words': 1187, 'path': 'Challenge_14_2.ipynb', 'repo_name': 'SanthikaEvo/Codility-Coding-Challenges', 'size': '6879'}