Wednesday, June 8, 2011
A place to avoid
The owners of this place, Don and Chris, were a nightmare to deal with. Part of our issue was that we had two young children. They insisted on "lapping" them along in their suburbans up the road"), instead of properly securing them in car seats. They refused to allow us to take our own vehicle, wasting time and effort in a parking lot simply to transfer people and cargo from our fully packed 4x4 into "theirs" - something that they had no rational explanation for (or bothered to provide). They also pointedly ignored multiple communications ahead of time as we prepared for our arrival and were hoping to have a few key questions answered. All in all, it made for a highly stressful situation and trip.
As a long time local mountain guide and former ranger, this is a gorgeous place and it's in a beautiful setting. That assertion excludes the owners however, who also are contributing to the air & noise pollution in the area by operating snowmobiles right up to (and sometimes crossing over into) the Alpine Lakes Wilderness (were I used to patrol, in fact). That makes this a place I'm wholeheartedly not recommending to anyone - avoid it like the plague.
Monday, July 20, 2009
Easy pause for console
{
public static int s_nCounter = 0;
static void Main(string[] args)
{
while (true)
{
CheckForPause();
DoMyWork();
}
}
private static void CheckForPause()
{
if (Console.KeyAvailable)
{
ConsoleKeyInfo key = Console.ReadKey(true);
Console.WriteLine();
Console.WriteLine("Program execution paused. Press any key to continue");
Console.WriteLine();
WaitForResume();
}
}
static void WaitForResume()
{
while (!Console.KeyAvailable)
{
Thread.Sleep(1000);
}
ConsoleKeyInfo key = Console.ReadKey(true);
}
static void DoMyWork()
{
Console.CursorLeft = 0;
Console.Write(s_nCounter++);
Thread.Sleep(1000);
}
}
This is the example console app that continually writes a incrementing counter to the screen. At any time you can press any key. This key is stored in the input stream. The next time CheckForPause() is called, it looks at that stream to see if there's data waiting to be read. If so, it first consumes that key (ConsoleKeyInfo key = Console.ReadKey(true)), prints a message telling you it's paused, and then goes into an infinite loop waiting for another key press to resume progress. Note that you could easily read the key variable to determine which key was pressed, and make some branching decision based on that as well.
Tuesday, July 14, 2009
Determining which ListViewItem was clicked on in a ListView when executing a ContextMenu MenuItem
So I decided to try and implement a command solution. I'm pretty pleased with how it's working now.
First, created my command:
{
public static RoutedCommand DisplayMetadata = new RoutedCommand();
}
Next in my custom listview control, I added a new command binding to the constructor:
{
CommandBindings.Add(new CommandBinding(CustomCommands.DisplayMetadata, DisplayMetadataExecuted, DisplayMetadataCanExecute));
}
And also there, added the event handlers:
{
var nbSelectedItem = (MyItem)e.Parameter;
// do stuff with selected item
}
public void DisplayMetadataCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
e.Handled = true;
}
I was already using a style selector to dynamically assign styles to the listview items, so instead of doing this in the xaml, I have to set the binding in the codebehind. You could do it in the xaml as well though:
public override Style SelectStyle(object item, DependencyObject container)
{
ItemsControl ic = ItemsControl.ItemsControlFromItemContainer(container);
MyItem selectedItem = (MyItem)item;
Style s = new Style();
var listMenuItems = new List<MenuItem>();
var mi = new MenuItem();
mi.Header= "Get Metadata";
mi.Name= "cmMetadata";
mi.Command = CustomCommands.DisplayMetadata;
mi.CommandParameter = selectedItem;
listMenuItems.Add(mi);
ContextMenu cm = new ContextMenu();
cm.ItemsSource = listMenuItems;
// Global styles
s.Setters.Add(new Setter(Control.ContextMenuProperty, cm));
// other style selection code
return s;
}
I like the feel of this solution much better than attempting to set a field on mouse click and try to access what was clicked that way.
Friday, July 10, 2009
Select Non-Distinct with Linq!
Thursday, July 9, 2009
Project Euler - Problem 51
By replacing the 1st digit of *57, it turns out that six of the possible values: 157, 257, 457, 557, 757, and 857, are all prime.
By replacing the 3rd and 4th digits of 56**3 with the same digit, this 5-digit number is the first example having seven primes, yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. Consequently 56003, being the first member of this family, is the smallest prime with this property.
Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family.
- The number of replacement digits MUST equal 3. Why? Because the sum of the digits of a prime must not be evenly divisible by 3. Replacing digits one or two at a time allows for a maximum family size of 7. Since the sum of the digits of a prime when replaced 3 at a time will always not be divisble by 3, this is the only way to avoid this issue.
- Second, generating digit masks and permutations is unnecessary. This is the biggest time saver. As long as the digit count is less than 10, we can simply go through a prime listing, and for each prime, replace digits that repeat (3 times per the above rule) with other digits and test for primality.
Tuesday, June 23, 2009
Project Euler - Problem 50
The prime 41, can be written as the sum of six consecutive primes:
41 = 2 + 3 + 5 + 7 + 11 + 13This is the longest sum of consecutive primes that adds to a prime below one-hundred.
The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.
Which prime, below one-million, can be written as the sum of the most consecutive primes?
Project Euler - Problem 48
The series, 11 + 22 + 33 + ... + 1010 = 10405071317.
Find the last ten digits of the series, 11 + 22 + 33 + ... + 10001000.