boolean-logic

Python `or`, `and` operator precedence example

坚强是说给别人听的谎言 提交于 2019-12-10 18:54:04
问题 I cannot produce example in Python which shows Boolean operator precedence rules combined with short circuit evaluation. I can show operator precedence using: print(1 or 0 and 0) # Returns 1 because `or` is evaluated 2nd. But the issue with short circuiting shows up when I change it to this: def yay(): print('yay'); return True def nay(): print('nay') def nope(): print('nope') print(yay() or nay() and nope()) # Prints "yay\nTrue" For each of 4 possibilities when expression before or is True

case insensitive equals method based on one attribute

寵の児 提交于 2019-12-10 17:30:55
问题 Orginal Question This is a really horrible method, which checks for equality on base of the code but case agnostic def ==(another_country) (code.nil? ? nil : code.downcase) == (another_country.code.nil? ? nil : another_country.code.downcase) unless another_country.nil? end Can you point my in the right direction how to write this more elegant w/o reliying on ugly if else structures? This is the solution I ended up using (+RSpecs) # Country model class Country < ActiveRecord::Base attr

Complex SOLR query including NOT and OR

自闭症网瘾萝莉.ら 提交于 2019-12-10 15:04:11
问题 I have some fairly complex requirements for a SOLR search that I need to execute against my database of tagged content. I need to first filter the database to those results matching my filter tags. Any of these results which have a tag from the blacklist should be removed unless they also contain tags from the whitelist. So let's say I want to retrieve all documents tagged "forest" or "conservation". But I want to exclude documents tagged "uk" or "europe" - unless they are also tagged with

Is it useful in C# to apply DeMorgan's theorem to manually optimize boolean expressions in conditional statements (e.g. if conditions)

♀尐吖头ヾ 提交于 2019-12-10 02:20:59
问题 Back in the day when I did most of my work in C and C++, as a matter of course, I would manually apply deMorgan's theorem to optimize any non-trivial boolean expressions. Is it useful to do this in C# or does the optimizer render this unnecessary? 回答1: On processors this fast, it's virtually impossible for rearranging boolean expressions to make any actual difference in speed. And the C# compiler is very smart, it will optimize it as well. Optimize for readability and clarity! 回答2: Your first

Simplify boolean expression algorithm

余生颓废 提交于 2019-12-10 01:10:11
问题 Anybody knows of an algorithm to simplify boolean expressions? I remember the boolean algebra and Karnaught maps, but this is meant for digital hardware where EVERITHING is boolean. I would like something that takes into account that some sub-expressions are not boolean. For example: a == 1 && a == 3 this could be translated to a pure boolean expression: a1 && a3 but this is expression is irreducible, while with a little bit of knowledge of arithmetics everibody can determine that the

Converting rgba values into one integer in Javascript

僤鯓⒐⒋嵵緔 提交于 2019-12-10 01:04:28
问题 I can already convert 32bit integers into their rgba values like this: pixelData[i] = { red: pixelValue >> 24 & 0xFF, green: pixelValue >> 16 & 0xFF, blue: pixelValue >> 8 & 0xFF, alpha: pixelValue & 0xFF }; But I don't really know how to reverse it. 回答1: To reverse it, you just have to combine the bytes into an integer. Simply use left-shift and add them, and it will work. var rgb = (red << 24) + (green << 16) + (blue << 8) + (alpha); Alternatively, to make it safer, you could first AND each

Cleaner Alternative to Extensive Pattern Matching in Haskell

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-09 05:04:22
问题 Right now, I have some code that essentially works like this: data Expression = Literal Bool | Variable String | Not Expression | Or Expression Expression | And Expression Expression deriving Eq simplify :: Expression -> Expression simplify (Literal b) = Literal b simplify (Variable s) = Variable s simplify (Not e) = case simplify e of (Literal b) -> Literal (not b) e' -> Not e' simplify (And a b) = case (simplify a, simplify b) of (Literal False, _) -> Literal False (_, Literal False) ->

How to use boolean 'and' in Python [duplicate]

旧时模样 提交于 2019-12-08 23:15:59
问题 This question already has answers here : Python's equivalent of && (logical-and) in an if-statement (10 answers) Closed 5 years ago . In C# we can use && (boolean and) like this: int i = 5; int ii = 10; if(i == 5 && ii == 10) { Console.WriteLine("i is 5, and ii is 10"); } Console.ReadKey(true); But try that with python: i = 5 ii = 10 if i == 5 && ii == 10: print "i is 5 and ii is 10"; I get an error: SyntaxError: invalid syntax If I use a single & , at least I get no syntax error. How do I do

Simplify boolean expression i.t.o variable occurrence

半城伤御伤魂 提交于 2019-12-07 14:16:20
问题 How to simplify a given boolean expression with many variables (>10) so that the number of occurrences of each variable is minimized? In my scenario, the value of a variable has to be considered ephemeral, that is, has to recomputed for each access (while still being static of course). I therefor need to minimize the number of times a variable has to be evaluated before trying to solve the function. Consider the function f(A,B,C,D,E,F) = (ABC)+(ABCD)+(ABEF) Recursively using the distributive

Kripke semantics: learning software available?

扶醉桌前 提交于 2019-12-07 09:18:37
问题 I am stuck on Kripke semantics, and wonder if there is educational software through which I can test equivalence of statements etc, since Im starting to think its easier to learn by example (even if on abstract variables). I will use ☐A to write necessarily A ♢A for possibly A do ☐true, ☐false, ♢true, ♢false evaluate to values, if so what values or kinds of values from what set ({true, false} or perhaps {necessary,possibly})? [1] I think I read all Kripke models use the duality axiom : (☐A)->