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
62
votes
13 answers

Multi-variable switch statement in C#

I would like use a switch statement which takes several variables and looks like this: switch (intVal1, strVal2, boolVal3) { case 1, "hello", false: break; case 2, "world", false: break; case 2, "hello", false: etc…
BanditoBunny
  • 3,658
  • 5
  • 32
  • 40
61
votes
10 answers

Most efficient/elegant way to clip a number?

Given a real (n), a maximum value this real can be (upper), and a minimum value this real can be (lower), how can we most efficiently clip n, such that it remains between lower and upper? Of course, using a bunch of if statements can do this, but…
Alex Z
  • 2,500
  • 2
  • 19
  • 23
61
votes
3 answers

Curry-Howard isomorphism

I've searched around the Internet, and I can't find any explanations of CHI which don't rapidly degenerate into a lecture on logic theory which is drastically over my head. (These people talk as if "intuitionistic proposition calculus" is a phrase…
MathematicalOrchid
  • 61,854
  • 19
  • 123
  • 220
60
votes
4 answers

Higher-order unification

I'm working on a higher-order theorem prover, of which unification seems to be the most difficult subproblem. If Huet's algorithm is still considered state-of-the-art, does anyone have any links to explanations of it that are written to be…
rwallace
  • 31,405
  • 40
  • 123
  • 242
58
votes
11 answers

Why does IQueryable.All() return true on an empty collection?

So I ran into a situation today where some production code was failing precisely because a method performed exactly as documented in MSDN. Shame on me for not reading the documentation. However, I'm still scratching my head as to why it behaves…
GalacticCowboy
  • 11,663
  • 2
  • 41
  • 66
52
votes
25 answers

Fastest way of finding the middle value of a triple?

Given is an array of three numeric values and I'd like to know the middle value of the three. The question is, what is the fastest way of finding the middle of the three? My approach is this kind of pattern - as there are three numbers there are six…
Gnark
  • 4,080
  • 7
  • 33
  • 44
51
votes
3 answers

Mustache - How to detect array is not empty?

I want to implement the following logic with Mustache: {{#if users.length > 0}}
    {{#users}}
  • {{.}}
  • {{/users}}
{{/if}} // eg. data = { users: ['Tom', 'Jerry'] } Should I modify the users…
Trantor Liu
  • 8,770
  • 8
  • 44
  • 64
49
votes
4 answers

Jasmine expect logic (expect A OR B)

I need to set the test to succeed if one of the two expectations is met: expect(mySpy.mostRecentCall.args[0]).toEqual(jasmine.any(Number)); expect(mySpy.mostRecentCall.args[0]).toEqual(false); I expected it to look like…
naugtur
  • 16,827
  • 5
  • 70
  • 113
48
votes
2 answers

Django edit form based on add form?

I've made a nice form, and a big complicated 'add' function for handling it. It starts like this... def add(req): if req.method == 'POST': form = ArticleForm(req.POST) if form.is_valid(): article =…
mpen
  • 272,448
  • 266
  • 850
  • 1,236
45
votes
5 answers

Median of 5 sorted arrays

I am trying to find the solution for median of 5 sorted arrays. This was an interview questions. The solution I could think of was merge the 5 arrays and then find the median [O(l+m+n+o+p)]. I know that for 2 sorted arrays of same size we can do…
codeObserver
  • 6,521
  • 16
  • 76
  • 121
45
votes
8 answers

C++ interview preparation

I have a Phone interview coming up next with with a company which works in financial software industry. The interview is mainly going to be in C++ and problem solving and logic. Please tell me the method of preparation for this interview. I have…
Light_handle
  • 3,947
  • 7
  • 29
  • 25
43
votes
11 answers

What is fuzzy logic?

I'm working with a couple of AI algorithms at school and I find people use the words Fuzzy Logic to explain any situation that they can solve with a couple of cases. When I go back to the books I just read about how instead of a state going from On…
DFectuoso
  • 4,877
  • 13
  • 39
  • 55
43
votes
2 answers

Why is '' > 0 True in Python 2?

In Python 2.x: >>> '' > 0 True Why is that?
parxier
  • 3,811
  • 5
  • 42
  • 54
43
votes
9 answers

Why are default arguments evaluated at definition time?

I had a very difficult time with understanding the root cause of a problem in an algorithm. Then, by simplifying the functions step by step I found out that evaluation of default arguments in Python doesn't behave as I expected. The code is as…
Mert Nuhoglu
  • 9,695
  • 16
  • 79
  • 117
42
votes
9 answers

Booleans have two possible values. Are there types that have three possible values?

Possible Duplicate: What's the best way to implement an 'enum' in Python? I’m writing a function that, ideally, I’d like to return one of three states: “yes”, “no”, and “don’t know”. Do any programming languages have a type that has three, and…
Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270