recursion

elimination of indirect left recursion

给你一囗甜甜゛ 提交于 2020-01-06 06:06:34
问题 I'm having problems understanding an online explanation of how to remove the left recursion in this grammar. I know how to remove direct recursion, but I'm not clear how to handle the indirect. Could anyone explain it? A --> B x y | x B --> C D C --> A | c D --> d 回答1: The way I learned to do this is to replace one of the offending non-terminal symbols with each of its expansions. In this case, we first replace B with its expansions: A --> B x y | x B --> C D becomes A --> C x y | D x y | x

Scheme Inserting Pairs into Heap

孤人 提交于 2020-01-06 05:51:57
问题 This is part of a homework assignment that I can't seem to figure out. I was wondering if someone could point me in the right direction? The problem reads: (insert-list-of-pairs vw-pair-list heap) evaluates to the heap resulting from inserting all of the value - weight pairs from the list vw-pair-list into the heap heap . The following functions were created earlier in the homework for use in this problem: (define (create-heap vw-pair left right) (list vw-pair left right)) (define (h-min heap

How to delete an element from a Leafy Binary Tree (Haskell)

最后都变了- 提交于 2020-01-06 05:51:19
问题 So, this tree is NOT a Binary Search Tree. It is in no particular order, and is just in this order for quick access to specific indices (nth element), rather than whether an element exists or not. The form of the Tree is like so: data Tree a = Leaf a | Node Int (Tree a) (Tree a) deriving Show For this specific tree, the "Int" from the Node constructor is the number of elements underneath that node (or number of leaves). Using this structure, I copied parts of the Tree functions available in a

Java recursion and integer double digit

两盒软妹~` 提交于 2020-01-06 05:47:26
问题 I'm trying to take an integer as a parameter and then use recursion to double each digit in the integer. For example doubleDigit(3487) would return 33448877 . I'm stuck because I can't figure out how I would read each number in the digit I guess. 回答1: To do this using recursion, use the modulus operator (%), dividing by 10 each time and accumulating your resulting string backwards, until you reach the base case (0), where there's nothing left to divide by. In the base case, you just return an

Recursive function on sublist returning None

丶灬走出姿态 提交于 2020-01-06 04:42:12
问题 I'm running a recursive function on sublist to search the element check_value in the list once it finds it, it verifies whether other_value is the first item of the corresponding list and finally return the index.But current code is returning None.can anyone please support as I'm not having much understanding on recursive functions functioning on sublists. def check_with_list(dd, check_value, other_value=None): global new_index for index, h in enumerate(dd): if isinstance(h, list): result =

OCaml count consecutive elements in a list

。_饼干妹妹 提交于 2020-01-06 04:30:29
问题 I'm writing OCaml code that reads in a list and removes any char 'i's that appear at the beginning of the list. For instance, the list removeI['i';'i';'a';'c';'i'] should return -: int * char list = ['a';'c';'i'] , because there are 2 'i's at the beginning of the list. I believe I know how to implement this properly; however, I want to return a tuple that includes the number of removed 'i's as well as the new list with the 'i's removed. I know that may sound confusing, but an example would be

OCaml count consecutive elements in a list

倖福魔咒の 提交于 2020-01-06 04:30:27
问题 I'm writing OCaml code that reads in a list and removes any char 'i's that appear at the beginning of the list. For instance, the list removeI['i';'i';'a';'c';'i'] should return -: int * char list = ['a';'c';'i'] , because there are 2 'i's at the beginning of the list. I believe I know how to implement this properly; however, I want to return a tuple that includes the number of removed 'i's as well as the new list with the 'i's removed. I know that may sound confusing, but an example would be

MySQL find all parents recursively [duplicate]

て烟熏妆下的殇ゞ 提交于 2020-01-06 04:23:25
问题 This question already has answers here : get a recursive parent list (2 answers) Closed 5 years ago . I have the following tables: Category id INT name VARCHAR Category_parent category_id INT parent_id INT In Category_parent I store category relationship, and both columns get id from category table. So I can identify what is parent of what. There can be any number of generations, so it is a bit difficult to find all categories from which a particular category inherited. For example, CAT10's

How to find if any values in array add up to n

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-06 04:10:06
问题 I have an array of random values, and a target value. #!/bin/bash objective='50' declare -a values=(1 2 2 6 8 14.5 15 28.7 .. 42) I need to find a way to extract any combination of numbers in the array 'values' that add up to 50 The array has duplicates, and floating point integers. A solution set might look like: 50 = 42 + 8 50 = 42 + 6 + 2 Initially I started in bash with some nested for loops, however I'm quickly realizing that this will grow exponentially with my array length. I took a

jquery/javascript error - too much recursion - with self-calling function

放肆的年华 提交于 2020-01-06 04:01:20
问题 I have a small problem with a function that calls itself from within itself. The following function pretty much works, as can be seen here ... http://jsfiddle.net/justinayles/frPZ8/2/ but if you click a square and after that click the back link and go back and forth a few times, eventually it will slow down and ultimately crash with a 'too much recursion error'. I cant really see any other way to achieve what I am trying to do here ? I kind of understand what the problem is, but was wondering