Skip to content

Add more examples #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

qryxip
Copy link
Member

@qryxip qryxip commented May 31, 2020

ABC169 - C - Multiplication 3

num_rational::Ratioを使う。

Bは小数点下が2桁で固定なので(_.replace('.', "") + "/100").parse().unwrap()としてパースできる。 そして後は何も考えずに(a * b).to_integer()を答えとすれば良い。

@qryxip qryxip force-pushed the ja-all-enabled-add-example branch from 70eebb9 to 820d764 Compare May 31, 2020 18:45
@qryxip qryxip changed the title Add an example for ABC169-C Add examples for ABC169-{B, C} May 31, 2020
@qryxip
Copy link
Member Author

qryxip commented May 31, 2020

(edit) saturating_mulfoldしたほうがよほど綺麗なので無し!!!

ABC169 - B - Multiplication 2

itertools::iterateを使う例その2。 今回は「途中で打ち切れるfold」として用いる。 具体的には

(1, [a, b, c])
(a, [b, c])
(ab, [c])
(abc, [])

という遷移を(状態, impl FnMut(&状態) -> 状態)という形式にあてはめ、.take_while(..).nth(_)とすることで途中で10^18を超過したらNoneになるようにする。

ただし、(std::iter::successorsもそうだけど)lastの一つ先まで評価されるため、ダミーの値をVec<u128>pushする。 どの道0への対処のためにソートする必要があるしそこまで無理矢理感は出ないはず。

@qryxip qryxip force-pushed the ja-all-enabled-add-example branch from 820d764 to 168e020 Compare May 31, 2020 19:14
@qryxip qryxip changed the title Add examples for ABC169-{B, C} Add an example for ABC169-C May 31, 2020
@qryxip qryxip changed the title Add an example for ABC169-C Add more examples Jun 14, 2020
@qryxip
Copy link
Member Author

qryxip commented Jun 14, 2020

東京海上日動 プログラミングコンテスト2020: A - Nickname

AsciiStr/AsciiStringstr[u8]の両方を持つのだけど、肝心のリテラルを記述するのが面倒。

let c = AsciiChar::A;
let c = AsciiChar::from_ascii('A').unwrap();
let s = AsciiStr::from_ascii("abc").unwrap();

なのでbyte (string) literalがある[u8]と比べると(競技プログラミングでは)微妙なのだが、

let c = b'a';
let s = b"abc";

この問題では AsciiCharを扱わずに

  • [u8]のように[..3]で部分文字列を得ることができる (strだと.chars().take(3).collect::<String>())
  • strのようにそのままprintln!できる ([u8]だとstd::str::from_utf8(_).unwrap())

という長所を生かすことができる。

@qryxip
Copy link
Member Author

qryxip commented Jun 14, 2020

東京海上日動 プログラミングコンテスト2020: C - Lamps

シンプルな1次元0次いもす法をItertoolsNum::cumsomで復号するだけ。

SAusizeで持ち、usize::saturating_subを使うことでnumeric castを一箇所に抑えた。

下手にstruct Imos1(Vec<i64>)のようなライブラリとして持つとas地獄に陥いるタイプの問題なのでこのような使い方の紹介をするのもいいんじゃないかなと思い追加した。 あとこのexamplesはRust入門者が読むだろうからC++やPythonには無いRust独自のイディオムを織り交ぜた方が良いのかなとも思ったので。

@qryxip
Copy link
Member Author

qryxip commented Jun 14, 2020

https://github.com/rust-lang-ja/atcoder-rust-base/pull/64/checks?check_run_id=769219266

マクロ(input!)が生み出す変数にclippy::many_single_char_namesが反応するようになったっぽいです。

AtCoderはしばらく1.42.0で固定だろうしrust-toolchainもあるからbeta(とstable)は外しても良いのではと思います。

@statiolake
Copy link
Contributor

ありがとうございます。なるほど、今までその clippy の warning はマクロで展開される変数には効いていなかったのですね。

今回のこの警告に関しては無視するのでよいと思います。この警告は Rust 的ではない点の指摘というよりは単に可読性についてのアドバイスと思われるので、問題文に合わせたほうがむしろ可読性が高まる競プロの文脈では構わないと個人的に思うからです。ただこの場合 input!() マクロを使わず普通に変数宣言をすれば現行の clippy でも同様の警告が出ますので、たとえ警告が出ないとしても強い意志表示として main()#[allow(clippy::many_single_char_names)] をつけるというのも悪くはないかも知れません (べつにしなくても良いとは思います) 。

確かにこのプロジェクトのターゲットは stable, nightly ではなく 1.42.0 になるので、外してもいいかもしれませんね。例えば、将来に代替物ができて非推奨になった機能に対して warning が出るなどとなっても本質的に邪魔なだけですし...。

Co-authored-by: statiolake <statiolake@gmail.com>
@statiolake statiolake merged commit c61d275 into rust-lang-ja:ja-all-enabled Jun 18, 2020
@qryxip qryxip deleted the ja-all-enabled-add-example branch June 18, 2020 13:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy