recursion

Why it is not a tail recursion?

China☆狼群 提交于 2021-02-07 20:11:29
问题 I have the following code, that I do not understand, why it is not a tail recursion: override fun drop(n: Int): List<A> = if (n == 0) this else tail.drop(n - 1) whereas this is a tail recursion: fun drop(n: Int): List<A> { tailrec fun drop(n: Int, list: List<A>): List<A> = if (n <= 0) list else when (list) { is Cons -> drop(n - 1, list.tail) is Nil -> list } return drop(n, this) } Why is the first example not a tail recursion? 回答1: It isn't tail recursion because Kotlin checks the recursive

Oracle CONNECT BY recursive child to parent query, include ultimate parent that self references

假装没事ソ 提交于 2021-02-07 19:59:18
问题 In the following example id parent_id A A B A C B select id, parent_id from table start with id = 'A' connect by nocycle parent_id = prior id I get A A B A C B In my database I have millions of rows in the table and deep and wide hierarchies and I'm not interested in all children. I can derive the children I'm interested in. So I want to turn the query on its head and supply START WITH with the children ids. I then want to output the parent recursively until I get to the top. In my case the

Oracle CONNECT BY recursive child to parent query, include ultimate parent that self references

六眼飞鱼酱① 提交于 2021-02-07 19:58:38
问题 In the following example id parent_id A A B A C B select id, parent_id from table start with id = 'A' connect by nocycle parent_id = prior id I get A A B A C B In my database I have millions of rows in the table and deep and wide hierarchies and I'm not interested in all children. I can derive the children I'm interested in. So I want to turn the query on its head and supply START WITH with the children ids. I then want to output the parent recursively until I get to the top. In my case the

Generate all leaf-to-root paths in a dictionary tree in Python

时光怂恿深爱的人放手 提交于 2021-02-07 18:09:48
问题 I have a dictionary-tree in an "non-standard" form like so: tree = {'0': {'A': {'B': {'C': {}}}}, {'D': {'E': {}}, {'F': {}}}} Leaf nodes are defined as dictionary key-value pairs where the values is an empty dictionary. I would like to extract all the leaf-to-root paths as lists of lists like so: paths_ = [['C', 'B', 'A', '0'], ['E', 'D', '0'], ['F', 'D', '0']] The paths can be reversed too if that is helpful. paths_ = [['0', 'A', 'B', 'C'], ['0', 'D', 'E'], ['0', 'D', 'F']] I know I have to

Generate all leaf-to-root paths in a dictionary tree in Python

梦想的初衷 提交于 2021-02-07 18:07:16
问题 I have a dictionary-tree in an "non-standard" form like so: tree = {'0': {'A': {'B': {'C': {}}}}, {'D': {'E': {}}, {'F': {}}}} Leaf nodes are defined as dictionary key-value pairs where the values is an empty dictionary. I would like to extract all the leaf-to-root paths as lists of lists like so: paths_ = [['C', 'B', 'A', '0'], ['E', 'D', '0'], ['F', 'D', '0']] The paths can be reversed too if that is helpful. paths_ = [['0', 'A', 'B', 'C'], ['0', 'D', 'E'], ['0', 'D', 'F']] I know I have to

Different results from yield vs return

半城伤御伤魂 提交于 2021-02-07 15:27:47
问题 I don't really understand how yield statement works in this situation. The problem says that given an expression without parentheses, write a function to generate all possible fully parenthesized (FP) expressions. Say, the input is '1+2+3+4' which should be generated to 5 FP expressions: (1+(2+(3+4))) (1+((2+3)+4)) ((1+2)+(3+4)) ((1+(2+3))+4) (((1+2)+3)+4) My code is as follows. OPS = ('+', '-', '*', '/') def f(expr): """ Generates FP exprs Recursive formula: f(expr1[op]expr2) = (f(expr1) [op

Count depth of a hierarchy of classes

大兔子大兔子 提交于 2021-02-07 11:11:15
问题 I've seen a lot of different examples of how to do this and am well aware that I could write out a loop that iterates my entire tree of classes to find the maximum depth, but I cannot help but think there has to be a simpler way. Basically I have two classes that I developed to host all my applications settings, SettingGroup which is exactly what it sounds like, basically a folder, and Setting which is the setting itself and the configurations that allow the application to know what the

C++ recursive_directory_iterator miss some files

做~自己de王妃 提交于 2021-02-07 10:48:15
问题 I'm trying to get all files in directory through c++17 on my visual studio 2017 but I've just encountered a really weird problem. If I specify directory like this I can get all files without any problem: for (auto& p : std::filesystem::recursive_directory_iterator("C:\\Users\\r00t\\AppData\\Roaming\\Mozilla")) { if (std::filesystem::is_regular_file(p.path())) { std::cout << p.path() << std::endl; } } But I need all file list on APPDATA, and I'm trying to get path with getenv() function and

Recursive query with ordered values in SQLite Android

强颜欢笑 提交于 2021-02-07 10:10:28
问题 I have one group table with a recursive relation, so each record has a parent_id . Given a group, I need to get all the student (each belong to a group) names in all its subgroups, but ordered by student name. Do you know if there is any "easy" way to do it? If I have to do multiple queries, then I should order the results of the different Cursors, but Cursor has no orderBy(). Any ideas? Thank you so much! 回答1: As SQLite does not support recursive queries I implemented the select with two

Recursive query with ordered values in SQLite Android

左心房为你撑大大i 提交于 2021-02-07 10:03:53
问题 I have one group table with a recursive relation, so each record has a parent_id . Given a group, I need to get all the student (each belong to a group) names in all its subgroups, but ordered by student name. Do you know if there is any "easy" way to do it? If I have to do multiple queries, then I should order the results of the different Cursors, but Cursor has no orderBy(). Any ideas? Thank you so much! 回答1: As SQLite does not support recursive queries I implemented the select with two