For those looking to get more with less keystrokes, zencoding plugin is a must. You can write lightening fast HTML using its powerful abbreviation engine.
From its project site -
Zen Coding is an editor plugin for high-speed HTML, XML, XSL (or any other structured code format) coding and editing. The core of this plugin is a powerful abbreviation engine which allows you to expand expressions—similar to CSS selectors—into HTML code.To appreciate its power you need to play with it. Its author Sergey Chikuyonok has very good article on smashing magazine - a speedy way of writing HTML code. Watch the 6 minute video and you will be a convert.
Here is an example of its power. The following expression
html:4t>div#header+div#main.contentexpands to the following snippet -
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title></title> </head> <body> <div id="header"></div><div id="main" class="content"></div> </body> </html>
Or the following example
ul>li#item-$*6which expands to
<ul> <li id="item-1"></li> <li id="item-2"></li> <li id="item-3"></li> <li id="item-4"></li> <li id="item-5"></li> <li id="item-6"></li> </ul>
Here is a link to the zencoding cheatsheet. For Resharper fans it is available as ZenCoding Powertoy and there is also a Visual Studio PlugIn
Solution
Two dimensional array is a good data structure to use for it. My first solution involved using three loops but on refinement I was able to use only one loop for the validation. I find the start of block location first. In the loop checking row and column is quite straight forward. It is checking for value in a block that is a bit tricky and I had to play with LinqPad and Excel before I came up with correct statements.
public static bool Validate(int number, int row, int col) { Console.WriteLine("Writing at pos {0},{1} - value {2}", row - 1, col - 1, number); var startBlockX = ((row - 1) / 3) * 3; var startBlockY = ((col - 1) / 3) * 3; for (int i = 0; i < 9; i++) { if (arr[i, col - 1] == number) { Console.WriteLine("found in col value {0}", arr[i, col - 1]); return false; } if (arr[row - 1, i] == number) { Console.WriteLine("found in row value {0}", arr[row - 1, i]); return false; } var mx = (i / 3) + startBlockX; var my = startBlockY + (i % 3); Console.WriteLine("block pos {0},{1}", mx, my); if (arr[mx, my] == number) { Console.WriteLine("found in block"); return false; } } return true; }
Checking
Using the following array and checking for highlighted spots in figure above for value of 9.
var arr = new int[9, 9]; arr[3, 6] = 9; arr[3, 8] = 7; arr[0, 8] = 9; arr[1, 2] = 2; arr[7, 7] = 9;Validate(9, 3, 3);
I just attended SSW Sydney .Net User group meet yesterday for the first time and thought would share a summary. The session was attended by a good crowd of about 50 people. Adam Cogan started the meeting with some industry news. The following wsj article "What They Know" was interesting and informative in showing how much of user tracking is being done even by some of the well respected brands.
The first talk was by Peter Gfader. It was good to see him explain object oriented principles and explain about SOLID which I have blogged about earlier. I had knowledge of Broken Window Theory but Boy Scout Rule was new to me and it was interesting. Peter also showed some examples of bad code and we could relate to it in our works. However because of time constraints he had to rush through showing tools like StyleCop, Code Analysis etc. which was a bit unfortunate. His blog post and presentation slides are available here.
The second talk was by TJ Gokcen and he created an apple iphone application using Monotouch. It was great to see a whole new application being developed using language we know pretty well. But I wonder how complicated native applications we can develop with that. I guess mostly sort of web applications which uses web browser but not heavy apps like games etc. The demonstration itself was very informative and I feel comfortable if I have to develop applications like that in iPhone.
Lastly, it was nice talking to people. I couldn't interact much because of my neck pain but it was good to see and listen to different developers. One gentleman I was talking to mentioned the difference between knowledge of a domain expert and coder and how he would rather teach a domain expert some coding than a coder some domain knowledge. I kind of disagree but that is for some other post. I will update this post with link to TJ's slides and code when it is available.
Takeaways
public class Person {} public class Customer : Person {}Covariance is about values being returned from an operation back to the caller. In the following example, the keyword out is used with generic parameter to support covariance. Contravariance is about values being passed in using keyword in.
delegate T MyFunc<out T>(); MyFunc<Customer> customer = () => new Customer(); // Converts using covariance - narrower type to wider type assignment MyFunc<Person> person = customer;
delegate void MyAction<in T>(T t); MyAction<Person> printPerson = (person)=> Console.WriteLine(person); // converts using contravariance - assignment from a wider type to a narrower type MyAction<Customer> printCustomer = printPerson;
class CSharpCode def BlankLine print "\n" end def Comment comment print "// #{comment}\n" end def StartMsg name print "public struct #{name} {\n" end def EndMsg print "}\n" end def SimpleType name, type print "\t#{type} #{name};\n" end def ComplexType name, type, size if(type == "char[") type = "string" print "\t#{type} #{name};\n" end end end if __FILE__ == $0 unless ARGV[0] print "cml usage: cml Input.txt\n" exit end if(File.exist?(ARGV[0])) CG = CSharpCode.new File.open(ARGV[0]).each_line { |line| line.chomp!; if(line =~ /^\s*S/) CG.BlankLine elsif line =~ /^\#(.*)/ CG.Comment $1 elsif line =~ /^M\s*(.+)/ CG.StartMsg $1 elsif line =~ /^E/ CG.EndMsg elsif line =~ /F\s*(\w+)\s*(\w+\[)(\d+)\]/ CG.ComplexType $1, $2, $3 elsif line =~ /^F\s+(\w+)\s*(\w+)/ CG.SimpleType $1, $2 else print "Invalid line" end } end end
# Add a product # to the 'on-order' list M AddProduct F id int F name char[30] F order_code int EOutput
// Add a product // to the 'on-order' list public struct AddProduct { int id; string name; int order_code; }
static int CountWhitespace(string text)
{
Contract.Requires(text != null, "text");
Contract.Ensures(Contract.Result() >= 0);
return text.Count(char.IsWhiteSpace);
}
Every Shakespeare play operates on three levels. The first level is embodied in a jester, who entertains the onlookers (mostly children) who are ill-equipped to understand what's going on in the plot. The second level is the plot, which entertains the onlookers who are capable of longer spurts of self-guided attention. And the third level is metaphorical, which entertains the onlookers who are capable of extracting a bottomless stream of interesting, non-explicit symbolic themes from the play's unraveling. Now, here's the kicker of it all: each level entices the onlooker to "upgrade" to the next level.Microsoft is interested in level one. And it wants its developers to be at level one. While researching for this article found this post by Ayende
John Lam, the guy writing IronRuby, cannot look at the Ruby source code. That is the way Microsoft works. This is setting yourself up for failure, hard.The post is more than 2 years old and it seems some things never change in Redmond (like Steve Ballmer). On the whole, it seems community wants it to be part of CodePlex Foundation. JB Evain has some of his thoughts on IronRuby. But the sad reality is, as one of comments say,
If Microsoft doesn’t want the Iron* to have a success for .NET it just won’t happen.I am curious as to what will be Microsoft's response. Perhaps it is time to leave Microsoft's matrix. Perhaps Google's App Engine is calling. If only they had C# and Visual Studio Integration!
Programming is a craft.
At its simplest, it comes down to getting a computer to do what you want it to do (or what your user wants it to do).
As a programmer, you are part listener, part advisor, part interpreter, and part dictator. You try to capture elusive requirements and find a way of expressing them so that a mere machine can do them justice.
You try to document your work so that others can understand it, and you try to engineer your work so that others can build on it. What's more, you try to do all this against the relentless ticking of the project clock.
You work small miracles every day.
It's a difficult job.