So I'm doing a question that reads as follows
Build a circuit using the abstract syntax for Prop to test if two inputs are equal. Justify that your circuit is correct.
This is the Prop in question.
data Prop = FALSE | TRUE | IN String | NOT Prop | OR Prop Prop | AND Prop Prop
Instinctively I am tempted to write AND(IN "A")(IN "B") and give a truth table to prove it but this seems to simple. Am I missing something?
EDIT: My bad, I ended up making a XNOR gate that solved the problem. I mistook AND for XNOR which was the root cause of the confusion. The solution in the answer field is more elegant than mine so please refer to that.
The two inputs would be equal in two cases: either both are true or both are false. It seems that the given language allows to encode that: you can check if an input is true simply by referencing it, i.e. IN "A", and you can check if an input is false by negating it, i.e. NOT (IN "A"). Then combine these checks with ANDs and an OR, and you're done:
OR
(AND -- Both "A" and "B" are true
(IN "A") -- "A" is true
(IN "B") -- "B" is true
)
(AND -- Both "A" and "B" are false
(NOT (IN "A")) -- "A" is false
(NOT (IN "B")) -- "B" is false
)
.
--------------------------------------------------------------------------
| A | B | NOT A | NOT B | AND A B | AND (NOT A) (NOT B) | Result |
--------------------------------------------------------------------------
| false | false | true | true | false | true | true |
| false | true | true | false | false | false | false |
| true | false | false | true | false | false | false |
| true | true | false | false | true | false | true |
--------------------------------------------------------------------------
Related
This question already has answers here:
Weird behaviour of NOT
(3 answers)
VBA: Why would the Not operator stop working? [duplicate]
(3 answers)
Closed 2 years ago.
tl;dr: In the code below, the following two conditions both evaluate to True!!! How? Why?
If Not IsSaved Then
If IsSaved Then
I'm working with the VBA Visual Basic Editor (VBE) object. The .Saved property has me baffled.
Both (.Saved) and Not (.Saved) return True if the VBComponent object is in fact saved. I even tried explicitly coercing the property to a Boolean before evaluating the conditional. Here are the steps to reproduce:
Open a blank workbook in a new instance of Excel
Copy and run the following code from a standard module (e.g., "Module1"):
Sub ListModules()
Dim VBComp As Object 'VBComponent
For Each VBComp In Application.VBE.ActiveVBProject.VBComponents
Dim IsSaved As Boolean
IsSaved = CBool(VBComp.Saved)
If CStr(VBComp.Saved) = "True" Then
Debug.Print vbNewLine; VBComp.Name; " saved? "; VBComp.Saved; " ("; TypeName(VBComp.Saved); ")"
If Not IsSaved Then
Debug.Print VBComp.Name; " is not saved"
End If
If IsSaved Then
Debug.Print VBComp.Name; " is saved"
End If
End If
Next VBComp
End Sub
'Sample output:
ListModules
ThisWorkbook saved? True (Boolean)
ThisWorkbook is not saved
ThisWorkbook is saved
Sheet1 saved? True (Boolean)
Sheet1 is not saved
Sheet1 is saved
NOTE: To run this code, you may have to grant access to the VBA project object model: File -> Options -> Trust Center -> [Trust Center Settings...] -> Macro Settings -> [x] Trust access to the VBA project object model
#JMP and #SiddharthRout comments on question revealed the answer.
It's a non standard boolean implementation, where Trueis 1not -1.
But with that, logical operations fail.
E.g a Not A operation is the bitwise inversion of A (booleans are stored as long). If A=-1 its bits are 1111111111111111 and the inverson is 0000000000000000.
But if A=1 then its bits are 0000000000000001 and the inversion gets 1111111111111110 what is representing -2. So Not 1 is -2 what is True as it is <> 0!
That's why Not VBComponent.Saved = True (=-2) when VBComponent.Saved = True (=1) too!
| Assignment (a As Boolean) | Bits | Value As Integer | Value As Boolean |
|---------------------------|------------------|------------------|------------------|
| a = True | 1111111111111111 | -1 | TRUE |
| Not a | 0000000000000000 | 0 | FALSE |
| | | | |
| a = False | 0000000000000000 | 0 | FALSE |
| Not a | 1111111111111111 | -1 | TRUE |
| | | | |
| VBComponent.Saved = True | 0000000000000001 | 1 | TRUE |
| Not VBComponent.Saved | 1111111111111110 | -2 | TRUE |
| | | | |
| VBComponent.Saved = False | 0000000000000000 | 0 | FALSE |
| Not VBComponent.Saved | 1111111111111111 | -1 | TRUE |
Sub TestInversion()
Dim VBComp As Object 'VBComponent
Debug.Print
For Each VBComp In Application.VBE.ActiveVBProject.VBComponents
IsSaved = CInt(VBComp.Saved)
Debug.Print VBComp.Name; VBComp.Type; VBComp.Saved; CInt(VBComp.Saved); Not VBComp.Saved; Not CInt(VBComp.Saved); VBComp.Saved = True; VBComp.Saved = False
Next VBComp
End Sub
Now we are missing a reason for that strange implementation, maybe someone knows?
I am using excel 2010 and I am having for each customer id certain events that can be true or false.
Furthermore a user can configure the order in which the true events for a customer ID should be given back.
So for example I have the following customers with the following events:
| Customer ID | Event 1 | Event 2 | Event 3 | Event 4 |
|------------- |--------- |--------- |--------- |--------- |
| 1 | TRUE | FALSE | TRUE | FALSE |
| 2 | FALSE | TRUE | FALSE | FALSE |
| 3 | TRUE | TRUE | TRUE | TRUE |
| 4 | FALSE | TRUE | FALSE | FALSE |
| 5 | TRUE | FALSE | TRUE | TRUE |
| 6 | TRUE | TRUE | FALSE | FALSE |
| 8 | FALSE | FALSE | TRUE | TRUE |
| 9 | TRUE | TRUE | FALSE | TRUE |
Secondly, the order all true events should be given back can be prioritized:
| Events | Prioritized (1...most important - 4... least important) |
|--------- |--------------------------------------------------------- |
| Event 3 | 1 |
| Event 1 | 2 |
| Event 2 | 3 |
| Event 4 | 4 |
So for example, for customer with ID 3 the following output and order of events should be given:
For a customer with ID 8 the following output and order of events should be given:
My example excel looks like the following:
I was thinking of using several IFs, however in reality I have around 100 events and 10.000 customers.
Any suggestions how to implement this in excel?
I appreciate your replies!
Use this array formula:
=IFERROR(INDEX($H$3:$H$6,AGGREGATE(15,7,(ROW($H$3:$H$6)-MIN(ROW($H$3:$H$6))+1)/(INDEX(INDEX(B:E,MATCH($A$19,A:A,0),0),N(IF({1},MODE.MULT(IF({1},MATCH($H$3:$H$6,$B$2:$E$2,0)*{1,1})))))=TRUE),ROW(1:1))),"")
Being an array formula you need to put this in B19 hit Ctrl-Shift-Enter and then copy down.
The one caveat is the list of order be sorted in order. This does not look at the number in column I but the order in Column H that the events are listed.
N(IF({1},MODE.MULT(IF({1},MATCH($H$3:$H$6,$B$2:$E$2,0)*{1,1}))))) creates an array of the relative column numbers(B:E) in order they are listed in Column H.
INDEX(B:E,MATCH($A$19,A:A,0),0) returns the the row where the customer id is found.
INDEX(INDEX(B:E,MATCH($A$19,A:A,0),0),N(IF({1},MODE.MULT(IF({1},MATCH($H$3:$H$6,$B$2:$E$2,0)*{1,1}))))) returns the full array of TRUE/FALSE from the correct row in the correct order.
The aggregate then returns the first relative column number that is true and then the second and the third... as it is dragged down to the outer index. Which then returns the correct value from column H.
If no True is found at the correct k then it returns an error and the IFERROR returns a null string.
I came up with an option that's not perfect but should work.
First of all I'd convert your table to a list so it would look like that:
Right, after I'd create a Helper Column that would act as unique_Reference id for each true column showing the id and the priority:
The value would be "id"_"priority" using the formula (for Cell B3):
=IF(E3=TRUE,C3&"_"&VLOOKUP(D3,$G$3:$H$7,2,FALSE),"")
And then, I'd create the result list which have the issue of getting blanks due to not using array formulas for saving calc time:
Where formula for cell L3 (event with max priority) would be:
=+IF(ISERROR(VLOOKUP($L$2&"_"&K3,$B$3:$D$8,3,FALSE)),"",VLOOKUP($L$2&"_"&K3,$B$3:$D$8,3,FALSE))
I have two datasets:
First dataset looks like this:
| Key 1 | Value 1 | Key 2 | Value 2 | Key 3 | Value 3|
| abc | True | bcd | False | cde | False |
| bcd | False | cde | True | def | False |
| def | False | abc | True | None | N/A |
Second data looks like this:
| abc | bcd | cde | def | status |
| False | False | True | False | Success |
| True | False | False | False | Failure |
| False | True | True | True | Success |
| False | False | True | False | Failure |
| True | False | False | False | Success |
| False | True | True | True | Success |
| False | False | True | True | Success |
| True | False | False | False | Failure |
| True | True | True | False | Failure |
Now for every row in first data set, i want to pickup the key value pairs and apply them as filters in second data set i.e subset the rows from second subset. Then count the number of rows applicable, No. of success, No of failures.
So the first data set transforms to :
| Key 1| Value 1| Key 2| Value 2| Key 3| Value 3| Row Count | Successes| Failures|
| abc | True | bcd | False | cde | False | 3 |1 |2 |
| bcd | False | cde | True | def | False | 2 |1 |1 |
| def | False | abc | True | None | N/A | 4 |1 |3 |
Explanation:
In First row (of first datset): abc - True; bcd - False; cde - False. Applying these filters in the second dataset, we will be left out with the following rows:
| abc | bcd | cde | def | status |
| True | False | False | False | Failure |
| True | False | False | False | Success |
| True | False | False | False | Failure |
Numbers of rows : 3
Failure : 2
Success :1
I believe you need:
from collections import Counter
#create dictionaries of boolean values for each row
L = [{a:b for a, b in (zip(v[::2], v[1::2])) if isinstance(b, bool)}
for k, v in df1.T.to_dict('l').items()]
print (L)
[{'abc': True, 'bcd': False, 'cde': False},
{'bcd': False, 'cde': True, 'def': False},
{'def': False, 'abc': True}]
#match with df2 and count index values by Counter
df22 = df2.set_index('status')
out = [Counter(df22.index[np.logical_and.reduce([df22[k] == v
for k, v in x.items()])]) for x in L]
print (out)
[Counter({'Failure': 2, 'Success': 1}),
Counter({'Success': 1, 'Failure': 1}),
Counter({'Failure': 3, 'Success': 1})]
#create DataFrame
df2 = pd.DataFrame(out, index=df1.index).fillna(0).astype(int)
#insert total row for first position
df2.insert(0, 'Row Count', df2.sum(axis=1))
#join together
df = df1.join(df2)
print (df)
Key 1 Value 1 Key 2 Value 2 Key 3 Value 3 Row Count Failure Success
0 abc True bcd False cde False 3 2 1
1 bcd False cde True def False 2 1 1
2 def False abc True None NaN 4 3 1
I have in a sheet a table like so:
+------+---------+
| Name | Boolean |
+------+---------+
| A | true |
| B | true |
| B | false |
| C | false |
| C | false |
| A | false |
+------+---------+
But in another sheet I need to consolidate it by having a column with unique name and another with the OR of all column Boolean matching the unique name.
Like so:
+------+---------+
| Name | Boolean |
+------+---------+
| A | true |
| B | true |
| C | false |
+------+---------+
I tried Consolidate (no OR operation) and also Index/Match combo, and I couldn't make it work with array-formulas.
I more or less succeed with an Pivot table, but the result wasn't much satisfactory.
There's a simple way to do this that I can't see?
Maybe try:
=COUNTIFS(Sheet1!A:A,A2,Sheet1!B:B,TRUE)>0
I am creating a "Basic Search" bar that users can type in terms.
I am unsure of the order of operations for boolean logic.
If someone types terms(With no quotes):
A and B or C
What is the correct way to treat this?
(A and B) or (C)
OR
(A) and (B or C)
Wikipedia to the rescue, this should help:
http://en.wikipedia.org/wiki/Order_of_operations#Programming_languages
From the looks of things, it would appear that AND takes precedence over OR in most languages.
Based on Quetzalcoatl's response, the correct answer for the OP question is:
(A and B) or C
That's the equivalent for "A and B or C"
Although Quetzalcoatl's link (http://en.wikipedia.org/wiki/Order_of_operations#Programming_languages) speaks about programming languages (as this site does), a more common precedence is specified for general logic in wikipedia:
https://en.wikipedia.org/wiki/Logical_connective#Order_of_precedence
(A) and (B or C)
and means intersection or "like" union
Like in math
"AND" is like a multiplier and "OR" like a sum
in a "truth table"
OR
A | B | result
true | true | true
true | false | true
true | false | true
false | false | false
AND
A | B | result
true | true | true
true | false | false
true | false | false
false | false | false