Pages

Wednesday, February 16, 2011

Test post for source code

測試透過 emacs + muse 將程式碼發表到 blogger 上

隨便一段 python 程式

import os
import hashlib

dupes = {}

for path, dirs, files in os.walk(os.getcwd()):
    for file in files:
        filename = os.path.join(path, file)
        hash = hashlib.sha1(open(filename).read()).hexdigest()
        if hash in dupes:
            print 'linking "%s" -> "%s"' % (dupes[hash], filename)
            os.rename(filename, filename + '.bak')
            try:
                os.link(dupes[hash], filename)
                os.unlink(filename + '.bak')
            except:
                os.rename(filename + '.bak', filename)
            finally:
        else:
            dupes[hash] = filename

同場加映 perl source listing

#!/usr/bin/perl -w
use Digest::MD5 qw(md5_hex);
use Digest:SHA1 qw(sha1_hex);

if (@ARGV !=1)
{
    print "Usage: perl $0 <word to encrypt>\n";
    exit;
}
($var) = @ARGV;
print "\n$var (MD5)" . " : " . md5_hex("$var")."\n";
print "$var (SHA1)" . " : " . sha1_hex("$var")."\n";
void swap(int *a, int *b)
{
  int t=*a; *a=*b; *b=t;
}
void sort(int arr[], int beg, int end)
{
  if (end > beg + 1)
  {
    int piv = arr[beg], l = beg + 1, r = end;
    while (l < r)
    {
      if (arr[l] <= piv)
        l++;
      else
        swap(&arr[l], &arr[--r]);
    }
    swap(&arr[--l], &arr[beg]);
    sort(arr, beg, l);
    sort(arr, r, end);
  }
}