Convention for Checking Equivalence of Three Values (Python) - boolean

I had a question re: checking for equivalence of three values in Python. I was wondering which of these two options was better, as in, is one a more excepted convention than the other?
a == b == c
a == b and b == c
Any insight would be appreciated, thank you!

Related

Working out truth table for Boolean equation

I am trying to work out the truth table for the Boolean Equation that I am badly explaining:
[A.B (NAND)] + [A+B (NOT NOR]
I would also be interested to know how I represent this equation properly using a standard character set. Help!
The syntax in many programming languages is similar to the following
AND:
A && B
OR:
A || B
NOT:
!A
NAND:
!(A && B)
NOR:
!(A||B)
Hope that helps. If you have any questions don't hesitate to comment below!

Constant in function of other constants - TI-Nspire CAS

I have these three expressions:
E1=(c1-c2)/(c1-c5)
E2=(c3-c4)/(c3-c7)
Es=(c1-c4)/(c1-c5)
How can I express Es in function of E1 and E2?
Es=E1+E2*(c3-c7)/(c1-c5)
Thank you for your time.
This is same type of problem you had in previous question and you were right - TI basic is not made for such things. There is no automatic way to do what you want but in each individual case you can do the same kind of gymnastics as previously. In this case, you have system of 2 equations with many variables, so you can eliminate 2 of them. You picked (for some reason) c2 and c4 to eliminate, so system must be solved for those 2 variables and solution included in third equation:
i1:=solve({E1=(c1-c2)/(c1-c5),E2=(c3-c4)/(c3-c7)},{c2,c4})
Es:=(c1-c4)/(c1-c5)|i1
result>
Es:=(c1+c3*(e2-1)-c7*e2)/(c1-c5)
Also, in this case E1 and E2 expressions are independent, so they can be solved separately, and Es expr. doesnt contain c2, you can just>
i1:=solve(E2=(c3-c4)/(c3-c7),c4)
Es:=(c1-c4)/(c1-c5)|i1
..with the same result. This is not what you wanted, but your solution is not correct anyway.

Is using ANY (or ALL) faster than checking manually against each entry in the ANY? - postgres

Say I want to check
"A does not equal any of the following: A, B, C"
I could write my condition as
A != ALL('{A, B, C})`
I could also write it as
A != A and A != B and A != C
I know I'm not really using types correctly here (mostly for brevity) but the idea should be clear. Is one of these faster than the other? I'm not sure how to test these kind of things on databases yet :(
Both versions should produce the same execution plan, but if you want to get rid of the subjunctive in that sentence, you should check yourself using EXPLAIN.
By the way, I recommend that you use the SQL standard operator <> rather than !=.

how to use forall X in answer set programming (dlv) (answer set prolog)

I have the following facts in dlv, knows (X,Y) means X knows Y.
knows(adam, dan).
knows(adam,alice).
knows(adam,peter).
knows(adam,eva).
knows(dan, adam).
knows(dan,alice).
knows(dan,peter).
knows(eva, alice).
knows(eva,peter).
knows(alice, peter).
knows(peter, alice).
I have defined the following predicates,
person(X) :- knows(X, _).
This will give all the persons from the facts. I am trying to find a predicate popular(X). that will give the popular person. It is defined such that if all persons knows X then X is popular. The answer for the above list of facts is alice and peter. I defined it as below,
popular(X):-person(X),knows(_,X).
X is popular if its a person and everyone knows X. But I am getting all persons as the result when I run it. Where am I making a mistake?
As per the comment string on the original post, you have defined popular to be "a person that is known by someone". Since - in your knowledge base - everyone is known by someone, everyone is popular.
Assuming "a popular person is one whom everyone knows but the popular person knows only other popular persons"; if we want to know if X is popular:
We either need to count all the people that know X and then compare that to the number of people;
Or we need to verify that it is never the case that someone doesn't know X.
I'll focus on the second way to do this, using forall. Take sometime and run some tests on your own to understand how that works. Here's an example of what you might do:
popular(X): - person(X),
forall(
( person(Y),
X \= Y
),
knows(Y,X)
).
If you run this, you get Alice and Peter as answers.
But if we include the other condition:
popular(X): - person(X),
forall(
( person(Y),
X \= Y
),
knows(Y,X)
),
forall(
knows(X,Z),
popular(Z)
).
That last line says X needs to know people that are popular exclusively... and now, if you run this, you're most likely going to get a 'out of local stack' - it's a bottomless recursive definition.
You always need to check if someone is popular to know if someone is popular to know if someone is popular... Try to think about the problem and why that is. Is there a way to check if someone is popular without needing to check if someone else is popular? What if someone 'knows' themselves? What if two people know each other? This might take a slightly more complex approach to solve.
By the way, notice that your definition of person returns multiple people - everyone is a person for every person they know. Besides making every check take a lot longer (since there are more 'people' to check), this might be a problem if you decide to go with the firs approach (the counting one).
Wouldn't it make sense to define explicitly who are the people and then define 'knowing' as a relation between people?
person('Alice').
person('Bob').
knows('Alice','Bob').
As said in lurker's comment (with slight modification and emphasis by me), the reason you get all persons as a result is
You have defined person as: X is a person if X knows someone. And you've defined popular as: X is popular if X is a person and someone knows X.
But you wanted to define: X is popular if X is a person and everyone knows X.
The following is an ASP solution for clingo 4. DLV might have slight differences in syntax.
% Project
person(P) :- knows(P, _).
% Separate helper predicate knows2/2.
% Not needed if polluting knows/2 with knows(X, X) is OK.
knows2(O, P) :- knows(O, P).
knows2(P, P) :- person(P).
% Everybody knows a popular person.
% When there is a person O that doesn't know P, #false is active.
% I.e. all rule instantiations where some O doesn't know P are discarded.
popular(P) :- person(P), #false : person(O), not knows2(O, P).
% Popular person knows only other popular persons.
% Redundant at this point, since the above rule already results
% in correct answer without further integrity constraints.
:- person(P), person(O), popular(P), not popular(O), knows(P, O).
#show popular/1.

comparing strings by lexicographical order in e/specman

Does specman have something like lex_lt(s1,s2) methods? (i.e. compare strings by lexicographical order). If not, is there a recommended way to achieve the same?
It seems that there isn't. You can do 2 things here. You can either implement your own strcmp() style function in e and use that directly, or you can integrate Specman with a C file that wraps strcmp() in function that can be called from your e code. Have a look at the Specman Integrator's Guide section in the product manual for details on how to do this.
As far as I know, we don’t have something pre-defined for this.
But it can be done, for example, in the following ugly way:
if {s1;s2}.sort(it)[0] == s1 …. // if it’s TRUE, then s1 is less that s2, otherwise not
Of course, as Tudor suggested, the best way will be to define C routine to wrap strcmp().