Hello, My name is Mana binding. When a person learns a new programming language, it is natural that he / she absorbs it in the light of the knowledge he / she has learned so far. Is it something like "this function of this language, in other words, this is what it looks like in this language"? I find this paraphrase method of explanation very valuable, and I wonder if it can be explained in any language. So, in this article, for those who are "I'm used to Python so much, but what should I do when I want to do this with Rust?", I leave a footprint like marking with a torn bread. It is an article to go. This time, I would like to talk about how to arrange an array when a sequence is passed, which is often the case in competitive programming.
The environment used is Windows. I don't talk about that critically, so you don't have to worry too much about it.
PS C:\> cargo --version
cargo 1.43.0 (3532cf738 2020-03-17)
PS C:\> python --version
Python 3.8.2
The input assumed this time is as follows. Consider a list that contains the sequence a.
n #Number of numbers
a1 a2... an #Sequence
For example
4
5 9 10 6
The subject is huge when it comes to Python-like thinking, but the best way to think about it is to first discard the number n
, apply a split to the sequence string, then convert it to an int and collect them with a map. Is it easy?
input()
n = list(map(int, input().split()))
print(n)
If you don't think about standard input macros and store them as functions,
fn main(){
input();
let n: Vec<i32> = (&input()).split(" ").map(|s| s.parse().unwrap()).collect();
println!("{:?}", n);
}
fn input() -> String{
let mut buf = String::new();
std::io::stdin().read_line(&mut buf).unwrap();
buf.trim_end().to_string()
}
If you write it like the above, you can easily understand it without any difficulty.
Speaking of what I'm doing, I throw away the n
, bring in a sequence of strings as the & str type, split it, and then parse (Rust is smart, so I interpret the display of the type on the left nicely. It will make it i32), ignore the case where the input is strange with unwrap, collect the values with map, and make it an appropriate collection with collect.
For the input function, see My Past Articles (the above example is different from trim).
So what if: Ignore the alphabet and store a and b in a two-dimensional list.
n #The number of data
x1 a1 b1 # x:Alphabet
x2 a2 b2 # a:Numbers
... # b:Numbers
xn an bn
For example
2
a 2 3
b 8 6
Is it a general idea to save n as the number of loops, hit the first character when entering a line of data, and read the second and subsequent characters?
n = int(input())
l = []
for _ in range(n):
l.append([int(e) for e in input().split()[1:]])
print(l)
fn main(){
let raw = input();
let n: i32 = raw.parse().unwrap();
let mut v: Vec<Vec<i32>> = Vec::new();
for _ in 0..n {
let raw = input();
let s: Vec<&str> = raw.split(" ").collect();
v.push([1, 2].iter().map(|i| s[*i as usize].parse::<i32>().unwrap()).collect());
}
println!("{:?}", v);
}
fn input() -> String{
let mut buf = String::new();
std::io::stdin().read_line(&mut buf).unwrap();
buf.trim_end().to_string()
}
If you write let raw = input ();
, you can bind the standard input value to raw
, which makes it easier to manage the lending and borrowing specific to this language.
I would like you to see around [1, 2] .iter (). Map (). Collect ();
, and it will be much easier to write like this for input with a fixed width. I will.
Using a fixed-length array with []
and pouring it into the map through iter makes it easier to control the order and location of element access. In this example, it seems that you can use get or something, but I will leave a defeat declaration that it worked.
** Master the map! ** ** ** Don't forget collect and unwrap! ** **
This time, it felt like a person who couldn't switch his head messed up in a different world. How was it? I hope I can continue to leave this kind of awareness in Qiita ... just in case I forget it. It has a title like a series, but I don't know if there is a next one because I have to go through the process of remembering Rust-> using it with atCoder-> giving it to Qiita. Please look forward to Mr. Mana's next work. `` Thank you for reading this far. I hope it helps.