Sunday, November 29, 2009

Introduction to .Net Framework

In his latest blog post about five rules for writing good code, Yan Pritzker states Rule 5 as Really learn the language and the framework. He says “No need to be a walking encyclopedia, but remember to occasionally open up that encyclopedia and read through it so that you know at least what’s out there.” So, I decided to revisit some .NET fundamentals  and jot down my notes here.

The following is a short introduction to .Net framework and its components.

The .Net architecture consists of the Common Language Runtime (CLR), .Net Framework Class library, and language support components.

.Net Framework

The Common Language Runtime (a.k.a. CLR) simplifies provides a robust and secure execution environment for .NET applications. It is also referred to as a managed environment, in which common services, such as garbage collection and security, are automatically provided.

Common Language Runtime Components

A short description of some of the components is below:

Component Description
Class Loader Manages metadata, loading and layout of classes
MSIL to Native Compiler JIT compiler for converting MSIL to Native code
Code Manager Manages code execution
Garbage collection --
Security Engine Provides security based on user identity and code origin
Debugger --
Type Checker --
Exception Manager Provides structured Exception handling, integrates with Windows Structured Error Handling (SEH)
Thread Support --
COM marshaler --
.NET Class Library support Integrates code with the runtime that supports the .NET Framework class library

The .NET Framework class library exposes features of the runtime and provides other high-level services.

ADO.NET, the next generation of ADO technology provides improved support for disconnected programming. It also provides rich XML support in the System.Xml namespace.

ADO.NETASP.NET is a programming framework built on the common language runtime that can be used on a server to build powerful Web applications

ASP.NET

References:

1.  http://skwpspace.com/2009/09/29/five-rules-for-writing-good-code/

2. http://msdn.microsoft.com/en-us/netframework/default.aspx

3. http://msdn.microsoft.com/en-us/library/aa139615.aspx

Tuesday, September 29, 2009

Why every other playoff system sucks

Many times we’ve seen a underdog team defeating a top-ranked team in the 1st round of a major tournament. Most people rejoice (except the fans of the losing team) since everybody loves a fairy tale  victory, The organizer’s are happy since it creates a lot of buzz and everybody’s happy that the sports is still not dead since anything can happen.

Let’s take a deeper look. Team A wins 63 games out of 80 games in the regular season plays Team B which won around 40 games. Team A loses the match and bows out of the tournament. Everybody is surprised for a moment, Team B probably loses in the next round since they again play a better team and are already happy with the results they have in hand [there was a reason it only won 40 games out of 80].

The above is a common format used across many competitions in the world (NBA, Cricket Tournaments, NCAA Tournament, Champions league). But, I think such upsets does more harm to the game than benefits it. Since the team who was definitely one of the better teams in the tournament is out due to having a bad day or couple of major players being out/suspended for the game.

ncaa_mens

Some organizers e.g. the NBA, try to avoid the above scenario by either keeping N (=3,5,7) no. of matches or sometimes  giving the top ranked team a home advantage or sometimes both. Playing multiple matches definitely lowers the odds of the better team losing, however, this usually leads to some boring matches in the initial part of the tournament.

Nba_format Here comes the format used by the Australian Football league, which looks like this:

800px-AFL_finals_v2 Clearly, this system has numerous advantages than the knockout system usually followed. To quote Wikipedia “Under this finals system, the final eight teams are broken up into four groups of two. Each group of two earns one extra benefit over the teams beneath it. These benefits are home ground finals and the double-chance, whereby a first-week loss will not eliminate the team from the finals.”

I think atleast some tournaments should explore this format like the NCAA basketball tournament and the Cricket world cup which have the worst structure since there is only a single match between the teams and there’s no clear home-court advantage.

References:

http://en.wikipedia.org/wiki/AFL_finals_system

Saturday, September 12, 2009

The Decorator Pattern

