Skip to content

Instantly share code, notes, and snippets.

View tsmsogn's full-sized avatar
:octocat:
Set your status

tsmsogn tsmsogn

:octocat:
Set your status
View GitHub Profile
<?php
function array_map_recursive($func, $array) {
$res = array();
foreach ($array as $key => $value) {
$res[$key] = (is_array($value)) ? array_map_recursive($func, $value) : $func($value);
}
return $res;
}
@tsmsogn
tsmsogn / gist:ad41366053893dfb550d
Last active September 20, 2016 07:40
[cakephp]気をつけていること

Model

Model#order は複数設定しない

  • find('neighbor') ができなくなる
  • Paginator では 1つしか引き継がれない

Behavior

TreeBehavior

def hamming_numbers(max)
nums = Array.new(max + 1, 0)
nums[1] = 1
(2..max).each do |num|
nums[num] = 1 if num % 2 == 0 && nums[num / 2] == 1
nums[num] = 1 if num % 3 == 0 && nums[num / 3] == 1
nums[num] = 1 if num % 5 == 0 && nums[num / 5] == 1
end
# Node
class Node
attr_accessor :id, :edges, :cost, :done
def initialize(id, edges = [], cost = nil, done = false)
@id, @edges, @cost, @done = id, edges, cost, done
end
end
# Edge
JUMP = [
[-1, -2], [0, -2], [1, -2],
[2, -1], [2, 0], [2, 1],
[1, 2], [0, 2], [-1, 2],
[-2, 1], [-2, 0], [-2, -1]
]
def dfs(x, y, splinklers, n, depth = 0)
return true if depth == n
# Node
class Node
attr_accessor :id, :edges, :cost, :done
def initialize(id, edges = [], cost = nil, done = false)
@id, @edges, @cost, @done = id, edges, cost, done
end
end
# Edge
@tsmsogn
tsmsogn / primes.rb
Last active May 6, 2019 01:45
[ruby]Sieve of Eratosthenes with Ruby
def primes(max)
nums = Array.new(max + 1, 1)
nums[0] = nums[1] = 0
(2..Math.sqrt(max)).each do |sieve|
if nums[sieve] == 1
(2 * sieve).step(max, sieve).each do |num|
nums[num] = 0
end
end
def primes(max)
nums = Array.new(max + 1, 1)
nums[0] = nums[1] = 0
(2..Math.sqrt(max)).each do |sieve|
if nums[sieve] == 1
(2 * sieve).step(max, sieve).each do |num|
nums[num] = 0
end
end
def magic_square(grid, n)
x, y = n / 2, n / 2 + 1
grid[y][x] = 1
for i in 2..(n * n)
y += 1
x += 1
done = false
while line = gets
n = line.chomp.to_i
break if n == -1
x, y, r = 0.0, 0.0, 0.0
n.times do
x += Math.cos(r)
y += Math.sin(r)
r = Math.atan2(y, x) + Math::PI / 2