2013年12月10日星期二

Interview Coding Question - Fibonacci

Fibonacci number


Recursive solution
int fib(int n)
{
  if (n < 2)
    return n;
  else
    return fib(n-1) + fib(n-2);
}




Iterative solution 1
int fib(int n)
{
int fibArray [MAXINT], i;
fibArray [0] = 0; fibArray [1] = 1;
for (i = 2; i <= n; i++)
    fibArray [i] = fibArray [i-1] + fibArray [i-2];
return fibArray [n];
}

Iterative solution 2

int fib(int n)
{
  int first = 0, second = 1;
  int i, tempSum;
  for(i = 2; i <= n; i++)
  {
    tempSum = first + second;
    first = second;
    second = tempSum;
  }
  return second;
}

Interview Coding Question - Prime Number



What's Prime Number

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

Simplest algorithm but most expensive

{
      if (n<=1) return false;

for (int i=2; i < n; ++i)
if (n%i==0)
return false;

return true;
}
If you stop here, you barely can be hired as programmer.

A little bit optimized 

/// If the number can NOT be exactly divided by 2, no need to test 4, 6, 8...
bool IsPrime (int n)
{
if (n <= 1) return false;

if (n % 2 == 0)
return false;
else 
for (int i=3; i < n; i+=2)
if (n%i==0)
      return false;

return true;
}

Further optimization with a theory  

if n=a b is composite (with a and b ≠ 1) then one of the factors a or is necessarily at most \sqrt{n}.
{
if (n <= 1) return false;

float sqrt_num = sqrt(n)

for (int i=2; i < sqrt_num; ++i)
if (n%i==0)
return false;

return true;

}

Note
Prime generation algorithm is heavily tested during interview, because it can test the interviewee's basic math knowledge, logical thinking and the sense to chase excellence which are a great programmer requires.
Prime algorithm optimization is a very very deep topic. You are NOT expected to be able to finish it with the highest efficiency algorithm if the position you apply is a junior / medium level software developer. Otherwise the interview will last more than 2 hours, and very likely the interviewer will think you have guessed the question and are prepared.



http://program-think.blogspot.com/2011/12/prime-algorithm-1.html

2013年12月9日星期一

Interview Coding Questions - Linked List


Linked List Data Structure
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Positon
struct Node
{
ElementType Element;
Position Next;
}
  
Algorithm 1 - Reverse a linked list (iterative implementation)   
  
    1     List   Reverse(List   l)   {   
    2     if(!l)   return   l;   
    3     list   cur   =   l.next;   
    4     list   pre   =   l;   
    5     list   tmp;   
    6     pre.next   =   null;   
    7     while   (   cur   )   {   
    8         tmp   =   cur;   
    9         cur   =   cur.next;   
  10         tmp.next   =   pre   
  11         pre   =   tmp;   
  12     }   
  13     return   tmp;   
  14   }   

Algorithm 2 - Reverse a linked list (recursive implementation)   
  1     List   Reverse(list   l)   {   
  2     if(!l   ||   !l.next)   return   l;   
  3       
  4         List   n   =   reverse(l.next);   
  5         l.next.next   =   l;   
  6         l.next=null;   
  7     }   
  8     return   n;   
  9   }   

2013年11月5日星期二

Excel - Setting Up The Worksheet

Excel - Setting Up The Worksheet

ShipRush works with spreadsheets in the Excel format. Excel versions 2000, XP, 2003 and 2007 can be used. Most modern Excel file types work with ShipRush: .xls, .xlsx, and .xlsb files are supported.

ShipRush reads from the spreadsheet into the ShipRush Order Manager. From the Order Manager, you can ship one-at-a-time or in bulk. Tracking numbers can be written back into the spreadsheet.

 Before a spreadsheet can be used with ShipRush, it needs two simple preparation steps.

 To download a sample xls file, click here (click Save when prompted -- note the file may be saved to your PC as Read Only, which will cause an error when saving tracking numbers. Uncheck the Read Only checkbox on the file to resolve this.).

 

Properly Named Worksheet

The data must be in a worksheet named either "shiprush" or "sheet1" as shown:





Column Names Set

The worksheet needs to have appropriate column names to identify the data. A basic set of columns is required. Additional columns are optional.

Column names are set by simply typing the names in row 1 of the worksheet:



Column Format (aka Data Type)

This step is required on Excel 2007 and higher if the PostalCode and/or OrderID columns contain any non-numeric data.
Simply select the entire column, right click, select Format Cells, select Text (as shown below), and press OK. Do this for both the PostalCode and OrderID columns.


This worksheet is ready to go:

 


Required column names:

  • Contact
  • Company
  • Address1
  • City
  • State
  • PostalCode

 For ShipRush to write the tracking number or shipped status (IsShipped) into Excel, additional columns are required:

  • OrderID
  • TrackingNumber
  • IsShipped

 The OrderID column must be unique within the worksheet. For example, it can be a unique customer ID or order number. For a quick way to generate unique ID's in Excel, see the section below.