Decorator pattern is a kind of structural design pattern which provides a way to attach new state and behavior to an object dynamically. The original object is left untouched which makes the pattern useful for changing systems. Decorators both inherit the original class and contain an instant of it.
The Decorator pattern is generally used when a object is needed with some extra functionality than an existing class but the existing class is not available for subclassing or one doesn't want to subclass for some reason (e.g. to avoid class explosion).
The following example illustrates the pattern:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DPPractice
{
    class DecoratorPattern
    {
        interface IWindow
        {
            string getDescription();
            void draw();
        }
        public class SimpleWindow : IWindow
        {
            public void draw()
            {
                //draw Simple Window
            }

            public string getDescription()
            {
                return "simple window";
            }

        }
        class VerticalScrollBarDecorator : IWindow
        {

            private IWindow decoratedWindow;

            public VerticalScrollBarDecorator(IWindow decoratedWindow)
            {
                this.decoratedWindow = decoratedWindow;
            }


            public void draw()
            {
                drawVerticalScrollBar();
                decoratedWindow.draw();
            }

            private void drawVerticalScrollBar()
            {
                // draw the vertical scrollbar
            }

            public String getDescription()
            {
                return decoratedWindow.getDescription() + ", including vertical scrollbars";
            }
        }

        static void Main(String[] args)
        {
            // create a decorated Window with horizontal and vertical scrollbars
            IWindow window = new VerticalScrollBarDecorator(new SimpleWindow());

            // print the Window's description
            Console.WriteLine(window.getDescription());
        }


    }
}





As can be seen from the example the Decorator pattern is heavily utilized in displaying windows etc. adding and excluding scroll bars easily while rendering.

Other uses of the Decorator Pattern:

1. Decorators are used in I/O API's of C#.
System.IO.Stream is decorated by
System.IO.BufferedStream
System.IO.FileStream Etc.

2. As illustrated by the example the Decorator pattern is heavily utilized in rendering windows/display objects

References:
http://en.wikipedia.org/wiki/Decorator_pattern
Design Patterns: Elements of Reusable Object-Oriented Software
C# 3.0 Design Patterns

Monday, September 7, 2009

Time Management

This weekend I took some time out to watch the Time Management talk by Randy Pausch.Randy Pausch was an Computer Science professor at CMU. He died of cancer in July, 2008. He was a great speaker and some of his lectures has become very famous in the last few years. The most famous one being “The Last Lecture: Really Achieving Your Childhood Dreams" which is one of the most inspiring talks I have ever heard.

The talk on Time management, however, is a more pragmatic talk and talks about techniques to manage time better. Again, the underlying principle in Randy’s talk is to maximize life and fun.

Here is the talk followed with my notes. The slides are available here.

1. Pragmatic Talk
    a. Time is only commodity that matters
2. Agenda:
    a. Set goals
    b. Avoid wasting time
    c. Deal w/ boss
    d. How to delegate to others
    e. Tools
    f. Stress and Procrastination
3. Slides on website, Red stars are important
4. Americans bad at dealing with time
    a. Waste time
    b. Time and money are equitable
    c. How much is your time worth/hr
    d. Costs company twice your salary
    e. 50000/yr --> 100k cost to org
    f. Decisions: Outsource vs. DIY
    g. Manage time as money
        i. Money is not well managed
        ii. Though better
    h. Money can be earned later, Time doesn’t comes back
    i. Boss == Academic Advisor/Parents
5. One good thief [11]
    a. Talk is composed mainly from nuggets from the 2 books
        i. Time Management for Teachers
        ii. Career Track Seminar: Taking control of Your Work Day
    b. Time Famine:
        i. Problem is systematic
        ii. Long term solution required
    c. This is Life Advice
