Created
June 23, 2017 22:03
-
-
Save andrew-medvedev/765a601980c920347949d94c67bd628c to your computer and use it in GitHub Desktop.
ABCO interview solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Problem solution: http://www.geeksforgeeks.org/abco-interview-experience-set-4/ | |
### (Second round) | |
N = 3 | |
print('Solving problem for ABCO interview with N=%d' % N) | |
split_digits = lambda x: [int(_x) for _x in str(x)] | |
steps = 0 | |
for i in range(10 ** (N - 1), 10 ** N - 1): | |
digits = split_digits(i) | |
is_ok = True | |
for d in range(1, len(digits)): | |
steps += 1 | |
if digits[d] <= digits[d - 1]: | |
is_ok = False | |
if is_ok: | |
print(i) | |
print('Done by method 1 with %d steps' % steps) | |
steps = 0 | |
ans2 = [] | |
if N >= 9: | |
steps += 1 | |
print('123456789') | |
elif N <= 0: | |
steps += 1 | |
print('0') | |
else: | |
def increment_digit(cd, ix): | |
global steps | |
steps += 1 | |
if cd[ix] == 9 - (N - ix - 1): | |
cd[ix] = increment_digit(cd, ix - 1) + 1 | |
else: | |
cd[ix] += 1 | |
return cd[ix] | |
cur_dts = [int(x) for x in range(1, N + 1)] | |
max_possible = sum([x for x in range(9, 1, -1)][:N]) | |
while True: | |
print(''.join([str(x) for x in cur_dts])) | |
if sum(cur_dts) == max_possible: | |
break | |
increment_digit(cur_dts, N - 1) | |
print('Done by method 2 with %d steps' % steps) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment