プログラミングのお題スレ Part18
■ このスレッドは過去ログ倉庫に格納されています
>>600 × 意識した解法を意識してしまうが、 ○ 意識した解法を思い浮かべてしまうが、 お題: アッカーマン関数の引数が与えられるので、計算過程を1ステップずつ出力してください 入力: 2 1 出力: A(2, 1) A(1, A(2, 0)) A(1, A(1, 1)) A(1, A(0, A(1, 0))) A(1, A(0, A(0, 1))) A(1, A(0, 2)) A(1, 3) A(0, A(1, 2)) A(0, A(0, A(1, 1))) A(0, A(0, A(0, A(1, 0)))) A(0, A(0, A(0, A(0, 1)))) A(0, A(0, A(0, 2))) A(0, A(0, 3)) A(0, 4) 5 >>605 Haskell astr m n = "A(" ++ m ++ "," ++ n ++ ")" ack' 0 n = (n+1, [astr (show 0) (show n), (show $ n+1)]) ack' m 0 = let (val, ss) = ack' (m-1) 1 in (val, (astr (show m) (show 0)) : ss) ack' m n = let (val1, ss1) = ack' m (n-1) (val2, ss2) = ack' (m-1) val1 ss = [astr (show $ m-1) s | s <-ss1] ++ (tail ss2) in (val2, (astr (show m) (show n)):ss) ack m n = map (++"\n") $ snd $ ack' m n main = do [x,y]<-(return.(map read).words)=<<getLine mapM_ putStr $ ack x y >>607 いや、上手な人ならもっと上手くやるはず アホほどライブラリがあってそれ使えば楽々のハズだけど修行中の身ではこんなもんorz >>606 ちょっとチューンナップ 達人なら***とか>>>とか意味わからん演算子使ってもっとスッキリ書くんだろうけどアホほどあって使いこなせんorz haskell ---- astr m s = "A(" ++ (show m) ++ "," ++ s ++ ")" ack 0 n = [astr 0 $ show n, show $ n+1] ack m 0 = (astr m $ show 0) : ack (m-1) 1 ack m n = let ss1 = ack m (n-1) in [astr m $ show n] ++ (map (astr (m-1)) ss1) ++ (tail $ ack (m-1) (read $ last ss1)) main = do [x,y]<-(return.(map read).words)=<<getLine mapM_ ((putStr).(++"\n")) $ ack x y お題: 野球のボールの動きを物理シミュレートして、ピッチャーの投げてきたボールをより遠くにかっ飛ばすバッターAIを開発せよ。 >>610 ─┼─┐─┼─ / ,. `゙''‐、_\ | / / │ │─┼─/| _,.イ,,.ィ' ─────‐‐‐ * ←>>蟻人間 │ |ツ │ | | | イン /´⌒`ヽ // | \ {, リ) / ./ | \ __ /}从、 リ( / / | ,. ,. -‐===‐- `つ/ ,.イ ’^ソハノリ / ∵|:・. 〃〃〃〃 //ミノ__ /´ /∴・|∵’ ヽ_I__I__I__I__I_I __ノ_/ / ヽミ、、 .∵.;|∵’:; ー{____,,二二二ニ) ノΘ(__ ) ゙ 、∵.;|∵’, /I I I I I I .^^^' \ !  ̄フ ゙ 、 |∴ ソ / ` ノ⌒ヽ') / ノ/ / /\/ ∧/ / ', / / ノ./ ./ ', ノ/ ヘ__、 ./ / ̄ ̄ ̄ヽ .', ヘ_'_, / \ / ', / \/ ', ゙゙゙゙゙゙゙゙゙゙゙゙゙゙゙゙゙゙゙゙゙゙゙゙ ゙゙゙゙゙゙゙゙゙゙゙゙゙゙゙゙゙゙゙゙゙゙゙゙ >>605 Perl5 use feature qw{signatures say}; no warnings 'experimental'; sub A($m, $n, $l, $r) { say "${l}A($m, $n)$r"; $m ? $n ? A($m-1, A($m, $n-1, "${l}A(".($m-1).', ', ")$r"), "${l}A(".$m.', ', ")$r") : A($m-1, 1, "${l}A(".($m-1).', ', ")$r") : $n + 1; } say A(2, 1, '', ''); 実行結果 ~ $ perl 18_605_Ackermann_func.pl A(2, 1) A(1, A(2, 0)) A(1, A(1, A(1, 1))) A(1, A(1, A(0, A(1, 0)))) A(1, A(1, A(0, A(0, A(0, 1))))) A(1, A(1, A(1, A(0, 2)))) A(2, A(1, 3)) A(2, A(0, A(1, 2))) A(2, A(0, A(0, A(1, 1)))) A(2, A(0, A(0, A(0, A(1, 0))))) A(2, A(0, A(0, A(0, A(0, A(0, 1)))))) A(2, A(0, A(0, A(1, A(0, 2))))) A(2, A(0, A(1, A(0, 3)))) A(2, A(1, A(0, 4))) 5 そうなのよね 入力もらって表示→ワンステップだけ展開して戻る の繰り返しが1番楽なんだけどなぁ >>605 Perl5、>>615 を修正。 >>617 あら正規表現で書けちゃうのかイナ… use feature qw{signatures say}; no warnings 'experimental'; sub A($m, $n) { $m ? $n ? do{ my ($m2, $n2) = ($m-1, $n-1); push @$l, "A($m2, "; push @$r, ')'; say @$l, "A($m, $n2)", @$r; A($m2, A($m, $n2)) } : do{ my ($m2, $n2) = ($m-1, 1); say @$l, "A($m2, $n2)", @$r; A($m2, $n2)} : do{ my $s = $n + 1; say @$l, $s, @$r; pop @$l; pop @$r; $s }; } ($m, $n) = (2, 1); say "A($m, $n)"; A($m, $n); 実行結果 $ perl 18_605_Ackermann_func.pl A(2, 1) A(1, A(2, 0)) A(1, A(1, 1)) A(1, A(0, A(1, 0))) A(1, A(0, A(0, 1))) A(1, A(0, 2)) A(1, 3) A(0, A(1, 2)) A(0, A(0, A(1, 1))) A(0, A(0, A(0, A(1, 0)))) A(0, A(0, A(0, A(0, 1)))) A(0, A(0, A(0, 2))) A(0, A(0, 3)) A(0, 4) 5 >>605 ocaml https://ideone.com/dqTO60 let (>>) f g x = g (f x) let rec a m n p = let () = p (Printf.sprintf "A(%d, %d)" m n) in if m = 0 then n + 1 else if n = 0 then a (m - 1) 1 p else a (m - 1) (a m (n - 1) (Printf.sprintf "A(%d, %s)" (m - 1) >> p)) p let () = print_int (a 2 1 print_endline) ↓ A(2, 1) A(1, A(2, 0)) A(1, A(1, 1)) A(1, A(0, A(1, 0))) A(1, A(0, A(0, 1))) A(1, A(0, 2)) A(1, 3) A(0, A(1, 2)) A(0, A(0, A(1, 1))) A(0, A(0, A(0, A(1, 0)))) A(0, A(0, A(0, A(0, 1)))) A(0, A(0, A(0, 2))) A(0, A(0, 3)) A(0, 4) 5 >>605 octave https://ideone.com/50i12U function c = a(m, n, p) p(sprintf('A(%d, %d)', m, n)); if m == 0 c = n + 1; elseif n == 0 c = a(m - 1, 1, p); else c = a(m - 1, a(m, n - 1, @(s) p(sprintf('A(%d, %s)', m - 1, s))), p); end end disp(a(2, 1, @disp)); お題: ax**2+b*x+c==0という形の二次方程式を解き、全ての実数解と実数解の個数を表示する。 実数解はルートを含んだ形でいいのか? あるいは小数点以下何桁まで正確に求めるのか? >>624 ∧_∧ / ̄ ̄ ̄ ̄ ̄ ( ´∀`)< オマエモナー ( ) \_____ | | | (__)_) お題:計算ができる文章テンプレートを実装し、つるかめ算を自動生成せよ。 文章テンプレートの例: 「ツルとカメが合わせて{$(ツルの数)+$(カメの数)}います。 足の合計本数が{$(ツルの数)*2+$(カメの数)*4}本です。 カメは何匹いますか。」 変数は「$(変数名)」のように表し、その具体的な値は入力として与えられる。 計算式は{ }で囲って表す。 計算式は変数や定数、加減乗除の演算子、または丸カッコを含むことができる。 入力の例:「ツルの数=3, カメの数=4」 >>625 js function quadraticWithCount(a, b, c) { const inSqrt = b ** 2 - 4 * a * c if (inSqrt < 0) return [[], 0] const l = - b / (a * 2) if (inSqrt === 0) return [[l], 1] const r = Math.sqrt(inSqrt) / (a * 2) return [[l - r, l + r], 2] } console.log(quadraticWithCount(3, -2, -1)) console.log(quadraticWithCount(3, 6, 3)) console.log(quadraticWithCount(1, 2, 3)) // [ [ -0.3333333333333333, 1 ], 2 ] // [ [ -1 ], 1 ] // [ [], 0 ] >>629 追記: パーサーを作れない人はevalを用いても構わない。 >>625 js 改善 function quadratic(a, b, c) { const rt = Math.sqrt(b ** 2 - 4 * a * c) return [(-b + rt) / (2 * a), (-b - rt) / (2 * a)] } function quadraticReal(...abc) { const ans = quadratic(...abc) const [a1, a2] = ans if (isNaN(a1)) return [] if (a1 === a2) return [a1] return ans } const quadraticRealWithCount = (...abc) => addCount(quadraticReal(...abc)) const addCount = a => [a, a.length] console.log(quadraticRealWithCount(3, -2, -1)) // [ [ 1, -0.3333333333333333 ], 2 ] console.log(quadraticRealWithCount(3, 6, 3)) // [ [ -1 ], 1 ] console.log(quadraticRealWithCount(1, 2, 3)) // [ [], 0 ] >>629 Ruby ツルの数 = 3 カメの数 = 4 Template =<<_EOT_ 「ツルとカメが合わせて{$(ツルの数)+$(カメの数)}います。 足の合計本数が{$(ツルの数)*2+$(カメの数)*4}本です。 カメは何匹いますか。」 _EOT_ puts Template.gsub( /\{(.*?)\}/ ){ eval $1.gsub( /\$\((.*?)\)/ ){ $1 } } >>629 Perl5 use utf8; no warnings 'utf8'; $ツルの数 = 3; $カメの数 = 4; print <<EOF 「ツルとカメが合わせて@{[${ツルの数}+${カメの数}]}います。 足の合計本数が@{[${ツルの数}*2+${カメの数}*4]}本です。 カメは何匹いますか。」 EOF 実行結果 ~ $ perl 18_633_ツルの数+カメの数.pl 「ツルとカメが合わせて7います。 足の合計本数が22本です。 カメは何匹いますか。」 お題: 2つのRGBカラーが#RRGGBB形式で与えられる(RRGGBBは六桁の十六進数とする)。 2つの中間色を#RRGGBB形式で出力せよ。 可能ならばこれら3つの色をカラーバーとして表示せよ。 >>635 入力例1: #112233, #99AAFF 入力例2: #AD03ff, #a39e75 >>635 ,636 自明な入力例だけを示しても、出力例も出さないと意味ないだろう。 2色の中間色ってのが曖昧じゃね? 単にR,G,Bそれぞれの平均を取るだけなのか、赤と紫の中間が赤紫になるように循環する色相で中間をとるのかとか。 >>634 >変数は「$(変数名)」のように表し、 お題: 20201010と出力してください。ただしコードに使っていい数字は1のみです。 >>640 python a=1;b=a+a;c=a-a print(b,c,b,c,a,c,a,c,sep='') #出力20201010 >>640 自分で js const t = ` `.charCodeAt() console.log(t + t + "" + (t + t) + t + t) const m = "ϲ".charCodeAt() console.log(m + m + "" + m) >>640 js本日限定 new Date().toLocaleDateString().split(/-|\//).join`` //=> "20201010" >>647 Windowsでもそのままでいけるのか? haskell import Data.Maybe main = print $ product $ map ((fromJust).(flip lookup (zip ['\t'..] [1..]))) "&myC" >>649 js var ctx = new window.AudioContext var osc = ctx.createOscillator() osc.type = 'sine' osc.frequency.value = 1000 osc.connect(ctx.destination) osc.start() osc.stop(1) >>640 haskell 改良 unicode = length . tail . (enumFromTo minBound) main = print $ product $ map unicode "ാᝇ" >>640 JavaScript [1+1,1-1,1+1,1-1,1,1-1,1,1-1].reduce((acc,n)=>acc+n,'') >>640 J smoutput ,&":/ 'amp' i. 'papamama' >>640 bash aa=1; bb=$((aa-aa)); cc=$aa$bb$aa$bb; printf "%d" $((cc+cc)) $cc; echo >>635 Kotlin https://paiza.io/projects/O1eUwROYKRQepDq3N-VyUA paiza.io はHTMLで出力させてブラウザに表示できるのでカラーバーも出してみました。 >>640 brainfuck >+++++++[<+++++++>-]<+.--.++.--.+.-.+.-. お題: 整数0〜10の一様分布の乱数を百回発生させて、その統計結果を基に各整数を横軸、回数を縦軸とする棒グラフ(ヒストグラム)を描画し(棒の最大幅32字)、ほぼ一様であることを確かめなさい。 paiza.io は、text, html, json で出力できるのか html も、部分的なタグだけを書いても動くのか >>667 訂正。 (誤)各整数を横軸、回数を縦軸とする棒グラフ (正)各整数を縦軸、回数を横軸とする棒グラフ haskell import Data.List import System.Random trial records = do x <- randomRIO (0, 1) :: IO Double return $ (truncate $ x * 11) : records experience = iterate (>>= trial) (return [0..10]) showrecord (x,y) = let clsstr = id $ reverse $ take 2 $ ( ++ (repeat '0')) $ reverse $ show x barstr = replicate y '*' in concat [ clsstr," ", barstr,"\n"] main = do records <- experience !! 100 putStr $ id $ concat $ map showrecord $ map (\x -> (head x, length $ tail x)) $ group $ sort $ records >>649 オーディオ系はシステム依存度が高いから余りサンプルが出てこないね。 WindowsやMacOSなら簡単そうだが 1kHz の音源ファイルを使うのは違反かな? >>649 C/C++ with Win32 #include <windows.h> int main(void) { Beep(1000, 1000); } >>649 Python with Windows import winsound winsound.Beep(1000, 1000); >>649 Python with Mac import os os.system('play -n synth 1 sin 1000'); シェル経由でSoX playコマンド呼んでるだけやんけ Pythonもmacも関係ねぇわ >>678 iOS じゃ、play コマンドがなさそうなんだよな。 結局このスレで盛り上がるのは環境によらず言語によらず、でも多少は骨のあるようなお題なんだよな へぇその言語ならそんなにキレイに書けるのかとか普段自分が使ってない言語ではどうやって実現できるのか眺めたりするのが面白い 音鳴らすなんてお題として面白いわけがない 蟻人間さんへのお題: 文字列を受け取って、RFC 5322にしたがってSubjectヘッダを出力してください(MUST、SHOULDを満たすこと) 文字コードはUTF-8、エンコーディングはBエンコードを使用してください >>685 Python text = 'Re: Re: This is a subject text. This is a subject text. This is a subject text.' if text[0:4] == 'Re: ': __while text[0:4] == 'Re: ': ____text = text[4:] __text = 'Re: ' + text import base64 text = 'Subject: ' + base64.b64encode(text.encode('utf-8')).decode('ascii'); ary = [] while len(text) > 78: __ary.append(text[0:78]) __text = text[78:] ary.append(text) import platform if platform.system() == 'Windows': __for item in ary: ____print(item) else: __for item in ary: ____print(item, end="\r\n") >>686 訂正。 (誤) while len(text) > 78: (正) while len(text) >= 78: >>686 いろいろ違うので・・・メールのソース眺めたが良いです 頭の悪いお題を連投してるやつはやっぱり頭悪いんだな そんなことないと思うけどな あまり他人の悪口言わない方が良いよ >>629 なんかはいいと思うんだけどな eval使えないHaskellerにはいい練習問題になってる ウェブアプリつくれだのGUIアプリつくれだの酷い事散々言いまくってるじゃん ◆QZaw55cn4cの方がまだマシ お題 自然数nを 2進数にした時、 1が連続する個数の最大値を求める。 例 20201016 -> 5 haskell import Data.List maxConsOne = maximum . map length . filter ((==1).head) . group . unfoldr (\n ->if n == 0 then Nothing else Just (mod n 2, div n 2)) main = print $ maxConsOne $ 20201016 ---- 5 >>695 ruby f = ->(n) {n.to_s(2).chars.chunk(&:itself).map {|k, a| k == '1' ? a.size : 0}.max} >>695 JavaScript const f = (n = 0) => Math.max(...(n.toString(2).match(/1+/g) ?? ['']).map(m => m.length)) console.log(f(20201016)) //=> 5 >>695 Perl5 use List::Util max; print max map{length} sprintf('%b', 20201016) =~ /(1+)/g; 実行結果 $ perl 18_695_bin1.pl 5 >>695 Ruby p 20201016.to_s(2).split("0").max.size ■ このスレッドは過去ログ倉庫に格納されています
read.cgi ver 07.5.5 2024/06/08 Walang Kapalit ★ | Donguri System Team 5ちゃんねる