Posted 9 mins ago

Checkio Count Inversions problem

Hi, I’ve solved a new problem using Python!

Github
Checkio problem statement

Here only need to count the number of inversions between pairs in the given sequence.

Here my code:

def count_inversion(sequence):
    res = []
    for i in range(len(sequence)):
        res.append(sequence[i])
    count = 0
    i = 0
    while i < (len(res) - 1):
        j = i
        while res[j] > res[j + 1]:
            temp = res[j + 1]
            res[j + 1] = res[j]
            res[j] = temp
            count += 1
            j += 1
            i = -1
            if j + 1 >= len(res):
                break
        i += 1
    return count
    

if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert count_inversion((1, 2, 5, 3, 4, 7, 6)) == 3, "Example"
    assert count_inversion((0, 1, 2, 3)) == 0, "Sorted"
    assert count_inversion((99, -99)) == 1, "Two numbers"
    assert count_inversion((5, 3, 2, 1, 0)) == 10, "Reversed"

Thank you very much

Posted 54 mins ago

Checkio Boolean Algebra problem

Hi, I’ve solved a new problem using Python!

Github
Checkio problem statement

Here we only need to use boolean operators.

Here my code:

OPERATION_NAMES = ("conjunction", "disjunction", "implication", "exclusive", "equivalence")

def boolean(x, y, operation):
    if operation == OPERATION_NAMES[0]:
    	return x & y
    elif operation == OPERATION_NAMES[1]:
    	return x | y
    elif operation == OPERATION_NAMES[2]:
    	if x == 0:
    		return 1
    	else:
    		return y
    elif operation == OPERATION_NAMES[3]:
    	return x ^ y
    else:
    	if x == y:
    		return 1
    	else:
    		return 0

if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert boolean(1, 0, "conjunction") == 0, "and"
    assert boolean(1, 0, "disjunction") == 1, "or"
    assert boolean(1, 1, "implication") == 1, "material"
    assert boolean(0, 1, "exclusive") == 1, "xor"
    assert boolean(0, 1, "equivalence") == 0, "same?"

Thank you very much

Posted 24 hours ago

Checkio Find Sequence problem

Hi, I’ve solved a new problem using Python!

Github
Checkio problem statement

Here is not necessary to check the 8 directions, we only need to check top-right diagonal, right, bottom-right diagonal and down. Then, with a while loop we check if there ir more than one equal, and if we found four equals, return True, otherwise, at the end, return False.

Here my code:

def checkio(matrix):
    for row in range(len(matrix)):
        for col in range(len(matrix
)): if (col + 1 < len(matrix[0])): i = 1 count = 1 while (col + i < len(matrix[0])): if matrix
[col + i] == matrix
[col]: count += 1 i += 1 else: break if count == 4: return True if (row + 1 < len(matrix)): i = 1 count = 1 while (row + i < len(matrix)): if matrix
[col] == matrix
[col]: count += 1 i += 1 else: break if count == 4: return True if (col + 1 < len(matrix[0]) and row + 1 < len(matrix)): i = 1 count = 1 while (col + i < len(matrix[0]) and row + i < len(matrix)): if matrix
[col + i] == matrix
[col]: count += 1 i += 1 else: break if count == 4: return True if (col - 1 >= 0 and row + 1 < len(matrix)): i = 1 count = 1 while (col - i >= 0 and row + i < len(matrix)): if matrix
[col - i] == matrix
[col]: count += 1 i += 1 else: break if count == 4: return True return False if __name__ == '__main__': #These "asserts" using only for self-checking and not necessary for auto-testing assert checkio([ [1, 2, 1, 1], [1, 1, 4, 1], [1, 3, 1, 6], [1, 7, 2, 5] ]) == True, "Vertical" assert checkio([ [7, 1, 4, 1], [1, 2, 5, 2], [3, 4, 1, 3], [1, 1, 8, 1] ]) == False, "Nothing here" assert checkio([ [2, 1, 1, 6, 1], [1, 3, 2, 1, 1], [4, 1, 1, 3, 1], [5, 5, 5, 5, 5], [1, 1, 3, 1, 1] ]) == True, "Long Horizontal" assert checkio([ [7, 1, 1, 8, 1, 1], [1, 1, 7, 3, 1, 5], [2, 3, 1, 2, 5, 1], [1, 1, 1, 5, 1, 4], [4, 6, 5, 1, 3, 1], [1, 1, 9, 1, 2, 1] ]) == True, "Diagonal"

