LoginSignup
1
0

More than 1 year has passed since last update.

駿台予備校広告のあの答え合わせをRで一行でやってみた

Last updated at Posted at 2022-01-20

https://qiita.com/sano192/items/a27a481395ce8827bbf6
https://qiita.com/TSeri/items/6958d0ad98d4beb7571d

要するに,6文字の順列を作り,重複を省いて,ソートして,100 番目の要素を出力する。
まあ,どんなパッケージを使っても良さそうなので,順列を生成する e1071::permuttions を使う。

library(e1071)
sort(unique(apply(permutations(6), 1, function(i) paste(unlist(strsplit("GAKKOU", ""))[i], collapse=""))))[100]

ところで,dplyr とかの %>% 演算子を使って,パイプで書いてみます。

permutations(6) %>% t %>% applyx %>% unique %>% sort %>% ans

applyx とか ans なんて見たことないですよね。dplyr とか tidyverse のマニュアル見てもないですよ。

applyx = function(a) apply(a, 2, function(i) paste(unlist(strsplit("GAKKOU", ""))[i], collapse=""))
ans = function(a) a[100]

と定義しているだけです。なあんだ!と思いますよね。
パイプで書けるように,関数を定義しているだけです。
これはユーザレベルだからバカバカしいと吐き捨てればよいのですが,tidyverse の開発側がやれば,素晴らしい!となるんですかね。

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0