ABC161D Lunlun Number with python3

ABC161D Lunlun Number

Correct code

import queue
q = queue.Queue()
for i in range(1,10):
    q.put(i)
K = int(input())
for k in range(K):
    x =q.get()
    last = x % 10
    if last != 0:
        q.put(10*x + last - 1)
    q.put(10*x + last)
    if last != 9:
        q.put(10*x + last + 1)
print (x)

Way of thinking

A new number of run-runs is generated by adding a number having a difference of 1 or less from the number of ones digit at the end from the number of run-runs from 1 to 9. Then, a new run-run number is generated from the generated run-run number. The process ends when the Kth run-run number is generated.

Point 1

Storing all K run-run numbers in a list would mess up memory. Therefore, if you use a queue instead of a list, you can use the get function to expel the used run-run numbers that have been generated from the memory.

Point 2

In this problem, it is necessary to distinguish between cases where the 1st place is 0, 9 or something else. I want to write the case as complete as possible. As far as embarrassing, I first wrote as follows.

if last==0:
    q.put(10*x + last)
    q.put(10*x + last + 1)
elif last==9:
    q.put(10*x + last)
    q.put(10*x + last - 1)
else:
    q.put(10*x + last)
    q.put(10*x + last - 1)
    q.put(10*x + last + 1)

To prevent this from happening, you should be aware that you do not write the same process twice. If you think about when to perform a certain process instead of writing the process for a case, the code will naturally be beautiful.

Summary

Using queue saves memory! Case classification is processed-> Think about the flow of cases!

Recommended Posts

ABC161D Lunlun Number with python3
[Python] ABC175D
Number recognition in images with Python
FizzBuzz with Python3
Scraping with Python
Scraping with Python
Python with Go
[Python] DP ABC184D
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
[Python] UnionFind ABC177D
Excel with Python
Microcomputer with Python
Cast with python
Stock number ranking by Qiita tag with python
Calculate the total number of combinations with python
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
[Python] Interval scheduling ABC103D
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Scraping with Python + PhantomJS
Drive WebDriver with python
[Python] Redirect with CGIHTTPServer
Voice analysis with python
Think yaml with python
Operate Kinesis with Python
Use DynamoDB with Python
Zundko getter with python
Handle Excel with python
Ohm's Law with Python
Run Blender with python
[Python] Binary search ABC155D
Solve Sudoku with Python
Python starting with Windows 7