Archives

All posts for the month July, 2013

Ok,

So I was recently presented with an example coding exercise as stated below:

Every athlete is characterized by his mass ‘m’ (in kg) and strength ‘s’ (in kg).
You are to find the maximum number of athletes that can form a tower standing one upon another.
An athlete can hold a tower of athletes with total mass equal to his strength or less than his strength.
Input contains the number of athletes n and their parameters.
For example:

n
m1 s1
m2 s2

mn sn

If mi > mj then si > sj, but athletes with equal masses can be of different strength.
Number of athletes n < 100000. Masses and strengths are positive integers less than 2000000.
For example:
Input #1

4
3 4
2 2
7 6
4 5

Would yield
Output #1

3

Working through the Solution

Phase one, figure out how to store the data.  Easy, store it in a linked list, could be a list of tuples/pairs, but I actually created a class to store the weight/strength, because I want to be able to do a complex sort on the data (see below).

Now, the problem description states that if one athlete weights more than another, it will have greater strength as well.  This provides us with one crucial optimization, we know that the strongest athlete will also be the heaviest.  This athlete, therefore, should go on the bottom of the tower.  We could do a linear search for the strongest, but we can also find the strongest after sorting the list, and we’ll want a sorted list for later.  Therefore, using the comparison operators we described above, we can sort the list of athletes, and begin processing as so:

In the above code, we grab the first entry after sorting, which is (by definition) the strongest. Starting with that athlete’s strength as the maximum weight for any tower to be formed. We then pass the array of athletes, the starting point and weight left to our recursive function. The “tot” variable is there for analysis purposes. The function is defined as below.

The analysis of the algorithm is actually contained in the comments above, but I’ll point out some key points here:

  1. The maximum size of the tower (within the current iteration) is limited to the number of athletes left in the list.  If we find a tower equal to the size of the list, we are done.
  2. If the current athlete weighs more than the remaining weight limit, they (and any other athletes of the same mass) cannot fit on the tower.
  3. The first athlete of any weight is also the strongest (thanks to the sorted list).  Therefore, we only need to consider ONE of any athlete with a given weight (e.g. ALL athletes of weight 4 will be able to support a tower of a most the size of the strength of the strongest athlete).

The loop in the above function scans the lists of athletes, to determine the maximum size of a tower each athlete could support, standing on top of the tower as it stands before the function is called.  Therefore, the first time the function is called, it stands each athlete, in sequence, on top of the shoulders of the strongest athlete, and sees how tall the tower can be built.

If the current athlete can fit on the tower at all (based on remaining weight limit), the recursive call then determines the size of the tower that could then stand on the shoulders of the current athlete. Thanks to item 3 above, we must only perform the recursive call once per athlete weight.  This greatly reduces the number of recursive calls.  The return value of the recursive call is compared to the current maximum tower height found previously (and stored if greater than).  The loops then checks that the maximum tower height does not equal the remaining number of athletes.  If it does, we have found the greatest tower height and can return.

The function returns the tallest tower it found that could stand on the shoulders of the previous athlete.  When the recursion returns completely, we have solved the problem. Worse case scenario of this algorithm is actual 2^n, which is terrible.  But this is because every possible combination of towers must be considered.  However, thanks to our end-conditions (the 3 steps above), we can actually reduce actual complexity to a linear (O(n)) complexity.  This is because we really only have to search the list once, excluding “duplicate” athletes (athletes that could not have supported a stronger tower are discounted thanks to step 3).  With step 1 above, we do not need to continue looping once we have found a maximum tower, and can exit.

Anyway, the full source is in my (poorly misspelled) repository here: http://svn.hellwig.us/repo/VC11/ExcersizeTest.

I’ve been looking into Git recently, not because I use or plan to use it, but I’m trying to figure out why it’s suddenly the go-to for source repositories (I’ve heard rumors that some companies actually use Git… internally?!).

What Sounds Good

“Distrubuted architecture”

“offline development”

“push/pull contributions”

However, these are all just buzzwords.

 

Why It’s Not “Good”

Nothing is truly distributed.

For something to be truly “distributed”, it must exist only “in the ether”.  However, there is no truly distributed system.  Skype has “super nodes”, computers that must do nothing but act as gateways for Skype connection information, these are run by Skype.  BitTorrent usually requires a master seeder and a central distribution point for the torrent files themselves (Pirate Bay hosted torrent files, but did not seed), otherwise anything shared via torrent is only short-lived (once the seeders are gone, so is the file).  With Git, you have GitHub.  GitHub acts as the master repository.  Sure, anyone can replicate your repository, but the developers using GitHub certainly aren’t going to use “Joe113465” as the source for anything.  I imagine any company using Git internally also uses a master respository, and therefore the distributed nature is useless.

