for counting loop iterations in twig i've noticed that it seems down to preference in my project wether loop.index0 gets used or loop.index-1 is used. Is there any performance difference between the two or is it actually just preference?
Ok, so i've looked into the inner workings of twig. it looks like that it starts off every for loop by creating an array and assigning the value of key index0 and index as 0 and 1 respectively. At the end of each loop it increments those counter, so it is slightly more efficient to just call index0 instead of index-1.
Related
I'm almost certain this question has been asked before but I couldn't find the answer and have - to be perfectly honest - not the slightest idea what to even call the following problem:
In an excel table I want a sort of false counter whith every number appearing n times.
It will be nested inside a bigger Index formula to basically extract every column n times, to make it look like this
By nesting INT into MOD I managed to get alternating counts:
=MOD(INT(ROW())/2;1) results in the alternating values of 0.5 and 0
However this doesn't seem to lead to a solution for my original problem. So anyone with a name for the functionality I'm looking for or a solution for it has my gratitude.
you were close:
=INDEX(Table1[Value],INT((ROW($ZZ1)-1)/2)+1)
Found another (way more complicated) way to do this:
=INDEX(Table1[Value],INT((ROW()-ROW([Formula])+2)/2))
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).
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.
Write a program how_many.py
which has the following functions in it:
freq(n,l) which will be passed a list of integers l and a single integer n. It will return the frequency of which l appears, that is how many times it appears. So, freq(3,[3,2,2,1,3,4,5,4,3,4,3]) would return 4 since 3 appears 4 times. DO NOT USE COUNT -- loop through the list and do this manually.
min(l) -- calculates the smallest value of the list - again, do this manually using a loop not using a built in function.
mode(l) which returns the mode - the most frequently occurring item in the list - you can assume a single mode in the list, that is there won't be two items that appear the most times.
Yes this is my homework. No I do not want you guys doing it for me. i want to understand or have some type of help to GUIDE me to start this. I am entirely new to python and to the world of code, so it essential that I understand the concept.
Title question
=====================================================
Initialise a counter which counts how many times you find n in your list. Then you must step through each index in your array, and at each step, check that this value is the same as n
If it is, increment n by 1, if it isn't, do nothing. When you reach the end of your list, return the final result.
=====================================================
Min(l)
Initialise a variable which stores the minimum value so far called min as you are checking through the list. At each index of the list, check to see if the value at this index is bigger than min. If it is, update min's value, otherwise do nothing. Return min when you get to the end of the list.
=====================================================
Mode(l)
The mode is the most frequently found number in the array. You must make a map of keys and values (your keys being the distinct numbers in your list, and value being how many times it appears). Once you have looped through your list and found out how many times they each appear, return the largest value in your new map.
======================================================
good luck
I have 100 documents with a multi-value field. The field holds 5 possible values (Albert,Ben,Chris,Don,Ed) let's say. The field must contain 1 value, but can contain up to 5.
I need to compute the number of docs that contain each value, so
Albert 56
Ben 22
Chris 79
etc.
This seemed easy. I constructed a view that contains the docs, the first column is the field, and I selected show multiple documents for multiple feeds.
In SSJS loop through my master list of values in the field, and for each one do a getDocumentByKey.
myArray = applicationScope.application;
var dc:NotesDocumentCollection;
for (index = 0; index < myArray.length; ++index) {
dc = view1.getAllDocumentsByKey(myArray[index]);
Print(dc.getCount())
}
This gets the first value correctly, but none after. If I just hard code a particular value, it works. But when I call the getAllDocumentsByKey a second time, it doesn't return the right value.
I think this would work fine in LS, but in SSJS I must clear or recycle or rest something, but I don't know what.
Bryan,
Two things to try in this order:
Your first line of myArray = applicationScope.application; doesn't look right. I suspect that you are not actually getting an array here. I think you are just getting the first value from your applicationScope. Add a print statement on the next line of print(myArray.length); If it is always equal to one, that is your problem. You are not using var and you should set the variable to some java type using a colon.
Before the end of your for loop, try setting your collection to null. dc = null; This way you know for sure that you are getting a new collection in the next iteration.
Are any of your multi-value field values ambiguous? getAllDocumentsByKey(Vector) does partial matches. Unless you ever want that, I would recommend always using the second parameter and setting it to true, same always for getAllEntriesByKey().
An alternative, which will definitely perform better, would be to add a total column to the view. There's a performance hit of that on the view indexing, but you can then use a ViewNavigator with getColumnValues() and getNextSibling(). getCount() is extremely poor performing in LS and will almost certainly be as poorly performing in SSJS/Java. See this blog post from a few years ago http://www.intec.co.uk/why-you-shouldnt-count-on-a-notesviewnavigator/
If I understand that right: you want to count all values that are possible over an amount of documents to have a 5 value list with corresponsing 5 value counts, right?
You could loop through all docs, loop through all values and add entries to a HashMap with the value as key and an int as value (which should be increased everytime). Afterall you have a Map with 5 values holding the sums of each keys.
You will never get the right answer with getAllDocumentsByKey(). When document shows in one category, it will be missing from the collection of next category. That's the way it works.
Use ViewNavigator instead. Build it by category and simply count ViewEntries.