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
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]))
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])
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)?
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.
@@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!
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); }
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 :)
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);
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