Currently showing entries with the tag: math

page 1 of 1
1 

10 Things This Skeptic Loves About Python's Syntax

August 15, 2007 • 9:38AM • permalink
First, a note...

Anyone that has worked with me in the past will know what a skeptic I am about new programming languages. I use a lot of different languages (new and old) and operating systems (mostly to fit the constraints of my client's environments) but I prefer to work in C++/C# and in Microsoft Visual Studio. I am always reading one tech book or another, so as a change of pace I started doing some random reading about Python.

While I still prefer C++/C#, the seamless integration of Python with C is very appealing. There is no question that Python is a RAD programming language, especially when looking over the syntax. I wanted to mention ten of my favorites, although there are many more than this - and I've just started learning it! I'm going to give very simple explanations to save space, but I suggest if you find the below interesting that you consider learning Python.

(Please note that the below are in no specific order.)


Divide-and-Floor Operator //

This is an additional divide operator that is used to truncate the entire decimal portion. This is a full truncation of the number with no rounding involved. Since this is a fairly common operation when dealing with floating-point numbers, it's nice to have a simple operator to handle it.


print 10.0 / 3
#prints 3.3333333333333335

print 10.0 // 3
#prints 3.0

x = 10.0
x //= 3
print x
#prints 3.0




Multiple Assignments In One Statement

Why waste space and time when you need to get things done? Python allows you to perform multiple actions in one statement through use of a comma. Items on either end of the equals sign are indexed in order, as seen here:


x, y = 1, 2
print x + y
#prints 3

increment, decrement = x + y, x - y
print increment, decrement
#prints 3, -1




Scope Delimiters

Python takes a very unique way of delimiting blocks of code: by using whitespace. Lines of code with the same indentations (after the start of a conditional block of code or other scoped construct) are considered part of the same block.


if x > 3:
    print 'this is inside the if-statement'
    print 'this is too!'

print 'this is executed regardless of whether x is greater than 3'




sprintf Operator %

StringBuilder.AppendFormat is probably one of the .NET functions that I use the most. At 26 letters, even with Intellisense, it's a handful.

Python has a much simpler way of doing the same thing:


print "I eat about %0.2f times every %d days" % (123.456789, 10)
#prints I eat about 123.46 times every 10 days




Use of the else Keyword with Loops

Python allows you to append an else clause to both while and for loops that always execute when a break clause is not executed inside the conditional block.

In many situations this allows for the elimination of some branching logic, in addition to allowing developers to remove an additional variable, in many cases, that would only serve to act as a flag for whether or not the intended case executed.

A common example is to implement simple search functionality:



search_number = 699

for i in range(1, 1000):
   if (i == search_number):
      print 'your number was found'
      break
else:
   print 'your number was not found'
#prints your number was found



search_number = 10000

for i in range(1, 1000):
   if (i == search_number):
      print 'your number was found'
      break
else:
   print 'your number was not found'
#prints your number was not found





Slice Operator [:]

Nevermind taking a portion of a string, this technique can also be used to take a subset of a list or a dictionary (Python's version of arrays/vectors and hashtables). The example below uses a list.


L = [1, 2, 3, 4, 5, 6]
#a (0-based) list

print L[2:4]
#prints [3, 4]

print L[2:]
#prints [3, 4, 5, 6]

print L[:3]
#prints [1, 2, 3]

print L[:]
#prints [1, 2, 3, 4, 5, 6]




Step Operator [::]

This is really an additional parameter to the Slice Operator, but this determines the direction and quantity of the index increment (or decrement), as if iterating through the list. It's best to show with an example or three:


L = [1, 2, 3, 4, 5, 6]
#a (0-based) list

print L[::-1]
#prints [6, 5, 4, 3, 2, 1]

print L[::3]
#prints [1, 4]

print L[::-2]
#prints [6, 4, 2]


Note that this can (obviously) be combined with the Slice Operator above to change the rate of index through sublists. As a continuation from the example above:


print L[:1:-1]
#prints [6, 5, 4, 3]




Optional Function Arguments (* and ** Operators)

The whole reason you can use a different programming language in a given situation is the fundamental rule that you should always use the right tool for the right job. In Python, these are known as Arbitrary Function Arguments.

Python gives developers several options (and several combinations of the options) that can be used when using function overloading with optional arguments.

The first is the * operator which returns the remaining arguments of a function in a Python data type known as a tuple.



def SomeFunction(first_arg, *other_args):
   print len(other_args)

SomeFunction('test', 'test2', 'test3', 4)
#prints 3



The other operator is the **operator which performs the same action, only returning a dictionary instead of a tuple. This only works with named arguments and will automatically map the argument name to the requested value.



def SomeFunction(**the_args):
   print the_args

SomeFunction(a=1, b=2, c=3, d=4, e=5, f=6)
#prints {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}




Multiple Comparison Operators

Unlike C and most other languages, Python allows you to chain the various comparison operations together to form complex expressions, just like in algebra. Note that like other boolean expressions, these are short-circuited to increase performance.


a = 1
b = 3
c = 5

print a < b < c
#prints True

print c < b < a
#prints False




Stop Reinventing the Wheel

Finally, Python takes many higher level constructs and implementations that have been propagated into multiple languages over the years.

Three (out of more than I can easily count) are shown below: simple string duplication, (theoretically) infinitely sized numbers and the Complex number type. These are shown very briefly below:



#1) Simple String Extension

print "*" * 20
#prints ********************



#2) Infinitely Sized Numbers (based on Memory Limits)


#Note that ** is the exponent operator in Python.

#Normally 2 ** 64 is the long type limit in most systems.


print 2 ** 100
#prints 1267650600228229401496703205376L



#3) Complex Number Type

x = 1 + 3j
y = 1 - 3j
print x * y
#prints (10+0j)




If you've ever programmed in any other language, you can see that Python has an extremely unique syntax. It is very unique, but surprisingly intuitive once you've begun to use it. The above is only a small example of how Python syntax varies from other languages. Now, I finally understand what people mean when they refer to Python code as being very "un-Python-like". Since Python supports most of the basic constructs of other languages, it's very easy to do most actions one of two ways: the normal way and the Python way.


.NET Interview Questions - Part 2

August 26, 2007 • 9:41PM • permalink
Part 1 can be found here.


Continuing where we left off...


3) What does the term "immutable string" mean?

Strings in C# are immutable, meaning that the string object cannot be modified once it has been instantiated. Take a look at the following:


string s1 = "Hello, my name is ";
s1 += "adam";



While it appears that the string 'adam' is simply being appended to the original string, a lot more work is taking place on the back end. Since the string s1 is immutable, it can't be changed. Implicitly, c# is creating a new buffer in memory that can hold both the "My name is " and "adam". Once this new buffer is filled (and a new string object is created) s1 is assigned the new reference and the old references eventually become garbage collected.

If you read my blog entry on the System.Diagnostics.Stopwatch class, you saw that this can have a huge effect on the performance of an application.

In the Stopwatch blog entry, I used the string.Concat method in the optimized test. While that method outperforms the standard += operator (and can be further optimized by using the overloaded version that allows passing in four string parameters), there is an even better way, for doing many append operations.


using System.Text;
//Place this at the top...



StringBuilder sb = new StringBuilder();
string letters = "abcdefghijklmnopqrstuvwxyz";
int iterations = 100000;

for (int i = 0; i < iterations; ++i)
   sb.Append(letters[i % 26]);



In my tests, the StringBuilder class' Append method was twice as fast as using string.Concat. This is because the StringBuilder class has preallocated a buffer of memory upon initialization, which the string.Concat object is still creating extra immutable string objects that aren't needed.


4) Describe the concept of lazy-initialization in OOP?

