[過去ログ]
プログラミングのお題スレ Part13 (1002レス)
プログラミングのお題スレ Part13 http://mevius.5ch.net/test/read.cgi/tech/1549160513/
上
下
前次
1-
新
通常表示
512バイト分割
レス栞
抽出解除
レス栞
このスレッドは過去ログ倉庫に格納されています。
次スレ検索
歴削→次スレ
栞削→次スレ
過去ログメニュー
リロード規制
です。10分ほどで解除するので、
他のブラウザ
へ避難してください。
279: デフォルトの名無しさん [sage] 2019/02/21(木) 18:54:22.61 ID:1axkeZoV お題:リスト等を", "で結合して表示せよ。ただし最後は" and "で結合する。 https://ideone.com/pVuO7T http://mevius.5ch.net/test/read.cgi/tech/1549160513/279
280: デフォルトの名無しさん [sage] 2019/02/21(木) 19:06:17.90 ID:TUYpSwcQ >>279 JavaScript const arr = ['apple', 'banana', 'orange', 'mango']; const last = ' and ' + arr.pop(); console.log(arr.join`, ` + last); //=> apple, banana, orange and mango http://mevius.5ch.net/test/read.cgi/tech/1549160513/280
281: デフォルトの名無しさん [sage] 2019/02/21(木) 19:17:09.75 ID:eUc7EqKm >>279 python l = [ 1,2,3,4,5] print( *l[0:-1] ,'and' , l[-1]) # 1 2 3 4 and 5 http://mevius.5ch.net/test/read.cgi/tech/1549160513/281
282: デフォルトの名無しさん [] 2019/02/21(木) 19:27:21.23 ID:2zdYL4u2 >>279 Haskell main = putStrLn.concat.concat $ [map addCanma (init lst),["and,"],[last lst]] addCanma s = s ++ "," lst = ["apple","banana","orange","mango"] output: apple,banana,orange,and,mango http://mevius.5ch.net/test/read.cgi/tech/1549160513/282
289: デフォルトの名無しさん [] 2019/02/21(木) 21:09:47.73 ID:y3+CYNZ6 >>279 Common Lisp (format t "~{~@{~A~^~#,1^, ~}~^ and ~A~}~%" (list 1 2 3 4)) https://ideone.com/6DcwO9 http://mevius.5ch.net/test/read.cgi/tech/1549160513/289
295: 279 [sage] 2019/02/21(木) 21:39:50.99 ID:1axkeZoV >>279 pike https://ideone.com/0nn6Wt http://mevius.5ch.net/test/read.cgi/tech/1549160513/295
297: デフォルトの名無しさん [] 2019/02/21(木) 22:55:49.76 ID:2zdYL4u2 >>279 >>292 Haskellでもワンライナー出来たけど、読みやすいとは思えない。(横長だし) 発想としては(要素数2以上なら)最後以外の要素(文字列)の頭に','追加して、全部結合して(大きな文字列にして)最後に頭の','を省く。 main = putStrLn.tail.concat $ if length lst < 2 then lst else [(concat.(map (\x -> ',':x)).init) lst," and ",last lst] lst = map show [0..5] 個人的に >>287 の方が発想も読みやすさも好みの書き方。 (読み難くなるなら関数分ける派) pythonのはワンライナーでも比較的読みやすくて、ちょっと羨ましい。 http://mevius.5ch.net/test/read.cgi/tech/1549160513/297
300: デフォルトの名無しさん [sage] 2019/02/22(金) 00:04:51.72 ID:EatRhjER >>279 Squeak/Pharo Smalltalk (0 to: 3) asCommaStringAnd "=> '0, 1, 2 and 3' " http://mevius.5ch.net/test/read.cgi/tech/1549160513/300
301: デフォルトの名無しさん [sage] 2019/02/22(金) 00:07:27.55 ID:wSgiM5bY >>299 アンカー間違えた >>279 宛だった…orz >>279 もう一丁 Perl5 sub g { my $z = ' and '.pop @_ if @_ > 1; join(', ', @_).$z; } use feature say; say g(qw[red green black white]); say g(qw[apple banana]); say g(qw[one]); 実行結果 ~ $ perl 13_279_and_2.pl red, green, black and white apple and banana one http://mevius.5ch.net/test/read.cgi/tech/1549160513/301
305: デフォルトの名無しさん [sage] 2019/02/22(金) 06:09:02.19 ID:HhIJRo4K >>279 Smalltalk (Pharo 7) #('apple' 'banana' 'orange' 'mango') asCommaStringAnd ==> 'apple, banana, orange and mango' #('apple' 'banana') asCommaStringAnd ==> 'apple and banana' #('apple') asCommaStringAnd ==> 'apple' http://mevius.5ch.net/test/read.cgi/tech/1549160513/305
306: デフォルトの名無しさん [sage] 2019/02/22(金) 06:36:03.41 ID:S1fPIClY >>279 python def f(lst): return ('空だ' if not lst else ','.join([str(x) for x in lst[:-1]]) + (' and ' if len(lst)>1 else'') + str(lst[-1]) ) print(f( [ 1,'二',3,4,'五'] )) # 1,二,3,4 and 五 print(f( [1,'二'] )) # 1 and 二 print(f( [1] )) # 1 print(f([])) # 空だ http://mevius.5ch.net/test/read.cgi/tech/1549160513/306
308: デフォルトの名無しさん [sage] 2019/02/22(金) 07:58:55.12 ID:6TNKk8ap >>279 Ruby f = -> a {a[0..-2].join(', ').sub(/.\K\z/, ' and ') << a[-1].to_s} (-1..3).each{|r| puts f[[*0..r]]} # => 0 0 and 1 0, 1 and 2 0, 1, 2 and 3 http://mevius.5ch.net/test/read.cgi/tech/1549160513/308
317: デフォルトの名無しさん [sage] 2019/02/22(金) 21:49:55.63 ID:2ARSyvEs >>279 >>289 Common Lispで~#[...~]を使ってみた (format t "~{~S~#[~; and ~:;, ~]~}" '(apple banana orage mango)) http://mevius.5ch.net/test/read.cgi/tech/1549160513/317
323: デフォルトの名無しさん [] 2019/02/23(土) 01:14:20.46 ID:9pS68leH >>279 Kotlin script 最後のカンマを正規表現の文字列置換で and にしただけ。 println(listOf("abc", "def", "xyz").joinToString(", ").replace(",([^,]*)$".toRegex(), " and$1")) 出力 abc, def and xyz http://mevius.5ch.net/test/read.cgi/tech/1549160513/323
メモ帳
(0/65535文字)
上
下
前次
1-
新
書
関
写
板
覧
索
設
栞
歴
スレ情報
赤レス抽出
画像レス抽出
歴の未読スレ
AAサムネイル
Google検索
Wikipedia
ぬこの手
ぬこTOP
0.043s