How many different orders are possible in which the key values can occur while searching for a particular key? - binary-search-tree

When searching for the key value 60 in a binary search tree, nodes containing the key values 10, 20, 40, 50, 70, 80, 90 are
traversed, not necessarily in the order given. How many different orders are possible in which these key values can occur on the
search path from the root node containing the value 60?

The answer is 7C4.
Searching for 60, we encounter 4 keys less than 60 (10,20,40,50) & 3 keys greater (70,80,90).
The four lesser keys must appear in ascending order while the three greater ones must appear in descending order otherwise some keys will be left out in the traversal.
Note that in the traversal, these lesser keys might not be continuous and can be separated by greater keys.
For eg: 10, 90, 20, 30, 80, 40, 70, 50
but the order of both groups of keys (lesser and greater) remains the same individually.
Now, out of total seven positions, the lesser keys acquire four positions which can be selected in 7C4 ways. Once we know the places of these four keys, the places of the three greater keys only have one permutation.
For eg: if we know that
10, _, 20, 30, _, 40, _, 50
Then there's only one permutation of places for 90, 80 & 70 which is place number 2, 5 & 7 respectively.
Therefore for each combination of lesser keys, there's a unique combination of greater keys.
So total combinations= 7C4*1
=35 ways

Related

Code to obtain the maximum value out of a list and the three consecutive values after the maximum value

I am writing a model to calculate the maximum production capacity for a machine in a year based on 15-min data. As the maximum capacity is not the sum of the required capacity for all 15-min over the year, I want to write a piece of code that determines the maximum value in the list and then adds this maximum value and the three next consecutive values after this maximum value to a new variable. An simplified example would be:
fifteen_min_capacity = [10, 12, 3, 4, 8, 12, 10, 9, 2, 10, 4, 3, 15, 8, 9, 3, 4, 10]
The piece of code I want to write would be able to determine the maximum capacity in this list (15) and then add this capacity plus the three consecutive ones (8,9,3) to a new variables:
hourly_capacity = 35
Does anyone now the code that would give this output?
I have tried using the max(), the sum() and a combination of both. However, I do not get a working code. Any help would be much appreciated!

Redshift - Breaking number into 10 parts and finding which part does a number fall into

I am trying to break down a given number into 10 equal parts and then compare a row of numbers and see in which of the 10 parts they fall under.
ref_number, number_to_check
70, 34
70, 44
70, 14
70, 24
In the above data set, I would like to break 70 into 10 equal parts (in this case it would be 7,14,21, and so on till 70). Next I would like to see in which "part" does the value in column "number_to_check" fall into.
Output expected:
ref_number, number_to_check, part
70, 34, 5
70, 44, 7
70, 14, 2
70, 24, 4
You want arithmetic. If I understand correctly:
select ceiling(number_to_check * 10.0 / ref_number)
Here is a db<>fiddle (the fiddle happens to use Postgres).

Redis - Get the aggregated score from three sorted set

i have three sorted sets:
setA {play1: 100, play2: 150, play3: 230}
setB {play1: 120, play4: 100, play5: 200}
setC {play1: 110, play3: 150, play5: 30}
Is it possible to get an aggregated result (sum the score for the same user in different sets and display all of users scores in the result) of the three sorted sets?
thanks!
ZUNIONSTORE setSetSetAllMySets 3 setA setB setC AGGREGATE sum
sum happens to be the default aggregation and therefore isn't strictly necessary. I just wanted to get you interested in what other aggregations are available.
http://redis.io/commands/zunionstore

Alternating threads in JMeter

I have made a selection form a huge amount of ID's, using the following query:
select ID from [tabelname] where id > 0 and id < 31
This gives me 30 ID's to work with.
What I would like to do now, is to use 3 threads, with the first one using ID 1, 4, 7, 10 etc, the second ID 2, 5, 8, 11 etc and the third one ID 3, 6, 9, 12 etc.
Up until now, I have only been able to have all threads use ID 1 through 30 parallel to each other. Would it be at all possible to do this?
Thanks in advance!
JMeter has a build-in operation that you can use in combination with a pre-processor to find the current thread number:
https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterContext.html#getThreadNum()
If you now use ctx.getThreadGroup().getNumThreads() to find the number of threads you're using, you can basically divide your testset into subsets and let each thread compute on its own subset (e.g. thread1 computes on 0..10, thread2 on 11..20, thread3 on 21..30, etc..)

How to floor a number in sql based on a range

I would like to know if there is a function or some sort of way to round a number to lowest whole value. Something like floor, but to a specified increment like 10. So for example:
0.766,5.0883, 9, 9.9999 would all be floored to 0
11.84848, 15.84763, 19.999 would all be floored to 10
etc...
I'm basically looking to fit numbers in the ranges of 0, 10, 20, 30, etc
Can I also do it with different ranges? For example 0, 100, 200, 300, etc
Thank you.
You can do this with arithmetic and floor():
select 10*floor(val / 10)
You can replace the 10s with whatever value you want.