Finding OuterMost Element of given array

Finding Outer Array

  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
import matplotlib.pyplot as plt


def draw_array(all_indices, solved_indices):
    x_cord = [item[0] for item in all_indices]
    y_cord = [item[1] for item in all_indices]

    sol_x_cord = [item[0] for item in solved_indices]
    sol_y_cord = [item[1] for item in solved_indices]

    plt.scatter(x_cord, y_cord, color='green')
    plt.scatter(sol_x_cord, sol_y_cord, color='red')
    
    plt.xlabel('X-axis')
    plt.ylabel('Y-axis')
    plt.title('Finding outline')
    plt.grid(True)
    plt.yticks(range(min(x_cord), max(y_cord)+1))
    plt.xticks(x_cord)
    plt.show()


# TODO: complete this
def solution(myarr):

    #remove duplicates cordinates and sort it
    mylist = sorted(list(set([tuple(item) for item in myarr])))
    final_value = list()
    
    size = len(mylist)

    x_cord = [item[0] for item in mylist]
   
    #getting starting and ending index
    max_x = max(x_cord)
    min_x = min(x_cord)

    #creating empty x_axix of given array
    x_axix = [None for _ in range(size +1)]
    
    #making index based Y element value list
    for i in range(max_x + 1):
        y_axix = list()
        for x in mylist:
            if i == x[0]:
                y_axix.append(x[1])
        x_axix[i] = y_axix

    print(x_axix)

    #just searching max and min element from given list and also selecting all element from last and first x axis 
    for i in range(max_x +1):
        tempmax = list()
        tempmin = list()

        if x_axix[i] != []:
           #selecting all first axix and last axix elements 
            if i == min_x or i == max_x:
               for y in x_axix[i]:
                    tempmin.append(i)
                    tempmin.append(y)
                    final_value.append(tempmin)
                    tempmin= list()

            else:
                #if element are 2 or more 
                if len(x_axix[i]) >=2:
                    
                    #making list again 
                    tempmax.append(i)
                    tempmax.append(max(x_axix[i]))
                    
                    tempmin.append(i)
                    tempmin.append(min(x_axix[i]))
                    
                    final_value.append(tempmin) 
                    final_value.append(tempmax) 

                #select only one element of array is given
                else:
                    tempmax.append(i)
                    tempmax.append(x_axix[i][0])

                    final_value.append(tempmax) 
    return final_value


myarr = [
    [0, 4],[1, 4],[1, 5],[1, 7], [2, 5],[2, 6],[2, 7],[2, 8],[2, 9], [4, 5], [5, 6],[5, 6], [5, 7], [6, 11], [8, 14],
    [12, 16], [2, 18], [5, 5], [6, 3], [7, 7],[7, 7],[7, 7],[7, 7],[7, 7],[7, 7],[7, 7],[7, 7],[7, 7],
    [8, 5], [5, 15], [8, 12], [9, 2], [9, 19],[3, 3], [18, 2],[18, 4],[18, 9],
]

sol = solution(myarr)

print("all arrays :{}".format(myarr))
print("outer array :{}".format(sol))

# Just Uncomment this part of code if you want to see x and y axis 
# draw_array(myarr,sol)