プログラミングのお題スレ Part22 (854レス)
前次1-
抽出解除 レス栞

リロード規制です。10分ほどで解除するので、他のブラウザへ避難してください。
160: デフォルトの名無しさん [] 2023/11/26(日) 11:41:26.07 ID:1iIVw+fD(1) AAS
>>156
156(10): デフォルトの名無しさん [sage] 2023/11/25(土) 20:07:06.40 ID:zpqT0hBE(1) AAS
お題:ランダムに1から9999までの整数を得た時、何回で全種類出揃うか確認せよ
擬似乱数列生成法については指定しないものとする

ruby
外部リンク:ideone.com
require 'set'
r = 1..9999
c = r.to_a.fill(0)
s = r.to_set
while !s.empty?
n = rand(r)
c[n - r.first] += 1
s.delete n
end
p c.sum

84736
Java
外部リンク:paiza.io
168: デフォルトの名無しさん [] 2023/11/27(月) 18:30:50.07 ID:O6HTjvgJ(1) AAS
>>156
Perl
perl -E 'for ($r = 9999; $n < $r; $_++) { $h{int(rand($r)) + 1} ||= ++$n }; say'
79596
176: デフォルトの名無しさん [sage] 2023/12/04(月) 20:26:07.07 ID:LtCaDuZa(1) AAS
>>171
171(5): デフォルトの名無しさん [sage] 2023/11/30(木) 06:46:26.20 ID:/rzYr39l(1) AAS
お題:英字の羅列された文字列が与えられる。この文字列を分析して数字列を出力せよ。数字の表記ルールは、その文字の両隣の文字がASCIIコードにおける奇数だったら1、そうでなければ0.
Ruby
def solution1( str )
a = 0
str.chars.inject(''){|s,c|
s << ( (5 & (a = 7 & a << 1 | c.ord & 1) == 5)? '1' : '0' )
}[2..-1] || ''
end

solution( '' ) #=> ""
solution( 'AB' ) #=> ""
223
(2): 221 [sage] 2024/02/05(月) 20:08:21.07 ID:tt/WRhkt(1/2) AAS
>>206
206(23): デフォルトの名無しさん [] 2024/02/02(金) 06:41:15.23 ID:CC6U77IS(1) AAS
お題
入力データをグループ分けして出力せよ

入力データの、= の左右は同じグループである。
出力する順番は、入力データの出現順とする

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 を自作してみた。
下はユニットテストです

外部リンク:paiza.io
外部リンク:paiza.io
ruby
外部リンク:ideone.com
>>221
221(1): 211 [sage] 2024/02/04(日) 23:55:45.21 ID:ytAuzkvH(1) AAS
>>206 ruby
外部リンク:ideone.com
>>211から若干のアレンジ
・同一グループの収集にSortedSetを使用
から若干のアレンジ
・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(:[])}
}
341: デフォルトの名無しさん [] 2024/05/02(木) 17:21:22.07 ID:pg1ymc2D(1) AAS
PC買って、脱衣AIで遊びまくってる「
一日一回無料で使えるみたい「
2chスレ:gymnastics
471: デフォルトの名無しさん [] 2025/01/04(土) 14:03:19.07 ID:uiECOsgP(1/2) AAS
>>468
468(2): デフォルトの名無しさん [sage] 2025/01/03(金) 17:52:02.97 ID:aGLRGnDr(3/3) AAS
>>464 lisp (chatgptに教えてもらった)
外部リンク:ideone.com
外部リンク:chatgpt.com
ChatGPTロジックだとAABABBが受理されちゃう
550: 警備員[Lv.19] [] 2025/02/09(日) 03:18:28.07 ID:Y8d2noVo(1/2) AAS
>>485
485(20): デフォルトの名無しさん [] 2025/01/22(水) 21:35:12.82 ID:JtEnwxKE(1) AAS
お題

文字列と長さを入力として受け取り
デカルト積のリストを出力してください

入力:ABC 1
出力:[A, B, C]

入力:ABC 2
出力:[AA, AB, AC, BA, BB, BC, CA, CB, CC]

