How many countries are there in this rectangular map?

This is my task3 assessment. You need to count how many countries there are in this rectangular map.


I answered this question in Python programming language:

def color (val, B, A, x, y): 
    if x < 0 or x >= len(A): 
        return 
    if y < 0 or y >= len(A[0]): 
        return 
    if not B[x][y] : 
        return 
    if A[x][y] != val: 
        return 
    B[x][y] = False 
    color(val, B, A, x + 1, y) 
    color(val, B, A, x - 1, y) 
    color(val, B, A, x, y - 1) 
    color(val, B, A, x, y + 1) 

def solution (A): 
    B = [[True for x in A[0]] for y in A] 
    count = 0 
    for x in xrange(0, len(A)): 
        for y in xrange(0, len(A[x])): 
            if not B[x][y]: 
                continue; 
            count += 1 
            color(A[x][y], B, A, x, y) 
    return count 

In this assessment, I did three tasks within 245 minutes. However, not all of them were good enough, especially in task2. Although I really don’t like this kind of assessment, if I want to obtain any interview opportunity from good companies, I have to keep practicing it…

(Visited 2,937 times, 1 visits today)

“How many countries are there in this rectangular map?” 有 1 則迴響.

迴響已被關閉。