1

For my recursion assignment, the only built in function we are allowed to use is len() and 'print()'. So, not even int() is allowed to be used. The function I am supposed to make is supposed to break apart a number and print out each digit in the number. So,

>>breakApart(757890)

7
5
7
8
9
0

This is what I have come up with so far,

def breakApart(number):
    c=0
    while number//10**c>=10:
        c+=1
    integer_part=((number-(number%10**c))//(10**c))
    if number//10**c==0:
        return
    elif number//10**c>0:
        print(integer_part)
        return breakApart(number%10**c)

This works for all numbers, except it will not print out 0s. I was thinking of writing something along the lines of,

if (number%10**c)>+10 and (number%10**c)//(10**(c-1))<1:
    print(0)
    return breakApart(number%10**c)

But if there are consecutive 0s, it will not print all of them. I guess I could use another counter like I did at the beginning, but if anyone could think of less convoluted ways for me to approach this, please let me know.

DrJessop
  • 462
  • 6
  • 26

3 Answers3

3

Use recursion to your advantage here, making the print call come after your recursive call:

In [1]: def tell_digits(n):
   ...:     rest, first = n // 10, n % 10
   ...:     if rest:
   ...:         tell_digits(rest)
   ...:     print(first)
   ...:

In [2]: tell_digits(757890)
7
5
7
8
9
0

Note what happens if I put the print call before the recursive call:

In [3]: def tell_digits(n):
   ...:     rest, first = n // 10, n % 10
   ...:     print(first)
   ...:     if rest:
   ...:         tell_digits(rest)
   ...:

In [4]: tell_digits(757890)
0
9
8
7
5
7
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
  • Thank you very much! I don't want to steal this answer, however, since it deviates quite a bit from my code, but I'll use it as an example –  Jul 26 '17 at 16:53
  • Also, I'm a bit of a beginner, so I am trying to understand your code. What does "if rest" mean? If rest is what? –  Jul 26 '17 at 17:02
  • 1
    @sneakysnake `if rest:` checks if `rest` is [*falsey*](https://stackoverflow.com/questions/39983695/what-is-truthy-and-falsy-in-python-how-is-it-different-from-true-and-false). Also, see the [docs](https://docs.python.org/3/library/stdtypes.html#truth-value-testing). In this case, it is the same as `if rest != 0:` – juanpa.arrivillaga Jul 26 '17 at 17:05
0

If number can be a string:

def breakApart(number):
    if len(number) == 0:
        return
    else:
        print(number[0])
        breakApart(number[1:])

Or reverse it:

def breakApart(number):
    if len(number) == 0:
        return
    else:
        breakApart(number[:-1])
        print (number[-1])
Easton Bornemeier
  • 1,918
  • 8
  • 22
  • No, we were given an example of the input being an integer type, not string type. –  Jul 26 '17 at 16:50
0

Something to keep you up at night:

>>> def breakApart(number):
...     return number > 9 and breakApart(number // 10) or print(number % 10)
... 
>>> breakApart(757890)
7
5
7
8
9
0
>>> 

Python 3 specific, of course.

cdlane
  • 40,441
  • 5
  • 32
  • 81
  • This might take me a little while to understand, but thank you for another interesting answer :) –  Jul 28 '17 at 13:36