プログラミングのお題スレ Part15

■ このスレッドは過去ログ倉庫に格納されています
2019/07/28(日) 19:39:57.54ID:832c/ukY
プログラミングのお題スレです。

【出題と回答例】
1 名前:デフォルトの名無しさん
  お題:お題本文

2 名前:デフォルトの名無しさん
  >>1 使用言語
  回答本文
  結果がある場合はそれも

【ソースコードが長くなったら】 (オンラインでコードを実行できる)
https://ideone.com/
http://codepad.org/
http://compileonline.com/
http://rextester.com/runcode
https://runnable.com/
https://code.hackerearth.com/
http://melpon.org/wandbox
https://paiza.io/

宿題は宿題スレがあるのでそちらへ。

※前スレ
プログラミングのお題スレ Part14
http://mevius.5ch.net/test/read.cgi/tech/1558168409/
465デフォルトの名無しさん
垢版 |
2019/09/16(月) 17:36:43.98ID:enU8we0d
>>464
なるほど全然わからん
2019/09/16(月) 20:23:01.84ID:LKlzCOg4
>>460 Pharo/Squeak Smalltalk

(1 to: 100) asArray shuffled first: 30
467デフォルトの名無しさん
垢版 |
2019/09/17(火) 17:37:59.85ID:IoM9hprN
プログラム言語標準の文字列(e.g. java.lang.String) が与えられ、ランレングス圧縮した結果をプログラム言語標準の文字列で返す関数を作成してください。ただし、入力に数字が入っていた場合のケースは考慮しなくても構いません。
テストケース(関数名 = f)
f("あいうえお") -> "あいうえお"
f("ああいいうう") -> "あ2い2う2"
f("あいうあいう") -> "あいうあいう"
2019/09/17(火) 18:15:13.62ID:kyKgfAv0
あうあうあー -> あうあうあー
ああああ -> あ4
2019/09/17(火) 18:37:25.07ID:IoM9hprN
そゆこと
2019/09/17(火) 19:42:43.30ID:N7hzq5cx
python に あ2い2う2
のようにカウンティング結果を含んだリストを返すライブラリが
はいっていたな
2019/09/17(火) 21:40:53.07ID:nuFS/S7T
>>467
haskellだよー

join.map ((:).head <*> (bool "".show.length <*> (1 <).length)).group
472デフォルトの名無しさん
垢版 |
2019/09/17(火) 21:47:22.78ID:s7WBnLNZ
>>467 JavaScript
const f = s => [...s]
.reduce((acc, ch) => {
const [lastCh, n = 1] = acc.pop() || [];
if (!lastCh) return [[ch]];
if (lastCh == ch) {
return [...acc, [lastCh, n + 1]];
} else {
return [...acc, [lastCh, n == 1 ? '' : n], [ch]];
}
}, [])
.flat(Infinity)
.join``;
2019/09/17(火) 23:32:50.16ID:htBCID9n
>>467 Perl5

use utf8;
binmode STDOUT, ':encoding(utf-8)';
for (qw{あいうえお ああいいうう あいうあいう あうあうあー ああああ}) {
 my $s;
 while (/((.)\g-1*)/g) {
  $s .= $2;
  $s .= $l if 1 < ($l = length $1);
 }
 print "$s\n";
}


実行結果
~ $ perl 15_467.pl
あいうえお
あ2い2う2
あいうあいう
あうあうあー
あ4
2019/09/17(火) 23:58:08.34ID:htBCID9n
>>467 Perl5、しくった、お題は「関数を作れ」だった…orz

use utf8;
binmode STDOUT, ':encoding(utf-8)';
sub f {
 my $s;
 while (/((.)\g-1*)/g) {
  $s .= $2;
  $s .= $l if 1 < ($l = length $1);
 }
 $s
}
for (qw{あいうえお ああいいうう あいうあいう あうあうあー ああああ}) {
 print f($_)."\n";
}


~ $ perl 15_467_f.pl
あいうえお
あ2い2う2
あいうあいう
あうあうあー
あ4
2019/09/18(水) 10:00:45.90ID:Qm7jF2Kv
>>467 Ruby

f = -> s {s.gsub(/(.)\1+/){[$1, $&.size].join}}

%w[あいうえお ああいいうう あいうあいう あうあうあー ああああ].each{|s| puts [s, f[s]].join(' => ')}
# =>
あいうえお => あいうえお
ああいいうう => あ2い2う2
あいうあいう => あいうあいう
あうあうあー => あうあうあー
ああああ => あ4
2019/09/18(水) 10:37:56.54ID:Qm7jF2Kv
>>432 Ruby

