Skip to content

Instantly share code, notes, and snippets.

@dannvix
dannvix / customized_combinations.rb
Created April 16, 2012 09:38
Customized format for combinations in Ruby
# for C(3, 1) it should returns %w(100 010 001)
def get_combinations (n, m)
(n.downto 1).to_a.combination(m).map do |c|
c.inject(0){ |s, k| s |= (1 << k-1) }.to_s(2).rjust(n, '0')
end
end
puts get_combinations(10, 5)
@dannvix
dannvix / paint_text.rb
Created March 24, 2012 10:32
Paint text on given image with RMagick
#!/usr/bin/env ruby
# encoding: utf-8
require 'RMagick'
include Magick
image = Image.read("image.png").first
text = Draw.new
text.annotate(image, 0, 0, 0, 60, "測試!Test!") {
@dannvix
dannvix / paint_text.py
Created March 24, 2012 10:02
Paint text on given image with PIL
#!/usr/bin/env python
#-*- encoding: utf8
# sudo easy_install PIL
import Image
import ImageFont
import ImageDraw
image = Image.open("image.png")
@dannvix
dannvix / Kirsch.rb
Created March 22, 2012 08:27
Kirsch filter with RMagick
class Kirsch
require 'RMagick'
include Magick
@@kernel = [
5, 5, -3, 5, 0, -3, -3, -3, -3,
5, 5, 5, -3, 0, -3, -3, -3, -3,
-3, 5, 5, -3, 0, 5, -3, -3, -3,
-3, -3, 5, -3, 0, 5, -3, -3, 5,
0, 0, 0, 0, 0, 0, 0, 0, 0,
@dannvix
dannvix / Sobel.rb
Created March 22, 2012 08:10
Sobel filter with RMagick
class Sobel
require 'RMagick'
include Magick
@@kernels = {
:up => [1, 2, 1, 0, 0, 0, -1, -2, -1],
:down => [-1, -2, -1, 0, 0, 0, 1, 2, 1],
:left => [1, 0, -1, 2, 0, -2, 1, 0, -1],
:right => [-1, 0, 1, -2, 0, 2, -1, 0, 1]
}
@dannvix
dannvix / pam_dotp.c
Created April 2, 2011 11:30
Simple Linux PAM module for OTP authentication
#include <security/pam_modules.h>
#include <security/pam_appl.h>
#include <sys/types.h>
#include <gcrypt.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#define SERVER_SECRET "MY_SECRET"
void gen_otp (char **otp) {
/* 引入 Win32 API 中的 User32.DLL
* 需要加上 using System.Runtime.InteropServices;
*/
[DllImport("user32.dll")]
public static extern Boolean GetWindowRect(IntPtr hWnd, ref Rectangle bounds);
public void CaptureWindow () {
/* 取得目標視窗的 Handle
* 需要加上 using System.Diagnostics;
*/
@dannvix
dannvix / upload.html.erb
Created February 25, 2011 17:48
Example Rails view with mod_upload_progress
<% uuid = (rand * 1e10).floor %>
<script type="text/javascript">
var timer;
var elapsed;
function update_progress() {
elapsed += 1;
jQuery.ajax({
type: "GET",
url: "/progress",
dataType: "json",
@dannvix
dannvix / main.m
Created December 2, 2010 03:03
iPhone Hello World Application
// copyed from "iPhone Developer's Cookbook", Addison-Wesley 2009
#import <UIKit/UIKit.h>
@interface HelloWorldViewController : UIViewController
@end
@implementation HelloWorldViewController
- (void) loadView
{
#!/usr/bin/ruby
require 'net/http'
require 'uri'
if ARGV.length < 1 then
puts "Usage: ./paste2plurk.rb [filename] ... "
exit
end
def get_language (filename)