Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7925047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:05:52+00:00 2026-06-03T18:05:52+00:00

def digits(n): res = [] while n > 0: res.append(n % 10) n /=

  • 0
def digits(n):
    res = []
    while n > 0:
        res.append(n % 10)
        n /= 10
    return res

I want to rewrite this function so it uses recursion. I’m currently lost as to what to do. Can anyone give me some direction?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T18:05:54+00:00Added an answer on June 3, 2026 at 6:05 pm

    Here’s a possible solution:

    def digits(n):
        if n < 10:
            return [n]
        return digits(n/10) + [n%10]
    
    digits(123)
    > [1, 2, 3]
    

    The above solution fixes a bug in your code, you were returning the digits in reverse order. Also notice that n must be an integer greater than or equal to zero for producing correct results.

    Here’s how it works:

    1. If the number is less than 10, then return a list with the number, as there are no more digits to be processed
    2. If the number is greater than 9, get the last digit in the current number and add it to the end of the list that results of recursively calling digits on a smaller number – namely, the number without the last digit that we just processed.

    The call to digits(123) will look like this at each step of the recursion:

    digits(123) = digits(123/10) + [3]
    digits(12)  = digits(12/10)  + [2]
    digits(1)   = [1]
    

    Now we go up the call stack:

    [1]
    [1] + [2]
    [1, 2] + [3]
    [1, 2, 3]
    

    EDIT :

    Accepting @thg435’s challenge, here’s a tail-recursive solution:

    def digits(n):
        def loop(i, acc):
            if i < 10:
                return [i] + acc
            return loop(i/10, [i%10] + acc)
        return loop(n, [])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

def sortProfiles(p): return sorted(p, key=itemgetter('first_name')) I have a list with dictionaries. This function allows
[Python 3.1] I'm following up on this answer : class prettyfloat(float): def __repr__(self): return
def self.get(server) return unless server server = server.to_s if klass = @handlers[server] obj =
def record return unless @supported klasses = profile_options[:formats].map { |f| RubyProf.const_get(#{f.to_s.camelize}Printer) }.compact klasses.each do
def myFunc(arg1, arg2): print This is a test with + arg1 + and +
import sys def keepsumming(number): numberlist = [] for digit in str(number): numberlist.append(int(digit)) total =
I have been thinking about this issue and I can't figure it out. Perhaps
I want to set up auto-login by giving the user a link/key they can
import numbers class base62(numbers.Number): digits='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' def __init__(self,value): if isinstance(value,int): self.value=value if value==0: self.string='0' else:
I have a hex-string made from a unicode string with that function: def toHex(s):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.