<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Minimiscience's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 07:32:03 GMT</pubDate>
    <description>DZone Snippets: Minimiscience's Code Snippets</description>
    <item>
      <title>Tabs-to-space function</title>
      <link>http://snippets.dzone.com/posts/show/4269</link>
      <description>This Perl function returns its argument with any tabs it contained converted into the appropriate number of spaces.&lt;br /&gt;&lt;code&gt;sub tabs2space($) {&lt;br /&gt; my $str = shift;&lt;br /&gt; 1 while $str =~ s/\t+/' ' x (length($&amp;)*8 - length($`)%8)/e;&lt;br /&gt; return $str;&lt;br /&gt;}&lt;/code&gt;</description>
      <pubDate>Fri, 06 Jul 2007 05:51:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4269</guid>
      <author>Minimiscience (Guildorn Tanaleth)</author>
    </item>
    <item>
      <title>basename &amp; dirname in Perl</title>
      <link>http://snippets.dzone.com/posts/show/4268</link>
      <description>These two Perl functions implement approximations of the UNIX utilities `basename` and `dirname`, though basename() automatically strips off the last extension no matter what.&lt;br /&gt;&lt;code&gt;sub basename($) {&lt;br /&gt; my $file = shift;&lt;br /&gt; $file =~ s!^(?:.*/)?(.+?)(?:\.[^.]*)?$!$1!;&lt;br /&gt; return $file;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;sub dirname($) {my $file = shift; $file =~ s!/?[^/]*/*$!!; return $file; }&lt;/code&gt;</description>
      <pubDate>Fri, 06 Jul 2007 05:49:34 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4268</guid>
      <author>Minimiscience (Guildorn Tanaleth)</author>
    </item>
    <item>
      <title>NetHack death statistics</title>
      <link>http://snippets.dzone.com/posts/show/4261</link>
      <description>This shell script reads in a NetHack logfile &amp; outputs a list of all the things that have killed you, with their frequencies.&lt;br /&gt;&lt;br /&gt;This script was originally posted to rec.games.roguelike.nethack by Faux_Pseudo.&lt;br /&gt;&lt;code&gt;grep killed /usr/games/lib/nethackdir/logfile | awk -F killed '{print"killed"$NF}' | sort | uniq -c | sort -r&lt;/code&gt;</description>
      <pubDate>Thu, 05 Jul 2007 07:29:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4261</guid>
      <author>Minimiscience (Guildorn Tanaleth)</author>
    </item>
    <item>
      <title>Unicode chart</title>
      <link>http://snippets.dzone.com/posts/show/4260</link>
      <description>This PHP-enhanced HTML page will display the first 4,096 (unless you change it) Unicode characters in a neat table.  Your browser's ability to render the characters properly may vary.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;HTML&gt;&lt;br /&gt;&lt;HEAD&gt;&lt;br /&gt;&lt;TITLE&gt;Unicode Chart&lt;/TITLE&gt;&lt;br /&gt;&lt;LINK REL="Stylesheet" TYPE="text/css" HREF="styles.css"&gt;&lt;br /&gt;&lt;STYLE TYPE="text/css"&gt;&lt;br /&gt;TH {text-align: center; }&lt;br /&gt;TD {text-align: center; }&lt;br /&gt;&lt;/STYLE&gt;&lt;br /&gt;&lt;/HEAD&gt;&lt;br /&gt;&lt;BODY&gt;&lt;br /&gt;&lt;TABLE ALIGN=CENTER BORDER=1&gt;&lt;br /&gt;&lt;TR&gt;&lt;TH&gt; &lt;/TH&gt;&lt;TH&gt;0&lt;/TH&gt;&lt;TH&gt;1&lt;/TH&gt;&lt;TH&gt;2&lt;/TH&gt;&lt;TH&gt;3&lt;/TH&gt;&lt;TH&gt;4&lt;/TH&gt;&lt;TH&gt;5&lt;/TH&gt;&lt;TH&gt;6&lt;/TH&gt;&lt;TH&gt;7&lt;/TH&gt;&lt;TH&gt;8&lt;/TH&gt;&lt;TH&gt;9&lt;/TH&gt;&lt;TH&gt;A&lt;/TH&gt;&lt;TH&gt;B&lt;/TH&gt;&lt;TH&gt;C&lt;/TH&gt;&lt;TH&gt;D&lt;/TH&gt;&lt;TH&gt;E&lt;/TH&gt;&lt;TH&gt;F&lt;/TH&gt;&lt;/TR&gt;&lt;br /&gt;&lt;?PHP&lt;br /&gt; for ($i=0; $i&lt;256; $i++) { //DON'T try to generate the whole chart&lt;br /&gt;  printf('&lt;TR&gt;&lt;TD&gt;%04X&lt;/TD&gt;', $i);&lt;br /&gt;  for ($j=0; $j&lt;16; $j++) {&lt;br /&gt;   printf('&lt;TD&gt;&amp;#x%X%X;&lt;/TD&gt;', $i, $j);&lt;br /&gt;  }&lt;br /&gt;  echo "&lt;/TR&gt;\n";&lt;br /&gt; }&lt;br /&gt;?&gt;&lt;br /&gt;&lt;/TABLE&gt;&lt;br /&gt;&lt;/BODY&gt;&lt;br /&gt;&lt;/HTML&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 05 Jul 2007 04:12:52 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4260</guid>
      <author>Minimiscience (Guildorn Tanaleth)</author>
    </item>
    <item>
      <title>Image aggregator</title>
      <link>http://snippets.dzone.com/posts/show/4259</link>
      <description>This PHP snippet outputs the contents of a table (beginning &amp; ending tags not included) containing all the images from the current directory.&lt;br /&gt;&lt;code&gt;&lt;?PHP&lt;br /&gt; $columns = 3;&lt;br /&gt; $im = glob("*.{gif,jpg,png}", GLOB_BRACE);&lt;br /&gt; $rows = ceil(count($im) / $columns);&lt;br /&gt; for ($i = 0; $i &lt; $rows; $i++) {&lt;br /&gt;  echo "\n&lt;TR&gt;";&lt;br /&gt;  for ($j = $columns*$i; isset($im[$j]) &amp;&amp; $j - $columns*$i &lt; $columns; $j++) {&lt;br /&gt;   echo "&lt;TD&gt;$im[$j]&lt;BR&gt;&lt;IMG SRC='$im[$j]'&gt;&lt;/TD&gt;";&lt;br /&gt;  }&lt;br /&gt;  echo "&lt;/TR&gt;";&lt;br /&gt; }&lt;br /&gt;?&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 05 Jul 2007 04:08:17 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4259</guid>
      <author>Minimiscience (Guildorn Tanaleth)</author>
    </item>
    <item>
      <title>Fibonacci function</title>
      <link>http://snippets.dzone.com/posts/show/4258</link>
      <description>This function returns the &lt;i&gt;n&lt;/i&gt;th Fibonacci number, though most platforms can go no higher than the 47th.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;int Fibonacci(int n) {&lt;br /&gt; int a = 1, b = 1, c, i;&lt;br /&gt; for (i=3; i&lt;=n; i++) {c = b; b += a; a = c; }&lt;br /&gt; return b;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 05 Jul 2007 03:54:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4258</guid>
      <author>Minimiscience (Guildorn Tanaleth)</author>
    </item>
    <item>
      <title>Factorial function</title>
      <link>http://snippets.dzone.com/posts/show/4257</link>
      <description>A (non-recursive) factorial function&lt;br /&gt;&lt;code&gt;int factorial(int x) {&lt;br /&gt; int fac = 1;&lt;br /&gt; for (int i=2; i&lt;=x; i++) fac *= i;&lt;br /&gt; return fac;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Also, with this function defined, you can use two macros for calculating combinations &amp; permutations:&lt;br /&gt;&lt;code&gt;#define nCr(n, r) (factorial(n) / factorial(n-r) / factorial(r))&lt;br /&gt;#define nPr(n, r) (factorial(n) / factorial(n-r))&lt;/code&gt;</description>
      <pubDate>Thu, 05 Jul 2007 03:50:14 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4257</guid>
      <author>Minimiscience (Guildorn Tanaleth)</author>
    </item>
    <item>
      <title>Modular inverse function</title>
      <link>http://snippets.dzone.com/posts/show/4256</link>
      <description>This function calculates &amp; returns the inverse of &lt;I&gt;a modulo n&lt;/I&gt;, both of which should be positive.  If the inverse does not exist, 0 is returned.  If you don't know what this means, don't bother.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;int modInverse(int a, int n) {&lt;br /&gt; int i = n, v = 0, d = 1;&lt;br /&gt; while (a&gt;0) {&lt;br /&gt;  int t = i/a, x = a;&lt;br /&gt;  a = i % x;&lt;br /&gt;  i = x;&lt;br /&gt;  x = d;&lt;br /&gt;  d = v - t*x;&lt;br /&gt;  v = x;&lt;br /&gt; }&lt;br /&gt; v %= n;&lt;br /&gt; if (v&lt;0) v = (v+n)%n;&lt;br /&gt; return v;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 05 Jul 2007 03:42:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4256</guid>
      <author>Minimiscience (Guildorn Tanaleth)</author>
    </item>
    <item>
      <title>Greatest Common Denominator function</title>
      <link>http://snippets.dzone.com/posts/show/4255</link>
      <description>This function returns the greatest common denominator (GCD) of its arguments.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;int gcd(int x, int y) {&lt;br /&gt; int a, b;&lt;br /&gt; if (x&lt;y) {a = y; b = x; }&lt;br /&gt; else if (x&gt;y) {a = x; b = y; }&lt;br /&gt; else {return x; }&lt;br /&gt; do {&lt;br /&gt;  int r = a % b;&lt;br /&gt;  a = b;&lt;br /&gt;  b = r;&lt;br /&gt; } while (b != 0);&lt;br /&gt; return a;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 05 Jul 2007 03:37:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4255</guid>
      <author>Minimiscience (Guildorn Tanaleth)</author>
    </item>
    <item>
      <title>Code execution timer</title>
      <link>http://snippets.dzone.com/posts/show/4254</link>
      <description>These two bits of code can be used to record &amp; output the execution time of a piece of code in microseconds.  NOTE: The functions used here are only available on BSD-like systems (I think).&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/* Put this line at the top of the file: */&lt;br /&gt;#include &lt;sys/time.h&gt;&lt;br /&gt;&lt;br /&gt;/* Put this right before the code you want to time: */&lt;br /&gt;struct timeval timer_start, timer_end;&lt;br /&gt;gettimeofday(&amp;timer_start, NULL);&lt;br /&gt;&lt;br /&gt;/* Put this right after the code you want to time: */&lt;br /&gt;gettimeofday(&amp;timer_end, NULL);&lt;br /&gt;double timer_spent = timer_end.tv_sec - timer_start.tv_sec + (timer_end.tv_usec - timer_start.tv_usec) / 1000000.0;&lt;br /&gt;printf("Time spent: %.6f\n", timer_spent);&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 05 Jul 2007 03:27:35 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4254</guid>
      <author>Minimiscience (Guildorn Tanaleth)</author>
    </item>
  </channel>
</rss>
