I am self-taught and participating in AtCoder in Python. So today, I'm going to give a brief explanation of for and arrangement, which is a point that people who have started learning (people like me in the past) will not understand! I will also explain about input using AtCoder for. It is just a brief explanation. If you can know at least this far, you will also acquire google ability (vocabulary ability when google), please google various functions by yourself and learn little by little! (I'm a fledgling brown coder and I'm not good at short notations. Also, if you should lie, please tell me> <)
for i in range(3):
print(i)
The for statement looks like this. ** The number of numbers in the parentheses of range ()
and the processing inside the for statement are repeated. ** **
In this, ʻi` is called a loop counter variable (I also googled it and now I know it w I don't care about the name) ** It increases by 1 each time it is repeated. ** **
The result of executing this code is
0
1
2
It will be.
As you can see here, ʻiof the first loop is 0. This will be a very important point later. You can see that
0,1,2is output 3 times in total. That's because I wrote
range (3)`.
for i in range(1,3):
print(i)
Actually, there is also such a way of writing. The output result of this is
1
2
is. You can see the rules by comparing it with the code above.
The for statement can be overwritten. Well, for now, just look at it.
for i in range(2):
for j in range(3):
print(i,j)
Output result
0 0
0 1
0 2
1 0
1 1
1 2
You may be a little confused, but you can get used to it! (By the way, if you stack many for statements, it will take a lot of execution time.)
Roughly speaking, an array (list) is like a variable that can store multiple elements together.
yasai = ["Carrots","Daikon radish","Burdock"]
The elements are separated by commas and surrounded by []
. In this case, think of yasai
as a hotel with three rooms.
Normally, each room in a hotel has a room number. The array is the same.
The arrays are numbered ** 0,1,2 .... `in order from the first element. ** This number is called index.
I'll say it again! !! ** The number of the first element is 0! !! !! ** ** It's the same as ʻi`. It's a very important point, so let's hold it down.
yasai = ["Carrots","Daikon radish","Burdock"]
print(yasai[1])
You can also get the element with that number in the array by writing array name [number]
like this.
Do you know the result of executing this code?
Do you think it's a carrot? It's wrong!
Daikon radish
** The number of the first element is 0 **, so be careful not to make a mistake.
The length of the array can be obtained with len (array)
.
yasai = ["Carrots","Daikon radish","Burdock"]
print(len(yasai))
Output result
3
I also use this often. Let's remember.
You can now write code like this.
yasai = ["Carrots","Daikon radish","Burdock"]
for i in range(len(yasai)):
print(yasai[i])
Output result ↓
Carrots
Daikon radish
Burdock
how is it! If you can understand so far, you will be able to solve the C problem. If you don't understand, please read the above again.
With AtCoder, I think that input using for will come out from around the C problem. For example, it looks like this ↓
Looking at the input field, it looks like this.
You may not understand the meaning. I didn't understand the meaning at first either. Especially subscripts. But if you can understand the above, you will understand it immediately.
N,M = map(int,input().split())
H = list(map(int,input().split()))
for i in range(M):
A,B = map(int,input().split())
Jajan.
To enter list, just cover the usual map ()
input with list ()
.
In the for statement below, the loop is turned M times and A and B are input M times.
As you can see here, Python doesn't need to specify the length of the array when inputting the array, so N is not used like M when inputting. It's easy.
From the input specification (see gray image), N == len (H)
holds.
One of the most frustrating errors for beginners is out-of-array references.
yasai = ["Carrots","Daikon radish","Burdock"]
print(yasai[3])
Well, there is a code like this. Do you know what is output?
In the unlikely event that someone thinks "burdock", go back to the page above and keep my words in mind.
The answer is "nothing is output". I get an error message.
Traceback (most recent call last):
File "Main.py", line 2, in <module>
print(yasai[3])
IndexError: list index out of range
I get this error.
Even if you can't speak English, it's not difficult at all if you read it calmly.
From line 2
, you can see that the code on the second line is wrong.
See ʻIndexError: list index out of range`. This is the index error I hate most (often), ** out-of-array references **.
In other words, if you read this error message, you can see that "** The index of the codeprint (yasai [3])
on the second line refers to the outside of the array **"!
This out-of-order reference may seem simple at first glance, but as the code gets more complicated, it becomes ** horror out-of-order reference on parade hell **. be careful.
If you can solve the C problem at a certain speed, you can definitely aim for a brown coder! Python is a language that has short code to write and is suitable for basic fast solving, and above all, it is easy to read, so it took me a lot of time to write, but it may be relatively quick to go to brown.
Execution time (competition pro has a limit on execution time, let's write code as efficient as possible, for details on the web) is the biggest problem, but if you get used to various calculations To some extent, you can feel it.
**Please do your best! ** **
Recommended Posts