Questions tagged [logic]

Logic refers to the ultimate flow of your code and how you arrive your desired solution. Questions should relate to finding a coding solution (or improving existing coding logic) to a given problem. Please use with an appropriate language tag, a thorough description of your logic, and the relevant code you're working on. General logic questions are off-topic. If you simply need a code review, consider https://codereview.stackexchange.com

Note that the bulk of questions tagged concern the broad sense of program logic described in the excerpt above. Before using the tag, consider if a tag about a more specialised logic-related topic would be a better fit. Examples include:

Furthermore, if your question is about one of those specialised topics, consider whether it essentially involves programming in some way. If it doesn't, it will likely be more appropriately posted elsewhere in the network. Some sites in which such questions might be a good fit, depending on the circumstances of each concrete case, are Philosophy, Mathematics and Computer Science.

9357 questions
42
votes
10 answers

Find the biggest interval that has all its members in list in O(n)

I was asked this in an interview. Given a list of integers, How can we find the biggest interval that has all its members in the given list? E.g. given list 1,3,5,7,4,6,10 then answer would be [3, 7]. Because it has all the elements between 3 and…
Jayram
  • 18,820
  • 6
  • 51
  • 68
41
votes
9 answers

XOR of three values

What is the simplest way to do a three-way exclusive OR? In other words, I have three values, and I want a statement that evaluates to true IFF only one of the three values is true. So far, this is what I've come up with: ((a ^ b) && (a ^ c) && !(b…
Josh
  • 1,387
  • 2
  • 13
  • 15
41
votes
18 answers

How do I determine if *exactly* one boolean is true, without type conversion?

Given an arbitrary list of booleans, what is the most elegant way of determining that exactly one of them is true? The most obvious hack is type conversion: converting them to 0 for false and 1 for true and then summing them, and returning sum ==…
SCdF
  • 57,260
  • 24
  • 77
  • 113
40
votes
3 answers

How does Djinn work?

OK, so I realise that I will probably regret this for the rest of my life, but... How does Djinn actually work? The documentation says that it uses an algorithm which is "an extension of LJ" and points to a long confusing paper about LJT. As best as…
MathematicalOrchid
  • 61,854
  • 19
  • 123
  • 220
40
votes
7 answers

AND/OR in Python?

I know that the and and or expressions exist in python, but is there any and/or expression? Or some way to combine them in order to produce the same effect as a and/or expression? my code looks something like this: if input=="a": if "a"…
JNat
  • 1,456
  • 4
  • 34
  • 37
39
votes
3 answers

Fastest way to get sign in Java?

I'd like to get the sign of a float value as an int value of -1 or 1. Avoiding conditionals is always a good idea in reducing computational cost. For instance, one way I can think of would be to use a fast bit-shift to get the sign: float a =…
Engineer
  • 8,529
  • 7
  • 65
  • 105
39
votes
2 answers

oracle sql date not later than today

I need to display some data if it's a - new data - updated data let's say, I will be basing these data from a publishdate column and updated column where publishdate and updateddate are both timestamps ? . so how to compute the date if it's a…
sasori
  • 5,249
  • 16
  • 86
  • 138
38
votes
2 answers

Visual Studio Project/Item Template Parameter Logic

Since I have only seen a few posts on the topic but no in-depth explanation of the logic for parameters in templates for Visual Studio, I figured I'd post this here. Following the MSDN article you can add custom parameters to your template that can…
maschall
  • 988
  • 1
  • 7
  • 13
36
votes
3 answers

ReferenceError: Invalid left-hand side in assignment

my code for a rock paper scissors game (called toss) is as follows: var toss = function (one,two) { if(one = "rock" && two = "rock") { console.log("Tie! Try again!"); } // more similar conditions with `else if` }; When I enter…
Kevin
  • 383
  • 1
  • 3
  • 5
36
votes
15 answers

jQuery password strength checker

I'm quite new to jQuery, and I've written a simple function to check the strength of a password for each keypress. The idea is that every time a user enters a character, the contents is evaluated to test the strengh of the password they have…
Evernoob
  • 5,551
  • 8
  • 37
  • 49
35
votes
9 answers

Reason for "all" and "any" result on empty lists

In Python, the built-in functions all and any return True and False respectively for empty iterables. I realise that if it were the other way around, this question could still be asked. But I'd like to know why that specific behaviour was chosen.…
detly
  • 29,332
  • 18
  • 93
  • 152
34
votes
8 answers

Is it common for 0 to mean ‘true’ and 1 to mean ‘false’ in a C API?

I came across an is_equals() function in a C API at work that returned 1 for non-equal SQL tables (false) and 0 for equal ones (true). I only realized it after running test cases on my code, one for the positive example and one for the negative and…
Ben
  • 2,430
  • 3
  • 22
  • 24
34
votes
8 answers

What are the best uses of Logic Programming?

By Logic Programming I mean the a sub-paradigm of declarative programming languages. Don't confuse this question with "What problems can you solve with if-then-else?" A language like Prolog is very fascinating, and it's worth learning for the sake…
mbac32768
  • 11,453
  • 9
  • 34
  • 40
33
votes
3 answers

Universal and Existential Quantifiers of First-Order Logic

I am taking a Scala programming course. At one point the instructor said: Functions blah and bladdy are the universal and existential quantifiers of first-order logic. Could someone translate "universal and existential quantifiers of first-order…
More Than Five
  • 9,959
  • 21
  • 77
  • 127
31
votes
17 answers

How can I simplify this set of if statements? (Or, what's making it feel so awkward?)

My colleague showed me this piece of code, and we both wondered why we couldn't seem to remove duplicated code. private List parseResponse(Response response) { if (response.status().code() != Status.OK.code() ||…
Andrew Cheong
  • 29,362
  • 15
  • 90
  • 145