de'morgans law boolean translation discrepency. what am i downg wrong? - boolean

demorgans law states in this article
https://ryanstutorials.net/boolean-algebra-tutorial/boolean-algebra-laws.php#introduction
that NOT(P & K) = NOT(P) OR NOT(K)
so i did a little testing with 1's and zeros
NOT(1 AND 1) = false
NOT(1 AND 0) = false
NOT(0 AND 1) = false
NOT(O AND 0) = true
so then i did the second expression
NOT(1) OR NOT(O) = TRUE
NOT(0) OR NOT(1) = TRUE
NOT(0) OR NOT(0) = TRUE
NOT(1) OR NOT(1) = FALSE
what am i doing wrong here?

You have to do this table :
P K | Q Q'
0 0 | 0 1
0 1 | 0 1
1 0 | 0 1
1 1 | 1 0
The Q is the Output and the Q' is the Output', 0=false and 1=true;
So, the first column is obtained doing P*K and the second denying Qso P'+K'.

Related

How to run a formula until it meets a certain criteria?

So, I have a formula ( =INDEX(Sheet1.A1:F15,RANDBETWEEN(1,15),RANDBETWEEN(1,6)) ) that returns a random number in the sheet. But, how to run the formula until the returned number is less than or equal to 25 ?
I thought of using for..next.. but couldn't get it how to run ...
Welcome!
As #thebusybee pointed out in his comment, a macro for this task is much easier than using built-in functions. As rightly pointed out #tohuwawohu, pre-filtering the values makes things a lot easier. The macro code could be, for example, like this
Option Explicit
Function getRandValue(aValues As Variant, nTypeCriteria As Integer, dCriteriaValue As Variant) As Variant
Rem Params: aValues - array of values,
Rem nTypeCriteria - -2 less then, -1 not more, 0 equal, 1 not less, 2 more than
Rem dCriteriaValue - value to compare
Dim aTemp As Variant
Dim i As Long, j As Long, k As Long
Dim bGoodValue As Boolean
k = UBound(aValues,1)*UBound(aValues,2)
ReDim aTemp(1 To k)
k = 0
For i = 1 To UBound(aValues,1)
For j = 1 To UBound(aValues,2)
bGoodValue = False
Select Case nTypeCriteria
Case -2
bGoodValue = (aValues(i,j) < dCriteriaValue)
Case -1
bGoodValue = (aValues(i,j) <= dCriteriaValue)
Case 0
bGoodValue = (aValues(i,j) = dCriteriaValue)
Case 1
bGoodValue = (aValues(i,j) >= dCriteriaValue)
Case 2
bGoodValue = (aValues(i,j) > dCriteriaValue)
End Select
If bGoodValue Then
k = k+1
aTemp(k) = aValues(i,j)
EndIf
Next j
Next i
If k<1 Then
getRandValue = "No matching values"
ElseIf k=1 Then
getRandValue = aTemp(k)
Else
getRandValue = aTemp(Rnd()*(k-1)+1)
EndIf
End Function
Just put a call to this function in a cell in the form
=GETRANDVALUE(A1:F15;-1;25)

DP Coin Change Algorithm - Retrieve coin combinations from table

To find how many ways we have of making change for the amount 4 given the coins [1,2,3], we can create a DP algorithm that produces the following table:
table[amount][coins.count]
0 1 2 3 4
-----------
(0) 1 | 1 1 1 1 1
(1) 2 | 1 1 2 2 3
(2) 3 | 1 1 2 3 4
The last position being our answer. The answer is 4 because we have the following combinations: [1,1,1,1],[2,1],[2,2],[3,1].
My question is, is it possible to retrieve these combinations from the table I just generated? How?
For completeness, here's my algorithm
func coinChange(coins: [Int], amount: Int) -> Int {
// int[amount+1][coins]
var table = Array<Array<Int>>(repeating: Array<Int>(repeating: 0, count: coins.count), count: amount + 1)
for i in 0..<coins.count {
table[0][i] = 1
}
for i in 1...amount {
for j in 0..<coins.count {
//solutions that include coins[j]
let x = i - coins[j] >= 0 ? table[i - coins[j]][j] : 0
//solutions that don't include coins[j]
let y = j >= 1 ? table[i][j-1] : 0
table[i][j] = x + y
}
}
return table[amount][coins.count - 1];
}
Thanks!
--
Solution
Here's an ugly function that retrieves the combinations, based on #Sayakiss 's explanation:
func getSolution(_ i: Int, _ j: Int) -> [[Int]] {
if j < 0 || i < 0 {
//not a solution
return []
}
if i == 0 && j == 0 {
//valid solution. return an empty array where the coins will be appended
return [[]]
}
return getSolution(i - coins[j], j).map{var a = $0; a.append(coins[j]);return a} + getSolution(i, j - 1)
}
getSolution(amount, coins.count-1)
Output:
[[1, 3], [2, 2], [1, 1, 2], [1, 1, 1, 1]]
Sure you can. We define a new function get_solution(i,j) which means all solution for your table[i][j].
You can think it returns an array of array, for example, the output of get_solution(4,3) is [[1,1,1,1],[2,1],[2,2],[3,1]]. Then:
Case 1. Any solution from get_solution(i - coins[j], j) plus coins[j] is a solution for table[i][j].
Case 2. Any solution from get_solution(i, j - 1) is a solution for table[i][j].
You can prove Case 1 + Case 2 is all possible solution for table[i][j](note you get table[i][j] by this way).
The only problem remains is to implement get_solution(i,j) and I think it's good for you to do it by yourself.
If you still got any question, please don't hesitate to leave a comment here.

