recursive-datastructures

How to generate recursive list in OCaml

拥有回忆 提交于 2019-12-01 23:00:44
问题 I would like to implement analog of Haskell cycle function. If I pass list elements explicitly it seems trivial: let cycle a b c = let rec l = a::b::c::l in l cycle 1 2 3 generates recursive list 1, 2, 3, 1... But, how to generate recursive list on basis of another regular list? let cycle lst = ... Usage cycle [1;2;3] 回答1: In an eager language like ML, you need to use streams. For example # let cycle = Stream.from (fun n -> Some (List.nth [1;2;3] (n mod 3)));; val cycle : int Stream.t =

How to generate recursive list in OCaml

岁酱吖の 提交于 2019-12-01 21:43:25
I would like to implement analog of Haskell cycle function. If I pass list elements explicitly it seems trivial: let cycle a b c = let rec l = a::b::c::l in l cycle 1 2 3 generates recursive list 1, 2, 3, 1... But, how to generate recursive list on basis of another regular list? let cycle lst = ... Usage cycle [1;2;3] In an eager language like ML, you need to use streams. For example # let cycle = Stream.from (fun n -> Some (List.nth [1;2;3] (n mod 3)));; val cycle : int Stream.t = <abstr> # Stream.npeek 10 cycle;; - : int list = [1; 2; 3; 1; 2; 3; 1; 2; 3; 1] As far as I can see, OCaml doesn

How to indicate decreasing in size of two Coq inductive types

蓝咒 提交于 2019-12-01 11:04:10
I'm trying to define the game inductive type for combinatorial games. I want a comparison method which tells if two games are lessOrEq , greatOrEq , lessOrConf or greatOrConf . Then I can check if two games are equal if they are both lessOrEq and greatOrEq . But when I try defining the mutually recursive methods for making this check, I get: Error: Cannot guess decreasing argument of fix . I think this is because only one game or the other decreases in size with each recursive call (but not both). How can I indicate this to Coq? Here's my code. The part that fails is the mutually recursive

How to indicate decreasing in size of two Coq inductive types

馋奶兔 提交于 2019-12-01 07:28:15
问题 I'm trying to define the game inductive type for combinatorial games. I want a comparison method which tells if two games are lessOrEq , greatOrEq , lessOrConf or greatOrConf . Then I can check if two games are equal if they are both lessOrEq and greatOrEq . But when I try defining the mutually recursive methods for making this check, I get: Error: Cannot guess decreasing argument of fix . I think this is because only one game or the other decreases in size with each recursive call (but not

Flattening Generic JSON List of Dicts or Lists in Python

只愿长相守 提交于 2019-11-30 23:23:22
I have a set of arbitrary JSON data that has been parsed in Python to lists of dicts and lists of varying depth. I need to be able to 'flatten' this into a list of dicts. Example below: Source Data Example 1 [{u'industry': [ {u'id': u'112', u'name': u'A'}, {u'id': u'132', u'name': u'B'}, {u'id': u'110', u'name': u'C'}, ], u'name': u'materials'}, {u'industry': {u'id': u'210', u'name': u'A'}, u'name': u'conglomerates'} ] Desired Result Example 1 [{u'name':u'materials', u'industry_id':u'112', u'industry_name':u'A'}, {u'name':u'materials', u'industry_id':u'132', u'industry_name':u'B'}, {u'name':u

Generating all possible trees of depth N?

狂风中的少年 提交于 2019-11-30 15:48:05
I have several different types of tree nodes, each of which may have anywhere from 0 to 5 children. I'm trying to figure out an algorithm to generate all possible trees of depth <= N. Any help here? I'm having trouble figuring out how to recursively walk the tree given that each change I make to a node may expose new subtrees (or remove old ones). Here's a Python program I wrote up that I think does what you're asking. It'll return all of the possible trees given a starting node. Essentially, it boils down to a trick with bit manipulation: if a node has 5 children, then there are 2 5 = 32

populate treeview from a list of path

可紊 提交于 2019-11-27 19:24:29
I'm trying to populate a treeview from a list of folder path, for example: C:\WINDOWS\addins C:\WINDOWS\AppPatch C:\WINDOWS\AppPatch\MUI C:\WINDOWS\AppPatch\MUI\040C C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409 with an ouput like this: ├───addins ├───AppPatch │ └───MUI │ └───040C ├───Microsoft.NET │ └───Framework │ └───v2.0.50727 │ └───MUI │ └───0409 Notice there's no 'C:\WINDOWS\Microsoft.NET' or 'C:\WINDOWS\Microsoft.NET\Framework' in the list. I've been working on this for almost two

Confusing […] List in Python: What is it?

老子叫甜甜 提交于 2019-11-27 02:08:20
So I was writing up a simple binary tree in Python and came across [...] I don't believe this to be related to the Ellipsis object, more it seems to have something to do with an infinity loop (due to Python's shallow copy?). The source of this infinity loop and why it doesn't get expanded while expanding when accessed is something I'm completely lost to, however >>> a [[[[[], [], 8, 3], [[], [], 3, 2], 6, 3], [], 1, 4], [[], [], -4, 2], 0, 0] >>> Keys(a)#With a+b [0, 1, 6, 8, 3, -4] >>> Keys(a)#With [a,b] [8, [...], [...], 3, [...], [...], 6, [...], [...], 1, [...], [...], -4, [...], [...], 0,

populate treeview from a list of path

怎甘沉沦 提交于 2019-11-26 22:48:15
问题 I'm trying to populate a treeview from a list of folder path, for example: C:\WINDOWS\addins C:\WINDOWS\AppPatch C:\WINDOWS\AppPatch\MUI C:\WINDOWS\AppPatch\MUI\040C C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409 with an ouput like this: ├───addins ├───AppPatch │ └───MUI │ └───040C ├───Microsoft.NET │ └───Framework │ └───v2.0.50727 │ └───MUI │ └───0409 Notice there's no 'C:\WINDOWS

What do I use for a max-heap implementation in Python?

♀尐吖头ヾ 提交于 2019-11-26 15:39:45
Python includes the heapq module for min-heaps, but I need a max heap. What should I use for a max-heap implementation in Python? The easiest way is to invert the value of the keys and use heapq. For example, turn 1000.0 into -1000.0 and 5.0 into -5.0. Lijo Joseph You can use import heapq listForTree = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] heapq.heapify(listForTree) # for a min heap heapq._heapify_max(listForTree) # for a maxheap!! If you then want to pop elements, use: heapq.heappop(minheap) # pop from minheap heapq._heappop_max(maxheap) # pop from maxheap The solution is to negate your