{"id":96,"date":"2021-10-24T16:28:02","date_gmt":"2021-10-24T07:28:02","guid":{"rendered":"https:\/\/th0x0472.net\/?p=96"},"modified":"2021-10-24T16:28:02","modified_gmt":"2021-10-24T07:28:02","slug":"hackerrank-compute-the-average","status":"publish","type":"post","link":"https:\/\/th0x0472.net\/index.php\/2021\/10\/24\/96\/","title":{"rendered":"HackerRank: Compute the Average"},"content":{"rendered":"<p><a href=\"https:\/\/www.hackerrank.com\/\" target=\"_blank\" rel=\"noopener\">HackerRank<\/a>\u306e<a href=\"https:\/\/www.hackerrank.com\/challenges\/bash-tutorials---compute-the-average\/problem\" target=\"_blank\" rel=\"noopener\">\u3053\u3061\u3089\u306e\u554f\u984c &#8220;Compute the Average&#8221;<\/a>\u3092\u89e3\u304d\u307e\u3059\u3002<!--more--><\/p>\n<p>\u554f\u984c\u306f\u30bf\u30a4\u30c8\u30eb\u901a\u308a\u3001\u4e0e\u3048\u3089\u308c\u305f\u6570\u5b57\u305f\u3061\u306e\u5e73\u5747\u3092\u6c42\u3081\u308b\u3068\u3044\u3046\u30e2\u30ce\u3002\u50d5\u306e\u56de\u7b54\u306f\u3053\u3061\u3089\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>read N\r\nSUM=0\r\n\r\nfor ((i=0;i&lt;${N};i++));do\r\n    read X\r\n    SUM=$((${SUM}+${X}))\r\ndone\r\nprintf \"%.3f\\n\" $(echo ${SUM}\/${N} | bc -l)\r\n<\/code><\/pre>\n<\/div>\n<p>\u5165\u529b\u306e1\u884c\u76ee\u3067\u4e0e\u3048\u3089\u308c\u308b\u6570\u5b57\u306e\u500b\u6570<em>N<\/em>\u3092\u8aad\u307f\u53d6\u308a\u3001\u4ee5\u4e0b\u7565\u3002\u898b\u305f\u307e\u3093\u307e\u3067\u3059\u3002<br \/>\n\u3053\u306e\u304f\u3089\u3044\u306a\u3089awk\u306e\u30ef\u30f3\u30e9\u30a4\u30ca\u30fc\u3067\u3044\u3051\u308b\u3093\u3058\u3083\u306a\u3044\u306e\u3001\u3068\u601d\u3063\u3066\u4f5c\u3063\u305f\u5225\u89e3\u304c\u3053\u3061\u3089\u3067\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>awk 'NR==1{N=$0;SUM=0} NR&gt;1{SUM=SUM+$0} END{printf(\"%.3f\\n\",SUM\/N)}'<\/code><\/pre>\n<\/div>\n<p>\u30b3\u30ec\u3082\u89e3\u8aac\u306f\u7701\u7565\u3067\u3059\u3002<\/p>\n<p>\u3053\u306e\u554f\u984c\u3092\u89e3\u3044\u3066\u3044\u304f\u904e\u7a0b\u30672\u3064\u307b\u3069\u65b0\u3057\u3044\u6c17\u3065\u304d\u304c\u3042\u3063\u305f\u306e\u3067\u3001\u30b7\u30a7\u30a2\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002\u30b5\u30de\u30ea\u3068\u3057\u3066\u306f\u3053\u3061\u3089\u3002<\/p>\n<ol>\n<li>bc\u30b3\u30de\u30f3\u30c9\u3088\u308a$(())\u306e\u307b\u3046\u304c\u8efd\u3044<\/li>\n<li>bc\u30b3\u30de\u30f3\u30c9\u306f\u56db\u6368\u4e94\u5165\u3058\u3083\u306a\u304f\u3066\u5207\u308a\u6368\u3066\u3059\u308b\uff08\u3060\u304b\u3089\u6700\u5f8c\u306e\u51fa\u529b\u306fprintf\u3092\u4ecb\u3057\u3066\u307e\u3059\uff09<\/li>\n<\/ol>\n<p>2\u306f\u305d\u306e\u307e\u3093\u307e\u3067\u3059\u30021\u306b\u3064\u3044\u3066\u3001\u3082\u3046\u3061\u3087\u3044\u8a73\u3057\u304f\u8aac\u660e\u3057\u307e\u3059\u3002<\/p>\n<p>\u6c17\u3065\u3044\u305f\u30ad\u30c3\u30ab\u30b1\u306f\u3042\u308b\u30c6\u30b9\u30c8\u30b1\u30fc\u30b9\u3067\u6642\u9593\u5207\u308c\u3067NG\u3068\u306a\u3063\u305f\u3053\u3068\u3067\u3059\u3002\u305d\u306e\u3068\u304d\u306f\u3001for\u30eb\u30fc\u30d7\u306e\u4e2d\u306e\u8a08\u7b97\u3082bc\u30b3\u30de\u30f3\u30c9\u3067\u3084\u3063\u3066\u307e\u3057\u305f\u3002\u6bd4\u8f03\u7528\u306e\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u66f8\u3044\u3066time\u30b3\u30de\u30f3\u30c9\u3067\u6bd4\u8f03\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<p>tc1.sh<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>#!\/bin\/bash\r\nfor ((i=0;i&lt;1000000;i++));do\r\n    echo $((1+1))&gt;\/dev\/null\r\ndone<\/code><\/pre>\n<\/div>\n<p>tc2.sh<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>#!\/bin\/bash\r\nfor ((i=0;i&lt;10000;i++));do\r\n    echo 1+1 | bc&gt;\/dev\/null\r\ndone<\/code><\/pre>\n<\/div>\n<p>\u30eb\u30fc\u30d7\u306e\u56de\u6570\u306b\u3054\u6ce8\u610f\u304f\u3060\u3055\u3044\u3002100\u500d\u9055\u3044\u307e\u3059\u3002<\/p>\n<p>\u3067\u3001\u305d\u308c\u305e\u308c\u8a08\u6e2c\u3057\u305f\u7d50\u679c\u304c\u3053\u3061\u3089\u3067\u3059\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>$ time .\/tc1.sh\r\n\r\nreal 0m7.277s\r\nuser 0m4.475s\r\nsys 0m2.659s\r\n\r\n$ time .\/tc2.sh \r\n\r\nreal 0m19.721s\r\nuser 0m16.516s\r\nsys 0m8.084s<\/code><\/pre>\n<\/div>\n<p>100\u500d\u56de\u3057\u3066\u308btc1.sh\u306e\u307b\u3046\u304c\u901f\u3044\u3067\u3059\u3002<br \/>\n\u30d6\u30ec\u30fc\u30b9\u5c55\u958b\u3067\u306fseq\u306b\u8ca0\u3051\u3066\u305fBash\u3067\u3059\u304c\u3001\u3084\u308c\u3070\u3067\u304d\u308b\u5b50\u3067\u3059\uff08\u9055\u3002<br \/>\n\u3055\u3066\u3001\u3053\u3046\u306a\u308b\u3068\u6c17\u306b\u306a\u3063\u3066\u304f\u308b\u306e\u306f\u3001seq\u3068\u30d6\u30ec\u30fc\u30b9\u5c55\u958b\u3001\u305f\u304f\u3055\u3093\u5b9f\u884c\u3057\u305f\u3068\u304d\u306f\u3069\u3061\u3089\u304c\u901f\u3044\u304b\u3001\u3067\u3059\u3002\u8a66\u3057\u3066\u307f\u307e\u3057\u305f\u3002\u7d50\u679c\u306f\u3053\u3061\u3089\u3002<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>$ time for ((i=0;i&lt;10000;i++));do seq 3 &gt;\/dev\/null; done \r\nreal 0m13.548s\r\nuser 0m8.710s\r\nsys 0m5.435s\r\n\r\n$ time for ((i=0;i&lt;10000;i++));do echo {1..3} &gt;\/dev\/null; done\r\n\r\nreal 0m0.128s\r\nuser 0m0.071s\r\nsys 0m0.056s<\/code><\/pre>\n<\/div>\n<p>Bash\u3001\u3067\u304d\u308b\u5b50\uff01\uff01<\/p>\n<p>\u3053\u3093\u306a\u6bd4\u8f03\u3092\u3057\u3066\u308f\u304b\u308b\u306e\u306f\u3001\u591a\u91cd\u30eb\u30fc\u30d7\u56de\u3059\u3068\u304d\u306b\u5916\u5074\u306fseq\u3067\u3001\u5185\u5074\u306f\u30d6\u30ec\u30fc\u30b9\u5c55\u958b\u3067\u3084\u308b\u307b\u3046\u304c\u65e9\u305d\u3046\u3068\u3044\u3046\u3053\u3068\u304f\u3089\u3044\u3067\u3059\u3002\u304c\u3001\u305d\u3093\u306a\u4e8b\u3092\u3057\u305f\u3089\u53ef\u8aad\u6027\u304c\u843d\u3061\u307e\u3059\u3002\u53ef\u8aad\u6027\u3068\u30b9\u30d4\u30fc\u30c9\u306f\u306a\u304b\u306a\u304b\u4e21\u7acb\u3057\u307e\u305b\u3093\u306d\u3047\u3002<\/p>\n<p>\u4eca\u65e5\u306f\u3053\u3053\u307e\u3067\u3067\u3059\u3002\u6700\u5f8c\u307e\u3067\u304a\u4ed8\u304d\u5408\u3044\u3044\u305f\u3060\u304d\u3042\u308a\u304c\u3068\u3046\u3054\u3056\u3044\u307e\u3057\u305f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>HackerRank\u306e\u3053\u3061\u3089\u306e\u554f\u984c &#8220;Compute the Average&#8221;\u3092\u89e3\u304d\u307e\u3059\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,3,4],"tags":[],"class_list":["post-96","post","type-post","status-publish","format-standard","hentry","category-blog","category-hackerrank","category-linux"],"_links":{"self":[{"href":"https:\/\/th0x0472.net\/index.php\/wp-json\/wp\/v2\/posts\/96","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/th0x0472.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/th0x0472.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/th0x0472.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/th0x0472.net\/index.php\/wp-json\/wp\/v2\/comments?post=96"}],"version-history":[{"count":8,"href":"https:\/\/th0x0472.net\/index.php\/wp-json\/wp\/v2\/posts\/96\/revisions"}],"predecessor-version":[{"id":104,"href":"https:\/\/th0x0472.net\/index.php\/wp-json\/wp\/v2\/posts\/96\/revisions\/104"}],"wp:attachment":[{"href":"https:\/\/th0x0472.net\/index.php\/wp-json\/wp\/v2\/media?parent=96"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/th0x0472.net\/index.php\/wp-json\/wp\/v2\/categories?post=96"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/th0x0472.net\/index.php\/wp-json\/wp\/v2\/tags?post=96"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}