6. Overall goal is Fun
        a. Maximize time is a means so that we maximize fun
    7. Typical Office worker wastes 2 hours/day[13]
        a. Messy desk etc.
    8. Manage time well to be successful
        a. Meta skills
        b. If you want to run with ppl faster than you, optimize the skills you have
    9. Planning [16]
        a. Great if you can cross stuff from your to-do list
        b. More important to do the right things adequately than Doing (wrong) things right
        c. 80/20 rule: Very small things on ur list is gonna matter most
        d. Experience comes with time and no shortcuts.
        e. When things don’t go well, you're learning a lot and it'll be better soon.
    10. Inspiration
        a. If you can dream it, you can do it.
        b. If you refuse to dream it, you won't do it.
        c. Disneyland: 366 days
            i. Used everyone of them
    11. Planning
        a. Daily/Weekly/Semester
        b. You can always change the plan
    12. To do lists
        a. Break stuff into small steps
        b. Boss: Grow your people
        c. Eat that frog. Start with the big one first
        d. *Covey's four quadrants
            i. Go to Quad 2 after Q1 rather than Q3
        e. *Paperwork
            i. No clutter
            ii. Touch each piece of paper/email only once
                1) Inbox is not to-do list
        f. Filing system
            i. All paper goes there in alpha order
            ii. Everything should have a place
    13. Desk [ime - 28:06]
        a. Atleast 2 monitors
            i. Left:
                1) To do list
            ii. Mid:
                1) Inbox
            iii. Right:
                1) Calendar
        b. *Speaker Phone
            i. Great for waiting
            ii. Timer: Great for making people guilty on other side     
            iii. Telephone Techniques
                1) Group your calls
                2) Keep calls short
                3) Use a headset
        c. *Thank you cards
                1) Very important
                2) Tangible way to tell someone how much you appreciated things
        d. Recycle Bin:
            i. Can dig stuff out
            ii. Post-its/notepad
            iii. Find a system for yourself
        e. Others
            i. Address Stamper
            ii. Kleenex
    14. Scheduling
        a. You don’t find time for important things, you make it
                1) By not wasting time on other things
                2) Learn to say no
                    a) “I’ll do it if nobody else steps forward”
                    b) “I’ll be your deep fall back," but you have to keep searching.
        b. Find your Most productive times
        c. Find your dead times
        d. *Interruptions
            i. 6-9 minutes, 4-5 minute recovery
            ii. reduce frequency and length of interruptions
            iii. No Email "ding"
        e. Cut things short
            i. Stand up, stroll to the door, complement, thank, shake hands
        f. Time journal
            i. Record your time
            ii. Update not at EOD
        g. Make up fake meeting b/w near by meetings
            i. Go to lib.
    15. Work-life balance
            i. Work fewer hours/get more done
    16. Focus on things that are important
            i. Smoking women concerned about noise of jackhammers on her unborn child
            ii. Effective vs. Efficient
    17. Procrastination
        a.   Quotes
            i. If I wait long enough it will go away
            ii. Parkinson's mail
        b. Make fake deadlines
        c. Get into a comfort zone
            i. Identify why you aren’t enthusiastic
            ii. Don't be afraid to ask people
    18. Delegation
        a. Authority with responsibility
            i. Empower ppl to get it done
            ii. Always do the ugliest/dirtiest job yourself
        b. Can't be vague
            i. Specific thing
            ii. Specific time
            iii. Specific reward/penalty for them
        c. Challenge people
            i. Delegate until people complain
            ii. Email: Get it in writing
            iii. Give objectives not procedures
            iv. Relative importance of tasks
        d. Never too early to delegate
        e. Reinforce behavior you want repeated
    19. Meetings
        a. No black-berries
        b. Agenda
        c. 1 minute minutes: an efficient way to keep track of decisions made in a meeting: who is responsible for what by when?
    20. Technology
        a. Computer's are faster but they take longer
        b. End to end
        c. It must make your life better
        d. New way of doing things
    21. Email Tips
        a. Save all of it/Searchable
        b. If you want something done, only one recipient.
        c. If you really want something done, CC their boss.
        d. Follow up after 48 hours
    22. Communication with Boss
        a. Write things down
        b. When's next meeting
        c. What's the goal
        d. Who to ask for help
        e. They want Results!
    23. General Advice
        a. No vacation if you're connected to work
        b. Kill TV
        c. Convert Money to Time esp if you have children
        d. Eat, sleep and exercise
        e. Never break a promise, but re-negotiate them if need be
        f. If you haven’t got time to do it right, you don’t have time to do it wrong
        g. Recognize that most things are pass/fail.
        h. Feedback loops: ask in confidence
    24. Reading
        a. One minute Manager
        b. 7 Habits
    25. Do it now
        a. Get a day-timer/PDA
        b. TODO list in priority order/Covey 4 quads
        c. Time Journal/Count hours of TV
        d. Revisit the talk in 30 days and ask "What have I changed?"
    26. "Time is the only thing you have and one day you may find that you have less than you imagined".

Since the formatting above is not great, Here are the notes in a one note format:

http://cid-c10db90c8a3e3ef0.skydrive.live.com/self.aspx/.Public/Time%20Management.one

