-2

I'm trying to to find number that equals or is greater than N and divisible by 3 and only consists of 0 and 1

Here is my code:

for x in range(1, 11):
    p = x**10 # power x by 10
    print(x, p) # output number itself and power result

At this point I'm stuck: We need to find number equals or greater than N divisible by 3 consists only of only 0 and 1.

If i try:

while True:
   try:
       N = int(input('Enter number from 1 to 10: '))
   except ValueError:
       print("That's not a number!")
       pass
   else:
        if 1 <= N < 10:
            print('Everythig is OK, you entered number in 1 to 10 range')
            # start variant 1
            found = False
            i = N
            while not found:
                if i % 3 == 0:
                    try:
                        dummy = int(str(i), 2)
                        print(i)
                        found = True
                    except:
                        pass
                i += 1
            # end variant 1
            pass
        else:
           print('Out of range. Try again')
           pass

result entering any number in range is 111 Same result returns:

import itertools

while True:
   try:
       N = int(input('Enter number from 1 to 10: '))
   except ValueError:
       print("That's not a number!")
       pass
   else:
        if 1 <= N < 10:
            print('Everythig is OK, you entered number in 1 to 10 range')    
            # start variant 2            
            ints = itertools.count(N + 1)
            filtered = filter(lambda x: set(str(x)) <= {"0", "1"}, ints)
            filtered = filter(lambda x: not x % 3, filtered)
            result = next(filtered)
            print(result)            
            # end variant 2
            pass
        else:
           print('Out of range. Try again')
           pass

if we change condition of numbers list if 1 <= N < 10 to if 1 <= N < 1010** both variants are correct.

P.s. if we enter e.g. 234234234 number algorithm almost died. Less numbers it founds faster )) Thank you for help!

BKI
  • 43
  • 1
  • 10
  • Can you use regex as shown here? http://stackoverflow.com/questions/10992279/regex-filter-numbers-divisible-by-3 – happyvirus Nov 20 '14 at 07:37
  • You're going to have to experiment with a much larger input range. These are the first 10 numbers starting from 1 that meet your criteria. See if you can notice a pattern. `[111, 1011, 1101, 1110, 10011, 10101, 10110, 11001, 11010, 11100]` – Dunes Nov 20 '14 at 09:38

2 Answers2

2

Let's take it step by step:

generate all integers larger than N:

ints = itertools.count(N + 1)

keep only those whose decimal representation consists of 0's and 1's:

filtered = filter(lambda x: set(str(x)) <= {"0", "1"}, ints)

keep inly those devisible by 3:

filtered = filter(lambda x: not x % 3, filtered)

take the first of these:

result = next(filtered)

And you're done!

P.S. Welcome to the world of functional programming, made beautiful by Python :)

Dima Tisnek
  • 11,241
  • 4
  • 68
  • 120
  • 2
    Of course, if you wanted to be truly efficient, you'd figure out a way to generate integers from 0's and 1's only, divisibility is easy, as you only need to count 1's: `for x in range(...): for y in range(...): int(mix("0" * x, "111" * y))` – Dima Tisnek Nov 20 '14 at 07:50
  • 1
    You can take advantage of that fact that only 0 and 1 are allowed (base 2) to make creation of the relevant numbers much quicker. `zero_one_digits_only = map(lambda x: int("{:b}".format(x)), ints)`. That is, get a string of the binary representation of the number and then parse that string in base 10. – Dunes Nov 20 '14 at 08:23
0

Assuming you already have N defined,

i = N
while True:
    if i % 3 == 0:
        try:
            dummy = int(str(i), 2)
            print i
            break
        except:
            pass
    i += 1

int(str(i), 2) raises an exception if the number is not in binary

There's a more efficient way that involves generating all the numbers that contain only 1's and 0's that are above N, but I don't really think you need that level of optimization.

Vedaad Shakib
  • 739
  • 7
  • 20