Subset sum problem

Here is my algorithm. It runs in O(2^(n/2)) and solves SubsetSum(1000, list-of-1000-ones) in 20 milliseconds. See the comments at the end of IVlad’s post. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; namespace SubsetSum { class Program { static void Main(string[] args) { var ns = new List<int>(); for (int i = 0; … Read more

Changing App.config at Runtime

I understand this is quite an old thread, but I could not get the listed methods to work. Here is a simpler version of the UpdateAppSettings method (using .NET 4.0): private void UpdateAppSettings(string theKey, string theValue) { Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); if (ConfigurationManager.AppSettings.AllKeys.Contains(theKey)) { configuration.AppSettings.Settings[theKey].Value = theValue; } configuration.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection(“appSettings”); } Pretty readable and avoids … Read more

CommandParameters in ContextMenu in WPF

The problem is that the ContextMenu is at the root of its own visual tree, so any RelativeSource.FindAncestor bindings won’t go past the ContextMenu. One solution is to use the PlacementTarget property to set up a two-stage binding from your Label: <Label Tag=”{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={ x:Type TreeView}}}”> <Label.ContextMenu> <ContextMenu> <MenuItem Header=”Delete” Command=”{x:Static local:Commands.DeleteCommand}” CommandParameter=”{Binding … Read more

Serializing anonymous delegates in C#

Did you see this post that I wrote as a followup to the CountingDemo: http://dotnet.agilekiwi.com/blog/2007/12/update-on-persistent-iterators.html ? Unfortunately, Microsoft have confirmed that they probably will change the compiler details (one day), in a way that is likely to cause problems. (e.g. f/when you update to the new compiler, you won’t be able to deserialise the stuff … Read more

What is a good way to shutdown Threads blocked on NamedPipeServer#WaitForConnection?

This is cheesy, but it is the only method I have gotten to work. Create a ‘fake’ client and connect to your named pipe to move past the WaitForConnection. Works every time. Also, even Thread.Abort() did not fix this issue for me. _pipeserver.Dispose(); _pipeserver = null; using (NamedPipeClientStream npcs = new NamedPipeClientStream(“pipename”)) { npcs.Connect(100); }

Dapper and SQL Injections

How does Dapper help protect against SQL injections? It makes it really, really easy to do fully parameterized data access, without ever needing to either concatenate input. In particular, because you don’t need to jump through lots of “add parameter, set the parameter type, check for null because ADO.NET has sucky null-handling, rinse/repeat for 20 … Read more