Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete elements of an integer recursively

My parameter, n is a phone number as an integer.

Using recursion I want to return the first three numbers in the integer.

I've turned the integer into a list of individual number characters and I'm attempting to delete the last number over and over again until I'm left with the last three, but I'm stuck on how to repeat it.

def areaCodes(n):
    n = str(n)  
    n = list(n)
    del n[-1] 
    #n = reduce(opperator.add, n)
    n =  ''.join(n)
    n = int(n)
    return n

I know I'm supposed to repeat the name in the return somehow, but because n isn't an integer that I can use to repeat. What do I do?

like image 306
Malaikatu Kargbo Avatar asked Nov 14 '16 22:11

Malaikatu Kargbo


1 Answers

How about something like this?

def areaCodes(n):
  # if n is less than 1000, what does it mean about the number of digits?
  if n < 1000:
    return  # fill... 

  # otherwise, if n is greater than 1000, how can we alter n to remove the last
  # digit? (hint: there's an operation similar to division called f...r division)
  return areaCodes( # operate on n somehow...)
like image 194
גלעד ברקן Avatar answered Oct 12 '22 10:10

גלעד ברקן