Lazy-initialization is best shown with a working example...

Let us take for instance that we have a class that represents a Building, perhaps for a game. In this Building class, we have various properties to represent the different rooms that you might find in the building.

For sake of this argument, let us also suppose that the general architecture of the game dictates that the Building class reference is going to be persisted in a database and not in static memory (maybe it's web-based). When we create the instance of the Building object, it would be a huge mistake to instantiate all the various rooms of the Building in the constructor.

There could be a Kitchen, a JanitorCloset, a SupplyRoom, a Gym, a Hallway or maybe even Bathroom[]. If only one of the above is used in the current execution, then we would waste a lot of processing on both the web server and possibly on either a cache server or a database server (or both), retrieving data for the rooms we don't need. This is a very common mistake that is made by developers that are new to OOP.


Lazy-initialization can help us improve our performance by only creating the objects as they are needed. Like in the following example:



public class Building
{
   private Room kitchen;
   private Room supply_room;

...


   public Room Kitchen
   {
      get {
         if (kitchen == null)
            kitchen = new Room(building_id, 'kitchen');

         return kitchen;
      }
   }


   public Room SupplyRoom
   {
      get {
         if (supply_room == null)
            supply_room= new Room(building_id, 'supply_room');

         return supply_room;
      }
   }
}



In the code above, the first time the Kitchen or SupplyRoom properties are used, their associated private fields will be null and will be instantiated through their respective Room constructors. If they aren't used in a given request, they won't be instantiated at all!


5) Write a Generic Method that takes one parameter of any type and returns the same type that is passed in.

