Skip to content

Instantly share code, notes, and snippets.

@motatoes
Created October 9, 2019 22:40
Show Gist options
  • Save motatoes/87b4e2269f53d09729ee3830494aa8f3 to your computer and use it in GitHub Desktop.
Save motatoes/87b4e2269f53d09729ee3830494aa8f3 to your computer and use it in GitHub Desktop.
import math
import sys
# a = 999
# b = 1234
# 901
# 123
# a = 881
# 892
# the idea is to loop over all digits of a and for the ith digit we find a
# number greater than or equal to a number. if at any point we find a number larger
# than the current digit, then we start checking from 0.
# Once we generate this if it is valid we print it. If it is not valid, we do a check
# on each number within the range of digits larther than a. If one exists we print it
# otherwise we print -1
a, b = map(int, input().split())
d = {}
higherUsed = False
ln = len(str(a))
i = 0
res = ""
sa = str(a)
sb = str(b)
while i < ln:
c = int(sa[i])
lowerLimit = c
if higherUsed:
lowerLimit = 0
nextDigit = -1
for f in range(lowerLimit, 10):
if f not in d:
nextDigit = f
break
if nextDigit == -1:
break
if nextDigit > c:
higherUsed = True
d[nextDigit] = True
res += str(nextDigit)
i += 1
if i == ln and int(res) >= a and int(res) <=b:
print(res)
sys.exit(0)
# check for |b| > |a|
if len(sa) != len(sb):
ourLength = len(sa) + 1
dd = 0
if ourLength == 2:
dd = 10
elif ourLength == 3:
dd = 102
elif ourLength == 4:
dd = 1023
elif ourLength == 5:
dd = 10234
elif ourLength == 6:
dd = 102345
if dd >= a and dd <= b:
print(dd)
sys.exit(0)
print(-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment