9

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?

2
  • Must you use recursion? Commented Nov 14, 2016 at 22:54
  • Use a while loop to iterate until size is three Commented Nov 14, 2016 at 22:56

4 Answers 4

3

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...)
Sign up to request clarification or add additional context in comments.

Comments

2

I assume that this is an exercise where recursion is necessary. If so, try this (there are better ways to accomplish your end goal, but I tried to modify your existing code as little as possible):

def areaCodes(n): 
    n_lst = list(str(n))
    del n_lst[-1]
    n_str =  ''.join(n_lst)
    n_int = int(n_str)
    if len(n_lst) > 3:
        return areaCodes(n_int)
    return n_int

This will call the function again if the length of the number is greater than three, and return the number otherwise. Basically, the only part you were missing in your original function was the following, which is the recursive part:

if len(n_lst) > 3:
    return areaCodes(n_int)

Comments

2

Remember that for a function to be recursive, it will have two main attributes:

  1. It will at some point call itself. (this is what makes it 'repeat')
  2. It will have some stopping condition (or base case).

You mentioned #1 when you wrote that you're supposed to use "the name in the return," so that's great! You just need to write that in your code:

return areaCodes(n), Where n is the updated phone number with a digit removed.

As you can see, each recursive call should do some work towards the solution, and should pass its mini-solution to the next recursive call.

Along with #2 above, you need to specify a base case, where the recursion will cease. So, since you're taking away a digit each time you call your function, you should include some kind of check to see if the current input is the length you want yet.

If it is the right length, you're done, and you should return the current number (not another recursive call).

Otherwise, you aren't done with the recursion yet.

Comments

1
import sys

def areaCodes(n): 
#Create a list 
myList = list(str(n))
#Delete last element
del myList[-1]
#Combine your elements into string list
myListStr =  ''.join(myList)
#Type cast to int
myListInt = int(myListSte)

#Check whether your list satisfies your condition
if len(myList) > 3:
    #Recusivley call the function again
    return areaCodes(myListInt)

#Return your list when recursion completes    
return myListInt


n = 12345
print areaCodes(n)

3 Comments

The loop should have the condition len(n) > 3 otherwise it will end up in an infinite loop when given inputs shorter than 3.
My bad will fix it
how is this recursive?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.