I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)

Introduction

Let's solve the APG problem that exists in AtCoder with Python. This is a beginner who is new to Python, so it would be greatly appreciated if you could tell me if there is something like "There is a better way to write!". Also, the reason why only 4.01 and 4.04 are because I heard that these are the only parts that can be done with Python. C ++ can be done in various ways and it's amazing ...

4.01.include directive

It means "import" in Python! #Include <(filename)> in C ++ means ʻimport (module name)` in Python.

In C ++, it seems that output operation cannot be performed unless #include <bits / stdc ++. H> is inserted. In Python you can print () without having to include a module.

Also, in C ++, you can include your own source file and use it in another source code. Well, in Python, how do you get the source of another file ... find out.

It's the same way ... Just bring the file name without the "~ .py (that is, extension)" of the file you want to inport in the form of ʻimport (file name without the extension)`. With usual.

[Reference] include directive

a.py


import b

print(b.f(10)) #100

b.py


def f(x):
    return x*x #Just square x

b.py is imported a.py is the same as the code below.

def f(x):
    return x*x

print(f(10)) #100

Even when importing a module, I wasn't really aware of the image of bringing the function from the source file in which various functions called "module name.py" are written. Good study.

4.04. Iterator

What is an iterator ...? I don't know at all. It looks like a list, but I wonder what this is. Used when doing sort () ...? why……? What about the original array ...?

all right! !! !! !! !! !! Doesn't it mean that sort () does not sort the original array, but sorts a copy of the original array called an iterator ...? sort () doesn't sort the original array. Yeah, but what's the difference between an iterator and an array?

[** The good points of iterators **]

--Data structures other than arrays can be handled and organized in the same way.

I see, in other words, if you use an iterator for both array and dictionary type, you can operate the position with the same operation ...? It's a little difficult ... the image is difficult ...

Let's make an iterator with list and dict ...

#Create with list
list = [1,2,3,4,5,]
iter_list = iter(list)

#Created with dict
dict = {"first":1,"second":2,"third":3,"fourth":4,"fifth":5}
iter_dict = iter(dict)

for i in iter_list:
    print(i) # 1 2 3 4 5

for i in iter_dict:
    print(i) # first second third fourth fifth

Well, if it is dict, the key will be output. Did you make a mistake? that.

I'm going to get stuck in a quagmire, so for the time being

--Iterators can copy container elements such as list and dict and treat them in the same way (verification required) (iterators are not yet well understood) --It is used for sort ().

I will read the day again ...

Recommended Posts

I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
I want to solve APG4b with Python (Chapter 2)
I want to handle optimization with python and cplex
I want to work with a robot in python.
I want to debug with Python
I wanted to solve ABC160 with Python
I want to analyze logs with Python
I want to play with aws with python
I wanted to solve ABC159 in Python
I wanted to solve ABC172 with Python
[Python / AWS Lambda layers] I want to reuse only module in AWS Lambda Layers
I want to improve efficiency with Python even in an experimental system (2) RS232C and pySerial
I want to do Dunnett's test in Python
I want to use MATLAB feval with python
I wanted to solve NOMURA Contest 2020 with Python
I want to create a window in Python
I want to make a game with Python
I want to merge nested dicts in Python
I want to use Temporary Directory with Python2
#Unresolved I want to compile gobject-introspection with Python3
I want to write to a file with Python
I want to display the progress in Python!
Environment maintenance made with Docker (I want to post-process GrADS in Python
I want to do a monkey patch only partially safely in Python
I want to write in Python! (1) Code format check
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to transition with a button in flask
I want to write in Python! (2) Let's write a test
Even in JavaScript, I want to see Python `range ()`!
I tried to solve the soma cube with python
I want to randomly sample a file in Python
I want to solve Sudoku (Sudoku)
I was addicted to scraping with Selenium (+ Python) in 2020
I want to inherit to the back with python dataclass
I want to write in Python! (3) Utilize the mock
I want to AWS Lambda with Python on Mac!
[ML Ops] I want to do multi-project with Python
I tried to solve the problem with Python Vol.1
I want to get the file name, line number, and function name in Python 3.4
I want to use the R dataset in python
I want to run a quantum computer with Python
I want to do something in Python when I finish
I want to manipulate strings in Kotlin like Python!
I tried to solve AOJ's number theory with Python
[For beginners in competition professionals] I tried to solve 40 AOJ "ITP I" questions with python
I want to replace the variables in the python template file and mass-produce it in another file.
I want to write a triple loop and conditional branch in one line in python
I want to extract only pods with the specified label using Label Selector in Client-go
Design and test Verilog in Python only with Veriloggen and cocotb.
I want to specify another version of Python with pyvenv
I want to be able to analyze data with Python (Part 1)
I want to do something like sort uniq in Python
I want to be able to analyze data with Python (Part 4)
I want to be able to analyze data with Python (Part 2)
I want to automatically attend online classes with Python + Selenium!
[Python] I want to use the -h option with argparse
I want to know the features of Python and pip
I want to count unique values in arrays and tuples
I tried to make GUI tic-tac-toe with Python and Tkinter
I want to be able to run Python in VS Code