入力:ABC 3
出力:[AAA, AAB, AAC, ABA, ABB, ABC, ACA, ACB, ACC, BAA, BAB, BAC, BBA, BBB, BBC, BCA, BCB, BCC, CAA, CAB, CAC, CBA, CBB, CBC, CCA, CCB, CCC]
Kotlin
外部リンク:paiza.io
774: デフォルトの名無しさん [sage] 2025/07/26(土) 12:26:55.07 ID:xCVVpUlx(1) AAS
>>772
772(1): デフォルトの名無しさん [sage] 2025/07/25(金) 20:46:23.28 ID:dyl0C+2U(1) AAS
>>771
例がおかしい
わかりやすい最後の例で示すと

入力: 0 1 0 1 0 1 0 1
出力: 0 4 1 5 2 6 3 7 間違い
出力: 0 2 4 6 1 3 5 7 正解

安定ソートなので同値は順序を保ってこうなる
出題者だけど「各要素の移動先のインデクス」って書いた方がよかったか
文言削りすぎた
815: デフォルトの名無しさん [sage] 2025/08/22(金) 06:56:46.07 ID:qlaiAqZd(2/2) AAS
>>814
814(1): デフォルトの名無しさん [sage] 2025/08/22(金) 06:55:21.34 ID:qlaiAqZd(1/2) AAS
>>812
Rust >>799の逆変換の重複順列の何番目か算出

use itertools::Itertools;
use num::{BigUint, One};

// 重複順列の何番目かを求める ex. "222331434114" → "123456"番目
fn to_nth(input: &str) -> String {
// 出現数
let (mut counts, chars): (Vec<usize Vec<char>) = input.chars().sorted().dedup_with_count().multiunzip();
// index化input
let input: Vec<usize> = input.chars().map(|c| chars.iter().position(|&c0| c0 == c).unwrap()).collect();
// 階乗関数
fn factorial(x: usize) -> BigUint { (1..=x).fold(BigUint::one(), |p, x| p * x) }
// 重複順列の総数
let mut whole: BigUint = factorial(input.len()) / counts.iter().map(|&x| factorial(x)).product::<BigUint>();
// nth番目を算出
(1..=input.len()).rev().zip(input).fold(BigUint::one(), |mut nth, (len, index)| {
// 自分より前までの総数をnthに足す
nth += &whole * counts[..index].iter().sum::<usize>() / len;
// 自分の総数へ更新
whole *= counts[index];
whole /= len;
counts[index] -= 1;
nth
})
.to_string()
}
の検証分

fn main() {
assert_eq!(to_nth("123456789"), "1");
assert_eq!(to_nth("123456798"), "2");
assert_eq!(to_nth("123456879"), "3");
assert_eq!(to_nth("416589732"), "123456");
assert_eq!(to_nth("684753219"), "234567");
assert_eq!(to_nth("987654321"), "362880");

assert_eq!(to_nth("111222333444"), "1");
assert_eq!(to_nth("111222334344"), "2");
assert_eq!(to_nth("111222334434"), "3");
assert_eq!(to_nth("222331434114"), "123456");
assert_eq!(to_nth("324424331112"), "234567");
assert_eq!(to_nth("444333222111"), "369600");

assert_eq!(to_nth("111333442545225"), "123456");
assert_eq!(to_nth("555444333222111"), "168168000");

assert_eq!(to_nth("11111222223333344444555556666677777899⑩⑩889889⑩⑩⑩9"), "123456");
assert_eq!(to_nth("⑩⑩⑩⑩⑩999998888877777666665555544444333332222211111"), "49120458506088132224064306071170476903628800");

assert_eq!(to_nth("314159"), "127");
assert_eq!(to_nth("3141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067"), "11503448027594046007734790817193859678406683579515952737315863863451534425766911708030454269");
assert_eq!(to_nth("なまむぎなまごめなまたまご"), "10515454");
assert_eq!(to_nth("かえるぴょこぴょこみぴょこぴょこあわせてぴょこぴょこむぴょこぴょこ"), "8273693808428448039784");
}
前次1-
スレ情報 赤レス抽出 画像レス抽出 歴の未読スレ AAサムネイル

ぬこの手 ぬこTOP 0.074s