This question really isn't fair, but we like to throw it out there anyways. We don't base any candidate on their ability to answer it (because although 1 person actually got close, nobody has ever answered such a simple question). This is actually one of the first questions we ask, mostly to set the bar. Most of the time we see a person writing a function that attempts to use Generics, which is better than some other things I've seen... (Note that this is a .NET 2.0 specific feature)

The solution is actually very simple:


using System.Collections.Generic;
//Place this at the top...




public T SomeFunc<T>(T obj)
{
   T new_object_copy = obj;
   return new_object_copy;
}



That's it! I only added the line T new_object_copy = obj to do something more interesting than just return the object.

We can now call this with virtually any type we want!


string s = SomeFunc<string>("hi");
int i = SomeFunc<int>(5);
my_class m = SomeFunc<my_class>(new my_class());



As a bonus, you can constrain the types of objects that you want to allow the function to operate on by adding a where clause:



public T SomeFunc<T>(T obj) where T : IList, IEnumerable
{
   T new_object_copy = obj;
   return new_object_copy;
}

List l = new List<string>();
List l2 = SomeFunc<List<string>>(l);
//This will work




string s = SomeFunc<string>("hi");
//This will no longer compile



Simple!

Maybe so, but we've yet to see somebody answer it correctly (and I've done literally fifty interviews in the last six months). We even joke around the office that if somebody can answer it correctly, they are an "instant hire". (Offer now null and void, since I've given the solution away.)




.NET Quickies

* What is the C# coalesce operator and how is it used?
The coalesce operator in C# is the ?? operator. It is used to do a conditional assignment to a variable, evaluating from left to right and stopping on the first non-null result. For example:

string a = null;
string b = "Yay!";
string c = a ?? b;
//The value in b will be assigned to c


* What is the size (in bytes) of the following data types on a 32-bit machine: byte, short, int, float and double?

byte = 1 byte
short = 2 bytes
int = 4 bytes
float = 4 bytes
double = 8 bytes


* What is the result of bit-shifting an integer (either to the right or the left)?
Bit-shifting an integer to the left will multiply the number by two for each bit shifted.
Bit-shifting an integer to the right will divide the number by two for each bit shifted.



I could talk about simple interview questions forever! I'm sure that I will have much more to write about on this topic in the future!


.NET Interview Questions - Part 1

August 07, 2007 • 9:52AM • permalink
With MANY open .NET development positions in the Media Department, I've had many opportunities to refine my interview process. Below is a summary of my experience, including some of the general .NET questions we ask most job candidates at Demand Media (and why). Since we are constantly refining the interview process and adding new questions and since this is going to be a rather long entry, I'll split it up into several separate posts.


General Interview Notes


First, a couple of general caveats:

1) I try to give the interviewee the benefit of the doubt. A lot of the time, the people I talk to have more years of experience than I've been alive, so I understand that a lot of them are intimidated by my youth.