The TrackingNumber column should be empty. This is where ShipRush will write tracking numbers. Note that any data in this column will be overwritten by ShipRush.
Additional Optional Columns

  • Address2
  • Country
  • Email
  • Phone
  • IsPaid                 (use value of Y if true)
  • IsShipped           (use value of Y if true)
  • ItemSKU
  • ItemAccountingID
  • ItemID
  • ItemDescription
  • ItemQuantity
  • ItemPrice
  • ItemTotal
  • TotalWeight
  • OrderNum        (note: If OrderNum is empty, the OrderID value will automatically flow into OrderNum)
  • OrderDate
  • CustomerPO
  • ApprovalTransactionID
  • MarketingCode
  • Reference
    • To merge this onto the shipment reference line, use the option in Order Manager settings to set the Reference field, and enter %Reference%
  • ShipMethod
  • ShippingPaid

Quick and Dirty Unique OrderID Values

If your source data lacks a unique value for each row, take these steps:

  1. In row 1 of an empty column, enter: OrderID
  2. In row 2 of this column, enter the formula: =Row()

     
  3. Select the cell from #2 above and press ctrl-c

     
  4. Now select all cells in this column from row 3 down to the bottom of the data
  5. Press ctrl-v
  6. This will paste the formula into all cells in this column
  7. Each row should now have a unique row number in the OrderID column. This will allow ShipRush to write tracking numbers back into Excel.

2013年11月2日星期六

Magento Installation Guide against 1and1 hosting

1. Check Magento Requirements (Check PHP Version, Extensions and MySQL Version Number)
2. Setup a MySQL 5 Database
3. Download, Upload and Unpack Latest Magento Package (Use full package if possible, not the installer)
4. Move the Magento Folders & Files to appropriate location (eg: http://www.your_domain.com/magento OR http://www.your_domain.com)
5. CHMOD 777 the following: [file] magento/var/.htaccess [dir] magento/app/etc [dir] magento/var [dir] magento/media
6. Configure .htaccess located at magento/.htaccess with the following:

AddType x-mapp-php .php4
AddType x
-mapp-php5 .php
AddHandler x
-mapp-php .php4
AddHandler x
-mapp-php5 .php
Configure the RewriteBase as well according to the following rules:
eg: http://www.your_domain.com

RewriteBase /
eg: http://www.your_domain.com/magento

RewriteBase /magento/
7. Run the installation by going to: http://www.your_domain.com/magento/index.php/install or http://www.your_domain.com/index.php/install or etc.
8. Setup is very easy.
- Select locale settings.
- Type in MySQL DB Info.
- Checkmark “Use Web Server (Apache) Rewrites”.
- Checkmark “Use Secure URLs (SSL)”...IF YOU HAVE SSL CERTIFICATE INSTALLED (eg: https:// access)
- NOTE: AFTER THIS IT MAY GIVE AN ERROR 500, JUST HIT THE BACK BUTTON AND CONTINUE AS NORMAL. (You can double check the DB to ensure all the tables are in, which it should be. There should be a ton of tables all the way down to tables starting with a ‘w’).
- Type in Admin Information and Create Encryption Key.
9. Access (depending on where you installed it):
- Frontend: http://www.your_domain.com/magento OR http://www.your_domain.com
- Backend: http://www.your_domain.com/magento/admin OR http://www.your_domain.com/admin

source: http://www.magentocommerce.com/boards/viewthread/3462/

2013年10月30日星期三

Magento - How do I remove “index.php” in url?

I have the exact same issue on how to remove index.php from the url.  Here are the steps:
1. I first enabled the rewrite in Admin>System>Configuration>Web>Search Engine Optimization>Use Web Server Rewrites
2. I also enabled mod_rewrite in apache2 in the terminal with: sudo a2enmod rewrite and then restart apache2 with sudo /etc/init.d/apache2 restart
I checked that mod_rewrite is enabled in the directory: /etc/apache2/mods-enabled/rewrite.load
At this point, I can properly display the homepage without the index.php in the home url. All is well until I click on any product category or any link, the index.php in the url is removed but I receive a 404 error.  If I manually type in the index.php in the address bar, I can navigate to the page.
After searching the forum on this problem, people having the same problem resolved it with .htaccess. Here’s what I did:
3. I copied the content of .htaccess.sample in the magento root and paste it to a new file called .htaccess in the same directory.
4. I uncommented the statement: “RewriteBase /magento/” and change it to “RewriteBase /” since my magento directory is already the root
This didn’t resolve the problem, I still have the 404 error. I search the forum again, and tried:
5.  I issued the command in terminal to edit http.conf: sudo gedit /etc/apache2/httpd.conf and paste the following in the file
# Each directory to which Apache has access can be configured with respect
# to which services and features are allowed and/or disabled in that
# directory (and its subdirectories).
#
# First, we configure the “default” to be a very restrictive set of
# features. 
#

Options FollowSymLinks
AllowOverride All

After restarting apache2, I’m still having the same 404 error.  Another search in the forum and
6. I refreshed the catalog cache in Admin>Cache Management>Catalog Rewrites>Refresh

But I still have the same 404 error when I clicked on the category page or any other links in the homepage. What could be the problem and how do I fix this? Thanks.

Source: http://www.magentocommerce.com/boards/viewthread/22199/

2013年3月2日星期六

Pageline 使用技巧


如何去掉頁面底部的pageline圖標
http://udinra.com/blog/removing-pagelines-logo-from-pagelines-theme-footer-tutorial

2013年2月28日星期四

序言