Upgrade to Pro — share decks privately, control downloads, hide ads and more …

今日の"どう書く"問題のアルゴリズム例

 今日の"どう書く"問題のアルゴリズム例

Takashi Kanemoto

February 13, 2019
Tweet

More Decks by Takashi Kanemoto

Other Decks in Programming

Transcript

  1. ͨ ͭ ͖ ͪ twitter.com/ttskch ͋͞ɺ͋ͳͨ΋ࠓ͙͢ϑΥϩʔ͠Α͏!!! ׬શແྉ ·͞ʹ໊ݴ੡଄ػ ਓੜͰେ੾ͳ͜ͱ͸ɺ͢΂ͯ ͖͔ͨͭͪΒڭΘͬͨ

    ͖ͨͭͪΛϑΥϩʔͨ͠Β
 ࠊ௧͕࣏Γ·ͨ͠ 100% φϯτʂ ϑΥϩϫʔ͔Βͷࢧ࣋཰ 100% ͍ͯ͠Δ ͍ͯ͠ͳ͍ ϑΥϩϫʔ͞Μ100ਓʹฉ͖·ͨ͠ ͖ͨͭͪΛϑΥϩʔ͍ͯ͠Δʁ 5/39
  2. We are hiring!ʢϚδͰʣ 10 • PHPer/JSer ઈࢍେืूதʂʢϦϞʔτ΍όΠτ΋Մʣ • Symfony+Angular ͰɺWeb޿ࠂۀքΛ


    ϗϫΠτʹ͢Δ࢓૊ΈΛ࡞ͬͯ·͢ • ܦӦϏδϣϯ͸ʮੈքҰޮ཰తͳ୅ཧళʯ /39
  3. $count = 0; for ($i = $min; $i < $max;

    $i++) { $number = base_convert(strval($i), 10, $base); if ($number === strrev($number)) { $count++; } } return $count; 16 ࠷ॳʹॻ͍ͨίʔυ ۪௚ʹ૯౰ͨΓͰ൑ఆ͍ͯ͘͠΍ͭ /39
  4. public function dataProvider() { return [ ['12,34,5', 5], ['10,11,10', 0],

    ['1,100,3', 18], : : ['66160071,153732445,5', 5605], ['18799557,189007582,14', 33741], ['78547566,225312226,20', 18346], ['143084571,506549072,18', 62323], ['2099642384,2789567569,6', 14787], ]; } 17 ςετέʔε /39
  5. 1. ܻ਺͕ۮ਺͔ح਺͔ 23 ࠨ൒෼ ӈ൒෼ ճจ਺ͷܻ਺  12 23 3

     12 34 4  123 345 5  123 456 6 ح਺ܻͷ৔߹͸ɺਅΜதͷܻΛࠨӈ྆ํʹؚΊΔ /39
  6. 2. ࠨ൒෼Λ1૿΍͔͢Ͳ͏͔ 25 ࠨ൒෼ ӈ൒෼ ࠨ൒෼ͷ൓స ࣍ͷճจ਺  10 00

    01 101  12 20 21 121  12 21 21 131  12 23 21 131 ӈ൒෼ >= ࠨ൒෼ͷ൓స ͷͱ͖͸ࠨ൒෼Λ1૿΍͢ < < >= >= /39
  7. $ tree src src ├── Dokaku14.php └── Palindrome ├── Finder.php

    ├── Generator.php └── Operator.php 29 ΍Δ͜ͱ͕ෳࡶͳͷͰΫϥε෼͚ ࣍ͷճจ਺Λݟ͚ͭΔ αʔϏε ʮࠨ൒෼ʯΛݩʹ ճจ਺Λ࡞ΔαʔϏε จࣈྻʹର͢ΔܭࢉΛ αϙʔτ͢ΔαʔϏε /39
  8. 30 ݺͼग़͠ݩ͸͜Μͳײ͡ $count = 0; $current = base_convert(strval($min - 1),

    10, $base); while (base_convert($current, $base, 10) < $max) { $current = $this->finder->findNextPalindrome($current, $base); $count++; } $count--; return $count; ࣍ͷճจ਺·Ͱ௚઀ਐΊΔͷͰܭࢉྔେ෯Χοτ /39
  9. public function findNextPalindrome(string $number, int $base): string { list($leftHalf, $rightHalf)

    = $this->operator->cutIntoHalf($number); $palindromeLength = strlen($number); if ($this->operator->compare($rightHalf, strrev($leftHalf), $base) >= 0) { $halfLength = strlen($leftHalf); $leftHalf = $this->operator->increment($leftHalf, $base); if (strlen($leftHalf) > $halfLength) { $palindromeLength++; } } return $this->generator->generate($leftHalf, $palindromeLength); } 31 Finderͷ࣮૷͸͜Μͳײ͡ /39