Recursive function with strings-Python

I'm not able to solve an exercise. The statement is as follows: implement the Disturb(n) function that returns a string containing "disturb" (the word followed by a space) n times. If n is not a strictly positive integer, the function must return an empty string. This function must be implemented using recursion.

Using the above function, implement the elephants(n) function that returns a string containing the letter of " an elephant bothers lots of people..."from 1 to N elephants. If n is not greater than 1, the function should return an empty string. This function should also be implemented using recursion.

For example, the callback Elephants(4) should be:

Um elefante incomoda muita gente 2 elefantes incomodam incomodam muito mais 2 elefantes incomodam incomodam muita gente 3 elefantes incomodam incomodam incomodam muito mais 3 elefantes incomodam incomodam incomodam muita gente 4 elefantes incomodam incomodam incomodam incomodam muito mais

How can I implement the second function recursively? So far I have only managed as an iterative function.

def incomodam(n):
    if n <= 0:
        return ''
    else:
        return 'incomodam ' + incomodam(n - 1)

def elefantes(n):
    if n <= 1:
        return ''
    else:
        count = 1
        string = 'Um elefante incomoda muita gente ' 
        while count < n:
            count += 1
            if count < n:
                string += str(count) + ' elefantes ' + incomodam(count) + 'muito mais '
                string += str(count) + ' elefantes ' + incomodam(count) + 'muita gente '
            else:
                string += str(count) + ' elefantes ' + incomodam(count) + 'muito mais '

    return string
Author: Rafael Lima, 2017-05-28

3 answers

I did like this:

def elefantes(n):
    if n <= 0: return ""
    if n == 1: return "Um elefante incomoda muita gente"
    return elefantes(n - 1) + str(n) + " elefantes " + incomodam(n) + ("muita gente" if n % 2 > 0 else "muito mais") +  + "\r\n"

def incomodam(n):
    if n <= 0: return ""
    if n == 1: return "incomodam "
    return "incomodam " + incomodam(n - 1)

>>> print(elefantes(0))

>>> print(elefantes(1))
Um elefante incomoda muita gente

>>> print(elefantes(2))
Um elefante incomoda muita gente
2 elefantes incomodam incomodam muito mais

>>> print(elefantes(3))
Um elefante incomoda muita gente
2 elefantes incomodam incomodam muito mais
3 elefantes incomodam incomodam incomodam muita gente

>>> print(elefantes(4))
Um elefante incomoda muita gente
2 elefantes incomodam incomodam muito mais
3 elefantes incomodam incomodam incomodam muita gente
4 elefantes incomodam incomodam incomodam incomodam muito mais
 6
Author: Leonel Sanches da Silva, 2017-05-28 02:55:07

I did like this:

    def incomodam(n):
        if n >= 1:
            return 'incomodam ' + incomodam(n-1)
        else:
            return ''

    quantidade = 0      
    muito= False        # pra saber em que parte esta da musica

    def elefantes(n):
        global quantidade
        global muito
        if quantidade == 0:
            quantidade,n = n,1
        if n <= quantidade:
            if n == 1:
                muito = True
                return 'Um elefante incomoda muita gente\n'+elefantes(n+1)
            else:
                if muito:
                    muito = False
                    return str(n) + ' elefantes ' + incomodam(n) + 'muito mais\n' + elefantes(n)
                else:
                    muito = True
                    if n+1>quantidade:
                        quantidade = 0
                        return ''
                    else:
                        return str(n) + ' elefantes ' + incomodam(n) + 'muita gente\n' + elefantes(n+1)
        else:
            return ''
 1
Author: , 2017-12-22 14:53:16

I also had to make this code for a course. I realized that the case was special not only for n=1, but also for n=2. For n = 2, the function would be called "partially". Therefore, I solved it this way:

def incomodam(n):
    if n <= 0:
        return ""
    elif n % 1 == 0:
        return "incomodam " + incomodam(n-1)
    else:
        return ""

def elefantes(n):
    if n == 1:
        return "Um elefante incomoda muita gente"
    elif n == 2:
        return elefantes(n-1) + f"\n{n} elefantes "+ incomodam(n) +"muito mais" 
    elif n > 2:
        frase1 = f"\n{n-1} elefantes incomodam muita gente"
        frase2 = f"\n{n} elefantes "+ incomodam(n) +"muito mais"
        return elefantes(n-1) + frase1 +frase2
    else:
        return ""

It is noticed that when n = 2, only sentence 2 is called, since sentence 1 has to be in the singular...

I suggest using pytest to check the results.

import pytest
@pytest.mark.parametrize("test_input,expected", [
    (0, ""),
    (1, "Um elefante incomoda muita gente"),
    (2, "Um elefante incomoda muita gente\n2 elefantes incomodam incomodam muito mais"),
    (3,"Um elefante incomoda muita gente\n2 elefantes incomodam incomodam muito mais\n2 elefantes incomodam muita gente\n3 elefantes incomodam incomodam incomodam muito mais"),
    (4,"Um elefante incomoda muita gente\n2 elefantes incomodam incomodam muito mais\n2 elefantes incomodam muita gente\n3 elefantes incomodam incomodam incomodam muito mais\n3 elefantes incomodam muita gente\n4 elefantes incomodam incomodam incomodam incomodam muito mais"),
    (-1,""),
])
def test_eval(test_input, expected):
    assert elefantes(test_input) == expected
 0
Author: Stela Ishitani, 2019-08-21 05:42:23