Created
November 1, 2012 06:57
-
-
Save vglebov/3992240 to your computer and use it in GitHub Desktop.
Решение задачи из учебника
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Задача из учебника по ПХП: | |
| Создайте программу, которая трансформирует первое предложение во второе, и выводит результат. Оба предложения представлены ниже: | |
| A. Теперь пора всем хорошим людям прийти на помощь стране; | |
| Б. Пора теперь стране прийти на помощь всем хорошим людям. | |
| Интересно, как бы ее решили знакомые разработчики. Просьба код выкладывать на gist.github.com |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class testForIstomin extends PHPUnit_Framework_TestCase | |
| { | |
| public function testReplaceWordsByMapAndAdjustCase() | |
| { | |
| $map = array(1, 0, 8, 5, 6, 7, 2, 3, 4); | |
| $original = 'Теперь пора всем хорошим людям прийти на помощь стране'; | |
| $actual = $this->replaceWordsByMapAndAdjustCase($original, $map); | |
| $expected = 'Пора теперь стране прийти на помощь всем хорошим людям'; | |
| $this->assertEquals($expected, $actual); | |
| } | |
| private function replaceWordsByMapAndAdjustCase($str, $map) | |
| { | |
| $words = preg_split('/ /', $str); | |
| $words[0] = $this->toLowerCaseFirst($words[0]); | |
| $result = array(); | |
| foreach ($map as $word_num) { | |
| $result[] = $words[$word_num]; | |
| } | |
| $result[0] = $this->toUpperCaseFirst($result[0]); | |
| return join(' ', $result); | |
| } | |
| function toLowerCaseFirst($str, $encoding = 'utf-8') | |
| { | |
| preg_match_all("/^(?<first_letter>.)(?<remains>.*)$/u", $str, $matches); | |
| return mb_strtolower($matches['first_letter'][0], $encoding) . $matches['remains'][0]; | |
| } | |
| function toUpperCaseFirst($str, $encoding = 'utf-8') | |
| { | |
| preg_match_all("/^(?<first_letter>.)(?<remains>.*)$/u", $str, $matches); | |
| return mb_strtoupper($matches['first_letter'][0], $encoding) . $matches['remains'][0]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment