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 ...
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.
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