2) Look up something about the company on the Internet. Of course nobody has time to redo their entire resume to cater to a specific company, but look up something about the company and try to tune your responses to items that might relate.

3) Most interviews I've been on included the question: "Do you have any questions for me?" That's your cue. Ask any intelligent question without appearing to be a snob, but make sure you ask something (besides your compulsory question about which snacks are kept in which kitchen.) A few stand-by questions I used to use are: "Will I be able to continue my personal education and learn here?", "How quickly do you adapt to new technologies and upgrades to existing technologies?", "What is the team dynamic like?" and "How much room is there for career growth at XXXXXXX Company?"

4) Your resume is your first impression. Use it as a way to highlight your strengths, not as a laundry list of items you "used once in school". There are two main items in particular I see all the time: C++ and .NET 3.5 - usually only by candidates who can't explain ANY intricate features of either.

5) An interview should be a learning process, as well. If you don't understand a question or an answer to a question, ask!

6) TURN OFF YOUR CELL PHONE!!! For many people I've encountered during my career (including me), a cell phone going off during an interview is an instant death sentence. You can start juggling keyboards after that - it's not going to matter...

And now finally...


.NET Questions


1) Describe the difference between a reference type variable and a value type variable?

This is a good question we use to get rid of the riff-raff right away. If you can't form some semblence of an answer to this question, we won't even consider you a candidate for a job.

While we look for many keywords, a simple description of the fact that a value type stores the actual value of the data and the reference type stores an address or pointer to the data usually suffices.

If you don't somehow relate the reference and value types to their storage location on the stack and the heap, that will definitely become question 1b.


2) Describe the process of using an integer variable (of any size) as a bitmap for boolean flags?

This is a process I brought with me to Demand (inherited from Intermix Media, where I propagated the idea as well), but I think it's fairly clear why this is a simple, but vital idea. (Although, I'm assuming below a basic knowledge of binary numbers and bitwise mathematics.)

The idea is that any number can be viewed as a series of bytes and then bits. First I'll describe the process, then I'll demonstrate it in both basic C/C++/C# code and then using .NET additions.

As you should already know, any number can be represented in binary form, which means any number can be represented as a sum of one or more powers of two. We can show this number a number of ways:

123 (decimal) OR

(100 * 1) + (10 * 2) + (1 * 3) OR

(10^2 * 1) + (10^1 * 2) + (10^0 * 3) OR

(64 * 1) + (32 * 1) + (16 * 1) + (8 * 1) * (4 * 0) + (2 * 1) + (1 * 1) OR

(2^6 * 1) + (2^5 * 1) + (2^4 * 1) + (2^3 * 1) * (2^2 * 0) + (2^1 * 1) + (2^0 * 1) OR

1111011 (binary)


If the above binary number is extended to represent a 32-bit int,it would look like:

00000000000000000000000001111011

with the leftmost bit being bit 31 and the rightmost bit being bit 0.

This allows us to see the above as a series of 32 switches or Boolean (True/False) flags that can be accessed (counting from 0 and from the right) by taking a bitwise AND of the number and the "on" value.

As you can see from the above representation, the fourth bit from the right is in the 2^3 position. So, if we take the bitwise AND of our value with 2^3 (8), we get the following:

= 123 & 8
= 8

Since the result (8) is the same as the flag (8), the integer (123) does contain the flag!


When adding flags into your integer, you instead use the bitwise OR. This is always used to switch flags on, so if the flag already exists in your number - it will have no effect, as shown in the following examples:

= 123 | 8
= 123


= 123 | 4
= 127


Finally, to remove a class you take the bitwise AND of the bitwise NOT of the value you're trying to remove. The bitwise NOT inverts the value, so the AND masks any other bits that are currently set, like:

= 127 & (~4)
= 123



The example I usually like to use is to build out the character classes in an RPG-like setting.

Simple C-style Example:


