ファイル内の重複行削除(後優先)
Posted feedbacks - OCaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | let mem s ic =
try while s <> input_line ic do () done; true
with End_of_file -> false;;
let uniq ic oc =
try
while true do
let s = input_line ic in
let current_pos = pos_in ic in
if mem s ic then () else output_string oc (s ^ "\n");
seek_in ic current_pos;
done
with End_of_file -> flush oc;;
let main in_file out_file =
let i = open_in in_file
and o = open_out out_file in
uniq i o; close_in i; close_out o;;
(* main "in.txt" "out.txt";; *)
|


raynstard
#3422()
Rating1/1=1.00
入力されたテキストデータから重複する行をとりのぞいて、その結果を標準出力へ出力するプログラムを作成してください。
重複行の排除については、以下の仕様を満たしてください。
#4.はおまけですがある/なしで作りが変わってくると思われるので追加しました。
この問題はraynstardさんにご投稿いただきました。ご協力ありがとうございます。 ところで、素朴な実装のしかたをするとメモリ容量の数倍のサイズのすべての行が異なっているファイルを読ませたときに大変なことが起こりそうな気がしますが、そういうシビアなお題設定ではないので素朴に解いてしまって構いません。シビアなのは続編にしたいと思います。
[ reply ]