Giter VIP home page Giter VIP logo

sum_of_digits's Introduction

Sum of digits calculator

Write a class with a method that takes a number as an input and calculates the sum of its digits, until there is a single digit answer. Include the tests.

For examples:

  • When receiving 16 as a parameter it returns 7 (16 --> 1 + 6)
  • When receiving 942 as a parameter it returns 6 (942 --> 9 + 4 + 2 = 15 --> = 1 + 5 = 6)
  • When receiving 99999999999 as a parameter it returns 9 (99999999999 --> 9 + 9 + 9 + 9 + 9 + 9 + 9 + 9 + 9 + 9 + 9 = 99 --> = 9 + 9 = 18 --> = 1 + 8 = 9)

Solution

In order to solve the described problem I created the following structure and files:

  • Gemfile - I have added rspec for specs and require_all gem used to require entire folders when needed.
  • /bin - Having the executable file used to run it from the console + a small validation of the input
  • /lib - Containing actually the sum_of_digits_calculator.rb used to provide us the sing digits sum of the input number
  • /rspec - Containing the specs to validate the implementation

Assumptions

  1. I have restricted the input passed to the calculator to be a positive integer (including 0). Didn;'t created a separate validator as was not discussed or asked before.
  2. I didn't used ruby build in methods like reduce(:+) as I thought are doing to much complex for what we need
    • This could have been a different implementation using reduce
        def self.calculate_sum(num)
          return num if num < 10
      
          sum = num.to_s.split('').map(&:to_i).reduce(:+)
      
          self.calculate_sum(sum)
        end

How to run it

Calculator

> bin/sum_of_digits <number> or ruby bin/sum_of_digits <number> in case permision denied.

Specs

> rspec spec

sum_of_digits's People

Watchers

Alin Banica avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.