module Hoge
  refine Array do
    def oddlist
      select.with_index{|s, i| i.odd?}
    end
  end
end

class C1
  p [0, 1, 2, 3, 4].oddlist rescue p$!
  # => #<NoMethodError: undefined method `oddlist' for []:Array>
end

class C2
  using Hoge
  p [0, 1, 2, 3, 4].oddlist
  # => [1, 3]
end
2019/09/18(水) 15:45:23.52ID:GIOjMe2C
>>467
https://ideone.com/3jC9wU
C++。いい感じにかけた。
478デフォルトの名無しさん
垢版 |
2019/09/18(水) 17:30:11.83ID:quy23QLp
むずかし過ぎワロタwww
俺にC++は絶対ムリwwwww
2019/09/18(水) 17:40:05.70ID:GIOjMe2C
>>478
自分の福祉のために圧迫しない程度に冗長に書いてるけど、ショートコーディングももちろんできるよ。
記憶力と直感と若干の経験で書いている。
2019/09/18(水) 19:34:59.80ID:Dukdxvvo
完成品はないけどなw
2019/09/18(水) 19:48:16.27ID:GIOjMe2C
>>480
ウインドウズのアプリっぽいの一個位は作ったことあるよ。
2019/09/18(水) 19:56:52.36ID:Dukdxvvo
はいはいいつもの
2019/09/18(水) 20:22:10.02ID:GIOjMe2C
>>482
ボット認定していい?
2019/09/18(水) 22:47:30.76ID:Zy9clRCR
>>483
bot(だと自分で思ってる)相手にレスしちゃう男の人なんて……
2019/09/19(木) 03:17:38.21ID:fzYUHnaE
お題: 明日の東京都の天気を教えてくれる「お天気ねえさん」を実装せよ。

https://www.drk7.jp/weather/
ここのXMLデータを参考に、明日の東京都の天気を優しく説明してくれるような説明テキストを生成せよ。

例) 明日XX月YY日の東京都の天気は○○です。○○注意報が出ています。傘を忘れないでね。
2019/09/19(木) 03:40:46.99ID:fzYUHnaE
ある種のデータは石油のように重要だから、データ自動生成は条件が揃えばお金儲けにつながる。まあ、とことんチャレンジしたまえ。
2019/09/19(木) 04:53:29.17ID:kXMoSLFb
>>485
sh と perl5
https://pastebin.com/3dXzkYvR
https://i.imgur.com/G3ehbXa.png

colorlizerの中がperl入りで
見やすくするユーティリティー
2019/09/19(木) 20:26:10.81ID:tW2U1Dga
google の検索結果を 10 づつ合計 1000 件ダウンロードするプログラム
2019/09/19(木) 20:38:41.32ID:fzYUHnaE
>>488
ボットはあかん
490デフォルトの名無しさん
垢版 |
2019/09/19(木) 21:01:33.69ID:Iq+eqHsU
>>467 Lua
マルチバイト文字には対応していない。
function f(s)
   local r,c="",1
   for i=1,#s do
   if s:sub(i,i)==s:sub(i+1,i+1) then
   c=c+1
   else
     r=r..s:sub(i,i)
   if c>1 then
   r=r..c
   c=1
   end
   end
   end
   return(r)
end
print(f("aaabbc"))

実行結果
a3b2c
2019/09/19(木) 21:11:19.84ID:VNRPdI7o
>>460 octave
https://ideone.com/0BVjSC

>>467 ruby
https://ideone.com/GR4Uu2
2019/09/19(木) 21:20:20.35ID:7Zlc7qce
>>467
perl5
デバッガが日本語で化けるんで英語だけ
print("aabbccddd" =~ s{ (.) \1* }{ "$1" . length"$&" }xgre);
a2b2c2d3
2019/09/20(金) 00:35:55.08ID:brDhMnbX
>>485 Perl5 (LWP::UserAgent、LWP::Protocol::https、XML::Simple のinstallしてあるPCで)

