calculate possible combinations between multiple arrays - ruby-on-rails

I'm making a question list. There are 3 questions, each with 4 answers. I'm trying to calculate which combinations are possible.
There should be 4x4x4 (=64) possible combinations. And I expect an array like this [1,1,1] (user answers all 3 questions with the first answer).
I see that ruby has a nice permatation method, but it's not permatiation. The combination method, only takes 1 array to an account.
So in short, I have 3 array, each with [1,2,3,4] and I like 64 arrays with every combination

Use Array#product method.
[1,2,3,4].product([1,2,3,4],[1,2,3,4]).size # => 64
I used Array#size to show you, that 64 combinations are being generated.

If the number of answers to all questions is the same you can calculate the number of possible combinations by using number of answers raised to the number of questions. The ruby code for that would be:
answer_count = 4
question_count = 3
combinations = answer_count**question_count
If the number of answers to each question is different you can count them and then multiply them for your answer (as you were on the road to doing in your question). For example if you have 3 questions and the first has 4 answers, the second has 5 answers and the third has 3 answers you could do something like this:
answer_counts = [4, 5, 3]
answer_counts.inject(1) {|product, answers| product * answers}

Related

Virtual Memory To Physical Translation?

My book had the following image:
And a question of how many PTE's are there in L2 (or L1), the answer was 16 = 2^4 but why is that?
In the question it's given that each PTE size is 2 bytes so the answer should be 2^4/2 = 8 Since L2 index holds where we will point to. For example if it's 2 then it point to second half of the first PTE and not the second PTE.
What am I missing here?
You are overthinking. There are 4 bits used to index the L1 page table entry, so there are 2^4 possible values for this index and thus 2^4 possible page tables entries.
The size of a page table entry is not relevant to this question. It is asking "how many", not "how large".

Reverse percentage formula [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
Apparently "reverse percentage calculations" are a confusing topic (I looked through existing questions) - but I'm not sure I understand it myself.
I have a counter in my Google Sheets that is counting down a certain number of cells, and then I have the total range of cells.
So for example: my counter is at 6000 and its counting down through a range of 7000 cells.
What I'm trying to do is calculate the percentage that it's done counting down.
When I divide the part by the total, I obviously get something like 90% - which is not what I want. It should be like 2% (or whatever) :P
Does anyone know the formula I should apply here?
try:
=1-COUNTA(A:A)/A1
(cell formatted as a percentage)
I think I solved it.
If A is counting down from 100 and 0 = 100%, then the information I was originally missing was the difference between 100 and A.
So in my case:
A3:
=ROWS(A1:A)-A2
(A1:A is 100%)
(A2 is the current countdown)
So for example if ROWS(A1:A) = 10 (total rows in your sheet) & if my current countdown is 3 then:
A3:
=ROWS(A1:A)-A2 = 7
Then we calculate the percentage by:
A4:
=A3/ROWS(A1:A)
That should give you the correct percentage.

How does Lua 5.x represent sparse arrays?

Say, I have an array like this:
T = {1,2,[1000] = 3, [-1] = -1}
I know 1 and 2 will be in continous array part and -1 will be in hash part.
But I don't know where 3 will be. How it will be represented 'inside' Lua.
Would there be 997 wasted spaces between 2 and 3? Would 3 be delegated to hash part for efficency? Would there be 2 linked continous tables, one starting from index 1 and second starting from index 1000?
It depends on which version of Lua you use. In Lua 4, tables are implemented strictly as hash tables. In Lua 5, tables are part hash tables and part arrays, see the Lua Implementation where Section 4 covers tables and sparse arrays.
The array part tries to store the values corresponding to integer keys
from 1 to some limit n. Values corresponding to non-integer keys or
to integer keys outside the range are stored in the hash part. ... The
computed size of the array part is the largest n such that at least
half the blocks between 1 and n are in use... and there is at least
one slot used between n/2+1 and n.
In your example, 1000 would likely be outside the initial n, and would not cause the array part to grow as it would be too sparse.
You shouldn't need to worry about these details: just trust that Lua tables are implemented efficiently with expected constant-time access to an entry given its key. The array part is just an implementation detail to reduce memory usage by not needing to store some keys.
As explained by rpattiso, there is no memory waste in your example.

Active Record true random? [duplicate]

This question already has answers here:
Rails 3: Get Random Record
(15 answers)
Closed 8 years ago.
I have a pretty simple application that needs to (as fairly as possible) randomly assign a person to a team.
At the moment, I am iterating through with a few different methods.
Team.where(assigned: false).order("RANDOM()").first
as well as loading it into an array and using sample()
arr.sample().inspect
However, these don't appear to be truly random, they typically leave the edges (1,2..8,9 where count = 10) til last. Is there a better method that does or doesn't involve AR? Is there a mathematically noticeable difference between PSQL rand and sqlite3 random()?
Any assistance on loops to generate said random distribution are appreciated!
Although there may be more ways to generate random numbers to select a record (even extreme measures like random.org) the random selections you have provided should prove to be more than sufficient and with little or no difference between them.
If I were you I would however realise the difference in overhead between the two; one should select only one record from the database, the other pulls all the records into memory and picks one at random. If the Team table were to become large this could lead to a memory and calculation drain that would slow the whole calculation. The second method I would only use if I needed the whole table in memory for some other purpose as well.

Lua: Is decreasing iterator from inside of loop possible?

array={}
size=10
math.randomseed(os.time())
for i=1,size do
array[i]=math.random(size)
if(i>1) then
for j=1,i do
if array[j]==array[i] then
i=i-1
break
end
end
end
end
for i=1,size do
print(array[i])
end
The code above was meant to generate an array of random numbers from 1 to 'size' avoiding repeating values. I tried to achieve that by repeating top level 'for' loop once more if newly generated value was present before somewhere in array - by decreasing its iterator. Somehow it doesn't work. Why?
Is modifying iterator value from inside of loop not possible?
Example output with repeating values in array:
>lua5.1 "pairsss.lua"
2
1
10
6
5
2
5
7
7
4
>Exit code: 0
The solution to your problem is to shuffle the array, like Random iteration to fill a table in Lua.
To answer your question, from Lua 5.1 reference manual:
§2.4.5 – For Statement
All three control expressions are evaluated only once, before the loop starts. They must all result in numbers.
That means, no matter how you change the value of i inside the for loop, it doesn't affect how the iteration is done.
You can use a set instead of an array, as done by the author of question Randomize numbers in Lua with no repeats. As one of the answers points out, as your set gets closer in size to your range of randome numbers (say, you have random numbers 1 to 100 and your set is size 50) it will be more and more difficult to find a number that hasn't already been picked. You can see that for a set of size 50 and picking a random # from 1 to 100, then by the time you have the set half full, you have a 25-50 % chance of finding the random pick is already in use in your set. In that case, shuffling is the way to go, as explained in one of the answers to that post (Randomize numbers in Lua with no repeats).

Resources