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

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

8e844e43f029ec38e206a464cf4587b3?s=128

Takashi Kanemoto

February 13, 2019
Tweet

Transcript

  1. 1 ࠓ೔ͷ”Ͳ͏ॻ͘”໰୊ͷ ΞϧΰϦζϜྫ 2019/02/13 #nagoyaphp /39

  2. ๻ʹ͍ͭͯ 2/39

  3. ͖ͨͭͪʢ໊͋ͩʣ 3 • (ג)ΧϧςοτίϛϡχέʔγϣϯζCTO • WebΤϯδχΞྺ7೥ɺPHPྺ7೥ • #nagoyaphpʊ • #symfony_meetup

    /39
  4. ͖ͨͭͪʢ໊͋ͩʣ 4 • ͓ख఻͍ͨ͠ຊʢ2015/12ʣ • Schooߨࢣʢ2018/04ʣ ʮGitHubΛ࢖ͬͨσϓϩΠࣗಈԽ࣮ફʯ ʮجຊ͔Β͔ͬ͠ΓֶͿ Symfony2ೖ໳ʯ /39

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

    ͖ͨͭͪΛϑΥϩʔͨ͠Β
 ࠊ௧͕࣏Γ·ͨ͠ 100% φϯτʂ ϑΥϩϫʔ͔Βͷࢧ࣋཰ 100% ͍ͯ͠Δ ͍ͯ͠ͳ͍ ϑΥϩϫʔ͞Μ100ਓʹฉ͖·ͨ͠ ͖ͨͭͪΛϑΥϩʔ͍ͯ͠Δʁ 5/39
  6. ձࣾʹ͍ͭͯ 6/39

  7. (ג)Χϧςοτίϛϡχέʔγϣϯζ 7 https://quartet-communications.com /39

  8. (ג)Χϧςοτίϛϡχέʔγϣϯζ 8 https://www.google.co.jp/search?q=Χʔςϯ /39

  9. (ג)Χϧςοτίϛϡχέʔγϣϯζ 9 https://lisket.jp /39

  10. We are hiring!ʢϚδͰʣ 10 • PHPer/JSer ઈࢍେืूதʂʢϦϞʔτ΍όΠτ΋Մʣ • Symfony+Angular ͰɺWeb޿ࠂۀքΛ


    ϗϫΠτʹ͢Δ࢓૊ΈΛ࡞ͬͯ·͢ • ܦӦϏδϣϯ͸ʮੈքҰޮ཰తͳ୅ཧళʯ /39
  11. We are hiring!ʢϚδͰʣ 11 • ࣗࣾαʔϏεʮLisketʯͰࣗࣾͷۀ຿΋ޮ཰Խ • શࣾһͷฏۉ࢒ۀ͕࣌ؒ5h/݄͙Β͍Ͱ͢ ಛผ৆ड৆ /39

  12. We are hiring!ʢϚδͰʣ 12 ΤϯδχΞͷಇ͖΍͢͞௒େࣄʹͯ͠·͢✨ /39

  13. We are hiring!ʢϚδͰʣ 13 ·ͣ͸͓࿩͚ͩͰ΋ʂؾܰʹ੠͔͚͍ͯͩ͘͞ /39

  14. 14 ࠓ೔ͷ”Ͳ͏ॻ͘”໰୊ͷ ΞϧΰϦζϜྫ 2019/02/13 #nagoyaphp /39

  15. ໰୊ͷ಺༰ 15 http://nabetani.sakura.ne.jp/hena/orde17palin/ /39

  16. $count = 0; for ($i = $min; $i < $max;

    $i++) { $number = base_convert(strval($i), 10, $base); if ($number === strrev($number)) { $count++; } } return $count; 16 ࠷ॳʹॻ͍ͨίʔυ ۪௚ʹ૯౰ͨΓͰ൑ఆ͍ͯ͘͠΍ͭ /39
  17. 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
  18. ݁Ռ 18 ܭࢉྔ͕େ͖͗ͯ͢λΠϜΞ΢τ… /39

  19. ͪΐͬͱ ಄Λ࢖ͬͯΈΔ 19/39

  20. ਓؒ͸Ͳ͏΍ͬͯղ͍͍ͯΔ͔ 20 ྫʣ10ਐ਺ͷ123ʙ150Ͱճจ਺Λ୳͢৔߹ /39

  21. ਓؒ͸Ͳ͏΍ͬͯղ͍͍ͯΔ͔ 20 123 ྫʣ10ਐ਺ͷ123ʙ150Ͱճจ਺Λ୳͢৔߹ /39

  22. ਓؒ͸Ͳ͏΍ͬͯղ͍͍ͯΔ͔ 20 123 131 ྫʣ10ਐ਺ͷ123ʙ150Ͱճจ਺Λ୳͢৔߹ /39

  23. ਓؒ͸Ͳ͏΍ͬͯղ͍͍ͯΔ͔ 20 123 131 141 ྫʣ10ਐ਺ͷ123ʙ150Ͱճจ਺Λ୳͢৔߹ /39

  24. ਓؒ͸Ͳ͏΍ͬͯղ͍͍ͯΔ͔ 20 123 131 141 151 ྫʣ10ਐ਺ͷ123ʙ150Ͱճจ਺Λ୳͢৔߹ /39

  25. ਓؒ͸Ͳ͏΍ͬͯղ͍͍ͯΔ͔ 20 123 131 141 151 ྫʣ10ਐ਺ͷ123ʙ150Ͱճจ਺Λ୳͢৔߹ /39

  26. ਓؒ͸Ͳ͏΍ͬͯղ͍͍ͯΔ͔ 20 • 1ͣͭ૿΍͠ͳ͕Βʮ͜Ε͸ճจ਺͔ʁʯ ͳΜͯ΍ͬͯͳ͍ • ճจ਺͕ࠨӈରশͳ͜ͱΛར༻͍ͯ͠Δ • ࠨ൒෼Λ1ͣͭ૿΍͠ͳ͕ΒڸจࣈྻΛ ࡞ͬͯΔ

    123 131 141 151 ྫʣ10ਐ਺ͷ123ʙ150Ͱճจ਺Λ୳͢৔߹ /39
  27. ͍͔ͭ͘ύλʔϯ͕͋Δͳʁ 21 • ܻ਺͕ۮ਺͔ح਺͔ʢ123ͱ1234ʣ • ࠨ൒෼Λͦͷ··൓స͢Δ͔ɺ1૿΍͔ͯ͠Β ൓స͢Δ͔ʢ120→121ͱ123→131ʣ • ࠨ൒෼Λ1૿΍ͨ͠ͱ͖ʹܻ਺͕૿͑Δ͔૿͑ ͳ͍͔ʢ99→101ͱ88→99ʣ

    /39
  28. /39 ͍͔ͭ͘ύλʔϯ͕͋Δͳʁ 22 • ܻ਺͕ۮ਺͔ح਺͔ʢ123ͱ1234ʣ • ࠨ൒෼Λͦͷ··൓స͢Δ͔ɺ1૿΍͔ͯ͠Β ൓స͢Δ͔ʢ120→121ͱ123→131ʣ • ࠨ൒෼Λ1૿΍ͨ͠ͱ͖ʹܻ਺͕૿͑Δ͔૿͑

    ͳ͍͔ʢ99→101ͱ88→99ʣ
  29. 1. ܻ਺͕ۮ਺͔ح਺͔ 23 ࠨ൒෼ ӈ൒෼ ճจ਺ͷܻ਺  12 23 3

     12 34 4  123 345 5  123 456 6 ح਺ܻͷ৔߹͸ɺਅΜதͷܻΛࠨӈ྆ํʹؚΊΔ /39
  30. /39 ͍͔ͭ͘ύλʔϯ͕͋Δͳʁ 24 • ܻ਺͕ۮ਺͔ح਺͔ʢ123ͱ1234ʣ • ࠨ൒෼Λͦͷ··൓స͢Δ͔ɺ1૿΍͔ͯ͠Β ൓స͢Δ͔ʢ120→121ͱ123→131ʣ • ࠨ൒෼Λ1૿΍ͨ͠ͱ͖ʹܻ਺͕૿͑Δ͔૿͑

    ͳ͍͔ʢ99→101ͱ88→99ʣ
  31. 2. ࠨ൒෼Λ1૿΍͔͢Ͳ͏͔ 25 ࠨ൒෼ ӈ൒෼ ࠨ൒෼ͷ൓స ࣍ͷճจ਺  10 00

    01 101  12 20 21 121  12 21 21 131  12 23 21 131 ӈ൒෼ >= ࠨ൒෼ͷ൓స ͷͱ͖͸ࠨ൒෼Λ1૿΍͢ < < >= >= /39
  32. ͍͔ͭ͘ύλʔϯ͕͋Δͳʁ 26 • ܻ਺͕ۮ਺͔ح਺͔ʢ123ͱ1234ʣ • ࠨ൒෼Λͦͷ··൓స͢Δ͔ɺ1૿΍͔ͯ͠Β ൓స͢Δ͔ʢ120→121ͱ123→131ʣ • ࠨ൒෼Λ1૿΍ͨ͠ͱ͖ʹܻ਺͕૿͑Δ͔૿͑ ͳ͍͔ʢ99→101ͱ88→99ʣ

    /39
  33. 3. ࠨ൒෼Λ1૿΍ܻͯ͠਺͕૿͑Δ͔ 27 1૿΍ͨ͠ͱ͖ʹࠨ൒෼ͷܻ਺͕૿͑Δ৔߹͸
 ճจ਺ͷܻ਺΋ݩͷ਺ࣈΑΓ૿͑Δ ࠨ൒෼ 1૿΍͢ͱ ࣍ͷճจ਺ ճจ਺ͷܻ਺ 

    8 9 99 2  9 10 101 3  19 20 202 3 /39
  34. ίʔυʹͯ͠ΈΔ 28/39

  35. $ tree src src ├── Dokaku14.php └── Palindrome ├── Finder.php

    ├── Generator.php └── Operator.php 29 ΍Δ͜ͱ͕ෳࡶͳͷͰΫϥε෼͚ ࣍ͷճจ਺Λݟ͚ͭΔ αʔϏε ʮࠨ൒෼ʯΛݩʹ ճจ਺Λ࡞ΔαʔϏε จࣈྻʹର͢ΔܭࢉΛ αϙʔτ͢ΔαʔϏε /39
  36. 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
  37. 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
  38. ෳࡶʹݟ͑Δ͚Ͳ 32 ͜ΕΒͷॲཧΛ͍ͯ͠Δ͚ͩͰ͢ /39

  39. GeneratorͱOperator͸ 33 • ಺༰୯७ͳͷͰ͜͜Ͱ͸ׂѪ͠·͢
 ʢGitHubʹ্͕ͬͯΔͷͰݟͯΈ͍ͯͩ͘͞ʣ /39

  40. ಈ͔ͯ͠ΈΔ 34/39

  41. 35 υϠΝʂ /39

  42. ·ͱΊ 36/39

  43. ΊͬͪΌ಄࢖ͬͯർΕ·ͨ͠ 37 • ͳΜ͔ͩΜͩ׬੒·Ͱ਺͔͔࣌ؒΓ·ͨ͠ • ࢴͷ্Ͱ࣮ݧ͠ͳ͕Βߟ͑Δͷେࣄˣ /39

  44. ιʔε͸GitHubʹ্͛ͯ·͢✋ 38 https://github.com/ ttskch/nagoyaphp14 /39

  45. 39 @ttskch ʘThanks!ʗ /39