/* CC_ is for Character Class */

int CC_DWARF = 1;
int CC_FIGHTER = 2;
int CC_NINJA = 4;
int CC_SAMURAI = 8;
int CC_ELF = 16;
int CC_MAGICIAN = 32;
int CC_PALADIN = 64;
int CC_HOBBIT = 128;
int CC_PRIEST = 256;
int CC_DARKLORD = 1073741824;


int character_dwarf_fighter = CC_DWARF | CC_FIGHTER;
//value is 3


int character_samurai_elf = CC_SAMURAI | CC_ELF;
//value is 24


int super_crazy_bad_guy = CC_FIGHTER | CC_NINJA | CC_SAMURAI | CC_MAGICIAN | CC_DARKLORD;
//value is 1073741870



//let's remove the CC_DARKLORD class

int super_crazy_bad_guy2 = super_crazy_bad_guy;
super_crazy_bad_guy2 &= ~(CC_DARKLORD);
//value of super_crazy_bad_guy2 is now 46
//OR CC_FIGHTER | CC_NINJA | CC_SAMURAI | CC_MAGICIAN


//let's check super_crazy_bad_guy2 for CC_DARKLORD

if ((super_crazy_bad_guy2 & CC_DARKLORD) == CC_DARKLORD)
   Response.Write("This guy is nothing now!");
//this will print...



//let's check super_crazy_bad_guy2 for CC_DARKLORD

if ((super_crazy_bad_guy2 & CC_PRIEST) == CC_PRIEST)
   Response.Write("This guy is holier than I am!");
//this will NOT print...




Simple .NET/C#-style Example:


[Flags]
public enum CharacterClasses : int
{
   Dwarf = 1,
   Fighter = 2,
   Ninja = 4,
   Samurai = 8,
   Elf = 16,
   Magician = 32,
   Paladin = 64,
   Hobbit = 128,
   Priest = 256,
   DarkLord = 1073741824
};



CharacterClasses super_crazy_bad_guy = CharacterClasses.Fighter | CharacterClasses.Ninja | CharacterClasses.Samurai | CharacterClasses.Magician | CharacterClasses.DarkLord;
Response.Write(super_crazy_bad_guy.ToString());
//prints Fighter, Ninja, Samurai, Magician, DarkLord


super_crazy_bad_guy -= CharacterClasses.DarkLord;
Response.Write(super_crazy_bad_guy.ToString());
//prints Fighter, Ninja, Samurai, Magician


super_crazy_bad_guy |= CharacterClasses.Elf;
Response.Write(super_crazy_bad_guy.ToString());
//prints Fighter, Ninja, Samurai, Elf, Magician


if ((super_crazy_bad_guy &= CharacterClasses.Elf) != 0)
   Response.Write("An elf!");
   
//this will print



if ((super_crazy_bad_guy &= CharacterClasses.Paladin) != 0)
   Response.Write("A paladin!");
   
//this will NOT print





.NET Quickies

* What is the root class that all other .NET classes are derived from?
System.Object

* Name as many ways as you can think of to find a certain character in a string?
String.Contains, "Character Crawl" using any of (for/foreach, String.Chars, String.CharAt, String[], bit masking, plus many others), String.IndexOf, String.LastIndexOf, etc.

* What is a GUID?
Global Unique IDentifier - A 128-bit value that is statistically impossible to be duplicated in a closed environment. (Bonus points if you throw in a note about using it in a multi-server environment by setting the machinekey, generating on the SQL server, using a Key server, etc.)


More to come! Also, if anyone has any suggestions for questions that I should add into my interview process, please send me them!





page 1 of 1
1 




Tags

anime convention PHP programming SQL Generic Method Adrianne testing Windows internals tools SQL Server 2005 c languages server client-side SQL Server 2000 optimization query parsing Stopwatch ASP software SQL Server Microsoft c plus plus love Remote Desktop help Windows csharp IIS lazy initialization driver debug AdSense module script generation book review interview mathematics books MySql Immutable String development syntax concurrency string parsing expert HTTP reflection injection