Advertisement
Grork

[TopCoders] ConsecutiveOnes

Mar 30th, 2017
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 0
  1. import argparse
  2.  
  3. def isValid(n, k):
  4.     if ('1' * int(k) in str(n)):
  5.         return True
  6.     return False
  7.  
  8. # Args
  9. # Usage: python thisfile.py --num=5 --count=2
  10. # Result: 6 (0b00000110)
  11. parser = argparse.ArgumentParser(description='ConsecutiveOnes')
  12. requiredNamed = parser.add_argument_group('Required arguments')
  13. requiredNamed.add_argument('--num', help='Your number', required=True)
  14. requiredNamed.add_argument('--count', help='How many times 1 need to be repeated', required=True)
  15. args = parser.parse_args()
  16.  
  17. # Let's rock
  18. num = int(args.num)
  19. binNum = bin(num)
  20. while isValid(binNum, args.count) is False:
  21.     num += 1
  22.     binNum = bin(num)
  23.  
  24. print('Your number is %d' % (num))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement