Софтуерно Инженерство
Loading...
+ Нов въпрос
MishoMoney avatar MishoMoney 14 Точки

За пайтънджиите - първото примерче от лекцията (рекурсия)

Понеже съм от новаците и съм правил само Python Basics и Python Fundamentals, тези примери на C# плюс логиката на нещата ми идват малко нанагорно... Затова споделям за други пайтънджии в моето положение, ако има такива... 

 

def sum_num(list_input, index):

    if index == len(list_input):
        return 0

    return list_input[index] + sum_num(list_input, index + 1)


my_list = [1, 2, 3, 4, 5]

result = sum_num(my_list, 0)

print(result)
Тагове:
MishoMoney avatar MishoMoney 14 Точки

А ето го и второто, с факториела:

 

def factoriel(number):
    if number == 0:
        return 1
    return number * factoriel(number - 1)


num = int(input())

result = factoriel(num)
print(result)
0
11/03/2019 20:22:22
MishoMoney avatar MishoMoney 14 Точки

За да ми стане по-ясно решението на 4-та задача (Generating 0/1 Vectors) си направих следната табличка. В случая векторът е с три елемента за по-просто. Може да е полезна и за други

For 1 и For 0 покзават къде е съответният цикъл, на нула или на едно. Итерациите на циклите са един под друг. "Вложените" цикли (ако можем така да ги наречем, доколкото рекурсията на практика ги влага един в друг) са по-навътре 

 

Index = 0

                  For 0-> [0,N,N]

                                    Index = 1

                                                      For 0-> [0,0,N]

                                                                        Index = 2

                                                                                          For 0->  [0,0,0]

                                                                                                            Index = 3 -> PRINT -> Exits 

                                                                        Index = 2

                                                                                          For 1-> [0,0,1]

                                                                                                            Index = 3 -> PRINT -> Exits

                                    Index = 1

                                                      For 1  -> [0,1,1]

                                                                        Index = 2

                                                                                          For 0-> [0,1,0]

                                                                                                            Index = 3 -> PRINT -> Exits

                                                                        Index 2

                                                                                          For 1-> [0,1,1]

                                                                                                            Index = 3 -> PRINT -> Exits

Index = 0

                  For 1-> [1,1,1]

                                    Index = 1

                                                      For 0-> [1,0,1]

                                                                        Index = 2

                                                                                          For 0->  [1,0,0]

                                                                                                            Index = 3 -> PRINT -> Exits

                                                                        Index = 2

                                                                                          For 1->  [1,0,1]

                                                                                                            Index = 3 -> PRINT -> Exits

                                    Index = 1

                                                      For 1  -> [1,1,1]

                                                                        Index = 2

                                                                                          For 0->  [1,1,0]

                                                                                                            Index = 3 -> PRINT -> Exits

                                                                        Index = 2

                                                                                          For 1->  [1,1,1]

                                                                                                            Index = 3 -> PRINT -> Exits (all loops are done!)

0
17/03/2019 12:16:57
MishoMoney avatar MishoMoney 14 Точки

А ето пак и код за пайтънджиите

 

def bits_vector(index, vector):
    if index == n:
        print(vector)
    else:
        for i in range(0, 2):
            vector[index] = i
            bits_vector(index + 1, vector)

n = int(input())

vector = [None] * n
bits_vector(0, vector)
0