traversal

Traversing an html nested list, follow-up question - detecting a node's position

不羁的心 提交于 2020-01-05 07:28:11
问题 With reference to to an previous question of mine: link I have an additional question, as follows: I need the ability to start the "position" at a point within the tree. Users can select a node within the tree by way of a hash (e.g. #post9) - they can click a node anywhere in the list to select it, or they can bookmark the url, which would include that node's own hash. So my further question is: how would I locate a node within the tree and get it's position, using the hash in the URL? The

How can I loop over HDF5 groups in Python removing rows according to a mask?

不羁岁月 提交于 2020-01-05 04:05:40
问题 I have an HDF5 file containing a number of different groups all of which have the same number of rows. I also have a Boolean mask for rows to keep or remove. I would like to iterate over all groups in the HDF5 file removing rows according to the mask. The recommended method to recursively visit all groups is visit(callable) , but I can't work out how to pass my mask to the callable. Here is some code hopefully demonstrating what I would like to do but which doesn't work: def apply_mask(name,

Huffman Tree Encoding

牧云@^-^@ 提交于 2020-01-05 02:53:27
问题 My Huffman tree which I had asked about earlier has another problem! Here is the code: package huffman; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.ArrayList; import java.util.PriorityQueue; import java.util.Scanner; public class Huffman { public ArrayList<Frequency> fileReader(String file) { ArrayList<Frequency> al = new ArrayList<Frequency>(); Scanner s; try { s = new Scanner(new FileReader(file)).useDelimiter(""); while (s.hasNext()) { boolean found =

How to traverse array from both left to right and from right to left?

房东的猫 提交于 2020-01-04 01:57:29
问题 Suppose I have an imperative algorithm that keeps two indices left and right and moves them from left to right and from right to left var left = 0 var right = array.length - 1 while (left < right) { .... } // move left and right inside the loop Now I would like to write this algorithm without mutable indices. How can I do that ? Do you have any examples of such algorithms ? I would prefer a non-recursive approach. 回答1: You can map pairs of elements between your list and its reverse, then go

N-Tree Traversal with Scala Causes Stack Overflow

眉间皱痕 提交于 2020-01-03 07:24:11
问题 I am attempting to return a list of widgets from an N-tree data structure. In my unit test, if i have roughly about 2000 widgets each with a single dependency, i'll encounter a stack overflow. What I think is happening is the for loop is causing my tree traversal to not be tail recursive. what's a better way of writing this in scala? Here's my function: protected def getWidgetTree(key: String) : ListBuffer[Widget] = { def traverseTree(accumulator: ListBuffer[Widget], current: Widget) :

How do you iterate over all configurations of m variables belonging to the same domain of size n?

孤人 提交于 2020-01-02 05:27:12
问题 EDIT: My solution is added to the end of the question. Thanks for the hint. I'll just go with an example. Suppose I have an array with length n : arr = { 1, 4, 8, 2, 5, ... } If I want to traverse all combinations of TWO elements I would write: for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { // do something with arr[i] and arr[j] } } I If I want to traverse all configurations of THREE elements I would simply add another layer of for iteration: for (int i = 0; i < n; i++) { for

Traversing an object getting the key and all parents keys

China☆狼群 提交于 2020-01-01 19:30:07
问题 traverse tree(json), fulfill getKeys(data, str) function using JS. get the key and all parents keys. const data = { key1: 'str1', key2: { key3: 'str3', key4: 'str4', key5: { key6: 'str6', key7: 'str7', key8: 'str8', }, } } for example: getKeys(data, 'str1'); return: 'key1' getKeys(data, 'str3'); return: 'key2, key3' getKeys(data, 'str6'); return: 'key2, key5, key6' I think it can be done be recursion, but how? this is my solution, but failed let s = []; function getKeys(data, str, key='') {

How to traverse keys of a Hashtable in alphabetical order?

匆匆过客 提交于 2020-01-01 18:50:30
问题 What is the easiest way to traverse a hashtable's keys in ascending alphabetical order? 回答1: This is fairly dependent upon what the type of the key is. But lets assume for a minute that they are strings. You could use the following LINQ query Hashtable table = GetHashTable(); var keys = table.Keys.Cast<String>().OrderBy(x => x); For more complex structures the LINQ query is only slightly different. Lets assume you had the following definition for a key struct Name { public string First;

Get a random element in single direction linked list by one time traverse

风流意气都作罢 提交于 2020-01-01 04:41:08
问题 I have a single direction linked list without knowing its size. I want to get a random element in this list, and I just have one time chance to traverse the list. (I am not allowed to traverse twice or more) What’s the algorithm for this problem? Thanks! 回答1: This is just reservoir sampling with a reservoir of size 1. Essentially it is really simple Pick the first element regardless (for a list of length 1, the first element is always the sample). For every other element with probability 1/n

Cartesian product traverse in scalaz

馋奶兔 提交于 2020-01-01 03:38:24
问题 In Eric Torreborre's blogpost on the paper Essence of the Iterator Pattern, he describes how the cartesian product of a traverse is also a traverse. Can anyone show me an example of this using the scalaz library as I can't figure it out. Let's say the problem is that, for a List[Int] I want to provide both of: The Int sum of the elements in the list A List[String] the elements of which are created by appending the "Z" to the String representation of the Int s My understanding is that I can do