# -*- coding: utf-8 -*-
use utf8; use Data::Dump 'dump'; use Data::Dumper;
binmode STDOUT, ':encoding(utf-8)';
use feature say;
$pno = 13; # ex) 01:北海道, 13:東京, 20:長野, 47:沖縄
$url = "https://www.drk7.jp/weather/xml/$pno.xml";;
require LWP::UserAgent; # with LWP::Protocol::https
$ua = LWP::UserAgent->new;
$ua->agent('Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; Touch; rv:11.0) like Gecko');
$xml = LWP::UserAgent->new->get($url)->content;
use XML::Simple;
my $xs = new XML::Simple; #(keyattr => ['area']); # (forcearray => ['info'], keyattr => ['pref']);
my $data = $xs->XMLin($xml);
say "$_: $data->{$_}" for qw{title link description pubDate author managingEditor};
$pref = $data->{pref}; say "都道府県: $pref->{id}";
%area = %{$pref->{area}};
for $region (keys %area) {
 $area = $area{$region};
 printf "地域: $region (緯度=%s, 経度=%s)\n", @{$area->{geo}}{qw{lat long}};
 for $info (@{$area->{info}}) {
  say " $info->{date} 天気:$info->{weather} (画像 $info->{img})";
  $info->{$_} and say ' 'x9, $info->{$_} for qw{weather_detail wave};
  %temp = %{$info->{temperature}};
  say ' 'x6, " 気温[$temp{unit}]: ", join 'から', sort map{"$_->{content}°"} @{$temp{range}};
  %rfc = %{$info->{rainfallchance}};
  say ' 'x6, " 降水確率 : ", join ', ', map{"$_->{hour}時:$_->{content}$rfc{unit}"} @{$rfc{period}};
 }
}
2019/09/20(金) 00:38:16.81ID:brDhMnbX
実行結果(例)

