Invert a frequency table - google-sheets-formula

Given a frequency table that is the result of many manipulations (there is no range like A1:B5 - if it were then this is straight forward)
Is there a way to invert using formulas like
source:
[
[ 1, 3],
[ 2, 0],
[ 5, 4]
]
result (as a string):
"1,1,1,5,5,5,5,5"
I would like to avoid using macros.

I posted the questions, then resolved it.
Solution is to process before the 2d array is created.
=TEXTJOIN( ",", TRUE, MID( REPT( "," & first_column, _second_column ), 2, 1000 ) )

Related

Wrap array in an array in JSONata

Lets say I have a data structure that looks like this:
{ a: [ 1, 2, 3] }
I want to return 'a' wrapped in an array:
[ [ 1, 2, 3] ]
Is there any way to do this in JSONata?
Intuitively you would try [a], which you would expect to return the array as [[1,2,3]], but this returns [1,2,3], because of array singleton equivalence in JSONata.
You can try the below query
[[a]] - wrapping a within 2 set of square brackets
Since 'a' returns
1, 2, 3
[[a]] returns
[[1,2,3]]

Find vector of unique elements and vector of repeats using for loops and if conditions

I want a program that takes a vector of integers and creates two new vectors, one which contains each unique element, and another which contains the number of times these unique elements were repeated in the original vector.
So,
[1, 2, 2, 3, 3, 3]
has unique elements:
[1, 2, 3]
with the following number of repeats:
[1, 2, 3]
I know this has been asked before, but I want to achieve this using purely for loops and if conditions if need be. For the same reason, the code can be given in pseudocode obviously.
Please be nice, I am literally a beginner who learned programming syntax a few days ago.
Here a solution
var arr = [1, 2, 2, 2, 3, 3, 3, 3]
var unique = [...new Set(arr)]
var repeated = []
var i = 0
unique.map((a) => {
arr.map((b) => {
if(a === b) i++
})
repeated.push(i)
i = 0
})
console.log("[" + arr.join(",") + "] has unique elements [" + unique.join(",") + "] with the following number of repeats [" + repeated.join(",") + "]")

Find the maximal k such that there exists a point covered by k intervals

Suppose that n closed intervals [a[i], b[i]] on the real line are given (i = 1..n). Find the maximal k such that there exists a point covered by k intervals (the maximal number of “layers”). The number of operations should be of order nlogn.
There is a solution about it.
[Hint. Sort all the left and right endpoints of the intervals together. While sorting, assume that the left endpoint precedes the right endpoint located at the same point of the real line. Then move from left to right counting the number of layers. When we cross the left endpoint, increase the number of layers by 1; when we cross the right endpoint, decrease the number of layers by 1. Please note that two adjacent intervals are processed correctly; that is, the left endpoint precedes the right endpoint according to our convention.]
My question is, how do I know if the point encountered is left endpoint or right endpoint? Do I need extra spaces to take record?
The question itself contains the steps of the expected algorithm, almost a pseudo-code.
I turned the description into a python 3 program as an excercise:
def prefix_sum(seq, acc = 0):
for i in seq:
acc += i
yield acc
def count_layers(intervals):
endpoints = sorted([(s, -1) for s,e in intervals] + [(e, +1) for s,e in intervals])
return -min(prefix_sum(delta for _,delta in endpoints))
print(count_layers([[2,3],[1,2]]))
Tested with:
def test(intervals):
print()
print('Test')
for s,e in intervals:
print(' ' * s + '-' * (e - s + 1))
print('Answer:', count_layers(intervals))
TEST_CASES = [
[ [ 1, 5], [ 4, 9], [ 2, 4], [ 6,12], ],
[ [ 1, 3], [ 3, 5], [ 7, 9], [ 5, 7], ],
[ [ 3, 4], [ 1, 2], ],
[ [ 2, 3], [ 1, 2], ],
]
for test_case in TEST_CASES:
test(test_case)

Any way to align 2 dimensional arrays in Rubymine

I'm working on a project where our data setup is similar to this:
data = [
[:col1, :col2, :col3],
[1, 1, 1],
[1, 2, 1],
[2, 1, 1]
]
With enough columns present, it starts to get unreadable.
I'd like it to look similar to this:
data = [
[:col1, :col2, :col3],
[ 1, 1, 1],
[ 1, 2, 1],
[ 2, 1, 1]
]
Is there anyway to make Rubymine's autoformat to format arrays similar to the second example?

What is the difference between 0..size and 0...size in Ruby?

I'm reading some ruby code, and see 0..size and 0...size are used in similar situations.
Are there any difference, or they are just identical?
Quite simple actually. .. includes the ending value in the range (so 2..5 is 2,3,4,5) and ... excludes it (so 2..5 is 2,3,4). Nothing more to it.
.. (two dots) means that ju want an inclusive range. ... (three dots) means that you want a range but ommit the last element. Example:
0..5 returns 0, 1, 2, 3, 4, 5
0...5 returns 0, 1, 2, 3, 4
.. creates an inclusive Range, ... creates a Range object where the right limit is excluded.
(0..5).to_a
# => [0, 1, 2, 3, 4, 5]
(0...5).to_a
# => [0, 1, 2, 3, 4]

Resources