How to store pyramid structure in a database - node.js

I would like to store pyramid structure in database preferably in NoSQL like mongodb.
So if I have a structure like below
0
0 0 <-tier 2
0 0 0 <-tier 1
*each zero is a node and each level is a tier and each node is connected to single next tier node
What I initially thought of doing is to store the objectID of the direct parent's node into each child's node - that way I can consecutively climb up the ladder by searching for nodes that are referenced by the previous node. However, that would require multiple queries to get to the top and I don't think it would be very favourable in terms of performance especially if I have to search for highest parent for many nodes.
So another way that I thought of was to store every single parent in order as an array into each node so that I can get the nth parent just by retrieving single node. However, it feels like this would lead to storing a lot of unnecessary data as opposed to the solution before.
Is there another way to solve this problem other than what I have come up with?

If the pyramid structure is regular, you can store every element in an array without wasting space, and still work out where an element should go in the array from its position and vice versa.
If the number of elements in each row is 1,2,3,4,5... as you have drawn then the number of elements in the first n rows (counting from 1) is 1+2+3+..n = n(n+1)/2 (this is just the sum of an arithmetic progression). Counting from 1 again, element i in row j is at array offset counting from zero of j(j+1)/2 + i - 2. To find a row number from an array offset, you can just use binary chop.

Related

Application of a custom function to generate iterations across a distance range

Bit of a complex query but I will try to articulate it as best I can:
Essentially I have a list of objects for which I am trying to work out the probability of said items hitting particular points below on the seabed having been dropped at the surface. So there are two steps I need guidance on:
I need to define a custom function, ERF(a,b), where I need to refer to specified values dependent on the Item Number (see in the column names) in order to then use them as multipliers:
These multipliers can be found in a dictionary, Lat_Dev (note please ignore the dataframe column reference as this was a previous attempt at coding a solution, info is now found in a dictionary format).
The function then needs to be repeated for set iterations between a & b with the step size defined as 0.1m. This range is from 0-100m. a is the lower limit and b the upper limit (e.g. a = 0.1m & b = 0.2m). This is done for each column (i.e. Item_Num).
Hopefully the above is clear enough. Cheers,

Firebase limit_to_last(1) doesn't work as expected

I have data in firebase that is recording temperature like the following:
I will eventually have a number of weeks, the key starts at 1, next week the key will be 2 then 3 etc etc
I wanted to write a query that gives me the data from the last week (The week with the highest numbered key)
I have this line of code in a python script:
rtn = root.child('bedroom').child('weeks').order_by_key().limit_to_last(1).get()
print(rtn)
This is what is printed out:
[None, {'date_time': '2018-06-08 19:38:41.634010', 'temperature': '21'}]
Why is None at the start of the array? Do I assume it is always here? I want to use the data in the second location of the array. But if it isn't always in the second location of the array my code will then break. I thought that query would return an array of size 1.
I think when I was testing I did see the array only being 1 element in size with the json structure as the first element but I cannot confirm this.
When you use numeric keys (like you do), Firebase may interpret your data as an array. To learn more about this, read Best Practices: Arrays in Firebase.
If this array coercion is a problem for your app, I recommend prefixing the number with a fixed string, e.g. "week_1" or even better "week_01 (since that may you can filter on ranges of weeks).

Looking for a way to distinguish identical string entries for index use

I am making a function in python 3.5.2 to read chemical structures (e.g. CaBr2) and then gives a list with the names of the elements and their coefficients.
The general rundown of how I am doing it is i have a for loop, it skips the first letter. Then it will append the previous element when it reaches one of: capital letter/number/the end. I did this with index of my iteration, and then get the entry with index(iteration)-1 or -2 depending on the specifics. For the given example it would skip C, read a but do nothing, reach B and append to my name list the translation of Ca, and append 1 to my coefficient list.
This works perfectly for structures with unique entries, but with something like CaCl2, the index of the iteration at the second C is not 2, but zero as index doesn't differentiate between the two. How would I be able to have variables in my function equal to the value at previous index(es) without running in to this problem? Keeping in mind inputs can be of any length, capitalization cannot change, and there could be any number of repeated values

Generating unique combinations of text

I'm building a string of text from different parts. Group A + GROUP B + GROUP C + GROUP D.
The text is put together in this exact order. Each sentence is unique.
I randomly take one sentence from each group and put them together so the total combination of unique text would be A*B*C*D where A,B,C,D are the number of sentences in their respective group.
My problem is that how do i track that i don't generate duplicates in this way and when to know that i have used up all possible combinations?
Storing all possible combinations somewhere seems rather inefficient way to do this. So what options do i have?
As random strings of text are pulled from each group, simply store the starting position of the sentence within the group along with the length into a container like a dictionary or perhaps HashSet. This would act as the key to the container. If the number of sentences in each group is small enough, you might be able to pack the data into a single integer or long value, otherwise define a structure or class for it. The code should look in the container to see if the random combination generated has already been used. If it has been used, then loop until a unique one has been found. If the total number of combinations is small enough such that the user might go through them all, then pre-calculate the total-count and check to see if the container reaches that count, in which case some sort of exit processing should be performed.

How to store an object and its row/column position + have an index

I am making a space invaders game. Each invader has its position on the screen stored. This would form a nice grid of 5 rows with each 11 invaders.
There are 3 types of invader A, B, and C. A consists of 22 invaders, B also, and C 11. Because of this I could not use their positions alone to form the grid on the screen. So, I added variables for how many rows and columns there are and with these I could use a nested for loop to get the right amount of invadertypes.
Now, I have an idea for some kind of algorithm to get them to do something, but for that I need to store them in a certain way. How I'm thinking of doing it is to use a Dictionary<int, Tuple<Point, Invader>>, where int will be the index like in a list, Point will be used for storing row-column, and Invader for well, the invader.
Before I used a List to store invaders and so I could with a for loop access the invader I needed to perform an operation on. Like invaders[i].DoSomething().
I want to be able to still do that, and have not only the invader, but also what row-column it is occupying.
What are my options?
Why not add row / column variables to your invader class?

Resources