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

Exercism: Armstrong Numbers (Ruby)

Exercism: Armstrong Numbers (Ruby)

A presentation outlining my solution to the Exercism Armstrong Numbers problem in Ruby (https://exercism.io/tracks/ruby/exercises/armstrong-numbers).

Presented at the Sydney Ruby on Rails meetup on 10 September 2019.

Presentation slide deck markdown and speaker notes (useable in Deckset 2):
https://github.com/paulfioravanti/presentations/tree/master/exercism_ruby_armstrong_numbers

Abda861707b1e78e0fce47ced55f84ee?s=128

Paul Fioravanti

September 10, 2019
Tweet

Transcript

  1. Armstrong Numbers

  2. None
  3. None
  4. Narcissist Number

  5. Armstrong Number

  6. Sum of its own digits

  7. Raised to the power of the number of its digits

  8. Armstrong Numbers

  9. Armstrong Numbers ✅ 9

  10. Armstrong Numbers ✅ 9 = 9^1 = 9

  11. Armstrong Numbers ✅ 9 = 9^1 = 9 ❌ 10

  12. Armstrong Numbers ✅ 9 = 9^1 = 9 ❌ 10

    != 1^2 + 0^2 = 1
  13. Armstrong Numbers ✅ 153 = 1^3 + 5^3 + 3^3

    = 153
  14. Armstrong Numbers ✅ 153 = 1^3 + 5^3 + 3^3

    = 153 ❌ 154 != 1^3 + 5^3 + 4^3 = 190
  15. Armstrong Numbers

  16. Armstrong Numbers ! Get the number of digits

  17. Armstrong Numbers ! Get the number of digits ! Calculate

    the power of each digit
  18. Armstrong Numbers ! Get the number of digits ! Calculate

    the power of each digit ! Sum all the powers
  19. ! Take 1

  20. Get number of digits

  21. Get number of digits module ArmstrongNumbers module_function def include?(number) end

    end
  22. ArmstrongNumbers.include?(154)

  23. Get number of digits module ArmstrongNumbers module_function def include?(number) end

    end
  24. Get number of digits module ArmstrongNumbers module_function def include?(number) number

    end end
  25. Get number of digits module ArmstrongNumbers module_function def include?(number) number

    .digits end end
  26. Sum Powers module ArmstrongNumbers module_function def include?(number) number .digits .then(&method(:sum_powers))

    end end
  27. Sum Powers module ArmstrongNumbers module_function def include?(number) sum = number

    .digits .then(&method(:sum_powers)) end end
  28. Sum Powers module ArmstrongNumbers module_function def include?(number) sum = number

    .digits .then(&method(:sum_powers)) sum == number end end
  29. Sum Powers module ArmstrongNumbers module_function # ... def sum_powers(digits) end

    private_class_method :sum_powers end
  30. Sum Powers module ArmstrongNumbers module_function # ... def sum_powers(digits) digits

    end private_class_method :sum_powers end
  31. Sum Powers module ArmstrongNumbers module_function # ... def sum_powers(digits) digits

    .each end private_class_method :sum_powers end
  32. Sum Powers module ArmstrongNumbers module_function # ... def sum_powers(digits) digits

    .each .with_object(digits.length) end private_class_method :sum_powers end
  33. Sum Powers module ArmstrongNumbers module_function # ... def sum_powers(digits) digits

    .each .with_object(digits.length) .sum(&method(:power)) end private_class_method :sum_powers end
  34. Calculate Power of Each Digit module ArmstrongNumbers module_function # ...

    def power((digit, length)) digit**length end private_class_method :power end
  35. module ArmstrongNumbers module_function def include?(number) sum = number .digits .then(&method(:sum_powers))

    sum == number end def sum_powers(digits) digits .each .with_object(digits.length) .sum(&method(:power)) end private_class_method :sum_powers def power((digit, length)) digit**length end private_class_method :power end
  36. None
  37. None
  38. exercism submit armstrong_numbers.rb

  39. Done!

  40. Done?

  41. Complex...?

  42. ! Take 2

  43. module ArmstrongNumbers module_function def include?(number) end end

  44. module ArmstrongNumbers module_function def include?(number) digits = number.digits end end

  45. module ArmstrongNumbers module_function def include?(number) digits = number.digits length =

    digits.length end end
  46. module ArmstrongNumbers module_function def include?(number) digits = number.digits length =

    digits.length number == digits.sum { |digit| digit**length } end end
  47. None
  48. Choose Your Own Adventure

  49. You'll rarely get it right the first *me

  50. Submit o)en, get feedback

  51. Read other people's code

  52. Refactor un,l you're happy

  53. github.com/paulfioravanti/exercism

  54. BONUS FACTOIDS!

  55. BONUS FACTOIDS! ! Number of Armstrong numbers is finite

  56. BONUS FACTOIDS! ! Number of Armstrong numbers is finite !

    Only 88 Armstrong numbers in Base 10
  57. 115,132,219,018, 763,992,565,095, 597,973,971,522,401

  58. None
  59. Thanks! @paulfioravanti