fork(8) download
  1. def ascending( na, nb ):
  2. assert nb>=na
  3. # split each number into a list of digits
  4. a = list( int(x) for x in str(na))
  5. b = list( int(x) for x in str(nb))
  6.  
  7. d = len(b) - len(a)
  8.  
  9. # if both numbers have different length add leading zeros
  10. if d>0:
  11. a = [0]*d + a # add leading zeros
  12. assert len(a) == len(b)
  13. n = len(a)
  14.  
  15.  
  16. # check if the initial value has increasing digits as required,
  17. # and fix if necessary
  18. for x in range(d+1, n):
  19. if a[x] <= a[x-1]:
  20. for y in range(x, n):
  21. a[y] = a[y-1] + 1
  22. break
  23.  
  24. res = [] # result set
  25.  
  26. while a<=b:
  27. # if we found a value and add it to the result list
  28. # turn the list of digits back into an integer
  29. if max(a) < 10:
  30. res.append( int( ''.join( str(k) for k in a ) ) )
  31.  
  32. # in order to increase the number we look for the
  33. # least significant digit that can be increased
  34. for x in range( n-1, -1, -1): # count down from n-1 to 0
  35. if a[x] < 10+x-n:
  36. break
  37. # digit x is to be increased
  38. a[x] += 1
  39. # all subsequent digits must be increased accordingly
  40. for y in range( x+1, n ):
  41. a[y] = a[y-1] + 1
  42.  
  43. return res
  44.  
  45. print( ascending( 5000, 9000 ) )
  46.  
Success #stdin #stdout 0.1s 10104KB
stdin
Standard input is empty
stdout
[5678, 5679, 5689, 5789, 6789]