Martim Correia
Martim Correia

Reputation: 505

getting only the odd digits in a number with recursion

So my problem is this i have an number like 123 and as the tilte sugests i want the result to be 13.

The problem is that firstly with the method im using im going to get the invert result (31 for example), secondly im getting a zero at the end that shouldn't be there and instead of joining the digits its summing them and i dont understand why. BTW i cant use strings

So to clarify:

My output:

>>> apenas_digitos_impares(123)
40

Correct output:

>>> apenas_digitos_impares(123)
13

program:

def apenas_digitos_impares(n):
    if n == 0:
        return 0
    elif (n%10)%2 == 0:
        return apenas_digitos_impares(n//10)
    elif (n%10)%2 == 1:
        return 10*(n%10) + apenas_digitos_impares(n//10)


Upvotes: 2

Views: 987

Answers (4)

Ahx
Ahx

Reputation: 7985

  • Solution#1: You can do the operation with a single method

import math


def convert_int_to_list(n):
    result = []

    while n > 0:
        if (n % 10) % 2 == 1:
            result.append(n % 10)
        n = math.floor(n / 10)

    return result


print(convert_int_to_list(1345986))

Output:

[9, 5, 3, 1]
  • Solution#2 You don't have to change your method, just declare an array before the method

result = []
  • You want odd numbers, then place the array into the correct place.

 elif (n%10)%2 == 1:
      res.append(n%10)
      return 10*(n%10) + apenas_digitos_impares(n//10)
  • print res

[3, 1]
  • For various examples:

apenas_digitos_impares(1235)
apenas_digitos_impares(12356789)
[5, 3, 1]
[9, 7, 5, 3, 1]

Upvotes: 0

ReverseFlowControl
ReverseFlowControl

Reputation: 826

I mean, if python is where you want the solution just do;

Def pluck_even_digits(N):
    str_N = str(abs(N))

    for digit in [ 0, 2, 4, 6, 8]:
        str_N.replace( str(digit), “”)

    Return int(str_N)

It’s python, you are not restricted to just use pure math. Also, this way allows you to easily choose a base that is not 10 and pluck values from the representation in that new base.

Upvotes: 0

Abhishek Bhagate
Abhishek Bhagate

Reputation: 5766

You could do it as follows -

def apenas_digitos_impares(n):
    if n == 0:
        return 0
    elif (n%10)%2 == 0:
        return apenas_digitos_impares(n//10)
    elif (n%10)%2 == 1:
        # Include the digit and recurse for remaining...
        return (n%10) + 10*apenas_digitos_impares(n//10)
        
print(apenas_digitos_impares(123))

OUTPUT :

13

The only change that your code needed was in the last line of the function.

  • You will just include the odd digit(done by n%10) and,

  • move on(or recurse) to check for remaining digits. You need to multiply next digit by 10, so - 10*apenas_digitos_impares(n//10)

Upvotes: 2

Animesh Mukherkjee
Animesh Mukherkjee

Reputation: 430

Look at the operation on last line, you are getting the last digit if it is odd and then multiplying it with 10 and adding it to recursion result of next call, I believe that is why it is not working, try the code below, here you are appending the last digit at end always so final number comes in correct order.

def apenas_digitos_impares(n):
    if n == 0:
        return 0
    elif (n % 10) % 2 == 0:
        return apenas_digitos_impares(n // 10)
    elif (n % 10) % 2 == 1:
        return 10 * apenas_digitos_impares(n // 10) + (n % 10)

Upvotes: 2

Related Questions