DEV Community

Discussion on: Daily Challenge #264 - Digital Root

Collapse
 
j_mplourde profile image
Jean-Michel Plourde • Edited

Here's my solution in Python. Not bad considering I gave it only 5 minutes. I could have gone with reduce and lambda, but it's not really pythonic, not fast and the reading would be terrible. I like the short circuit behavior.

def digital_root(n):
  while True:
    n_list = [int(d) for d in str(n)]
    if len(n_list) == 1:
      return n
    n = sum(n_list)