Lets not forget that being “distributed” means every single person who replicates the repository has their own copy of the data.  That sounds fine when every person is in a different country, but when you have one thousand employees all working at the same company, your employees all have (at least) 1000 copies of your repo. Even worse, if you “encourage” your employees to do their development on network shares, your IT department is responsible for providing storage and access to 1000+ copies of the SAME data.  I’ve heard rumors about how expensive SAN/networked storage can be, and I can’t imagine any company wants to have to pay for that.

Off-line Development?  Sounds rogue to me.

Off-line development sounds good on the surface. ” I can make updates on the plane!” And lets face it, in today’s world, that’s about the only place a tech-savvy engineer DOESN’T have access to the internet.  How long would someone be flying that they would need to make multiple commits and/or check multiple revisions of a file?  If you KNOW that you will need this data, you could easily check out these revisions ahead of time using centralized CMS. And if you need to make multiple commits, it’s not that hard to just make a copy of the file when you’re done so you can commit it the next time you have internet access.  Really, there’s no reason to move to an entirely different CMS like Git JUST so you can work off-line.

And besides, how much ACTUAL work can you do while disconnected from the rest of the world?  Can you really keep track of all the problem reports or change requests offline?  But at the same time, you couldn’t possibly keep track of all the corresponding changes YOU are making to the source code?  What if someone sent you an email with the details you need, or worse, sent you an email with changes/revisions SINCE you got on the plane?  You might do 4 hours of work, then find out Joe sent you the wrong data.

Contributions Cannot be Ignored

When someone makes a pull request to your repo, they have already PUSHED their files to your repo, and the request show’s up in your repo overview.  YOU must take time to address this requests, regardless of it’s value.   I could go onto GitHub right now and push all kinds of nothing to various repos, and someone will have to waste their time clearing them out (or they would build up eternally).  I won’t do it, but I could.  There is something to be said about centralized servers where you can control the access, but granted, this is (should be) a moot point where companies use Git internally.  I just don’t understand under what situation having a stranger push data to your repo could be beneficial.  What happened to communicating with other developers?  Synchronizing what needs to be done and where?  If Joe makes a change in his branch, why can’t he just send me an email saying he made a change he wants me to check out?  Did the whole CMS really need to be designed around the ability for Joe to push his changes to my repo then send me a “pull request”?

 

What’s It All Mean?

It doesn’t really mean anything.  My opinion on the matter is totally irrelevant.  Some people see benefits and choose to use Git (Linus Torvalds obviously saw a benefit when he created it).  However, I imagine most other people use it because “it’s Git”.  GitHub seems to really be helping out the adoption of Git.  Not sure what happened to SourceForge or why people switched or choose GitHub over SourceForge (especially when SourceForge lets you choose between Git, SVN, or Mercurial).  I suppose it’s how FireFox got so popular over Opera, then Chrome over FireFox, marketing.  Anyway, I just needed to rant.

Ok, so while creating my Agbar game, I wanted to play around with particles.  I’m think of making the world destroyable, and this would involve individual particles.

So, I created ParticleWorld.  The example is shown in the video below.


In this video, there are 7 sources of particles (in seven different colors).  One source sprouts up from the bottom middle like a fountain, two come up from the corners, two flow down from the corners, one sprouts in the center (almost like a firework), and there is a constant (orange colored) “rain”.

The debug information on the screen show the Processing FPS (60), Drawing FPS (30), the number of particles in each color and total particles (~7400). The final line shows the time it takes to process all 7400 particles (3-4ms running in a single thread on my 5+ year old Athlon X2 5000+, OCed to 3.2GHz).

Each particle maintains a constant, random X velocity (no air resistance, orange pixels have no lateral movement).  The Y velocity is subject to “normal” gravitational acceleration (9.81 pixels/second/second “down”). The rain is populated in a 1:50 ratio (roughly every 50th column gets a pixel each Processing cycle). Each “fountain” gets one new particle/per Processing cycle.

So anyway, I think it’s a pretty reasonable demo.  7400 independent particles updating at least 250 times per second (if the Processing cycle wasn’t locked to 60Hz) isn’t bad.  I could processing roughly 8 times this number of particles without any lag (at 60Hz).  So now all I need to do is figure out how to use this in my Agbar game.

Source is in my repo.

 

Back in high school, and friend and I wrote a 8-bit video game for our C++ class.  He wrote the graphics engine (this was Pre-DirectX/OpenGL technology), and I wrote the game engine.  I also did the graphics, but that’s not something I’m bragging about.  We tried to get a fried of ours who was studying to become a graphic designer to redo the images for us, but when we graduated high school we kinda lost track of the whole project.

Anyway,  armed with my 15-year old graphics, and nothing else, I decided to re-create the game in a modern setting (the old game doesn’t even run under DosBox 🙁 ).  Of course, being the genius that I am, I didn’t make a backup of the source code for the original game, literally all I have is the old graphics, the DOS-based map editor (which DOES run in DosBox), and a vague recollection of how the game worked.

