2023-01-01から1年間の記事一覧

辞書順で何番目か(Ruby)

辞書順(lexicographical order)に並べて何番目か、あるはn番目のものは何か。要素に重複がないとして考える。数列 (1, 2, .... , n) の並べ替えとして解こう。 数列 (a1, .. , an) は辞書順で何番目か。 def number_in_lex_order(ary) n = ary.size return…