CAKP05 - Editorial


Halry vs Harry

Author: Halry Bhalodia






Lucky numbers are numbers that only consists of lucky digits, i.e. 4 or 7. For example: 4, 7, 447, 47474 are lucky and 437, 147 are not.

Halry likes Lucky number whereas Harry doesn’t. Harry likes nearly lucky numbers.

Suppose you have an integer M. If the number of lucky digits in M is a lucky number and sum of the digits of the number M is also lucky then, the number is said to be nearly lucky.

Halry decides to give some numbers and asks harry to find how many numbers are lucky, nearly lucky or none.

Note: If a number is both lucky and nearly lucky, consider it as only lucky.


This was a simple implementation problem in which you need to do as the question says.

To check lucky number, you need to check all digits and if any of them is neither 4 nor 7, then the number can not be lucky.

To check nearly lucky number, count of digit 4 + count of digit 7 in the number should be lucky and the sum of all digits should also be lucky.


Setter's Solution
def isLucky(n):
	if n.count('4') + n.count('7') == len(n):
		return True
	return False

def isNearlyLucky(n):
	lucky_digits = n.count('4') + n.count('7')
	sum_of_digits = 0

	for ch in n:
		sum_of_digits += int(ch)

	if isLucky(str(lucky_digits)) and isLucky(str(sum_of_digits)):
		return True

	return False	

def solve(n, a):
	lucky = 0
	nearly_lucky = 0
	no = 0

	for item in a:
		item = str(item)

		if isLucky(item):
			lucky += 1
		elif isNearlyLucky(item):
			nearly_lucky += 1
			no += 1

	print(lucky, nearly_lucky, no)

n = int(input())
a = list(map(int, input().split()))

solve(n, a)