Going Random on an Enumerable

   I have not published any programming articles recently and I know that a throng of readers eagerly await them so I decided to share an interesting task that was brought up by a guy on IRC. You have an enumerable sequence and you are required to write a method that selects a random element from the sequence with just one iteration over the sequence. This means that you are not allowed to call methods like Count because it will iterate through the sequence. You are not allowed to save the elements in another collection. Of course an even distribution of the results is required (i.e. every element should have equal chance to be selected). When you are ready look at the full text to see my solution...
Posted by:   Stilgar
00:37 26.01.2009

C# 4.0 Features Announced

   The big news (actually the big news was Windows Azure but I care more about C# 4.0) from yesterday is that Anders (Hallowed be his name) has spoken. The future of C# have been announced. I bet that many of you mortals are eagerly awaiting my interpretation of the words of the Prophet.
Posted by:   Stilgar
04:49 29.10.2008

OOP in Math

   My coworker Lyubo came up with the following OOP problem while we were having lunch the other day. He says it is a well known problem that you can see on numerous places but it is new to me.

   We know from mathematics that the square is a special case of rectangle. In OOP terms "special case" means inheritance. Obviously the mathematical square inherits from mathematical rectangle. Your task is to create OOP model for that relationship. Lets say that we want the rectangle to expose get property (getter for you Java readers) – the area of the rectangle. This is not as simple as it sounds. Here is one of the simplest (and very wrong) solutions:

   class Rectangle
   {
       /// <summary>
       /// A side of the rectangle
       /// </summary>
       public uint A { get; set; }

       /// <summary>
       /// B side of the rectangle
       /// </summary>
       public uint B { get; set; }

       virtual public uint Area
       {
           get { return A * B; }
       }
   }

   class Square : Rectangle
   {
       public override uint Area
       {
           get
           {
               return A * A;
           }
       }
   }

However this means that the B property is not actually used. This can lead to unexpected results in polymorphic client code. We expect a rectangle to have a area of A*B and somehow it will not if it is an instance of Square. We can override the property setters, add some exceptions but what inheritance means is that the child class can do ANYTHING that the base class can. If you need to add special case behavior for any of the child classes then the inheritance hierarchy you have created is simply wrong. So go ahead make it right... (or view full text for explanation)
Posted by:   Stilgar
00:20 25.10.2008

Functional Sieve of Eratosthenes

   I was thinking of how I can use functional constructs to implement Sieve of Eratosthenes and I bet that millions of readers are dying to learn what I came up with...
Posted by:   Stilgar
01:07 13.10.2008

Приложна математика

   Мислите, че разбирате от математика? Мислите, че разбирате от програмиране? Следната математическа задача възникна от съвсем реална ситуация.

В пицария Годзила са увеличили диаметъра на пицата от 27 на 30cm.

   a) с колко процента се увеличи площа на пицата?
   b) Ако от старата (27cm) пица се отреже парче отговарящо на централен ъгъл X то в центъра на парчето ще има два пъти повече сирене отколкото на ръба му. Ако промяната в дисперсията на сиренето е постоянна, колко процента по-малко сирене има по ръба на новата пица отколкото в центъра и.
   c) да се изчисли с колко процента се е увеличило сиренето в пицата при данните от подусловие b).
   d) да се състави програма, която при зададени входни данни стар и нов диаметър на пицата да изчислява резултати за горните подусловия.
   e) да се направи горната програма разширяема като се позволи написване на plugin съдържащ функция за изменение на дисперсията приемаща аргумент разстояние от центъра и връщаща резултат коефициент (сирене в центъра)/(сирене на съответния сантиметър).

Много обичам пиците от Годзила.
Posted by:   Stilgar
17:16 18.09.2008
First Previous ... 11 12 13 14 15 Next Last