Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Guildorn Tanaleth

« Newer Snippets
Older Snippets »
Showing 1-10 of 19 total  RSS 

Tabs-to-space function

This Perl function returns its argument with any tabs it contained converted into the appropriate number of spaces.
sub tabs2space($) {
 my $str = shift;
 1 while $str =~ s/\t+/' ' x (length($&)*8 - length($`)%8)/e;
 return $str;
}

basename & dirname in Perl

These two Perl functions implement approximations of the UNIX utilities `basename` and `dirname`, though basename() automatically strips off the last extension no matter what.
sub basename($) {
 my $file = shift;
 $file =~ s!^(?:.*/)?(.+?)(?:\.[^.]*)?$!$1!;
 return $file;
}

sub dirname($) {my $file = shift; $file =~ s!/?[^/]*/*$!!; return $file; }

NetHack death statistics

This shell script reads in a NetHack logfile & outputs a list of all the things that have killed you, with their frequencies.

This script was originally posted to rec.games.roguelike.nethack by Faux_Pseudo.
grep killed /usr/games/lib/nethackdir/logfile | awk -F killed '{print"killed"$NF}' | sort | uniq -c | sort -r

Unicode chart

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.
<HTML>
<HEAD>
<TITLE>Unicode Chart</TITLE>
<LINK REL="Stylesheet" TYPE="text/css" HREF="styles.css">
<STYLE TYPE="text/css">
TH {text-align: center; }
TD {text-align: center; }
</STYLE>
</HEAD>
<BODY>
<TABLE ALIGN=CENTER BORDER=1>
<TR><TH> </TH><TH>0</TH><TH>1</TH><TH>2</TH><TH>3</TH><TH>4</TH><TH>5</TH><TH>6</TH><TH>7</TH><TH>8</TH><TH>9</TH><TH>A</TH><TH>B</TH><TH>C</TH><TH>D</TH><TH>E</TH><TH>F</TH></TR>
<?PHP
 for ($i=0; $i<256; $i++) { //DON'T try to generate the whole chart
  printf('<TR><TD>%04X</TD>', $i);
  for ($j=0; $j<16; $j++) {
   printf('<TD>&#x%X%X;</TD>', $i, $j);
  }
  echo "</TR>\n";
 }
?>
</TABLE>
</BODY>
</HTML>

Image aggregator

This PHP snippet outputs the contents of a table (beginning & ending tags not included) containing all the images from the current directory.
<?PHP
 $columns = 3;
 $im = glob("*.{gif,jpg,png}", GLOB_BRACE);
 $rows = ceil(count($im) / $columns);
 for ($i = 0; $i < $rows; $i++) {
  echo "\n<TR>";
  for ($j = $columns*$i; isset($im[$j]) && $j - $columns*$i < $columns; $j++) {
   echo "<TD>$im[$j]<BR><IMG SRC='$im[$j]'></TD>";
  }
  echo "</TR>";
 }
?>

Fibonacci function

This function returns the nth Fibonacci number, though most platforms can go no higher than the 47th.
int Fibonacci(int n) {
 int a = 1, b = 1, c, i;
 for (i=3; i<=n; i++) {c = b; b += a; a = c; }
 return b;
}

Factorial function

A (non-recursive) factorial function
int factorial(int x) {
 int fac = 1;
 for (int i=2; i<=x; i++) fac *= i;
 return fac;
}


Also, with this function defined, you can use two macros for calculating combinations & permutations:
#define nCr(n, r) (factorial(n) / factorial(n-r) / factorial(r))
#define nPr(n, r) (factorial(n) / factorial(n-r))

Modular inverse function

This function calculates & returns the inverse of a modulo n, 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.
int modInverse(int a, int n) {
 int i = n, v = 0, d = 1;
 while (a>0) {
  int t = i/a, x = a;
  a = i % x;
  i = x;
  x = d;
  d = v - t*x;
  v = x;
 }
 v %= n;
 if (v<0) v = (v+n)%n;
 return v;
}

Greatest Common Denominator function

This function returns the greatest common denominator (GCD) of its arguments.
int gcd(int x, int y) {
 int a, b;
 if (x<y) {a = y; b = x; }
 else if (x>y) {a = x; b = y; }
 else {return x; }
 do {
  int r = a % b;
  a = b;
  b = r;
 } while (b != 0);
 return a;
}

Code execution timer

These two bits of code can be used to record & 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).
/* Put this line at the top of the file: */
#include <sys/time.h>

/* Put this right before the code you want to time: */
struct timeval timer_start, timer_end;
gettimeofday(&timer_start, NULL);

/* Put this right after the code you want to time: */
gettimeofday(&timer_end, NULL);
double timer_spent = timer_end.tv_sec - timer_start.tv_sec + (timer_end.tv_usec - timer_start.tv_usec) / 1000000.0;
printf("Time spent: %.6f\n", timer_spent);
« Newer Snippets
Older Snippets »
Showing 1-10 of 19 total  RSS