Thank you very much

Posted 1 day ago

Checkio The Hamming Distance problem

Hi, I’ve solved a new problem using Python!

Github
Checkio

Here only need to use the bitwise operator ^ in order to get the corrert integer. Then, convert to bits and count the number od ‘1’s in there and return there.

Here my code:

def checkio(n, m):
    count = 0
    res = n ^ m
    for element in str('{0:08b}'.format(res)):
        if element == '1':
            count += 1
    return count


if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert checkio(117, 17) == 3, "First example"
    assert checkio(1, 2) == 2, "Second example"
    assert checkio(16, 15) == 5, "Third example"

Thank you very much

Posted 3 days ago

Checkio Letter Queue problem

Hi, I’ve solved a new problem using Python!

Github
Checkio

Here I’ve used a list as a queue. Then, split the commands, and if it’s a POP, using pop() we remove the first element of the list, the index 0. At the end, return the joined elements from the list in a single string.

Here my code:

def letter_queue(commands):
    queue = []
    for i in range(len(commands)):
        splitt = commands[i].split(" ")
        if (splitt[0] == "PUSH"):
            queue.append(splitt[1])
        else:
            if (len(queue) > 0):
                element = queue.pop(0)
    return "".join(queue)

if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert letter_queue(["PUSH A", "POP", "POP", "PUSH Z", "PUSH D", "PUSH O", "POP", "PUSH T"]) == "DOT", "dot example"
    assert letter_queue(["POP", "POP"]) == "", "Pop, Pop, empty"
    assert letter_queue(["PUSH H", "PUSH I"]) == "HI", "Hi!"
    assert letter_queue([]) == "", "Nothing"

Thank you very much

Posted 3 days ago

Checkio Brackets problem

Hi, I’ve solved a new problem using Python!

Github Checkio Brackets problem
Checkio Problem Statement

Here, I’ve used a list like a queue. I store each new element, then, check each new one, and if it’s the opposite to the last element of the list, remove the last one in the list, otherwise, add the new one at the end of the list. At the end, only check if the list is empty or not, if it’s empty, return True, otherwise, return False.

Here my code:

def checkio(expression):
    a = ["(", ")"]
    b = ["{", "}"]
    c = ["[", "]"]
    last = []
    countList = -1
    for i in range(len(expression)):
        if (expression[i] == "(" or expression[i] == ")" or expression[i] == "{" or expression[i] == "}" or expression[i] == "[" or expression[i] == "]"):
            if (len(last) == 0):
                last.append(expression[i])
                countList += 1
            else:
                if (expression[i] in a and last[countList] in a):
                    last.pop(countList)
                    countList -= 1
                elif (expression[i] in b and last[countList] in b):
                    last.pop(countList)
                    countList -= 1
                elif (expression[i] in c and last[countList] in c):
                    last.pop(countList)
                    countList -= 1
                else:
                    last.append(expression[i])
                    countList += 1
            
    if (len(last) == 0):
        return True
    else:
        return False

#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
    assert checkio("((5+3)*2+1)") == True, "Simple"
    assert checkio("{[(3+1)+2]+}") == True, "Different types"
    assert checkio("(3+{1-1)}") == False, ") is alone inside {}"
    assert checkio("[1+1]+(2*2)-{3/3}") == True, "Different operators"
    assert checkio("(({[(((1)-2)+3)-3]/3}-3)") == False, "One is redundant"
    assert checkio("2+3") == True, "No brackets, no problem"

Thank you very much

Posted 3 days ago

Checkio Moore Neighbourhood problem

Hi, I’ve solved this problem using Python!

Github Checkio Moore Neighbourhood problem
Checkio Problem Statement

Here we only need to count the number of neightbours given a grid and two points (row and column).

Here my code:

