プログラミングのお題スレ Part22 (863レス)
プログラミングのお題スレ Part22 http://mevius.5ch.net/test/read.cgi/tech/1691038333/
上
下
前
次
1-
新
通常表示
512バイト分割
レス栞
抽出解除
レス栞
リロード規制
です。10分ほどで解除するので、
他のブラウザ
へ避難してください。
206: デフォルトの名無しさん [] 2024/02/02(金) 06:41:15.23 ID:CC6U77IS お題 入力データをグループ分けして出力せよ 入力データの、= の左右は同じグループである。 出力する順番は、入力データの出現順とする UnionFind を使えば良いかも 入力データ ["a1=a2", "b1=b2", "b3=b2", "c1=c2", "e1=e2", "a3=a4", "c3=c4", "e1=e3", "a2=a4", "c3=c1", "b3=a4", "c2=d1", "a4=a5", "d2=c1", "b4=b3", "d3=c3"] 出力 [["a1", "a2", "b1", "b2", "b3", "a3", "a4", "a5", "b4"], ["c1", "c2", "c3", "c4", "d1", "d2", "d3"], ["e1", "e2", "e3"]] Ruby で、UnionFind を自作してみた。 下はユニットテストです https://paiza.io/projects/e6nk1EOu3utyWpV3iuWAFQ?language=ruby https://paiza.io/projects/kjeVtTKeDwEnWVrBU5_nbg?language=ruby http://mevius.5ch.net/test/read.cgi/tech/1691038333/206
207: デフォルトの名無しさん [sage] 2024/02/02(金) 10:50:23.49 ID:fEMhv+T7 >>206 Rust fn foo<'a, 'b>(input: &'b [&'a str]) -> Vec<Vec<&'a str>> { struct Data<'a> { name: &'a str, rep: usize, coll: Option<Vec<usize>>, } let mut data = Vec::<Data>::new(); let mut map = HashMap::<&str, usize>::new(); for s in input { let (index0, index1) = s.splitn(2, '=') .map(|s| match map.get(s) { Some(&index) => data[index].rep, None => { let index = data.len(); map.insert(s, index); data.push(Data { name: s, rep: index, coll: Some(vec![index]), }); index }, }) .sorted().tuple_windows().next().unwrap(); if index0 != index1 { let coll0 = data[index0].coll.take().unwrap(); let coll1 = data[index1].coll.take().unwrap(); coll1.iter().for_each(|&index| data[index].rep = index0); data[index0].coll = Some(itertools::merge(coll0, coll1).collect()); } } data.iter().map(|data| &data.coll).flatten() .map(|coll| coll.iter().map(|&index| data[index].name).collect()).collect() } http://mevius.5ch.net/test/read.cgi/tech/1691038333/207
209: デフォルトの名無しさん [sage] 2024/02/02(金) 22:48:33.27 ID:UezRkqGy >>206 ruby https://ideone.com/eF5lww f = -> a { w = a.map {|s| s.split('=')}.flatten.uniq.map.with_index.to_h a.each_with_object([]) {|s, acc| x, xa, y, ya = s.split('=').map {|k| [k, acc.find {|b| b.include? k}]}.flatten(1) if xa && ya then xa.concat (acc.delete ya) << x << y elsif xa then xa << x << y elsif ya then ya << x << y else acc << [x, y] end }.map {|a| a.uniq.sort_by {|s| w[s]}}.sort_by {|a| w[a[0]]} } http://mevius.5ch.net/test/read.cgi/tech/1691038333/209
210: デフォルトの名無しさん [sage] 2024/02/02(金) 22:51:42.74 ID:UezRkqGy >>206 rust https://ideone.com/MEZMPO fn f<'a>(a: &[&'a str]) -> Vec<Vec<&'a str>> { // ' let h = a.iter().map(|&s| s.split('=')).flatten().rev().enumerate().map(|(p, s)| (s, p)).collect::<HashMap<_, _>>(); let mut acc = Vec::<Vec<&str>>::new(); for xy in a.iter().map(|s| s.split('=').collect::<Vec<_>>()) { match (acc.iter().position(|b| b.contains(&xy[0])), acc.iter().position(|b| b.contains(&xy[1]))) { (Some(xi), Some(yi)) => { let ys = acc[yi].clone(); acc[xi].extend(ys); acc[xi].extend(xy); acc.remove(yi); }, (Some(xi), None) => acc[xi].extend(xy), (None, Some(yi)) => acc[yi].extend(xy), _ => acc.push(xy), } } for b in acc.iter_mut() { b.sort_by(|c, d| h.get(d).cmp(&h.get(c))); b.dedup(); } acc.sort_by(|c, d| h.get(d[0]).cmp(&h.get(c[0]))); acc } http://mevius.5ch.net/test/read.cgi/tech/1691038333/210
211: デフォルトの名無しさん [sage] 2024/02/02(金) 23:24:19.60 ID:UezRkqGy >>206 ruby https://ideone.com/daI0QL ・若干の修正 f = -> a { w = a.map {|s| s.split('=')}.flatten.uniq.map.with_index.to_h a.each_with_object([]) {|s, acc| x, xa, y, ya = s.split('=').map {|k| [k, acc.find {|b| b.include? k}]}.flatten(1) if xa && ya then xa.concat (acc.delete ya) elsif xa then xa << y elsif ya then ya << x else acc << [x, y] end }.map {|a| a.sort_by {|s| w[s]}}.sort_by {|a| w[a[0]]} } http://mevius.5ch.net/test/read.cgi/tech/1691038333/211
212: デフォルトの名無しさん [sage] 2024/02/02(金) 23:24:45.86 ID:UezRkqGy >>206 rust https://ideone.com/dO4xea ・若干の修正 fn f<'a>(a: &[&'a str]) -> Vec<Vec<&'a str>> { // ' let h = a.iter().map(|&s| s.split('=')).flatten().rev().enumerate().map(|(p, s)| (s, p)).collect::<HashMap<_, _>>(); let mut acc = Vec::<Vec<&str>>::new(); for xy in a.iter().map(|s| s.split('=').collect::<Vec<_>>()) { match (acc.iter().position(|b| b.contains(&xy[0])), acc.iter().position(|b| b.contains(&xy[1]))) { (Some(xi), Some(yi)) => { let ys = acc[yi].clone(); acc[xi].extend(ys); acc.remove(yi); }, (Some(xi), None) => acc[xi].push(xy[1]), (None, Some(yi)) => acc[yi].push(xy[0]), _ => acc.push(xy), } } acc.iter_mut().for_each(|b| b.sort_by(|c, d| h.get(d).cmp(&h.get(c)))); acc.sort_by(|c, d| h.get(d[0]).cmp(&h.get(c[0]))); acc } http://mevius.5ch.net/test/read.cgi/tech/1691038333/212
213: デフォルトの名無しさん [] 2024/02/02(金) 23:58:11.98 ID:Uk0I9chw >>206 R https://ideone.com/FOwwk2 http://mevius.5ch.net/test/read.cgi/tech/1691038333/213
214: デフォルトの名無しさん [] 2024/02/03(土) 02:58:35.44 ID:bEsWZIv5 >>206 Java https://paiza.io/projects/TzHsf-cnqzdxSASpwlgg_w http://mevius.5ch.net/test/read.cgi/tech/1691038333/214
215: デフォルトの名無しさん [] 2024/02/03(土) 10:26:51.46 ID:kmOXhk/V >>206 >>213 Rでもっと短く書けた。 https://ideone.com/vmtYAJ http://mevius.5ch.net/test/read.cgi/tech/1691038333/215
217: 9 [sage] 2024/02/04(日) 18:22:17.66 ID:jTY6zdRX >>208 宛てじゃなかった >>206 の回答だったわ… orz http://mevius.5ch.net/test/read.cgi/tech/1691038333/217
218: デフォルトの名無しさん [] 2024/02/04(日) 18:32:39.04 ID:fS5H2fbQ >>206 >>215をPowerShellに移植 $in = "a1=a2", "b1=b2", "b3=b2", "c1=c2", "e1=e2", "a3=a4", "c3=c4", "e1=e3", "a2=a4", "c3=c1", "b3=a4", "c2=d1", "a4=a5", "d2=c1", "b4=b3", "d3=c3" $in -split "=" |% {$h = @{}; $n = 0} {if (!$h[$_]) {$h[$_] = $n++}} $eq = $in |% {, $h[$_ -split "="]} $g = 1..$n do { $changed = $false $eq |% { $i, $j = $_ switch ($g[$i] - $g[$j]) { {$_ -gt 0} {$g[$i] = $g[$j]; $changed = $true} {$_ -lt 0} {$g[$j] = $g[$i]; $changed = $true} } } } while ($changed) $h.keys | sort {$h[$_]} | group {$g[$h[$_]]} |% {"[$($_.group -join ", ")]"} -- 実行結果 -- [a1, a2, b1, b2, b3, a3, a4, a5, b4] [c1, c2, c3, c4, d1, d2, d3] [e1, e2, e3] http://mevius.5ch.net/test/read.cgi/tech/1691038333/218
220: デフォルトの名無しさん [] 2024/02/04(日) 19:43:39.57 ID:NiYs7EK6 >>206 C++ https://mevius.5ch.net/test/read.cgi/tech/1434079972/125 完全にやっつけ仕事、いろいろ課題がありますね http://mevius.5ch.net/test/read.cgi/tech/1691038333/220
221: 211 [sage] 2024/02/04(日) 23:55:45.21 ID:ytAuzkvH >>206 ruby https://ideone.com/2UiT8U ・>>211から若干のアレンジ ・同一グループの収集にSortedSetを使用 http://mevius.5ch.net/test/read.cgi/tech/1691038333/221
222: 17 [] 2024/02/05(月) 02:54:15.12 ID:8tY/Vubv >>206 Kotlin 入力データを標準入力から入力したり、クラス作ってその中でまとめる等、色々やって長くなった。 https://paiza.io/projects/zdysD5ygRDFVbY2gAGCwOw http://mevius.5ch.net/test/read.cgi/tech/1691038333/222
223: 221 [sage] 2024/02/05(月) 20:08:21.07 ID:tt/WRhkt >>206 ruby https://ideone.com/j2xPyB ・>>221から若干のアレンジ ・SortedSet単位でのみいじるようにした f = -> a { g = -> a {a.combination(2) {|x, y| break g.(a.tap {x.merge y; a.delete y}) if x.intersect? y}} h = a.map {|s| s.split('=')}.flatten.uniq.map.with_index.to_h a = a.map {|s| s.split('=').map {|k| h[k]}.to_set SortedSet} g.(a).map {|set| set.map &h.invert.method(:[])} } http://mevius.5ch.net/test/read.cgi/tech/1691038333/223
224: デフォルトの名無しさん [] 2024/02/05(月) 23:26:45.85 ID:YjqgZClx >>206 >>218-219をC#化 https://ideone.com/qWA5TB http://mevius.5ch.net/test/read.cgi/tech/1691038333/224
225: 223 [sage] 2024/02/05(月) 23:51:15.55 ID:tt/WRhkt >>206 rust https://ideone.com/Ma1WGV ・>>223の移植 ・色々迷いアリ .map(|k| *h.get(k).unwrap())のところは当初 .map(|k| h.get(k).map(|&i| i)).flatten()などとしていたが 正解がわからないので迷った挙げ句に短く書けるほうを採用 ・これに限らずrustは不慣れなので色々珍妙なことをしている可能性アリ http://mevius.5ch.net/test/read.cgi/tech/1691038333/225
226: 225 [sage] 2024/02/06(火) 22:07:13.88 ID:6T/Xuns0 >>206 rust https://ideone.com/m5INyJ ・>>225から若干の修正 ・不必要なループ回数を訂正 ・二重forを一重に(でもかえって煩雑に) ・まだまだ迷いアリ .map(|k| *h.get(k).unwrap())は結局 .flat_map(|k| h.get(k)).cloned()に置き換え こっちのほうが個人的にはスッキリ感アリ >>206 rust https://ideone.com/hT5zGF ・上記のmutナシ版 ・パフォーマンス的な観点もナシ http://mevius.5ch.net/test/read.cgi/tech/1691038333/226
227: デフォルトの名無しさん [] 2024/02/06(火) 22:17:05.66 ID:ICpsP2hv >>206 C#で>>224とは別の解法 https://ideone.com/fvtgZa http://mevius.5ch.net/test/read.cgi/tech/1691038333/227
228: デフォルトの名無しさん [] 2024/02/09(金) 20:11:03.28 ID:xlZlW34G >>206 C#でHashSet型を使用。実効速度は>>224と>>227より遅い。 https://ideone.com/cPsAYu http://mevius.5ch.net/test/read.cgi/tech/1691038333/228
メモ帳
(0/65535文字)
上
下
前
次
1-
新
書
関
写
板
覧
索
設
栞
歴
スレ情報
赤レス抽出
画像レス抽出
歴の未読スレ
AAサムネイル
Google検索
Wikipedia
ぬこの手
ぬこTOP
0.051s