recursion

Can a convolution function written in tail recursive form?

旧巷老猫 提交于 2020-01-13 18:23:05
问题 I have a function that I want to write in tail recursive form. The function calculates the number of ways to get the sum of k by rolling an s sided die n times. I have seen the mathematical solution for this function on this answer. It is as follows: My reference recursive implementation in R is: sum_ways <- function(n_times, k_sum, s_side) { if (k_sum < n_times || k_sum > n_times * s_side) { return(0) } else if (n_times == 1) { return(1) } else { sigma_values <- sapply( 1:s_side, function(j)

Converting String of binary digits to decimal number… using recursion

自闭症网瘾萝莉.ら 提交于 2020-01-13 18:00:53
问题 Comp sci professor gave us this problem in our homework... I'm not sure how to proceed and the code I have written seems to be failing miserably. Here is the prompt: (binary to decimal) Write a recursive method that parses a binary number as a string into a decimal integer. The method header is: public static String bin2Dec(String binaryString) write a test program that prompts the user to enter a binary string and displays its decimal equivalent. Any help greatly appreciated. Here is my code

Converting String of binary digits to decimal number… using recursion

淺唱寂寞╮ 提交于 2020-01-13 18:00:04
问题 Comp sci professor gave us this problem in our homework... I'm not sure how to proceed and the code I have written seems to be failing miserably. Here is the prompt: (binary to decimal) Write a recursive method that parses a binary number as a string into a decimal integer. The method header is: public static String bin2Dec(String binaryString) write a test program that prompts the user to enter a binary string and displays its decimal equivalent. Any help greatly appreciated. Here is my code

Converting String of binary digits to decimal number… using recursion

南笙酒味 提交于 2020-01-13 18:00:03
问题 Comp sci professor gave us this problem in our homework... I'm not sure how to proceed and the code I have written seems to be failing miserably. Here is the prompt: (binary to decimal) Write a recursive method that parses a binary number as a string into a decimal integer. The method header is: public static String bin2Dec(String binaryString) write a test program that prompts the user to enter a binary string and displays its decimal equivalent. Any help greatly appreciated. Here is my code

Recursive function vs recursive variable in F#

左心房为你撑大大i 提交于 2020-01-13 11:22:09
问题 The first method is OK. The second repeats constantly the same pair of numbers. It is quite obscure to me why... Could you point to the good direction ? module Normal = let rnd = new MersenneTwister() let sampleNormal = fun () -> let rec randomNormal() = let u1, u2 = rnd.NextDouble(),rnd.NextDouble() let r, theta= sqrt (-2. * (log u1)), 2. * System.Math.PI * u2 seq { yield r * sin theta; yield r * cos theta ; printfn "next";yield! randomNormal() } randomNormal() let sampleNormalBAD = fun () -

Dynamic nested for loops to be solved with recursion

雨燕双飞 提交于 2020-01-13 10:33:29
问题 I'm trying to get a result looking something like this: Miniors | Boys | 54kg - 62kg where every value delimited by a pipe | comes from an array containing a certain "type of restriction". For example: ageGroups, genders, weightClasses (as seen above). The way I'm able to get this result right now is if I hard code the nested forEach-loops (using underscorejs), but this means I have to now how many arrays I have to loop over to get wanted result. This works "fine": var categories = []; _.each

What is the algorithm of thinking recursive? (on the specific example)

南楼画角 提交于 2020-01-13 10:14:08
问题 I just can't wrap my head around recursion. I understand all of the concepts (breaking solution into smaller cases) and I can understand solutions after I read them over and over again. But I can never figure out how to use recursion to solve a problem. Is there any systematic way to come up with a recursive solution? Can someone please explain to me their thought process when they try to solve the following recursive problem: "Return all permutations of a string using recursion" . Here is an

R - merge lists with overwrite and recursion

天大地大妈咪最大 提交于 2020-01-13 08:40:48
问题 Suppose I have two lists with names, a = list( a=1, b=2, c=list( d=1, e=2 ), d=list( a=1, b=2 ) ) b = list( a=2, c=list( e=1, f=2 ), d=3, e=2 ) I'd like to recursively merge those lists, overwriting entries if the second argument contains conflicting values. I.e. the expected output would be $a [1] 2 $b [1] 2 $c $c$d [1] 1 $c$e [1] 1 $c$f [1] 2 $d [1] 3 $e [1] 2 Any hint? 回答1: I am not so sure if a custom function is necessary here. There is a function utils::modifyList() to perform this

Vectorize a numpy discount calculation

独自空忆成欢 提交于 2020-01-13 05:01:08
问题 A common term in finance and reinforcement learning is the discounted cumulative reward C[i] based on a time series of raw rewards R[i] . Given an array R , we'd like to calculate C[i] satisfying the recurrence C[i] = R[i] + discount * C[i+1] with C[-1] = R[-1] (and return the full array C ). A numerically stable way of calculating this in python with numpy arrays might be: import numpy as np def cumulative_discount(rewards, discount): future_cumulative_reward = 0 assert np.issubdtype(rewards

emacs ff-find-other-file and ff-search-directories isn't recursive

匆匆过客 提交于 2020-01-13 04:40:06
问题 Can we make ff-find-other-file to search recursively in directories which are listed in ff-search-directories. Instead of searching only in /usr/include, it would also search in /usr/include/llvm. Or likewise. 回答1: I've added this function to my .emacs file: (defun get-all-subdirectories(dir-list) "Returns a list of all recursive subdirectories of dir-list, ignoring directories with names that start with . (dot)" (split-string (shell-command-to-string (concat "find " (mapconcat 'identity dir