def count_neighbours(grid, row, col):
    neig = 0
    if (col - 1 >= 0):
        if (grid
[col - 1] == 1): neig += 1 if (col - 1 >= 0 and row - 1 >= 0): if (grid
[col -1] == 1): neig += 1 if (row - 1 >= 0): if (grid
[col] == 1): neig += 1 if (col + 1 < len(grid[0]) and row - 1 >= 0): if (grid
[col + 1] == 1): neig += 1 if (col + 1 < len(grid[0])): if (grid
[col + 1] == 1): neig += 1 if (col + 1 < len(grid[0]) and row + 1 < len(grid)): if (grid
[col + 1] == 1): neig += 1 if (row + 1 < len(grid)): if (grid
[col] == 1): neig += 1 if (col - 1 >= 0 and row + 1 < len(grid)): if (grid
[col - 1] == 1): neig += 1 return neig if __name__ == '__main__': #These "asserts" using only for self-checking and not necessary for auto-testing assert count_neighbours(((1, 0, 0, 1, 0), (0, 1, 0, 0, 0), (0, 0, 1, 0, 1), (1, 0, 0, 0, 0), (0, 0, 1, 0, 0),), 1, 2) == 3, "1st example" assert count_neighbours(((1, 0, 0, 1, 0), (0, 1, 0, 0, 0), (0, 0, 1, 0, 1), (1, 0, 0, 0, 0), (0, 0, 1, 0, 0),), 0, 0) == 1, "2nd example" assert count_neighbours(((1, 1, 1), (1, 1, 1), (1, 1, 1),), 0, 2) == 3, "Dense corner" assert count_neighbours(((0, 0, 0), (0, 1, 0), (0, 0, 0),), 1, 1) == 0, "Single"

Thank you very much

Posted 2 months ago

Checkio Python Number Base

Hi, here a new problem I’ve solved using Python!

Github Checkio Python Number Base
Checkio Problem Statement

My way:
Here is so simple like using int() method, passing the second parameter the base that we need to convert the string. I’ve used a try/except block because the problem says that if you can’t convert something, you need to return -1, then, as int() method throw an exception if it can’t convert something, I used this except block to return -1.

Here my code:

def checkio(str_number, radix):
    try:
        return int(str_number, radix)
    except:
        return -1

#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
    checkio("AF", 16) == 175, "Hex"
    checkio("101", 2) == 5, "Bin"
    checkio("101", 5) == 26, "5 base"
    checkio("Z", 36) == 35, "Z base"
    checkio("AB", 10) == -1, "B > A > 10"

Thank you very much

Posted 2 months ago

Checkio Python Roman Numerals

Hi, here a new problem I’ve solved using Python!

Github Checkio Python Roman Numerals
Checkio Problem Statement

My way:
Here we only need the translate numbers fot the thousands, hundreds, tens and units. Then, translate the number into Roman using the thousands, hundreds, tens and units and return the string result.

Here my code:

def checkio(data):
    mil = ["M", "MM", "MMM"]
    cents = ["C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM", ""]
    tens = ["X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC", ""]
    units = ["I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", ""]
    roman = ""
    if len(str(data)) == 4:
        roman += mil[int(str(data)[0]) - 1]
        roman += cents[int(str(data)[1]) - 1]
        roman += tens[int(str(data)[2]) - 1]
        roman += units[int(str(data)[3]) - 1]
    if len(str(data)) == 3:
        roman += cents[int(str(data)[0]) - 1]
        roman += tens[int(str(data)[1]) - 1]
        roman += units[int(str(data)[2]) - 1]
    if len(str(data)) == 2:
        roman += tens[int(str(data)[0]) - 1]
        roman += units[int(str(data)[1]) - 1]
    if len(str(data)) == 1:
        roman += units[int(str(data)[0]) - 1]
    return roman

if __name__ == '__main__':
    checkio(3888) == 'MMMDCCCLXXXVIII', '3888'

Thank you very much

Posted 2 months ago

Checkio Python Feed Pigeons

Hi, here a new problem I’ve solved using Python!

Github Checkio Python Feed Pigeons
Checkio Problem Statement

My way:
Here I created a list to store the pigeons. Then, start with step = 1, with a while loop, while still food remains, add the number of steps (pigeons) to the list, and start to give them food, if bird[i] < 1, add 1, and so on.. until no more food. This way is easy, because at the end, we only need to return the count of 1s in the list.

Here my code:

def checkio(number):
    step = 1
    birds = []
    while number > 0:
        birds += [0] * step
        for i in range(len(birds)):
            if number == 0:
                break
            if birds[i] < 1:
                birds[i] += 1
            number -= 1
        step += 1
    print(birds.count(1))


if __name__ == '__main__':
    checkio(5)

Thank you very much

Visit Us On TwitterVisit Us On FacebookVisit Us On LinkedinCheck Our Feed