Posted 16 mins 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 6 hours 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 7 hours 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

Posted 13 hours ago

Checkio Python Find Friends

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

Posted 2 days ago

Codeforces Python Problem A Little Pony and Crystal Mine

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

Posted 2 weeks ago

HackerRank Python Cavity Map

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

Posted 3 weeks ago

HackerRank Python Find Digits

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

Posted 3 weeks ago

HackerRank Python Game Of Thrones 1

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

Github HackerRank Python Game Of Thrones 1
HackerRank Problem Statement

My way:
First I need to get the number that each letter appear in the text. Then, get the number of odds and even times that a letter appear. Then, if even is bigger or lesser than 0 and odd is lesser or equal to 1, return “YES”, otherwise, return “NO”.

Here my code:

def main():
	abc = "abcdefghijklmnopqrstuvwxyz"
	abcList = [0] * 26
	text = input()
	for i in range(len(text)):
		abcList[abc.index(text[i])] += 1
	odd = 0
	even = 0
	for i in range(len(abcList)):
		if abcList[i] != 0 and abcList[i] % 2 == 0:
			even += 1
		elif abcList[i] != 0 and abcList[i] % 2 == 1:
			odd += 1
	if even >= 0 and odd <= 1:
		print("YES")
	else:
		print("NO")

if __name__ == "__main__":
	main()

Thank you very much

Posted 3 weeks ago

HackerRank Python MazimizingXOR

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

Github HackerRank Python MazimizingXOR
HackerRank Problem Statement

My way:
Here only using a couple of for loops, with all the combinations of two elements, get the max between result and a ^ b. Then, return result.

Here my code:

def main():
	l = int(input())
	r = int(input())
	result = 0
	for i in range(l, r + 1):
		for a in range(i, r + 1):
			result = max(result, i ^ a)
	print(result)

if __name__ == "__main__":
	main()

Thank you very much

Posted 3 weeks ago

HackerRank Python The Love Letter Mistery

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

Github HackerRank Python The Love Letter Mistery
HackerRank Problem Statement

My way:
Here I’ve used a string initialized like the alphabet in lowercase, in order to check if the index of a letter if bigger than the other. Then, starting from 0 to len(word) / 2, add to the result the absolute value of substraction of both indices. Continue doing this until the middel of the word. Then return result.

Here my code:

def main():
	cases = int(input())
	for a in range(cases):
		abc = "abcdefghijklmnopqrstuvwxyz"
		result = 0
		word = input()
		j = -1
		for i in range(int(len(word) / 2)):
			result += abs(abc.index(word[i]) - abc.index(word[j]))
			j -= 1
		print(result)

if __name__ == "__main__":
	main()

Thank you very much

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