adplus-dvertising

Check if given integer has only 1s and 0s in it and not another number

Asked 2 days ago
Viewed 14 times

Example:

  • num_1 = 1010
  • num_2 = 1234
  • num_3 = 1034

Here num_1 is the only valid number since it has both 1s and 0s.

num_2 is invalid because it does not have any 1s or 0s.

num_3 should also be invalid since it has digits other than 0s and 1s which are 2 and 4.

Note: you cannot use any for loops or while loop or any python function or method or string methods or built-in python methods all you can use are if elif and else.

How can we solve this to find the number which has only 1s and 0s in it?

The given integer can be in between the range 0 and 10000.

asked 2 days ago

Correct Answer

Given the significant set of restrictions:

if num == 0: return True
if num == 1: return True
if num == 2: return False
if num == 3: return False
if num == 4: return False
if num == 5: return False
if num == 6: return False
if num == 7: return False
if num == 8: return False
if num == 9: return False
if num == 10: return True
if num == 11: return True
if num == 12: return False
# and so on...
answered 2 days ago