Skip to content

Instantly share code, notes, and snippets.

@hamakn
hamakn / quicksort.rb
Created December 25, 2012 18:46
quicksort
def quicksort(arr)
return arr if arr.size <= 1
pivot = arr.last
f, l = 0, arr.size - 1
loop do
loop { break if arr[f].nil? || arr[f] > pivot; f += 1 }
loop { break if arr[l].nil? || arr[l] < pivot; l -= 1 }
break if l < f
arr[f], arr[l] = arr[l], arr[f]
end
@hamakn
hamakn / 11st_cfcrjp.md
Created January 29, 2013 08:49
11st Cloud Foundry 輪読会 ホワイトボードのまとめ

11st Cloud Foundry 輪読会 ホワイトボードのまとめ

2013-01-28 atnd

Cloud Foundryについて興味のあること

  • 何かアプリを乗せる話
  • Java
@hamakn
hamakn / gist:5079781
Created March 4, 2013 03:50
perlbrew install perl-5.16.2 failed
% perlbrew install perl-5.16.2
Fetching perl 5.16.2 as /Users/katsunori.kawaguchi/perl5/perlbrew/dists/perl-5.16.2.tar.bz2
Installing /Users/katsunori.kawaguchi/perl5/perlbrew/build/perl-5.16.2 into ~/perl5/perlbrew/perls/perl-5.16.2
This could take a while. You can run the following command on another shell to track the status:
tail -f ~/perl5/perlbrew/build.perl-5.16.2.log
Installation process failed. To spot any issues, check
@hamakn
hamakn / gist:5131299
Last active December 14, 2015 18:39 — forked from yssk22/gist:5128840
use 5.016;
use DateTime;
# beginning_of_day
say DateTime->now( time_zone=>'local' )->truncate( to => "day" )->strftime('%Y/%m/%d %H:%M:%S')
# => 2013/03/15 00:00:00
# サマータイムで存在しない時刻を指定すると例外
# (America/Chicago時刻では、2003-04-06 01:59:59 の次は 2003-04-06 03:00:00)
DateTime->new( time_zone => 'America/Chicago', year => 2003, month => 4, day => 6, hour => 2, minute => 0, second => 0 );
@hamakn
hamakn / Guardfile
Last active December 15, 2015 11:29
perl用Guadfile (主にUNIVERSAL::requireでsyntax errorが出てこなくなる対策)
require "open3"
require "colorize"
guard 'shell' do
watch(/(.*).pm/) do |m|
arr = Open3.capture3("perl -c #{m[0]}")
line = arr.first.split("\n").first
unless line =~ /syntax OK/
puts line.red
else
@hamakn
hamakn / gist:5262528
Created March 28, 2013 11:37
1ヶ月を足す、とは
[1] pry(main)> require "active_support/time"
=> true
[2] pry(main)> Time.parse("2013-03-31").next_month
=> 2013-04-30 00:00:00 +0900
re.pl$ use DateTime
re.pl$ DateTime->new(year => 2013, month => 3, day =>31)->add( months => 1)->ymd;
"2013-05-01"
@hamakn
hamakn / encode.pl
Last active December 15, 2015 16:29
prelの内部文字列がわかってなくて死んだ
use 5.016;
use utf8;
use Encode;
use Encode::Guess qw/utf8 euc-jp shiftjis/;
my $str = 'あああ';
say guess_encoding($str)->name;
# => utf8
@hamakn
hamakn / pi.rb
Last active December 16, 2015 11:19
近似した円周率は円を何角形として扱うことになるのか https://twitter.com/onisci/status/325365096635322368
MAX_KETA = 15
# 半径1の正n角系の辺の長さ
def edge_length_of_regular_polygon(n)
2 * n * Math.sin(Math::PI / n)
end
n = 6
(0..MAX_KETA).each do |keta|
approximation_of_pi = (Math::PI * 10 ** keta).truncate * 1.0 / 10 ** keta
class Player
def initialize
@step = :forward
@health = 20
@max_health = 20
@last_health = 20
@touch_wall = false
end
def play_turn(warrior)