boolean-logic

Check if at least two out of three booleans are true

三世轮回 提交于 2019-11-26 08:38:36
问题 An interviewer recently asked me this question: given three boolean variables, a, b, and c, return true if at least two out of the three are true. My solution follows: boolean atLeastTwo(boolean a, boolean b, boolean c) { if ((a && b) || (b && c) || (a && c)) { return true; } else{ return false; } } He said that this can be improved further, but how? 回答1: Rather than writing: if (someExpression) { return true; } else { return false; } Write: return someExpression; As for the expression itself

How can I obtain the element-wise logical NOT of a pandas Series?

自闭症网瘾萝莉.ら 提交于 2019-11-26 05:54:00
问题 I have a pandas Series object containing boolean values. How can I get a series containing the logical NOT of each value? For example, consider a series containing: True True True False The series I\'d like to get would contain: False False False True This seems like it should be reasonably simple, but apparently I\'ve misplaced my mojo =( 回答1: To invert a boolean Series, use ~s: In [7]: s = pd.Series([True, True, False, True]) In [8]: ~s Out[8]: 0 False 1 False 2 True 3 False dtype: bool

Is (4 > y > 1) a valid statement in C++? How do you evaluate it if so?

亡梦爱人 提交于 2019-11-26 02:12:41
Is that a valid expression? If so, can you rewrite it so that it makes more sense? For example, is it the same as (4 > y && y > 1) ? How do you evaluate chained logical operators? The statement (4 > y > 1) is parsed as this: ((4 > y) > 1) The comparison operators < and > evaluate left-to-right . The 4 > y returns either 0 or 1 depending on if it's true or not. Then the result is compared to 1. In this case, since 0 or 1 is never more than 1 , the whole statement will always return false . There is one exception though: If y is a class and the > operator has been overloaded to do something

Is (4 > y > 1) a valid statement in C++? How do you evaluate it if so?

大兔子大兔子 提交于 2019-11-26 00:56:07
问题 Is that a valid expression? If so, can you rewrite it so that it makes more sense? For example, is it the same as (4 > y && y > 1) ? How do you evaluate chained logical operators? 回答1: The statement (4 > y > 1) is parsed as this: ((4 > y) > 1) The comparison operators < and > evaluate left-to-right. The 4 > y returns either 0 or 1 depending on if it's true or not. Then the result is compared to 1. In this case, since 0 or 1 is never more than 1 , the whole statement will always return false .

Does Python support short-circuiting?

六眼飞鱼酱① 提交于 2019-11-25 22:56:33
问题 Does Python support short-circuiting in boolean expressions? 回答1: Yep, both and and or operators short-circuit -- see the docs. 回答2: Short-circuiting behavior in operator and , or : Let's first define a useful function to determine if something is executed or not. A simple function that accepts an argument, prints a message and returns the input, unchanged. >>> def fun(i): ... print "executed" ... return i ... One can observe the Python's short-circuiting behavior of and , or operators in the

What are bitwise operators?

南楼画角 提交于 2019-11-25 22:29:15
问题 I\'m someone who writes code just for fun and haven\'t really delved into it in either an academic or professional setting, so stuff like these bitwise operators really escapes me. I was reading an article about JavaScript, which apparently supports bitwise operations. I keep seeing this operation mentioned in places, and I\'ve tried reading about to figure out what exactly it is, but I just don\'t seem to get it at all. So what are they? Clear examples would be great! :D Just a few more

How to test multiple variables against a value?

ぐ巨炮叔叔 提交于 2019-11-25 22:09:14
问题 I\'m trying to make a function that will compare multiple variables to an integer and output a string of three letters. I was wondering if there was a way to translate this into Python. So say: x = 0 y = 1 z = 3 mylist = [] if x or y or z == 0 : mylist.append(\"c\") if x or y or z == 1 : mylist.append(\"d\") if x or y or z == 2 : mylist.append(\"e\") if x or y or z == 3 : mylist.append(\"f\") which would return a list of [\"c\", \"d\", \"f\"] Is something like this possible? 回答1: You