Python Day 19: Interfaces

The AdvancedArithmetic interface and the method declaration for the abstract divisorSum(n) method are provided for you in the editor below.

Complete the implementation of Calculator class, which implements the AdvancedArithmetic interface. The implementation for the divisorSum(n) method must return the sum of all divisors of n.

-HackerRank solution

  1. class AdvancedArithmetic(object):
  2. def divisorSum(n):
  3. raise NotImplementedError
  4. class Calculator(AdvancedArithmetic):
  5. def divisorSum(self, n):
  6. #pass
  7. x = []
  8. for i in range(1, n+1):
  9. if n % i == 0:
  10. x.append(i)
  11. else:
  12. pass
  13. return sum(x)
  14. n = int(input())
  15. my_calculator = Calculator()
  16. s = my_calculator.divisorSum(n)
  17. print("I implemented: " + type(my_calculator).__bases__[0].__name__)
  18. print(s) is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube