fork download
  1. from functools import partial
  2. from operator import le
  3.  
  4.  
  5.  
  6. class Stack:
  7.  
  8. def __init__(self):
  9. self.stack = []
  10.  
  11. def IsEmpty(self):
  12. return not self.stack
  13.  
  14. def Peek(self):
  15. return self.stack[-1]
  16.  
  17. def Pop(self):
  18. return self.stack.pop()
  19.  
  20. def Push(self, value):
  21. self.stack.append(value)
  22.  
  23.  
  24.  
  25. class Queue:
  26.  
  27. def __init__(self):
  28. self.popper = Stack()
  29. self.pusher = Stack()
  30.  
  31. def Dequeue(self):
  32. while not self.pusher.IsEmpty():
  33. self.popper.Push(self.pusher.Pop())
  34. return self.popper.Pop()
  35.  
  36. def Enqueue(self, value):
  37. while not self.popper.IsEmpty():
  38. self.pusher.Push(self.popper.Pop())
  39. self.pusher.Push(value)
  40.  
  41. def IsEmpty(self):
  42. return self.popper.IsEmpty() and self.pusher.IsEmpty()
  43.  
  44.  
  45.  
  46. esthetic = set()
  47.  
  48. q = Queue()
  49.  
  50. for n in range(1, 10):
  51. q.Enqueue(n)
  52.  
  53. while not q.IsEmpty():
  54. n = q.Dequeue()
  55. if n < 10000 and n not in esthetic:
  56. esthetic.add(n)
  57. d = n % 10
  58. if d != 0:
  59. q.Enqueue(n * 10 + d - 1)
  60. if d != 9:
  61. q.Enqueue(n * 10 + d + 1)
  62.  
  63.  
  64.  
  65. print(*sorted(filter(partial(le, 1000), esthetic)), sep="\n")
Success #stdin #stdout 0.04s 9240KB
stdin
Standard input is empty
stdout
1010
1012
1210
1212
1232
1234
2101
2121
2123
2321
2323
2343
2345
3210
3212
3232
3234
3432
3434
3454
3456
4321
4323
4343
4345
4543
4545
4565
4567
5432
5434
5454
5456
5654
5656
5676
5678
6543
6545
6565
6567
6765
6767
6787
6789
7654
7656
7676
7678
7876
7878
7898
8765
8767
8787
8789
8987
8989
9876
9878
9898