language-agnostic

Preferred implementation of '<' for multi-variable structures

痞子三分冷 提交于 2019-12-19 18:12:06
问题 Initially this may seem overly abstract or philosophical, but I am genuinely interested to see if someone has a convincing argument in favor of one implementation over the other. Given operator< for std::pair<T1, T2> , which would be the better implementation: return x.first < y.first || x.first == y.first && x.second < y.second; or: return x.first < y.first || !(y.first < x.first) && x.second < y.second; My understanding is that the two implementations yield equivalent results. Is the latter

Preferred implementation of '<' for multi-variable structures

蹲街弑〆低调 提交于 2019-12-19 18:11:12
问题 Initially this may seem overly abstract or philosophical, but I am genuinely interested to see if someone has a convincing argument in favor of one implementation over the other. Given operator< for std::pair<T1, T2> , which would be the better implementation: return x.first < y.first || x.first == y.first && x.second < y.second; or: return x.first < y.first || !(y.first < x.first) && x.second < y.second; My understanding is that the two implementations yield equivalent results. Is the latter

How To Mutex Across a Network?

六月ゝ 毕业季﹏ 提交于 2019-12-19 17:15:33
问题 I have a desktop application that runs on a network and every instance connects to the same database. So, in this situation, how can I implement a mutex that works across all running instances that are connected to the same database? In other words, I don't wan't that two+ instances to run the same function at the same time. If one is already running the function, the other instances shouldn't have access to it. PS: Database transaction won't solve, because the function I wan't to mutex doesn

Word Jumble Algorithm

妖精的绣舞 提交于 2019-12-19 16:22:22
问题 Given a word jumble (i.e. ofbaor), what would be an approach to unscramble the letters to create a real word (i.e. foobar)? I could see this having a couple of approaches, and I think I know how I'd do it in .NET, but I curious to see what some other solutions look like (always happy to see if my solution is optimal or not). This isn't homework or anything like that, I just saw a word jumble in the local comics section of the paper (yes, good ol' fashioned newsprint), and the engineer in me

Word Jumble Algorithm

南楼画角 提交于 2019-12-19 16:19:12
问题 Given a word jumble (i.e. ofbaor), what would be an approach to unscramble the letters to create a real word (i.e. foobar)? I could see this having a couple of approaches, and I think I know how I'd do it in .NET, but I curious to see what some other solutions look like (always happy to see if my solution is optimal or not). This isn't homework or anything like that, I just saw a word jumble in the local comics section of the paper (yes, good ol' fashioned newsprint), and the engineer in me

How to create IN OUT or OUT parameters in Java

∥☆過路亽.° 提交于 2019-12-19 15:25:16
问题 In PL/SQL (or many other languages), I can have IN OUT or OUT parameters, which are returned from a procedure. How can I achieve a similar thing in Java? I know this trick: public void method(String in, String[] inOut, String[] inOut2) { inOut[0] = in; } Where the in parameter represents an IN parameter and the inOut parameter can hold a return value. The convention would be that String[] inOut is an array of inOut.length == 1 . That's kind of clumsy. EDIT Feedback to answers : Other tricks

How do you retrofit unit tests into a code base?

試著忘記壹切 提交于 2019-12-19 14:01:58
问题 Do you have any strategies for retrofitting unit tests onto a code base that currently has no unit tests ? 回答1: Read Working Effectively With Legacy Code by Feathers. Jimmy Bogard has a good blog series on SOC. 回答2: The best way to retrofit an existing project without any unit tests is to do it when fixing bugs. Write a test that fails on the logic that has the bug in it with the steps to reproduce the bug. Then refactor the code until the tests pass. Now you can have confidence that the bug

Searching in an array for numbers with each element +1 or -1 of preceding element [closed]

本秂侑毒 提交于 2019-12-19 13:38:07
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . An array of integers contains elements such that each element is 1 more or less than its preceding element. Now we are given a number, we need to determine the index of that number's first occurrence in the array. Need to optimize linear search. Its not homework. 回答1: My algorithm

What's the quickest way to find all possible pairs in list?

自作多情 提交于 2019-12-19 09:44:35
问题 Basically I have list of players, and I want to pair them up so that each player will play everyone once. What's the quickest way to find this data? 回答1: assuming that players do not appear in the list twice, a double for loop is very quick: for (int i=0, i <= playerList.Count - 2, i++) for (int j=i+1, j <= playerList.Count - 1, j++) //add a new pairing of player i and j 回答2: Such tournament schedule is often called round-robin. In wikipedia, there's also an example of possible scheduling

Theory, examples of reversible parsers?

一笑奈何 提交于 2019-12-19 08:56:47
问题 Does anyone out there know about examples and the theory behind parsers that will take (maybe) an abstract syntax tree and produce code, instead of vice-versa. Mathematically, at least intuitively, I believe the function of code->AST is reversible, but I'm trying to find work/examples of this... besides the usual resources like the Dragon book and such. Any ideas? 回答1: Such thing is called a Visitor. Is traverses the tree and does whatever has to be done, for example optimize or generate code