<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>code-diesel &#187; algorithm</title>
	<atom:link href="http://www.codediesel.com/tag/algorithm/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Sorting PHP arrays with substrings</title>
		<link>http://www.codediesel.com/php/sorting-arrays-with-value-substrings-in-php/</link>
		<comments>http://www.codediesel.com/php/sorting-arrays-with-value-substrings-in-php/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 12:35:17 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[algorithm]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=2141</guid>
		<description><![CDATA[Sorting PHP arrays using usort with substrings]]></description>
			<content:encoded><![CDATA[<p>A couple of days back I encountered the following request on a forum for sorting a single dimensional array. The programmer wanted to sort the following array by the substring after the colon. For example &#8216;CDF&#8217; in the string &#8217;66345:CDF&#8217;. Those values that do not have any colon should be ignored and pushed to the end of the array. Also, the length of the strings are not constant.</p>

<div class="wp_codebox"><table><tr id="p21411"><td class="code" id="p2141code1"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$values</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;66345:CDF&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;61179:HGT&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;64146:ABA&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">&quot;68768:BNG&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;68015:ZCZ&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;80231:LPO&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">&quot;64146:QWP&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;68736:HHB&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;86801:MNV&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">&quot;80178:OIU&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;80178:ASE&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;88178:BRT&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">&quot;801782OIU&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;801378ASE&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;881578BRT&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><span id="more-2141"></span></p>
<p>A quick solution was the following. But I&#8217;m sure that this is not the fastest, as a matter of fact it is quite slow for larger arrays.</p>

<div class="wp_codebox"><table><tr id="p21412"><td class="code" id="p2141code2"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$values</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;66345:CDF&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;61179:HGT&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;64146:ABA&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">&quot;68768:BNG&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;68015:ZCZ&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;80231:LPO&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">&quot;64146:QWP&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;68736:HHB&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;86801:MNV&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">&quot;80178:OIU&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;80178:ASE&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;88178:BRT&quot;</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">&quot;801782OIU&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;801378ASE&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;881578BRT&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> my_cmp<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$pieces_a</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;:&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$pieces_b</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;:&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pieces_a</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pieces_b</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pieces_b</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pieces_a</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pieces_a</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pieces_b</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #990000;">strcasecmp</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pieces_a</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pieces_b</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #990000;">usort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;my_cmp&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>So what will be the fastest algorithm for the same?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/sorting-arrays-with-value-substrings-in-php/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Luhn algorithm for validating credit cards</title>
		<link>http://www.codediesel.com/php/luhn-algorithm-for-validating-credit-cards/</link>
		<comments>http://www.codediesel.com/php/luhn-algorithm-for-validating-credit-cards/#comments</comments>
		<pubDate>Wed, 09 Jul 2008 14:32:19 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[algorithm]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=61</guid>
		<description><![CDATA[The Luhn algorithm also known as the &#8220;modulus 10&#8243; or &#8220;mod 10&#8243; algorithm, is a checksum formula which can be used to validate credit card numbers. Developed in the 1950&#8242;s by IBM scientist Hans Peter Luhn and described in U.S. Patent 2,950,048. A PHP implementation is shown below. &#160; function LuhnCheck&#40;$strDigits&#41; &#123; $sum = 0; [...]]]></description>
			<content:encoded><![CDATA[<p>The Luhn algorithm also known as the &#8220;modulus 10&#8243; or &#8220;mod 10&#8243; algorithm, is a checksum formula which can be used to validate credit card numbers. Developed in the 1950&#8242;s by IBM scientist Hans Peter Luhn and described in <a href="http://www.pat2pdf.org/pat2pdf/foo.pl?number=2950048">U.S. Patent 2,950,048</a>. A PHP implementation is shown below.</p>

<div class="wp_codebox"><table><tr id="p614"><td class="code" id="p61code4"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> LuhnCheck<span style="color: #009900;">&#40;</span><span style="color: #000088;">$strDigits</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$sum</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$alt</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$strDigits</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$alt</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
           <span style="color: #000088;">$temp</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$strDigits</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
           <span style="color: #000088;">$temp</span> <span style="color: #339933;">*=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
           <span style="color: #000088;">$strDigits</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$temp</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$temp</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$temp</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">9</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$temp</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000088;">$sum</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$strDigits</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$alt</span> <span style="color: #339933;">=</span> <span style="color: #339933;">!</span><span style="color: #000088;">$alt</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000088;">$sum</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">10</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/luhn-algorithm-for-validating-credit-cards/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