References:

http://en.wikipedia.org/wiki/Randy_Pausch
http://www.cs.virginia.edu/~robins/Randy/RandyPauschTimeManagement2007.pdf
http://www.cs.virginia.edu/~robins/Randy/

Wednesday, August 26, 2009

Josephus Problem

The Josephus problem  is a problem often discussed and seen in CS literature. I got introduced to the problem when I initially started programming and knowing nothing about linked lists and/or recursion had a really hard time solving it.

The problem is stated as:
There are people standing in a circle waiting to be executed. After the first man is executed, certain number of people are skipped and one man is executed. Then again, people are skipped and a man is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last man remains, who is given freedom.

The task is to choose the place in the initial circle so that you survive (are the last one remaining).

The easiest and most logical way to solve the problem is to simply simulate it using a circular link list.

N is no. of people, M as the count.

struct node {  
int item;
node* next;
};


int simple_simulation(int N, int M)
{
int i;
node *t=new node();
node *x=t;
t->item = 1; t->next = t;

//Construct the list
for (i = 2; i <= N; i++)
{
x = (x->next = new node ());
x->item = i;
x->next = t;
}

//Find the survivor
while (x != x->next)
{
for (i = 1; i < M; i++)
x = x->next;
x->next = x->next->next; N--;
}
return x->item;
}


A better way to solve the problem is through recursion. Its easy to find who is killed first, (Hint: it’s the Mth guy from the start) after that we are left with N-1 total people and the count starts from the new guy who was at M+1th position earlier. Going on like this gives us a simple recursive function:



int formula (int N, int M) 
{
if (N == 1)
return 0;
return (formula (N-1, M) + M) % N;
}


References:

http://en.wikipedia.org/wiki/Josephus_problem

Wednesday, August 12, 2009

Climb the Pyramid

Cal Newport describes the pyramid strategy to evaluate one’s progress.

http://calnewport.com/blog/2009/06/03/the-pyramid-method-a-simple-strategy-for-becoming-exceptionally-good/

From the blog:

[quote]

I call this general technique the Pyramid Method. I claim that it’s a powerful approach for anyone looking to transform an interest or natural talent into an expertise that cannot be ignored. Regardless of the pursuit in question, if you want to take it someplace serious, follow Chris’s example. This means:

  1. Pick a single relevant venue to join at the entry level and work to increase your standing.
  2. Make sure the venue offers clear metrics on your progress; use these metrics to guide your efforts to get better.
  3. Forget all the other bullshit advice and mini-strategies people offer for getting ahead in your pursuit. If you can’t master this one venue, then you don’t yet deserve the world’s respect.
  4. Put your head down, and get it done.

[/quote]

The strategy can work for many skills music, sports and coding. But choosing the proper venue is critical. IMO, apart from the above properties mentioned by Chris, The venue should have some other properties.  I would talk about them from a coder’s perspective.

1. Have a low entry barrier: So that you can start early from a entry-level. So, when you only know the syntax and basic constructs of a high level language (C++, Java, C#)

2. A high competition/quality: So, you don’t “win” the venue too soon. So, you are actively engaged when you can solve non-trivial problems in your arena also. This also ensures that good coders will keep coming to your venue rather than taking off someplace else.

3. Can cater to more than 1 skill: I find this important to be a successful programmer you have to do more than code! This can involve making system architecture, understanding existing codebases, soft skills like communication.

One of the venues I know is Topcoder.com as it offers all what Chris has outlined in his article and what I have outlined here in this post.

Now, I just have to go and follow the all important Step 4 from Chris’s post.

Monday, May 18, 2009

Largest Element in the Stack

Problem: Given a stack you have to insert a stream of numbers in it and you have to keep track of the highest element at any point in the stack

In general, the solution will require traversing through the entire stack hence taking O(n) time. However, given the option of using another data-structure this can be done using minimal extra memory using another "Stack" !.

The basic idea is when pushing an element in the original, check if the the "highest_elem_stack" is empty or if its top element is less than or equal to the element being pushed, if yes push this element on both stacks.

While popping check if the popped element is equal to the top element of the "highest_elem_stack". If yes pop both stacks.

The highest element in the original stack would always be equal to the top element in the "highest_elem_stack"
Related Posts with Thumbnails