How many countries are there in this rectangular map?

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.

codility_q3

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 1,159 time, 3 visit today)
Facebooktwittergoogle_plusredditpinterestlinkedinmail

One thought on “How many countries are there in this rectangular map?

  1. Thanks a lot for your sharing.
    Can u share please task1 and task2?
    Good luck with and hope good company would find u.

Comments are closed.

Comments are closed.