language-agnostic

How do I distinguish between 'binary' and 'text' files?

孤人 提交于 2019-12-28 04:52:26
问题 Informally, most of us understand that there are 'binary' files (object files, images, movies, executables, proprietary document formats, etc) and 'text' files (source code, XML files, HTML files, email, etc). In general, you need to know the contents of a file to be able to do anything useful with it, and form that point of view if the encoding is 'binary' or 'text', it doesn't really matter. And of course files just store bytes of data so they are all 'binary' and 'text' doesn't mean

Memory efficient power set algorithm

假如想象 提交于 2019-12-28 04:15:26
问题 Trying to calculate all the subsets (power set) of the 9-letter string 'ABCDEFGHI'. Using standard recursive methods, my machine hits out of memory (1GB) error before completing. I have no more physical memory. How can this be done better? Language is no issue and results sent to the standard output is fine as well - it does not need to be held all in memory before outputting. 回答1: There is a trivial bijective mapping from the power set of X = {A,B,C,D,E,F,G,H,I} to the set of numbers between

XOR of three values

▼魔方 西西 提交于 2019-12-28 03:45:09
问题 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 && c)) || ((b ^ a) && (b ^ c) && !(a && c)) || ((c ^ a) && (c ^ b) && !(a && b)) Is there something simpler to do the same thing? Here's the proof that the above accomplishes the task: a = true; b = true; c = true ((a ^ b) && (a ^ c) && !(b && c))

The type system in Scala is Turing complete. Proof? Example? Benefits?

梦想的初衷 提交于 2019-12-28 02:38:29
问题 There are claims that Scala's type system is Turing complete. My questions are: Is there a formal proof for this? How would a simple computation look like in the Scala type system? Is this of any benefit to Scala - the language? Is this making Scala more "powerful" in some way compared languages without a Turing complete type system? I guess this applies to languages and type systems in general. 回答1: There is a blog post somewhere with a type-level implementation of the SKI combinator

How are mutexes implemented?

倖福魔咒の 提交于 2019-12-28 02:26:10
问题 Are some implementations better than others for specific applications? Is there anything to earn by rolling out your own? 回答1: Check out the description of the Test-and-set machine instruction on Wikipedia, which alludes to how atomic operations are achieved at the machine level. I can imagine most language-level mutex implementations rely on machine-level support such as Test-and-set. 回答2: Building on Adamski's test-and-set suggestion, you should also look at the concept of "fast user-space

How do you mock a Sealed class?

痴心易碎 提交于 2019-12-28 01:51:27
问题 Mocking sealed classes can be quite a pain. I currently favor an Adapter pattern to handle this, but something about just keeps feels weird. So, What is the best way you mock sealed classes? Java answers are more than welcome . In fact, I would anticipate that the Java community has been dealing with this longer and has a great deal to offer. But here are some of the .NET opinions: Why Duck Typing Matters for C# Develoepers Creating wrappers for sealed and other types for mocking Unit tests

How to detect and debug multi-threading problems?

久未见 提交于 2019-12-27 16:27:27
问题 This is a follow up to this question, where I didn't get any input on this point. Here is the brief question: Is it possible to detect and debug problems coming from multi-threaded code? Often we have to tell our customers: "We can't reproduce the problem here, so we can't fix it. Please tell us the steps to reproduce the problem, then we'll fix it." It's a somehow nasty answer if I know that it is a multi-threading problem, but mostly I don't. How do I get to know that a problem is a multi

How to detect and debug multi-threading problems?

为君一笑 提交于 2019-12-27 16:26:32
问题 This is a follow up to this question, where I didn't get any input on this point. Here is the brief question: Is it possible to detect and debug problems coming from multi-threaded code? Often we have to tell our customers: "We can't reproduce the problem here, so we can't fix it. Please tell us the steps to reproduce the problem, then we'll fix it." It's a somehow nasty answer if I know that it is a multi-threading problem, but mostly I don't. How do I get to know that a problem is a multi

How to detect and debug multi-threading problems?

不羁的心 提交于 2019-12-27 16:26:05
问题 This is a follow up to this question, where I didn't get any input on this point. Here is the brief question: Is it possible to detect and debug problems coming from multi-threaded code? Often we have to tell our customers: "We can't reproduce the problem here, so we can't fix it. Please tell us the steps to reproduce the problem, then we'll fix it." It's a somehow nasty answer if I know that it is a multi-threading problem, but mostly I don't. How do I get to know that a problem is a multi

How to pair socks from a pile efficiently?

北慕城南 提交于 2019-12-27 16:06:31
问题 Yesterday I was pairing the socks from the clean laundry and figured out the way I was doing it is not very efficient. I was doing a naive search — picking one sock and "iterating" the pile in order to find its pair. This requires iterating over n/2 * n/4 = n 2 /8 socks on average. As a computer scientist I was thinking what I could do? Sorting (according to size/color/...) of course came to mind to achieve an O(NlogN) solution. Hashing or other not-in-place solutions are not an option,