Skip to content

Instantly share code, notes, and snippets.

View ackintosh's full-sized avatar
🎯
Focusing

Akihito Nakano ackintosh

🎯
Focusing
View GitHub Profile
@ackintosh
ackintosh / gist:6260199
Created August 18, 2013 06:26
#gunmacs BubbleSort
require 'pry'
require 'test/unit'
class Array
def bubble_sort
ary = self.dup
(size - 1).downto(0) do |i|
(0...i).each do |j|
ary[j], ary[j + 1] = ary[j + 1], ary[j] if ary[j] > ary[j + 1]
@ackintosh
ackintosh / gist:6260083
Last active December 21, 2015 05:49
#gunmacs
require 'pry'
require 'test/unit'
class Array
def bubble_sort
ary = self.dup
ary.size.times do
ary = ary.inject([]) do |result, num|
if result == []
require 'test/unit'
class Array
def bubble_sort
(0...(size)).each do |i|
((i + 1)...(size)).each do |j|
self[i], self[j] = self[j], self[i] if self[i] > self[j]
end
end
self
class Array
def flat?
self.each do |n|
return false unless first == n
end
true
end
end
def lcm(a, b)
@ackintosh
ackintosh / compose.php
Created July 31, 2013 13:28
Compose functions in PHP.
<?php
class Compose
{
public static function _()
{
$callables = func_get_args();
return array_reduce($callables, function ($a, $b) {
if ($a === null) {
@ackintosh
ackintosh / prime_factors.rb
Created July 30, 2013 12:42
The prime factors.
class Fixnum
def prime?
return false if self <= 1
(2..(Math::sqrt(self))).each do |n|
return false if self % n == 0
end
true
end
@ackintosh
ackintosh / fib.rb
Created July 4, 2013 13:29
Fibonacci number
class Fib
def at(n)
raise "invalid index" unless n > 0
if n == 1
0
elsif n == 2
1
else
at(n - 1) + at(n - 2)
end
require 'pry'
class Block
(class << self; self; end;).class_eval do
def output(input)
ary = input.each_line.inject([]) { |result, line| result << line.chomp! }
result = []
loop do
break if ary.first == '0 0'
result << judge(ary)
Pry.commands.alias_command 'c', 'continue'
Pry.commands.alias_command 's', 'step'
Pry.commands.alias_command 'n', 'next'
class NumberOfIsland
(class << self; self; end;).class_eval do
def output(input)
input.split("\n\n").inject([]) { |result, line| result << run(line) }.join("\n")
end
def run(input, x = 0, y = 0, checked = [], count = 0)
ary = convert_to_array(input)
ary.size.times do
row = []