Lucky Number – Code Fight

By December 31, 2015 No Comments

So Here is another challenge from the Code Fights, that I solved today. The problem is as following:

Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers which decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.

Petya loves tickets very much. As we know, each ticket has a number that is a positive integer. Its length equals n. Petya calls a ticket lucky if the ticket’s number is a lucky number, and the sum of the digits in the first half (the sum of the first n / 2 digits) equals the sum of digits in the second half (the sum of the last n / 2 digits). Check if the given ticket is lucky.


LuckyTicket(2, "47") = "NO"

  • [input] integer n
    • The number of digits. n is always even, 2 ≤ n ≤ 50
  • [input] string s
    • The number of the ticket.
  • [output] string
    • "YES" if the given number is lucky, "NO" otherwise.

The solution to the above problem is as following:

**NOTE: I have used ‘//’ instead of simple ‘/’ is because in Python 3 if you use ‘/’ to divide a number it will return a float instead of integer. In order to force the compiler to return an integer during a division we need to use ‘//’. Hope this helps.

Credit for the creation of Problem: Code Fights


Author Naveen

A Computer Science graduate student, a programmer, photographer, reader and blogger

More posts by Naveen

Leave a Reply