æ±å大å¦ã®ä¹¾ã»å²¡å´ç 究室ã®ã¦ã§ããµã¤ãã§ããè¨èªå¦ç 100 æ¬ããã¯ãã¨ããåé¡éãå
¬éããã¦ãã¾ããå®è·µçãªããã°ã©ãã³ã°ã®èª²é¡ã«åãçµã¿ãªããèªç¶è¨èªå¦çã®ç¥èãç¿å¾ã§ãããå
¨ 100 åã®åé¡éã§ãã
www.cl.ecei.tohoku.ac.jp
ããã°ã©ãã³ã°è¨èªã¯ Python ãæ³å®ããã¦ãã¾ãããä»ã®è¨èªã«ã対å¿ãã¦ããã¨ã®ãã¨ã§ããç§ã¯ PHP ã好ããªã®ã§ãPHP ã§ãã®åé¡éã«åãçµãã§ã¿ã¾ã*1ãä»åã¯ç¬¬ä¸ç« ã®åé¡ 00 ããåé¡ 04 ã¾ã§ã解ãã¦ã¿ã¾ãã
00. æååã®éé
æåå"stressed"ã®æåãéã«ï¼æ«å°¾ããå é ã«åãã£ã¦ï¼ä¸¦ã¹ãæååãå¾ãï¼
<?php $input = 'stressed'; $answer = strrev($input); echo $answer, "\n";
PHP ã§ã¯ strrev é¢æ°ã§æååãå転ã§ãã¾ããecho ã§çµæã表示ãã¦ããã¨ããã¯ã«ã³ãã§åºåã£ã¦ãã¾ããããã®ãããã®æ¸ãæ¹ã¯å¥½ã¿ã§ãããããå ¨ä½ãããã«ã¯ã©ã¼ãã§å²ã£ãããã«ã³ãã§ã¯ãªãããªãªãã§æååé£çµã¨ãã¦ãåãçµæã«ãªãã¾ããã¾ããæ¹è¡ã¯ PHP_EOL ã¨ããå®æ°ã使ãã°ç°å¢ã«åãã㦠"\r\n" ã«ãªã£ãããã¾ãããé¢åãªã®ã§ç´æ¥ "\n" ã¨æ¸ãã¦ãã¾ãã
01. ããã¿ãã¯ã«ã·ã¼ã¼ã
ããã¿ãã¯ã«ã·ã¼ã¼ãã¨ããæååã®1,3,5,7æåç®ãåãåºãã¦é£çµããæååãå¾ãï¼
<?php $input = 'ãã¿ãã¯ã«ã·ã¼ã¼'; $answer = ''; foreach ([1, 3, 5, 7] as $n) { $answer .= mb_substr($input, $n - 1, 1); } echo $answer, "\n";
PHP ã«ã¯æååã®ã¹ã©ã¤ã¹ããªãã®ã§ããããªãé¢åã«ãªãã¾ãããforeach ã使ã£ã¦ä¸æåãã¤åãåºãã¦é£çµãã¾ããã¾ããå ¥åæååãæ¥æ¬èªãªã®ã§ãã«ããã¤ãæååé¢æ°ãå©ç¨ããå¿ è¦ãããã¾ãã
foreach ã®å¦çãé¢æ°çã«æ¸ããã¨ãã§ãã¾ãããPHP ã«ã¯ã©ã ãå¼ã®æ§æããªãã®ã§ã以ä¸ã®ããã«çµæ§ãªè¨è¿°éã«ãªã£ã¦ãã¾ãã¾ãããã¾ããã£ãããã¾ãããã
<?php $answer = implode(array_map( function ($n) use ($input) { return mb_substr($input, $n - 1, 1); }, [1, 3, 5, 7]));
02. ãããã«ã¼ãï¼ãã¿ã¯ã·ã¼ãï¼ããã¿ãã¯ã«ã·ã¼ã¼ã
ãããã«ã¼ãï¼ãã¿ã¯ã·ã¼ãã®æåãå é ãã交äºã«é£çµãã¦æååããã¿ãã¯ã«ã·ã¼ã¼ããå¾ãï¼
<?php $input1 = 'ããã«ã¼'; $input2 = 'ã¿ã¯ã·ã¼'; $answer = ''; for ($i = 0; $i < mb_strlen($input1); ++$i) { $answer .= mb_substr($input1, $i, 1); $answer .= mb_substr($input2, $i, 1); } echo $answer, "\n";
ãã®åé¡ããç¹ã«å·¥å¤«ãããã¨ãªã foreach ã§æååãé£çµãã¾ããå ¥åæååã®é·ããç°ãªãå ´åãªã©ã®å¯¾å¿ãç¹ã«ãã¦ãã¾ããã
03. åå¨ç
"Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."ã¨ããæãåèªã«å解ãï¼ååèªã®ï¼ã¢ã«ãã¡ãããã®ï¼æåæ°ãå é ããåºç¾é ã«ä¸¦ã¹ããªã¹ããä½æããï¼
<?php $sentence= 'Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.'; $words = str_word_count($sentence, 1); $answer = array_map('strlen', $words); echo implode(',', $answer), "\n";
str_word_count é¢æ°ã使ã£ã¦å
¥åæãåèªã«å解ãã¾ãããã®é¢æ°ã¯ç¬¬äºå¼æ°ã®å¤ã«ãã£ã¦æ»ãå¤ã®å
容ãå¤ããã1 ãæå®ããã¨ç¬¬ä¸å¼æ°ã®æååãåèªã«å解ããé
åãæ»ãã¾ããåé¡ã®å
¥åæã«ã¯ã«ã³ããããªãªããå«ã¾ãã¾ããããã®é¢æ°ã¯ã¢ã«ãã¡ããã以å¤ã®æåãé¤å»ãã¦ããã¾ãã詳細ã¯ãªãã¡ã¬ã³ã¹ããã¥ã¢ã«ãåç
§ãã¦ãã ããã
PHP: str_word_count - Manual
ååèªã®æåæ°ãæ°ããå¦çã«ã¯ array_map ãå©ç¨ãã¾ãããåé¡ 01 ã§è¿°ã¹ããããªç¡åé¢æ°ã®å ´åã¨ã¯ç°ãªããå®ç¾©æ¸ã¿ã®é¢æ°ãå©ç¨ããå ´åã¯é¢æ°åãæååã¨ãã¦æ¸¡ãã ãã§ãããç°¡åã«æ¸ãã¾ãã
04. å ç´ è¨å·
"Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can."ã¨ããæãåèªã«å解ãï¼1, 5, 6, 7, 8, 9, 15, 16, 19çªç®ã®åèªã¯å é ã®1æåï¼ãã以å¤ã®åèªã¯å é ã«2æåãåãåºãï¼åãåºããæååããåèªã®ä½ç½®ï¼å é ããä½çªç®ã®åèªãï¼ã¸ã®é£æ³é åï¼è¾æ¸åãããã¯ãããåï¼ãä½æããï¼
<?php $sentence = 'Hi He Lied Because Boron Could Not Oxidize Fluorine. New Nations Might Also Sign Peace Security Clause. Arthur King Can.'; $oneLetterIndices = [1, 5, 6, 7, 8, 9, 15, 16, 19]; $words = str_word_count($sentence, 1); $answer = []; for ($i = 0; $i < count($words); ++$i) { $length = in_array($i + 1, $oneLetterIndices) ? 1 : 2; $key = substr($words[$i], 0, $length); $answer[$key] = $i + 1; } echo json_encode($answer, JSON_PRETTY_PRINT), "\n";
åé¡ 03 ã¨åæ§ã« str_word_count ã§å ¥åæãåèªã«å解ããå¾ãé¡æã®é£æ³é åãä½æãã¾ãã
*1:ãã®è¨äºãæ¸ãã¦ããæç¹ã§å ¨ 100 åãçµããããã§ã¯ããã¾ããããã¤ã«ãªããåããã¾ããããã§ããã¨ããã¾ã§å°ããã¤é²ãã¦ã¿ãã¤ããã§ãã