## 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

## 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

## 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

## 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

## 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

## 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

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

Github Checkio Python Find Friends

Checkio Problem Statement

**My way**:

Here I created two list, one with the first friend, and one with the second. Then, passing repeat this for example 10 times to check all the possible friends. Then, split each element of the network and store in a variable called, people, then check if each of the elements of the friends1 are in people[0] or people[1], if they are, add people[1] or people[0] respectively. The same with the friends2 list. Then at the end, only check that first friend are in friends2 list or second friend are in friends1 list.

Here **my code**:

def check_connection(network, first, second):
areConnected = False
friend1 = [first]
friend2 = [second]
for a in range(10):
for i in range(len(network)):
people = network[i].split('-')
print(friend1)
print(friend2)
for a in range(len(friend1)):
if friend1[a] in people[0]:
if people[1] not in friend1:
friend1.append(people[1])
if friend1[a] in people[1]:
if people[0] not in friend1:
friend1.append(people[0])
for a in range(len(friend2)):
if friend2[a] in people[0]:
if people[1] not in friend2:
friend2.append(people[1])
if friend2[a] in people[1]:
if people[0] not in friend2:
friend2.append(people[0])
if first in friend2 or second in friend1:
areConnected = True
if areConnected:
return "Connected"
return "Not connected"
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
check_connection(("dr101-mr99","mr99-out00","dr101-out00","scout1-scout2",
"scout3-scout1","scout1-scout4","scout4-sscout","sscout-super",),"super","scout2")

Thank you very much

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

Github Codeforces Python Problem A Little Pony and Crystal Mine

Codeforces Problem Statement

**My way**:

Here only we need to go adding 2x”D” in each line until we reach the mid one and then substract 2x”D” until the end. And in the beginning and in the end of each line, add “*” first * 1, and add +1 until the middel and then substract -1 until the end. At the end, print the diamond line by line.

Here **my code**:

def main():
d = "D"
star ="*"
n = int(input())
mid = int(n / 2)
times = int(n / 2)
timesD = 1
diamond = []
for i in range(n):
if i < mid:
temp = (star * times) + (d * timesD) + (star * times)
diamond.append(temp)
times -= 1
timesD += 2
elif i == mid:
temp = (d * timesD)
diamond.append(temp)
else:
times += 1
timesD -= 2
temp = (star * times) + (d * timesD) + (star * times)
diamond.append(temp)
for i in range(len(diamond)):
print(diamond[i])
if __name__ == "__main__":
main()

Thank you very much

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

Github HackerRank Python Cavity Map

HackerRank Problem Statement

**My way**:

Here, using a couple of for loops, check the inner part of the board, and check if an element is bigger than the element in the top, right, bottom and left sides. If it is, change for an “X”, otherwise, let the element equal. At the end, return the board with the “X” changed.

Here **my code**:

def main():
length = int(input())
board = []
result = []
for i in range(length):
board.append(input())
result.append(board[0])
for i in range(1, len(board) - 1):
result.append(board[i][0])
for a in range(1, len(board[i]) - 1):
mid = int(board[i][a])
top = int(board[i - 1][a])
right = int(board[i][a + 1])
bottom = int(board[i + 1][a])
left = int(board[i][a - 1])
if mid > top and mid > right and mid > bottom and mid > left:
result[i] += "X"
else:
result[i] += board[i][a]
result[i] += board[i][-1]
if length != 1:
result.append(board[-1])
for i in range(len(result)):
print(result[i])
if __name__ == "__main__":
main()

Thank you very much

## Hi, here a new problem I’ve solved right now!

Github HackerRank Python Find Digits

HackerRank Problem Statement

**My way**:

Here only need to check if the number is divided by each of the elements on it. Then, add +1 to the result if the whole number can be divided by each element. Then return result in each case.

Here **my code**:

def main():
cases = int(input())
for i in range(cases):
result = 0
number = input()
n = int(number)
for a in range(len(number)):
if number[a] != "0":
if n % int(number[a]) == 0:
result += 1
print(result)
if __name__ == "__main__":
main()

Thank you very much