Тёмный

Loops and wrapper functions | Recursion Series 

WilliamFiset
Подписаться 179 тыс.
Просмотров 5 тыс.
50% 1

Опубликовано:

 

7 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 13   
@Vancha112
@Vancha112 Год назад
Clear and short, nice :)
@xiaocheng1937
@xiaocheng1937 Год назад
i found this is pretty easy for me to understand for the multiply one def product_evens(i, lst): if len(lst) == 0: return 0 if i == len(lst): return 1 value = 1 # a*1 = a if lst[i] % 2 == 0: value = lst[i] return product_evens(i+1, lst) * value
@aditya.tak.17
@aditya.tak.17 4 месяца назад
Created a simple python code, hope this helps. Explanation: Here we provide default value 1 to product and then if the number is even we call product_of_even_numbers but with different value(product*even_number) of product, and eventually the list will be empty and we will check if the product is still 1, if yes it means either we provided an empty list or there were no even numbers in the list. def product_of_even_numbers(list_x, product=1): if not list_x: if product == 1: return 0 return product if list_x[0] % 2 == 0: product *= list_x[0] return product_of_even_numbers(list_x[1:], product) print(product_of_even_numbers([])) print(product_of_even_numbers([1])) print(product_of_even_numbers([1, 2])) print(product_of_even_numbers([2])) print(product_of_even_numbers([1, 2, 3])) print(product_of_even_numbers([1, 2, 3, 4])) print(product_of_even_numbers([1, 2, 3, 4, 5]))
@giovi2874
@giovi2874 11 месяцев назад
Hi, I love the way you are teaching! I personally thing that the solutions to the exercise is over complicated for beginners. I've tried to solve it and I've come up with a much "easy to read solution". I'm still learning and a feedback from you would be really appreciated function productEven(i,arr){ if(arr.length == 0) return 0 if(i == arr.length) return 1 let value = 1 if(arr[i]%2 == 0) value = arr[i] return productEven(i+1, arr) * value } productEven(0, [4,0,8])
@sethn896
@sethn896 Год назад
Pretty new to programming and algorithm design here. What can be said about the efficiency of a recursive function to sum the odd numbers where: - there is a separate “initializer” function call, where the size of the list (let’s call this n) is only checked once and initialized in a variable that is passed to the sumOdds function, which recursively calls itself in the same manner as in 3:41 BUT with “n-1” as an argument in the recursive call. - Then the base case is instead checking if n == 0 rather than if I is greater than the size of the list each function call. You’d be free of the cost of continuously accessing the length of the list each function call but you’d gain the cost of decrementing a variable n times as well as one extra outer function on the stack. Is this computationally worthwhile for sufficiently large n? Or is this not something we should worry about because it doesn’t affect the order of growth (checking the array is a constant factor)?
@WilliamFiset-videos
@WilliamFiset-videos Год назад
That's the classic space vs time tradeoff. On one hand you could precompute the value of length(lst) and pass it down the stack, but this adds space, or you could compute it on the fly, adding time. Something as trivial as the length of the list is typically not something to worry about. You should assume the length() function call runs in constant time. Moreover, I wouldn't be surprised if the compilers of modern programming languages optimize this problem away by recognizing that length(lst) is a constant value.
@sethn896
@sethn896 Год назад
@@WilliamFiset-videos it’s almost baffling to me that often the best test for efficiency is just to simply measure/compare the runtime. And the “winning” solution for the same input might vary across not just hardware but even compilers. Thanks for the reply!
@Ahryno781
@Ahryno781 Год назад
hey William, about the wrapper function, why not just make a default argument? like this int sumOdds(vector vec, int index = 0) { if (index == vec.size()) return 0; return (vec[index] % 2 != 0 ? vec[index] : 0) + sumOdds(vec, index + 1); }
@WilliamFiset-videos
@WilliamFiset-videos Год назад
I like it. Default args seems like a nice way to seed a recursive function too.
@aditya.tak.17
@aditya.tak.17 4 месяца назад
Could you please explain the program for productOfEvenNumbers? it was really confusing and hard for me to understand. Can we create this function using list slicing(to replace indexing) and without the wrapper function? thank you :)
@YashRekha594
@YashRekha594 8 месяцев назад
Can anyone please tell me why its failing when i am passing list = {6,0} instead of 0 this function returns 6. Thanks. int productOfEvenNumbers(vector& nums, int nIndex = 0) { if (nIndex == nums.size()) return 0; int product = productOfEvenNumbers(nums, nIndex + 1); if (nums[nIndex] % 2 != 0) return product; if (product == 0) return nums[nIndex]; return nums[nIndex] * product; } Function call - vector nums {6, 0}; productOfEvenNumbers(nums);
@CompulsiveProgrammerLear-qm6hj
@CompulsiveProgrammerLear-qm6hj Месяц назад
The solution you provides does not works full edge cases where the last element in the list is a odd numbers. A better approach that works for all cases. function productOfEvenNumbers(arr) { // Base case: if the array is empty, return 1 if (arr.length === 0) { return 1; } // Take the first element of the array const first = arr[0]; // If the first element is even, include it in the product if (first % 2 === 0) { return first * productOfEvenNumbers(arr.slice(1)); } else { // If the first element is not even, skip it return productOfEvenNumbers(arr.slice(1)); } } // Example usage: const numbers = [1, 2, 3, 4, 5, 6, 7, 8]; const result = productOfEvenNumbers(numbers); console.log(result); // Output: 384
@kvelez
@kvelez 10 месяцев назад
def sum_all_odd_nums(arr): sum = 0 for i in arr: if i % 2 != 0: sum += i return sum print(sum_all_odd_nums([1, 2, 3, 4, 5])) def recursive_product(arr): if len(arr) == 1: return arr[0] else: return arr[0] * recursive_product(arr[1:]) print(recursive_product([2,5])) def recursive_product_even(arr): if len(arr) == 0: return 1 elif arr[0] % 2 == 0: return arr[0] * recursive_product_even(arr[1:]) else: return recursive_product_even(arr[1:]) print(recursive_product_even([2,4]))
Далее
Strings & Palindromes | Recursion Series
9:44
Просмотров 5 тыс.
Programming Loops vs Recursion - Computerphile
12:32
Просмотров 1,5 млн
Starman🫡
00:18
Просмотров 11 млн
Premature Optimization
12:39
Просмотров 803 тыс.
5 Simple Steps for Solving Any Recursive Problem
21:03
My 10 “Clean” Code Principles (Start These Now)
15:12
Narrow Art Gallery | Dynamic Programming
20:51
Просмотров 9 тыс.
Tiling dominoes | Dynamic programming
14:50
Просмотров 40 тыс.
This is a Better Way to Understand Recursion
4:03
Просмотров 40 тыс.
What on Earth is Recursion? - Computerphile
9:40
Просмотров 743 тыс.