I was going to use Microsoft XNA, but they stopped supporting that.  Instead I’m using C# and  MonoGame, a great open-source implementation of XNA functionality targeted for OpenGL (XNA is obviously DirectX).  And behold, the first screenshot is posted:

AgbarTest Level 1

AgbarTest Level 1

So, what does the game currently do?  It loads a pre-defined map, textures, sprites, etc…  Draws a world with the textures from the map.  Spawns our hero (Agbar, Sheep Destroyer, (nee Sheep Laser Tagger (long story) ) ), loads a couple of his most cunning adversaries (sheep), and lets you control the character.  Movement is with W A S D, firing left and right with Q and E respectively.  Jumping is the space bar.  The window scrolls with the character, missiles collide with bricks or sheep, sheep walk back and forth on platforms, character can climb ladders, and that’s it for now.

Video Demo:

In the original game, there were 4 types of enemies, landing on a potion bottle gave the hero magical flying/invincibility, and you could move through the doors (brown to brown to brown, etc…).  None of that is implemented yet, but I’ll work on it.  The original also drew everything palette-based (this was 8/16-bit DOS we’re talking about here), so the potions and candles used to blink thanks to palette-swapping.  That’s a thing of the past, so I guess I’ll need to draw more sprites.

The source is, of course, in my repository, so check it out (if you dare).  NOTES:  This needs .NET 4.0/4.5, OpenGL, OpenTK, and possibly OpenAL (though there is no sound).

Ok,

So getting back into C++ programming, I’ve been allocating plenty of dynamic memory (using new  and new[] ) .  However, I’ve only been using delete , never delete[] .  If fact, I’ll be honest, I forgot about delete[] , but that’s OK, I didn’t need the [] form anyway.  Why you ask?

Take this example:

Now, almost every example you see online will actually have delete[] buf; , this is correct, but unnecessary.  Why?  The only difference between delete and delete[] is item deconstruction.

When you call delete, all the memory you allocated with new[] is still erased. The problem is, only the first element (the one being directly pointed to) is deconstructed. None of the other members of your array are deconstructed. In my example this is OK, because the array was an array of “char”s, you can’t deconstruct a char in C++, it’s not an object.

 

When is delete[] useful?

When you allocate an array of objects that themselves allocate more memory. delete[]  makes sure that each entry in the allocated array is properly deconstructed. Example:

 

Can I just call delete[] all the time?

Sure, well, almost.   delete[]  works on a specific type of pointer.  When you call myclass * myptr = new myclass[5] , you are telling the system to reserve you sizeof(myclass)*5  bytes of memory.  This size is all that is stored in the memory allocator.  When you call delete[] myptr; , the memory allocator goes through the following process:

  1. How much memory was reserved starting at the address stored in myptr (answer: sizeof(myclass) * 5);
  2. Start at offset = 0.
  3. Call the myclass.deconstructor at the address myptr + offset.
  4. Increase offset by sizeof(myclass)
  5. If offset < total allocated memory, goto 3.  Else, continue;
  6. Delete/Free all memory reserved (sizeof(myclasS) * 5 bytes) starting at address storef in myptr;.

However, if you tried to call delete[] (void *) myptr;  all calls to sizeof(myclass) above will be replaced with sizeof(void), which returns no size (no object can be of type void). Therefore, none of the objects in the array will be deconstructed.  Calling delete[] (void *)ptr;  is the same as calling delete (void *)ptr; .

Similarly, if you recast the pointer to a different type, delete[] will not know the proper size of each object, and any attempts to deconstruct those “objects” will most likely result in errors/exceptions.  Example:

Of course, casting pointers in C++ is always dangerous.  The compiler and system do not keep track of allocated memory by type, only size.  Therefore, you should always be careful when having to cast a pointer.

Note: There are acceptable times to cast a pointer (when reading in bytes that are actually multi-byte values, say casting an int* as a char*), but often times, this can also be handled with Unions.

Anyway, that’s my “quick” overview of the delete and delete[] operations in C++.

Ok, so many of the jobs I am applying for now want C++ experience.  Problem is, none of the companies I have worked for in the past 8 years wanted me to write in C++.  The industry used to use Ada, and many legacy programs still have all their code in Ada.  Not only that, but the companies that use C++ often use model-based development suites, meaning they don’t write any actual code, the compiler does it all for them.

Anyway, I am quite familiar with C++.  It was the first real language I ever programmed in (not counting Quick Basic).  I took 3 years of C++ in high school.  I did all sorts of things in the days before Java, Python, Perl, .NET, etc…  Now days, I’m not even sure people know what C++ means.  They probably use so many libraries, they really only use C++ for the Syntax and wide array and industry tested (and free) compilers.

Long story a little bit shorter, I have started to upload some of my old C++ code to my SVN server.  The page describing it all is on my website here: C++ Code.

That is all.