Python - Find Odd Tuple

0

I'm new to this area and recently, for a college job I could not turn that line of code into a tuple. function code:

def encontra_impares(lista):

    lis = []

    if len(lista) > 0:

        numero = lista.pop(0)

        if numero % 2 != 0:

            lis.append(numero)

        lis = lis + encontra_impares(lista)

    return lis

function call:

print(encontra_impares([1, 2, 3,5,5,9,7,32,1,6]))
    
asked by anonymous 12.05.2018 / 19:44

2 answers

1

You can adjust your function to something like:

def encontra_impares( tupla ):

    lista = []

    for n in tupla:
        if n % 2 != 0:
            lista.append(n)

    return lista


tpl = (1, 2, 3,5,5,9,7,32,1,6)
lst = encontra_impares( tpl )

print(tpl)
print(lst)

Or Simply:

def encontra_impares( tupla ):
    return [ n for n in tupla if n % 2 != 0 ]

tpl = (1, 2, 3,5,5,9,7,32,1,6)
lst = encontra_impares( tpl )

print(tpl)
print(lst)

Output:

(1, 2, 3, 5, 5, 9, 7, 32, 1, 6)
[1, 3, 5, 5, 9, 7, 1]
    
12.05.2018 / 20:15
0

Dude I was a bit confused with what exactly you want but according to your goal would do it this way:

 lis=[]
 lista=[0,1,2,3,4,5,6,8,9]
      for i in range(len(lista)):
         if lista[i]%2 == 0:
         lis.append(lista[i])
    
12.05.2018 / 19:58