Recursive function call - Python

2

I have a list of integers greater than zero. I need to identify the one with the largest number of divisors. For this I have created three functions: one that orders list, one that returns all the divisors of all the elements of a list and another that filters which element has more divisors. The problem is that I can not make the function moreDivisores directly receive a list with only the elements (without the dividers), you see? If I call function2 (listDivisors) within function 3 (moreDivisores) always gives stick. However if I call manually, it works fine. I've tried all the possibilities and nothing. How do I call the first function in the second so that it works by receiving the raw list?

  

function1 (sorts lists)       def qs (list):           if list == []:               return []           else:               pivor = list [0]               return (qs ([x for x in list if xpivor]))

     

function2: returns the divisors of a number

def listaDivisores(lista):
    if lista == []:
        return []
    else:
        lista=qs(lista)
        resultado=[]
        resultado.append((lista[0],[y for y in range(1,((lista[0])+1)) if (int(lista[0]))%y==0]))
        return resultado+listaDivisores(lista[1:])
    return listaDivisores(lista)
  

function3 returns the number of one with the largest number of divisors
      def moreDivisores (list):

    if len(lista)==[]:
        return "Nenhum número."

    else:
        **lista=listaDivisores(lista)**
  

When you add this command line the code does not execute               if int (len (list)) == 1:                   return list [0]               elif int (len (list [0] [1]))

ERROR LOG

>>> maisDivisores(lista)
Traceback (most recent call last):
  File "<pyshell#499>", line 1, in <module>
    maisDivisores(lista)
  File "D:/Google Drive/CIn/Prog1/EE2.py", line 58, in maisDivisores
    return maisDivisores(lista)
  File "D:/Google Drive/CIn/Prog1/EE2.py", line 46, in maisDivisores
    lista=listaDivisores(lista)
  File "D:/Google Drive/CIn/Prog1/EE2.py", line 35, in listaDivisores
    resultado.append((lista[0],[y for y in range(1,((lista[0])+1)) if (int(lista[0]))%y==0]))
TypeError: can only concatenate tuple (not "int") to tuple
    
asked by anonymous 26.06.2016 / 03:37

2 answers

1

The code had a flaw. I repaired (rsrsrs)

GENERAL FUNCTIONS

def qs(lista): # função quickSort: ordena uma lista, revomendo as duplicatas
    if lista==[]:
        return []
    else:
        pivor=lista[0]
        return (qs([x for x in lista if x<pivor])+[pivor]+qs([x for x in lista if x>pivor]))


def maior (lista): # função maior: devolve o maior elemento de uma lista de tuplas compostas por um inteiro e uma lista de inteiros

    if lista == []:
        return 'Lista vazia.'
    elif len(lista)==1: 
        return lista[0]
    else:
        x = int(len(lista[0][1]))
        y = int(len(lista[1][1]))
        if x > y: # se o tamanho da lista do elemento 0 for maior que o tamanho da lista do elemento 1, o elemento 1 é excluído
            lista.pop(1)
        elif x==y: # no caso das listas dos elementos possuírem o mesmo tamanho, o critério de desempate será o inteiro que precede a lista
            if lista[0]>lista[1]:
                lista.pop(1)
            else:
                lista.pop(0)          
        else:
            lista.pop(0)
        return maior(lista)


def divisores(x):

    if x <= 0:
        return False
    else:
        return [y for y in range (1, x+1) if x%y==0] # retorna uma lista com todos os divisores inteiros de x

Construct a function plusDivisors () that receives as a parameter a list of positive integers other than zero and returns, among those numbers, the one that has the largest number of integer divisors and which are these divisors. In case of a tie (several numbers have the largest number of divisors), the program must choose the largest of the numbers tied. A number x is a divisor of a number y if the rest of the integer division of y by x equals 0. Its program can not have loops (while, for), although it can use recursion and list comprehension. In addition, you should not use the in operator or functions such as max, min, sum, etc. It is explicitly permissible, however, to create as many functions as are precise (discovering the divisors of a number, determining which number has more divisors, etc.) or using functions of the previous question. Examples of using moreDivisors () are shown below.

-> moreDivisors ([24, 5, 9, 15, 42])

(24, [1, 2, 3, 4, 6, 8, 12, 24])

- > moreDivisores ([])

"No number."

- > moreDivisores ([1, 3, 5, 7, 11, 13, 17])

(17, [1,17])

ANSWER:

def maisDivisores(lista):

    if lista == []:
        return 'Nenhum número.'

    else:

        return maior(qs([(x,divisores(x)) for x in lista])) # retorna o inteiro com mais divisores de uma lista ordenada composta
                                                            # por tuplas  de inteiros e listas de divisores baseada na lista de 
                                                            # inteiros fornecida como parâmetro na função
    
27.06.2016 / 01:00
0

CODE RESOLVED !! Deletes the first function. I made one. Hence what I did ... from the raw list I take the first element in a destructive way. With a list understanding, I create a list with the divisors of it, if the list is different from empty I enter the function again, until I get all the elements from there it will give me a list with the divisors of all. then just compare and return the element that has the largest number of divisors

    
26.06.2016 / 05:14