Why !(0 || 1 || 0) is 0?

I am trying to understand Boolean logic and operators.
I found this example but can't understand why this expression will evaluate to the one shown below.
Say, a = 0, b = 1, c = 0
Expression Will Evaluate to
val1 = !(a || b || c); !(0 || 1 || 0) = !(1) = 0
As I see it, val1 is not a or not b or not c, so why it evaluates to not 1 ?
Not(a or b or c) evaluates the or operations first, so it's not the same as (not a) or (not b) or (not c).
Indeed, it's the same as (not a) AND (not b) AND (not c).
Either operand to an OR being true will give a true result, and then the NOT flips that to a false result for the expression as a whole.
As with integer or real number arithmetic, order of operation can greatly alter the result.
.... val1 is not a or not b or not c ...
No, this is incorrect. The 0 || 1 || 0 inside the parenthesis is evaluated first. The example has it right.
Let's say val1 = 1
1 = !(0 || 1 || 0)
1 = !(1) - because it is the only value that is equal to val1
1 = 0 - then it negates it afterwards
Let's go step-by-step.
val1 = !(0 || 1 || 0);
Firstly, 0 || 1 will evaluate to 1, because || means 'true if at least one of them is true, otherwise false', and 1 = true, 0 = false.
So now it is
val1 = !(1 || 0); Here 1 || 0 will again evaluate to 1, because at least one of them is 1. Now we've got val1 = !(1);. ! means the opposite of the input, so !(1) = 0.
As I see it, val1 is not a or not b or not c, so why it evaluates to not 1 ?
Because what you say would be written as val1 = !0 || !1 || !0. Its quite different, because it doesn't have parenthesis. Parenthesis means 'evaluate everything in the parenthesis first'.

Change values of an array of pscustomobject's with a for-loop

I have tried to create a array of custom objects (pscustomobject) and now i tried to change some values of the different custom objects with an for-loop. But it doesn't seem to work. Here is what i tried:
$obj = #([pscustomobject]#{value=0;type="D";used=$false})
$arr1 = #($obj) * 10
for($v = 0; $v -lt 4; $v++){
$arr1[$v].value = ($v+1)
$arr1[$v].type ="bubble"
}
The result is:
value type used
----- ---- ----
4 bubble False
4 bubble False
4 bubble False
4 bubble False
4 bubble False
4 bubble False
4 bubble False
4 bubble False
4 bubble False
4 bubble False
But i expected that the result will be:
value type used
----- ---- ----
1 bubble False
2 bubble False
3 bubble False
4 bubble False
4 D False
4 D False
4 D False
4 D False
4 D False
4 D False
This is only a snipped, and i was only trying something out. But i am a bit annoyed that i don't get it... Sorry i think it's a easy think but don't see whats worong.... i am still a PS noob... :-/
EDIT: PSv3 is used, but i think it doesn't matter...
This:
$arr1 = #($obj) * 10
is not creating 10 new objects. It created 10 references to the same object.
Note the difference if you do this:
$arr1 = 1..10 |% {[pscustomobject]#{value=0;type="D";used=$false}}
for($v = 0; $v -lt 4; $v++){
$arr1[$v].value = ($v+1)
$arr1[$v].type ="bubble"
}
$arr1
value type used
----- ---- ----
1 bubble False
2 bubble False
3 bubble False
4 bubble False
0 D False
0 D False
0 D False
0 D False
0 D False
0 D False

Difference between or and xor

My question is operator related. Today I studied about the operators. Having a confusion. In PHP what is the difference between "or" and "xor". I know both of them are related to Boolean expression. But can't find the original difference.
Anyone please help to understand it more clearly.
It has to do with mutual exclusion. xor is exclusive. or is inclusive.
Truth Table Comparison
$x $y ($x or $y) ($x xor $y)
0 0 0 0
1 0 1 1
0 1 1 1
1 1 1 0
Note: the difference in the last case. xor is only true when either $x or $y is true, but not both (as the case for or).
xor means "exclusive or". That is to say, it's or, but with the single change that if both parameters to the operation are true, the answer is false.
A xor B == (A or B) && !(A and B)
The difference is with an input of an even amount of 1's (or true's):
true or true = true
true xor true = false
true or true or true = true
true xor true xor true = true
true or false or true = true
true xor false xor true = false
Truth table of OR:
in1 | in2 | out
-------|-------|-----
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 1
Truth table of XOR:
in1 | in2 | out
-------|-------|-----
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 0
Another way of putting it: OR can be seen as an AND/OR connector. One or both choices can be true or chosen. XOR, on the other side, could be described as a "real" OR. Only one of both choices can be true (or chosen).
one is exclusive or -> xor, and the other is or which means at least one of the conditions are met in a truth table. Or is not mutually exclusive, xor is.
enter image description here
xor is exclusive. or is inclusive