~ $ perl 15_485.pl
title: weather forecast xml
link: http://www.drk7.jp/weather/xml/13.xml
description: 気象庁の天気予報情報を XML で配信。1日1回 AM 6:00 ごろ更新。
pubDate: Thu, 19 Sep 2019 18:00:01 +0900
author: 気象庁
managingEditor: drk7.jp
都道府県: 東京都
地域: 伊豆諸島南部 (緯度=33.2419, 経度=139.8418)
2019/09/19 天気:くもり時々雨 (画像 http://www.drk7.jp/MT/images/MTWeather/202.gif)
         北東の風 やや強く くもり 夜のはじめ頃 雨 八丈島 では 夜遅く 雷を伴い 激しく 降る
         波 3メートル うねり を伴う
       気温[摂氏]: 21°から23°
       降水確率 : 00-06時:0%, 06-12時:80%, 12-18時:50%, 18-24時:50%
2019/09/20 天気:くもり時々雨 (画像 http://www.drk7.jp/MT/images/MTWeather/203.gif)
         東の風 やや強く くもり 一時 雨
         波 4メートル のち 3メートル ただし 三宅島 では 3メートル
       気温[摂氏]: 20°から23°
       降水確率 : 00-06時:50%, 06-12時:60%, 12-18時:60%, 18-24時:50%
2019/09/21 天気:くもり一時雨 (画像 http://www.drk7.jp/MT/images/MTWeather/202.gif)
       気温[摂氏]: 21°から26°
       降水確率 : 00-06時:70%, 06-12時:70%, 12-18時:70%, 18-24時:70%
2019/09/22 天気:くもり一時雨 (画像 http://www.drk7.jp/MT/images/MTWeather/202.gif)
       気温[摂氏]: 23°から29°
       降水確率 : 00-06時:50%, 06-12時:50%, 12-18時:50%, 18-24時:50%
2019/09/23 天気:くもり一時雨 (画像 http://www.drk7.jp/MT/images/MTWeather/202.gif)
       気温[摂氏]: 25°から29°
       降水確率 : 00-06時:50%, 06-12時:50%, 12-18時:50%, 18-24時:50%
…(後略
2019/09/20(金) 01:09:22.21ID:brDhMnbX
お天気おねぃさんとは、ほど遠いなコリャ…
2019/09/20(金) 04:34:57.12ID:vpcMUgg5
お題

以下の、列区切りが空白である入力文字列を、
最後の列の数値が、小さい順番で、行を並べ替えよ

答えは、上下が反転する

a b c 44
x y 33
z 22
11
2019/09/20(金) 07:37:10.18ID:ZVVuMGcz
>>496
cat text
a b c 44
x y 33
z 22
11

cat text | perl -pe 's/.*? \s? (\d+)/$1:$&/xgm' | sort -n | perl -pe 's/^\d+://'
11
z 22
x y 33
a b c 44
498デフォルトの名無しさん
垢版 |
2019/09/20(金) 08:14:00.92ID:ImrTQZdo
>>467
Common Lisp https://ideone.com/1VXrLk
Perl https://ideone.com/8SVG3V
JavaScript https://ideone.com/Cn73uX
2019/09/20(金) 09:52:16.00ID:PYkAN+VQ
>>496 Ruby

puts <<~EOS.each_line.sort_by{|s| s[/\S+\n?/].to_i}
  a b c 44
  x y 33
  z 22
  11
EOS

# =>
11
z 22
x y 33
a b c 44
2019/09/20(金) 13:59:42.96ID:fZRbtgUM
>>489
合法的? にやるにはどうすればいいの?
2019/09/20(金) 14:08:52.93ID:qcElLt/f
>>500
初手でウエイト入れないで読みに行って、アクセス規制法に引っかかるのとかありそう。
鯖落としたら、怒られるからな。警察に。
502デフォルトの名無しさん
垢版 |
2019/09/20(金) 15:01:57.40ID:8HAndpSr
>>496 J
echo@>(/: {: @:(0&".) &.>) <;._2 stdin ''
503デフォルトの名無しさん
垢版 |
2019/09/20(金) 15:07:26.20ID:uyZD/oLi
http://appmarketinglabo.net/osaka-bakurotalk/
Q、hamonさん、ここだけの面白い話は何かありますか?

hamon:
絶対にこれをつくれば海外で10万DLされるという「超鉄板ジャンル」がある。
それは「3Dのリアルな人間をつかった実在のスポーツゲーム」、中身がクソでも10万DLはされる。
「ボクシングvs腕相撲」という実質もぐらたたきのようなゲームが30万DLいった。
「スキージャンプ アルティメイタム」というゲームが10万DLくらい。
マイナーなスポーツでもそのような結果なので、競技人口のベスト10のスポーツを調べてつくれば10万DLは固い。
普通につくるとコストがかかるが、UNITYでつくればストア上でモーションも売っているのでかなり稼げると思う。
504デフォルトの名無しさん
垢版 |
2019/09/21(土) 00:27:57.30ID:dB3ZeAcF
お題: 同じ文字列がいくつあるか数えるプログラムを作成しなさい。
※出力は出現回数順でソートされている必要はありません。

入力
Hoge
Fuga
Foo
Fuga
Foo
Hoge
Bar
Fuga

出力
Hoge 2
Fuga 3
Foo 2
Bar 1
2019/09/21(土) 00:39:37.54ID:6wFtRpPA
>>504 Perl5

$h{$_}++ for qw{Hoge Fuga Foo Fuga Foo Hoge Bar Fuga};
print "$_ $h{$_}\n" for keys %h;


実行結果
~ $ perl 15_504.pl
Fuga 3
Hoge 2
Bar 1
Foo 2
2019/09/21(土) 00:45:45.24ID:Ei1MM/mp
>>504
https://ideone.com/5PI58a
C++。今回はメインロジックの記述が少なくて楽だった。
2019/09/21(土) 00:46:28.49ID:YPz6oCKs
>>504
cat text
Hoge
Fuga
Foo
Fuga
Foo
Hoge
Bar
Fuga

cat text | perl -ne '$dirs{ s/\R//r } += 1; END{for $key (keys %dirs) {printf "%-4s %s\n", $key, $dirs{$key} } }'
Foo 2
Fuga 3
Bar 1
Hoge 2
2019/09/21(土) 00:50:19.09ID:Ei1MM/mp
うほっ。C++のunordered_mapはオーダー要求しないから、そこを使って最適化物故むとかやるんかいな。
VCとGCCと挙動違う。
2019/09/21(土) 00:59:47.45ID:6wFtRpPA
>>496 Perl5

sub f {$_[0] =~ /(\d+)/; $1};
print sort{ f($a) <=> f($b) } <DATA>;
__DATA__
a b c 44
x y 33
z 22
11


実行結果
~ $ perl 15_496.pl
11
z 22
x y 33
a b c 44
510デフォルトの名無しさん
垢版 |
2019/09/21(土) 04:32:25.24ID:DxluyDWg
>>496
Kotlin
https://paiza.io/projects/6vObtBsKGF8CSWLHW8cUSQ
511デフォルトの名無しさん
垢版 |
2019/09/21(土) 05:21:21.47ID:DxluyDWg
>>504
Kotlin
https://paiza.io/projects/iaaP87ST43fSPdMkOiqSGQ
512デフォルトの名無しさん
垢版 |
2019/09/21(土) 05:37:07.36ID:RmdAWOHY
>>504 Ruby
a=readlines.map(&:chomp)
a.uniq.each{|v| puts("#{v} #{a.count(v)}")}
2019/09/21(土) 15:59:00.83ID:uZ0K83Mw
>>496 ruby
https://ideone.com/VtI7C7
puts readlines.sort_by {|s| s[/\d+$/].to_i}

>>504 ruby
https://ideone.com/Sw6k17
puts $<.read.scan(/\w+/).group_by(&:itself).map {|a, b| '%s %d' % [a, b.size]}
2019/09/21(土) 16:12:27.45ID:ZyFDPzPp
お題:正の整数が与えられるので、英語の序数に変換せよ
1 => 1st
23 => 23rd
12 => 12th
2019/09/21(土) 16:40:16.71ID:6wFtRpPA
>>514 Perl5

%o = qw{1 st 2 nd 3 rd};
$o = $o{$_}//'th', print "$_$o\n" for 1..10,12,23;

実行
~ $ perl 15_514.pl
1st
2nd
3rd
4th
5th
6th
7th
8th
9th
10th
12th
23th
2019/09/21(土) 16:54:30.22ID:6wFtRpPA
>>514 Perl5、 >>515 はもう少しコンパクトに書けた

%o = qw{1 st 2 nd 3 rd};
printf "$_%s\n", $o{$_}//'th' for 1..10,12,23;
2019/09/21(土) 17:38:27.55ID:uZ0K83Mw
>>514 ruby
https://ideone.com/3Mupna
518デフォルトの名無しさん
垢版 |
2019/09/21(土) 18:24:27.24ID:9SoaGQ4l
>>504
Common Lisp https://ideone.com/VW5xja
Perl https://ideone.com/kfn4xI
519デフォルトの名無しさん
垢版 |
2019/09/21(土) 19:12:36.02ID:AojYT9zq
1st 2nd 3rd
11th 12th 13th
21st 22nd 23rd
101st 102nd 103rd
111th 112th 113th
121st 122nd 123rd

皆さんここらへん大丈夫?
2019/09/21(土) 19:41:28.17ID:6wFtRpPA
>>514 Perl5、>>519 で指摘された誤りの修正 テヘペロ

%h = qw{1 st 2 nd 3 rd};
for (1..4,11,12,13,21,22,23,101,102,103,111,112,113,121,122,123) {
 /(\d?)(\d)$/;
 printf "$_ => $_%s\n", $1 eq 1 ? 'th' : $h{$2}//'th';
}


実行結果
~ $ perl 15_514.pl
1 => 1st
2 => 2nd
3 => 3rd
4 => 4th
11 => 11th
12 => 12th
13 => 13th
21 => 21st
22 => 22nd
23 => 23rd
101 => 101st
102 => 102nd
103 => 103rd
111 => 111th
112 => 112th
113 => 113th
121 => 121st
122 => 122nd
123 => 123rd
2019/09/21(土) 20:51:54.21ID:BmADEcuL
お題: 与えられた西暦年月日を年号年月日に変換しなさい。ただし、変換するのは大正時代から令和までとし、範囲外であればERRORを表示しなさい。
2019/09/21(土) 21:04:33.10ID:BmADEcuL
お題: YYYY/MM/DD形式で与えられた西暦年月日の文字列が正しい年月日を表しているかどうか判定せよ。
2019/09/21(土) 21:24:10.22ID:BmADEcuL
>>522, >>521の順に解いて下さい。

1970/01/05
1990/12/31
2019/04/28
2019/05/01
1920/02/29
2019/09/21(土) 22:12:07.89ID:y97V3EOz
>>522 Perl5

use Time::Piece;
for (<DATA>) {
 chomp;
 my $t;
 eval {$t = Time::Piece->strptime($_, '%Y/%m/%d')};
 $t //= 'Invalid';
 print "$_: $t\n";
}
__DATA__
1970/01/05
1990/12/31
2019/04/28
2019/05/01
1920/02/29
0000/13/32


実行結果
1970/01/05: Mon Jan 5 00:00:00 1970
1990/12/31: Mon Dec 31 00:00:00 1990
2019/04/28: Sun Apr 28 00:00:00 2019
2019/05/01: Wed May 1 00:00:00 2019
1920/02/29: Sun Feb 29 00:00:00 1920
0000/13/32: Invalid
525524
垢版 |
2019/09/21(土) 22:29:42.44ID:y97V3EOz
>>524

Time::Piece->strptime は
1900/01/01 よりも前の日付だと
正しい西暦の年月日を入力しても
parse結果が不定となってしまうということがさっき分かりました。
したがって残念ながら>>524のcodeは>>522の題意を満たしているとは
言いがたいものでした。ゴメンね〜
2019/09/21(土) 22:37:53.07ID:tuxPF//K
>>521-523 Java
https://ideone.com/5XMVPB
2019/09/21(土) 22:44:04.41ID:7nrsNPcd
令和1年を令和元年にしないといけないという落とし穴があるようだ。
2019/09/21(土) 22:45:06.50ID:Awwp+36u
>>504 Ruby

p $<.map(&:chomp).yield_self{|e| e.uniq.map{|v| [v, e.count(v)]}}

# => [["Hoge", 2], ["Fuga", 3], ["Foo", 2], ["Bar", 1]]
2019/09/21(土) 23:03:57.96ID:tuxPF//K
>>527 Java
https://ideone.com/q1JPJ5
しゅうせいしまった
2019/09/22(日) 01:12:38.88ID:35++XhB6
>>496 Squeak Smalltalk

#('a b c 44' 'x y 33' 'z 22' '11') sortBy: [:x | x splitInteger last] ascending

"=> an OrderedCollection('11' 'z 22' 'x y 33' 'a b c 44') "
2019/09/22(日) 01:25:15.35ID:35++XhB6
>>504 Pharo/Squeak Smalltalk

'Hoge
Fuga
Foo
Fuga
Foo
Hoge
Bar
Fuga' lines asBag sortedElements

"=> {'Bar'->1 . 'Foo'->2 . 'Fuga'->3 . 'Hoge'->2} "
2019/09/22(日) 01:56:51.52ID:35++XhB6
>>467 Pharo/Squeak Smalltalk

| fn |

fn := [:str | String streamContents: [:ss |
(str as: RunArray) runsAndValuesDo: [:n :x |
ss nextPut: x; nextPutAll: (n = 1 ifTrue: [''] ifFalse: [n asString])
]
]].

fn value: 'あいうえお'. "=> 'あいうえお' "
fn value: 'ああいいうう'. "=> 'あ2い2う2' "
fn value: 'あいうあいう'. "=> 'あいうあいう' "
2019/09/22(日) 08:19:31.82ID:uuB9aO9i
>>514 Ruby

suffixes = %w[th st nd rd]
f = -> n {(-(-((n - 10) / 10 % 10) / 9)) * 41 / (n % 10 * 10 + 11) * (n % 10)}

[
  1, 2, 3, 4, 11, 12, 13, 21, 22, 23, 101, 102, 103, 111, 112, 113, 121, 122, 123
].each{|v| puts '%1$d => %1$d%2$s' % [v, suffixes[f[v]]]}

# =>
1 => 1st
2 => 2nd
3 => 3rd
4 => 4th
11 => 11th
12 => 12th
13 => 13th
21 => 21st
22 => 22nd
23 => 23rd
101 => 101st
102 => 102nd
103 => 103rd
111 => 111th
112 => 112th
113 => 113th
121 => 121st
122 => 122nd
123 => 123rd
2019/09/22(日) 08:57:02.33ID:yY5QhyPL
>>533
ruby知らんけどそのfって何?
2019/09/22(日) 09:05:16.49ID:8u0YwnlN
ラムダ式じゃねーの
おれもruby知らんが
2019/09/22(日) 09:16:58.95ID:W3ewN8v1
(dolist (n (list 1 2 3 11 12 13 21 22 23 101 102 103 111 112 113 121 122 123) ) (format t "~:R~%" n))
first
second
third
eleventh
twelfth
thirteenth
twenty-first
twenty-second
twenty-third
one hundred first
one hundred second
one hundred third
one hundred eleventh
one hundred twelfth
one hundred thirteenth
one hundred twenty-first
one hundred twenty-second
one hundred twenty-third
2019/09/22(日) 09:21:41.49ID:uuB9aO9i
>>534-535
ラムダ式.
任意の自然数 n に対して次の式は n の序数詞の末尾が th なら 0, st なら 1, nd なら 2, rd なら 3 を返す.(除算は切り捨て)
(-(-((n - 10) / 10 % 10) / 9)) * 41 / (n % 10 * 10 + 11) * (n % 10)
538デフォルトの名無しさん
垢版 |
2019/09/22(日) 10:14:00.13ID:UtdvR7ZT
>>514 Lua
function f(n)
   local r,x
   x=n%10
   if n%100-x==10 or x<1 or x>3 then
   r="th"
   else
   r=({"st","nd","rd"})[x]
   end
   return n..r
end
539デフォルトの名無しさん
垢版 |
2019/09/22(日) 11:43:05.12ID:OEThTvH6
>>514 JavaScript
const f=n=>n+=[,'st','nd','rd'][n%100>>3^1&&n%10]||'th'
実行結果略
540デフォルトの名無しさん
垢版 |
2019/09/22(日) 11:51:21.39ID:OEThTvH6
>>522, >>521 JavaScript
const isValid = seireki => {
let check
try {
check = new Intl
.DateTimeFormat('ja-JP', {year: 'numeric', month: '2-digit', day: '2-digit'})
.format(new Date(seireki))
} catch (e) { return false }
return seireki === check ? true : false
}
const seirekiToWareki = seireki => {
if (!isValid(seireki)) return 'ERROR'
const wareki = new Intl
.DateTimeFormat('ja-JP-u-ca-japanese', {era: 'long', year: 'numeric', month: 'numeric', day: 'numeric'})
.format(new Date(seireki))
.replace(/(正|和|成)1(年)/, '$1元$2')
return ['大正', '昭和', '平成', '令和'].includes(wareki.slice(0, 2)) ? wareki : 'ERROR'
}
`1979/01/05
1990/12/31
2019/04/28
2019/05/01
1920/02/29
0000/13/32`
.split`\n`.forEach(seireki => {console.log(seirekiToWareki(seireki))})
実行結果:
昭和54年1月5日
平成2年12月31日
平成31年4月28日
令和元年5月1日
大正9年2月29日
ERROR
2019/09/22(日) 14:55:10.69ID:yzO2ACAf
>>521-523 Perl5

use Time::Piece; #use Data::Dump 'dump';
@fy = (['2019/05/01', '令和'], ['1989/01/08', '平成'], ['1926/12/25', '昭和'], ['1912/07/30', '大正']);
$$_[0] = Time::Piece->strptime($$_[0], '%Y/%m/%d') for @fy;
for (qw{1970/01/05 1990/12/31 2019/04/28 2019/05/01 1920/02/29 1912/07/29 2019/02/29}) {
 my ($t, $t0, $era, $nen);
 eval {$t = Time::Piece->strptime($_, '%Y/%m/%d')};
 if ($t and $t->strftime('%Y/%m/%d') eq $_) {
  do {($t0, $era) = @$_, last if $$_[0] <= $t} for @fy;
  if ($t0) {
   $nen = ($y = $t->year - $t0->year, $y ? $y + 1 : '元').'年' if $t0;
   $t = "$era$nen".$t->mon.'月'.$t->mday.'日';
  } else {
   $t = 'ERROR';
  }
 } else {
  $t = 'Invalid';
 }
 print "$_: $t\n";
}

実行結果
~ $ perl 15_521.pl
1970/01/05: 昭和45年1月5日
1990/12/31: 平成2年12月31日
2019/04/28: 平成31年4月28日
2019/05/01: 令和元年5月1日
1920/02/29: 大正9年2月29日
1912/07/29: ERROR
2019/02/29: Invalid
2019/09/22(日) 14:59:46.00ID:yzO2ACAf
>>541
一行目のコメント
#use Data::Dump 'dump';
はdebugの際に使ったstatementの消し忘れにつき、不要
2019/09/22(日) 15:13:26.78ID:yzO2ACAf
>>541
$nen = ($y = $t->year - $t0->year, $y ? $y + 1 : '元').'年' if $t0;
の「 if $t0」も削除し忘れだな、ゴメンチャイ
2019/09/22(日) 16:19:32.48ID:iYBBHKU1
https://github.com/systemd/systemd/blob/82d1264048a3768fb8238387810f78c5d3912058/src/journal/journalctl.c#L2405-L2461
このif elseの羅列をリファクタリングしてください(´・ω・`)
2019/09/22(日) 17:24:37.46ID:W3ewN8v1
無駄に否定演算子で分かりにくくしている
if (!arg_reverse)
r = sd_journal_next_skip(j, 1 + after_cursor);
else
r = sd_journal_previous_skip(j, 1 + after_cursor);
2019/09/22(日) 17:45:39.81ID:vTpi5vA1
>>544
http://hengband.es.land.to/hengband-1.7.0-svn/

このあたりのspells1.cのproject_fのほうが豪華だよ
2019/09/22(日) 18:17:09.14ID:yzO2ACAf
>>521 Perl5 >>541 を少しコンパクトにしてみた

use Time::Piece;
@fy = (['2019/05/01','令和'],['1989/01/08','平成'],['1926/12/25','昭和'],['1912/07/30','大正']);
$$_[0] = Time::Piece->strptime($$_[0], '%Y/%m/%d') for @fy;
for (qw{1970/01/05 1990/12/31 2019/04/28 2019/05/01 1920/02/29 1912/07/29 2019/02/29}) {
 my ($t, $t0, $era, $nen);
 eval {$t = Time::Piece->strptime($_, '%Y/%m/%d')};
 $s = 'Invalid';
 if ($t and $t->strftime('%Y/%m/%d') eq $_) {
  do {($t0, $era) = @$_, last if $$_[0] <= $t} for @fy;
  $s = 'ERROR';
  if ($t0) {
   $nen = ($y = $t->year - $t0->year, $y ? $y + 1 : '元').'年';
   $s = "$era$nen" . $t->mon . '月' . $t->mday . '日';
  }
 }
 print "$_: $s\n";
}
2019/09/22(日) 20:58:52.87ID:Pb++lLAf
お題: マイドキュメントにある全てのファイルの更新日時を取得し、曜日ごとに平均時刻を集計せよ。
549デフォルトの名無しさん
垢版 |
2019/09/22(日) 21:11:37.57ID:PDMeYfrK
マイドキュメントがなかった
2019/09/22(日) 21:23:02.16ID:Pb++lLAf
お題: あるフォルダにある全てのファイルの更新日時を全て同じ日時にリセットせよ。
2019/09/22(日) 21:30:08.53ID:WdPSTicU
>>521-523 ruby 2.5.5
https://ideone.com/RLhbll
・令和対応はRuby 2.6.3から
2019/09/22(日) 21:35:34.35ID:rZK31NHQ
※未来の日時にするとMakefileなどで不具合の原因になるかもしれないので、やめよう。必ず過去の日付で。
2019/09/22(日) 21:40:33.36ID:2VFOPinw
>>1
◆T6も書き込み禁止
2019/09/22(日) 22:00:19.44ID:rZK31NHQ
>>553
カレンダー苦手?
そりゃごめんね。誰でも解けるような問題解いて面白い? もしかして
2019/09/22(日) 22:02:04.64ID:35++XhB6
とりあえずNGにした
2019/09/22(日) 22:03:45.82ID:35++XhB6
うっかり回答しないように連鎖にしとく方がいいか
2019/09/22(日) 22:23:23.82ID:W3ewN8v1
>>548
\ls -l --time-style=+%a:%T ~/ | perl -lane 'print $F[5]' | sed "/^$/d" | perl -F: -lane '{++$i{$F[0]}; $acc{$F[0]} += $F[1]*3600 + $F[2] *60 + $F[3] }END{ print "$_ ". $acc{$_}/$i{$_} . " seconds" for keys %acc}'
日 49262.7 seconds
金 52984.3125 seconds
火 56316.8461538462 seconds
木 47056.3333333333 seconds
月 53660.7894736842 seconds
土 49998.1538461538 seconds
水 48117.5 seconds
2019/09/22(日) 22:24:24.30ID:W3ewN8v1
スルーだったか
2019/09/22(日) 22:44:17.87ID:rZK31NHQ
お題: 自分のプログラムファイルが何バイトかを表示せよ。
2019/09/22(日) 22:48:43.53ID:Yf8464Y3
>>548 Perl5、なお>>550は(utimeを使えばできますが)回答は作成いたしません

use File::Find 'find';
sub hier {
 my $mtime = (lstat)[9];
 push @epochs, $mtime if -f _;
}
find({wanted => \&hier}, "$ENV{USERPROFILE}/My Documents");
@w = ([],[],[],[],[],[],[]);
for (@epochs) {
 ($sec, $min, $hour, $wday) = (localtime $_)[0..2,6];
 push @{$w[$wday]}, 3600*$hour + 60*$min + sec;
}
use List::Util 'sum';
use Time::Piece;
@dw = qw(日 月 火 水 木 金 土);
for (0..6) {
 $n = @{$w[$_]};
 $mean = sum(@{$w[$_]}) / $n;
 $s = Time::Piece->new($mean);
 printf "$dw[$_](%4d): %s\n", $n, $s->hms;
}

実行結果
~ $ perl 15_548.pl
日( 104): 21:04:43
月( 94): 22:57:25
火( 46): 21:41:46
水( 298): 12:55:47
木( 322): 05:04:39
金(2020): 07:01:42
土(1650): 22:50:54
2019/09/22(日) 22:50:50.35ID:JOdgUuSI
>>548
馬鹿っぽ
2019/09/22(日) 22:53:39.99ID:JOdgUuSI
蟻人間、プログラミングに興味を持ち始めた中学生臭がすごい
2019/09/22(日) 23:00:26.05ID:rZK31NHQ
>>559
はいはい、模範解答出せばいいんでしょ?

// C++/Win32
#include <windows.h>
#include <stdio.h>
int main(void)
{
char buf[MAX_PATH];
GetModuleFileNameA(NULL, buf, MAX_PATH);
WIN32_FIND_DATAA find;
HANDLE hFind = FindFirstFileA(buf, &find);
printf("%ld bytes", find.nFileSizeLow);
FindClose(hFind);
return 0;
}
2019/09/22(日) 23:03:43.50ID:rZK31NHQ
>>559 別解ね。
/* C */
#include <stdio.h>
int main(int argc, char **argv)
{
FILE *fp = fopen(argv[0], "rb");
fseek(fp, 0, SEEK_END);
printf("%ld bytes\n", ftell(fp));
fclose(fp);
return 0;
}
■ このスレッドは過去ログ倉庫に格納されています
5ちゃんねるの広告が気に入らない場合は、こちらをクリックしてください。