Skip to content

Instantly share code, notes, and snippets.

View yao2030's full-sized avatar

yao2030 yao2030

  • Shanghai, China
View GitHub Profile
@yao2030
yao2030 / application_controller.rb
Created August 17, 2012 14:21
make current_user available in authorizations.rb
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :current_user
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
end
@yao2030
yao2030 / Gemfile
Created August 21, 2012 10:48 — forked from croaky/Gemfile
Twitter authentication
gem "omniauth-twitter"
@yao2030
yao2030 / ar_example.rb
Created October 17, 2012 14:43
using ActiveRecord outside rails
require 'rubygems'
require 'active_record'
require 'mysql2'
require 'yaml'
dbconfig = YAML::load(File.open('database.yml'))
ActiveRecord::Base.establish_connection(dbconfig)
class Duck < ActiveRecord::Base
end
puts Duck.count
@yao2030
yao2030 / Kary.java
Created October 20, 2012 04:35
conver a number to k-base
public class Kary
{
public static void main(String[] args)
{
int i = Integer.parseInt(args[0]);
int k = Integer.parseInt(args[1]);
int v = 1;
while (v <= i/k)
v *= k;
int n = i;
@yao2030
yao2030 / Hadamar.java
Created October 23, 2012 06:40
Hadamard matrix
public class Hadamard
{
public static void main(String[] args)
{
int N = Integer.parseInt(args[0]);
boolean[][] H = new boolean[N][N];
H[0][0] = true;
for(int n = 1; n < N; n += n)
{
for(int i = 0; i < n; i++)
@yao2030
yao2030 / Calendar.java
Created October 28, 2012 08:58
calenday java implementating
public class Calendar
{
public static boolean isLeapYear(int year)
{
if((year % 4 ==0 && year % 100 != 0) || (year % 400 == 0))
return true;
return false;
}
public static String getMonth(int month)
{
@yao2030
yao2030 / Mine.java
Created October 28, 2012 08:59
mine sweeper
public class Mine
{
public static void main(String[] args)
{
int M = Integer.parseInt(args[0]);
int N = Integer.parseInt(args[1]);
double p = Double.parseDouble(args[2]);
boolean[][] mine = new boolean[M+2][M+2];
for(int i = 1; i <= M; i++)
@yao2030
yao2030 / Post.java
Created October 29, 2012 01:32
Post bar code. U.S. Postal System
public class Post
{
public static void halfHeight(double i)
{
StdDraw.setPenRadius(.01);
StdDraw.line(i, 0, i, 1);
}
public static void fullHeight(double i)
{
StdDraw.setPenRadius(0.01);
@yao2030
yao2030 / Ruler.java
Created October 30, 2012 10:30
a recursive program to write plot the subdivisions of a ruler using StdDraw library, <introduction to programming in java an interdisciplinary approach
public class Ruler
{
public static String ruler(int N)
{
if (N <= 0) return "";
if (N == 1) return "1";
return ruler(N-1) + N + ruler(N-1);
}
public static void drawRuler(String a)
@yao2030
yao2030 / BP.java
Created October 31, 2012 01:19
use recursion to print out the binary representation of an integer
public class BP
{
public static String binary(int N)
{
if(N / 2 == 0) return "" + N;
return binary(N/2) + "" + N % 2;
}
public static void main(String[] args)
{
int N = Integer.parseInt(args[0]);