WordPress Loop

This post is to sum­ma­rize and present the code snip­pets of the most Word­Press fun­da­men­tal: the loop.

1. Set up index.php
< ?php get_header(); ?>
<div id="posts-container">
  // loop starts here
</div>
< ?php get_sidebar(); ?>
< ?php get_footer(); ?>
2. Sim­plest loop
if (have_posts()) :
  while (have_posts()) :
    the_post();
    the_content();
  endwhile;
endif;

Within the while loop:

  • Func­tion the_post() makes the cur­rent item in the posts col­lec­tion avail­able for use inside this iteration.
  • Tem­plate tag the_content() returns the post con­tent. more »

Tower of Hanoi

1. Intro

Tower of Hanoi’ is a well-known puz­zle pre­sented in many CS text­books. These the rules:
– The are 3 poles A, B, and C.
– n disks are on pole A with the con­i­cal shape (the largest disk is at the bot­tom, then the smaller ones upper, and the small­est on top).
– You job is to move the entire n disks from pole A to pole C (the same con­i­cal shape as the final result) using 3 poles while obey­ing the rules:

+ only one disk is moved at a time
+ no disk is allowed to be placed on top of a smaller one

2. Tips to solve the game

This sim­ple and easy-to-follow guide will guar­an­tee you the least moves (and time if you prac­tice) to achieve the result:

[ Don’t look at this if you pre­fer try­ing the game first ] more »

Sorting Algorithms

Sort­ing is one of the most com­mon tasks in pro­gram­ing. Although .NET and other frame­works already pro­vided pre-written sort­ing func­tions for us, under­stand­ing how they works and the abil­ity to apply those tech­niques to each sit­u­a­tion are so valuable.

This is a col­lec­tion of sort­ing tech­niques. It is not a com­puter sci­ence book, nor a well-organized col­lec­tion of some­thing. It is intended to be sim­ply, flex­i­bly, and prac­ti­cally illus­trated pop­u­lar sort­ing tech­niques. more »

Sorting Algorithms (Quick Sort)

Quick Sort

Quick­sort is fast and suit­able for large num­ber of items need to be sorted, used for both edu­ca­tion and real world businesses.

Task: To sort an array, a, of n inte­ger num­bers
Lan­guage: C#

Algo­rithm:

  • Idea: Divide-and-Conquer strat­egy.
    Divide array into two parts: the first one includes all items that are less than or equal to the pivot value, and the sec­ond once with all items that are greater or equal to the pivot value. Sort those two parts recur­sively. An array of 0 or 1 items is already sorted.
  • Pick a pivot value: can be any value, usu­ally the mid­dle one.
  • Par­ti­tion: the array into two parts as men­tioned above.
  • Sort sub-parts recur­sively. more »

Sorting Algorithms (Insertion Sort)

Inser­tion Sort

Inser­tion Sort is fast with small array, but very slow with large array.

Task: To sort an array, a, of n inte­ger num­bers
Lan­guage: C#

Algo­rithm:

  • Imag­ine that the array is diviced into two parts: sorted and unsorted. At first, sorted part con­tains the first item and the rest belongs to unsorted part.
  • At each step, take the first item in the unsorted part and insert it into the right pos­tion in the sorted part. Con­tinue this step until unsorted part becomes empty. more »